ledisdb/ledis/t_set.go

645 lines
12 KiB
Go
Raw Normal View History

2014-08-11 10:52:43 +04:00
package ledis
import (
"encoding/binary"
"errors"
2015-05-04 17:42:28 +03:00
"time"
2014-09-24 08:34:21 +04:00
"github.com/siddontang/go/hack"
2014-08-13 17:54:27 +04:00
"github.com/siddontang/ledisdb/store"
2014-08-11 10:52:43 +04:00
)
var errSetKey = errors.New("invalid set key")
var errSSizeKey = errors.New("invalid ssize key")
2018-03-29 15:33:36 +03:00
// For set operation type.
2014-08-11 10:52:43 +04:00
const (
setStartSep byte = ':'
setStopSep byte = setStartSep + 1
UnionType byte = 51
DiffType byte = 52
InterType byte = 53
2014-08-11 10:52:43 +04:00
)
2014-08-13 17:54:27 +04:00
func checkSetKMSize(key []byte, member []byte) error {
2014-08-11 10:52:43 +04:00
if len(key) > MaxKeySize || len(key) == 0 {
return errKeySize
} else if len(member) > MaxSetMemberSize || len(member) == 0 {
return errSetMemberSize
}
return nil
}
func (db *DB) sEncodeSizeKey(key []byte) []byte {
2015-03-15 15:36:14 +03:00
buf := make([]byte, len(key)+1+len(db.indexVarBuf))
2014-08-11 10:52:43 +04:00
2015-03-15 15:36:14 +03:00
pos := copy(buf, db.indexVarBuf)
buf[pos] = SSizeType
2014-08-11 10:52:43 +04:00
2015-03-15 15:36:14 +03:00
pos++
copy(buf[pos:], key)
2014-08-11 10:52:43 +04:00
return buf
}
func (db *DB) sDecodeSizeKey(ek []byte) ([]byte, error) {
2015-03-15 15:36:14 +03:00
pos, err := db.checkKeyIndex(ek)
if err != nil {
return nil, err
}
if pos+1 > len(ek) || ek[pos] != SSizeType {
2014-08-11 10:52:43 +04:00
return nil, errSSizeKey
}
2015-03-15 15:36:14 +03:00
pos++
2014-08-11 10:52:43 +04:00
2015-03-15 15:36:14 +03:00
return ek[pos:], nil
2014-08-11 10:52:43 +04:00
}
func (db *DB) sEncodeSetKey(key []byte, member []byte) []byte {
2015-03-15 15:36:14 +03:00
buf := make([]byte, len(key)+len(member)+1+1+2+len(db.indexVarBuf))
pos := copy(buf, db.indexVarBuf)
2014-08-11 10:52:43 +04:00
buf[pos] = SetType
pos++
binary.BigEndian.PutUint16(buf[pos:], uint16(len(key)))
pos += 2
copy(buf[pos:], key)
pos += len(key)
buf[pos] = setStartSep
pos++
copy(buf[pos:], member)
return buf
}
func (db *DB) sDecodeSetKey(ek []byte) ([]byte, []byte, error) {
2015-03-15 15:36:14 +03:00
pos, err := db.checkKeyIndex(ek)
if err != nil {
return nil, nil, err
}
if pos+1 > len(ek) || ek[pos] != SetType {
return nil, nil, errSetKey
}
pos++
if pos+2 > len(ek) {
2014-08-11 10:52:43 +04:00
return nil, nil, errSetKey
}
keyLen := int(binary.BigEndian.Uint16(ek[pos:]))
pos += 2
2015-03-15 15:36:14 +03:00
if keyLen+pos > len(ek) {
2014-08-11 10:52:43 +04:00
return nil, nil, errSetKey
}
key := ek[pos : pos+keyLen]
pos += keyLen
if ek[pos] != hashStartSep {
return nil, nil, errSetKey
}
pos++
member := ek[pos:]
return key, member, nil
}
func (db *DB) sEncodeStartKey(key []byte) []byte {
return db.sEncodeSetKey(key, nil)
}
func (db *DB) sEncodeStopKey(key []byte) []byte {
k := db.sEncodeSetKey(key, nil)
k[len(k)-1] = setStopSep
return k
}
2014-08-13 17:54:27 +04:00
func (db *DB) sFlush() (drop int64, err error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
t.Lock()
defer t.Unlock()
2014-08-16 12:55:36 +04:00
return db.flushType(t, SetType)
}
2014-08-13 17:54:27 +04:00
2014-08-25 10:18:23 +04:00
func (db *DB) sDelete(t *batch, key []byte) int64 {
2014-08-13 17:54:27 +04:00
sk := db.sEncodeSizeKey(key)
start := db.sEncodeStartKey(key)
stop := db.sEncodeStopKey(key)
2018-03-29 15:33:36 +03:00
var num int64
2014-08-25 10:18:23 +04:00
it := db.bucket.RangeLimitIterator(start, stop, store.RangeROpen, 0, -1)
2014-08-13 17:54:27 +04:00
for ; it.Valid(); it.Next() {
2014-08-15 05:36:00 +04:00
t.Delete(it.RawKey())
2014-08-13 17:54:27 +04:00
num++
}
it.Close()
t.Delete(sk)
return num
}
func (db *DB) sIncrSize(key []byte, delta int64) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
sk := db.sEncodeSizeKey(key)
var err error
2018-03-29 15:33:36 +03:00
var size int64
2014-08-25 10:18:23 +04:00
if size, err = Int64(db.bucket.Get(sk)); err != nil {
2014-08-13 17:54:27 +04:00
return 0, err
2018-03-29 15:33:36 +03:00
}
size += delta
if size <= 0 {
size = 0
t.Delete(sk)
db.rmExpire(t, SetType, key)
2014-08-13 17:54:27 +04:00
} else {
2018-03-29 15:33:36 +03:00
t.Put(sk, PutInt64(size))
2014-08-13 17:54:27 +04:00
}
return size, nil
}
func (db *DB) sExpireAt(key []byte, when int64) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
t.Lock()
defer t.Unlock()
if scnt, err := db.SCard(key); err != nil || scnt == 0 {
return 0, err
2018-03-29 15:33:36 +03:00
}
db.expireAt(t, SetType, key, when)
if err := t.Commit(); err != nil {
return 0, err
2014-08-13 17:54:27 +04:00
}
return 1, nil
}
func (db *DB) sSetItem(key []byte, member []byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
ek := db.sEncodeSetKey(key, member)
var n int64 = 1
2014-08-25 10:18:23 +04:00
if v, _ := db.bucket.Get(ek); v != nil {
2014-08-13 17:54:27 +04:00
n = 0
} else {
if _, err := db.sIncrSize(key, 1); err != nil {
return 0, err
}
}
t.Put(ek, nil)
return n, nil
}
2018-03-29 15:33:36 +03:00
// SAdd adds the value to the set.
2014-08-13 17:54:27 +04:00
func (db *DB) SAdd(key []byte, args ...[]byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
t.Lock()
defer t.Unlock()
2014-08-14 05:15:46 +04:00
var err error
var ek []byte
2018-03-29 15:33:36 +03:00
var num int64
2014-08-13 17:54:27 +04:00
for i := 0; i < len(args); i++ {
2014-08-14 05:15:46 +04:00
if err := checkSetKMSize(key, args[i]); err != nil {
2014-08-13 17:54:27 +04:00
return 0, err
}
2014-08-14 05:15:46 +04:00
ek = db.sEncodeSetKey(key, args[i])
2014-08-25 10:18:23 +04:00
if v, err := db.bucket.Get(ek); err != nil {
2014-08-13 17:54:27 +04:00
return 0, err
2014-08-14 05:15:46 +04:00
} else if v == nil {
2014-08-13 17:54:27 +04:00
num++
}
2014-08-14 05:15:46 +04:00
t.Put(ek, nil)
2014-08-13 17:54:27 +04:00
}
2014-08-14 05:15:46 +04:00
if _, err = db.sIncrSize(key, num); err != nil {
return 0, err
}
err = t.Commit()
2014-08-13 17:54:27 +04:00
return num, err
2014-08-14 05:15:46 +04:00
2014-08-13 17:54:27 +04:00
}
2018-03-29 15:33:36 +03:00
// SCard gets the size of set.
2014-08-13 17:54:27 +04:00
func (db *DB) SCard(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.sEncodeSizeKey(key)
2014-08-25 10:18:23 +04:00
return Int64(db.bucket.Get(sk))
2014-08-13 17:54:27 +04:00
}
func (db *DB) sDiffGeneric(keys ...[]byte) ([][]byte, error) {
destMap := make(map[string]bool)
members, err := db.SMembers(keys[0])
if err != nil {
return nil, err
}
for _, m := range members {
2014-09-24 08:34:21 +04:00
destMap[hack.String(m)] = true
}
for _, k := range keys[1:] {
members, err := db.SMembers(k)
if err != nil {
return nil, err
}
for _, m := range members {
2014-09-24 08:34:21 +04:00
if _, ok := destMap[hack.String(m)]; !ok {
continue
} else if ok {
2014-09-24 08:34:21 +04:00
delete(destMap, hack.String(m))
}
}
// O - A = O, O is zero set.
if len(destMap) == 0 {
return nil, nil
}
}
slice := make([][]byte, len(destMap))
idx := 0
for k, v := range destMap {
if !v {
continue
}
slice[idx] = []byte(k)
idx++
}
return slice, nil
}
2018-03-29 15:33:36 +03:00
// SDiff gets the different of sets.
func (db *DB) SDiff(keys ...[]byte) ([][]byte, error) {
v, err := db.sDiffGeneric(keys...)
return v, err
}
2018-03-29 15:33:36 +03:00
// SDiffStore gets the different of sets and stores to dest set.
func (db *DB) SDiffStore(dstKey []byte, keys ...[]byte) (int64, error) {
n, err := db.sStoreGeneric(dstKey, DiffType, keys...)
return n, err
}
2018-03-29 15:33:36 +03:00
// SKeyExists checks whether set existed or not.
func (db *DB) SKeyExists(key []byte) (int64, error) {
2015-02-01 12:54:52 +03:00
if err := checkKeySize(key); err != nil {
return 0, err
}
sk := db.sEncodeSizeKey(key)
v, err := db.bucket.Get(sk)
if v != nil && err == nil {
return 1, nil
}
return 0, err
}
func (db *DB) sInterGeneric(keys ...[]byte) ([][]byte, error) {
destMap := make(map[string]bool)
members, err := db.SMembers(keys[0])
if err != nil {
return nil, err
}
for _, m := range members {
2014-09-24 08:34:21 +04:00
destMap[hack.String(m)] = true
}
for _, key := range keys[1:] {
if err := checkKeySize(key); err != nil {
return nil, err
}
members, err := db.SMembers(key)
if err != nil {
return nil, err
} else if len(members) == 0 {
return nil, err
}
2014-08-13 17:54:27 +04:00
tempMap := make(map[string]bool)
for _, member := range members {
if err := checkKeySize(member); err != nil {
return nil, err
}
2014-09-24 08:34:21 +04:00
if _, ok := destMap[hack.String(member)]; ok {
tempMap[hack.String(member)] = true //mark this item as selected
}
}
destMap = tempMap //reduce the size of the result set
if len(destMap) == 0 {
return nil, nil
}
}
2014-08-13 17:54:27 +04:00
slice := make([][]byte, len(destMap))
idx := 0
for k, v := range destMap {
if !v {
continue
}
2014-08-13 17:54:27 +04:00
slice[idx] = []byte(k)
idx++
}
2014-08-13 17:54:27 +04:00
return slice, nil
2014-08-13 17:54:27 +04:00
}
2014-08-13 17:54:27 +04:00
2018-03-29 15:33:36 +03:00
// SInter intersects the sets.
func (db *DB) SInter(keys ...[]byte) ([][]byte, error) {
v, err := db.sInterGeneric(keys...)
return v, err
2014-08-13 17:54:27 +04:00
}
2014-08-13 17:54:27 +04:00
2018-03-29 15:33:36 +03:00
// SInterStore intersects the sets and stores to dest set.
func (db *DB) SInterStore(dstKey []byte, keys ...[]byte) (int64, error) {
n, err := db.sStoreGeneric(dstKey, InterType, keys...)
return n, err
}
2014-08-13 17:54:27 +04:00
2018-03-29 15:33:36 +03:00
// SIsMember checks member in set.
2014-08-13 17:54:27 +04:00
func (db *DB) SIsMember(key []byte, member []byte) (int64, error) {
ek := db.sEncodeSetKey(key, member)
var n int64 = 1
2014-08-25 10:18:23 +04:00
if v, err := db.bucket.Get(ek); err != nil {
2014-08-13 17:54:27 +04:00
return 0, err
} else if v == nil {
n = 0
}
return n, nil
}
2018-03-29 15:33:36 +03:00
// SMembers gets members of set.
2014-08-13 17:54:27 +04:00
func (db *DB) SMembers(key []byte) ([][]byte, error) {
if err := checkKeySize(key); err != nil {
return nil, err
}
start := db.sEncodeStartKey(key)
stop := db.sEncodeStopKey(key)
v := make([][]byte, 0, 16)
2014-08-25 10:18:23 +04:00
it := db.bucket.RangeLimitIterator(start, stop, store.RangeROpen, 0, -1)
2014-11-27 04:20:49 +03:00
defer it.Close()
2014-08-13 17:54:27 +04:00
for ; it.Valid(); it.Next() {
_, m, err := db.sDecodeSetKey(it.Key())
if err != nil {
return nil, err
}
v = append(v, m)
}
return v, nil
}
2018-03-29 15:33:36 +03:00
// SRem removes the members of set.
2014-08-13 17:54:27 +04:00
func (db *DB) SRem(key []byte, args ...[]byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
t.Lock()
defer t.Unlock()
var ek []byte
var v []byte
var err error
2014-08-25 10:18:23 +04:00
it := db.bucket.NewIterator()
2014-08-13 17:54:27 +04:00
defer it.Close()
2018-03-29 15:33:36 +03:00
var num int64
2014-08-13 17:54:27 +04:00
for i := 0; i < len(args); i++ {
if err := checkSetKMSize(key, args[i]); err != nil {
return 0, err
}
ek = db.sEncodeSetKey(key, args[i])
v = it.RawFind(ek)
if v == nil {
continue
} else {
num++
t.Delete(ek)
}
}
if _, err = db.sIncrSize(key, -num); err != nil {
return 0, err
}
err = t.Commit()
return num, err
}
func (db *DB) sUnionGeneric(keys ...[]byte) ([][]byte, error) {
dstMap := make(map[string]bool)
for _, key := range keys {
if err := checkKeySize(key); err != nil {
return nil, err
}
members, err := db.SMembers(key)
if err != nil {
return nil, err
}
for _, member := range members {
2014-09-24 08:34:21 +04:00
dstMap[hack.String(member)] = true
}
}
slice := make([][]byte, len(dstMap))
idx := 0
for k, v := range dstMap {
if !v {
continue
}
slice[idx] = []byte(k)
idx++
}
return slice, nil
}
2018-03-29 15:33:36 +03:00
// SUnion unions the sets.
func (db *DB) SUnion(keys ...[]byte) ([][]byte, error) {
v, err := db.sUnionGeneric(keys...)
return v, err
}
2018-03-29 15:33:36 +03:00
// SUnionStore unions the sets and stores to the dest set.
func (db *DB) SUnionStore(dstKey []byte, keys ...[]byte) (int64, error) {
n, err := db.sStoreGeneric(dstKey, UnionType, keys...)
return n, err
}
func (db *DB) sStoreGeneric(dstKey []byte, optType byte, keys ...[]byte) (int64, error) {
if err := checkKeySize(dstKey); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
t := db.setBatch
t.Lock()
defer t.Unlock()
db.sDelete(t, dstKey)
2014-08-13 17:54:27 +04:00
var err error
var ek []byte
var v [][]byte
switch optType {
case UnionType:
v, err = db.sUnionGeneric(keys...)
case DiffType:
v, err = db.sDiffGeneric(keys...)
case InterType:
v, err = db.sInterGeneric(keys...)
}
if err != nil {
return 0, err
}
for _, m := range v {
if err := checkSetKMSize(dstKey, m); err != nil {
return 0, err
}
2014-08-13 17:54:27 +04:00
ek = db.sEncodeSetKey(dstKey, m)
2014-08-13 17:54:27 +04:00
2014-08-25 10:18:23 +04:00
if _, err := db.bucket.Get(ek); err != nil {
return 0, err
}
2014-08-13 17:54:27 +04:00
t.Put(ek, nil)
}
2014-08-13 17:54:27 +04:00
2014-09-24 09:29:27 +04:00
var n = int64(len(v))
2014-08-16 10:34:15 +04:00
sk := db.sEncodeSizeKey(dstKey)
2014-09-24 09:29:27 +04:00
t.Put(sk, PutInt64(n))
2014-08-16 10:34:15 +04:00
if err = t.Commit(); err != nil {
return 0, err
}
2014-09-24 09:29:27 +04:00
return n, nil
}
2014-08-13 17:54:27 +04:00
2018-03-29 15:33:36 +03:00
// SClear clears the set.
2014-08-13 17:54:27 +04:00
func (db *DB) SClear(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
t.Lock()
defer t.Unlock()
num := db.sDelete(t, key)
db.rmExpire(t, SetType, key)
err := t.Commit()
return num, err
}
2018-03-29 15:33:36 +03:00
// SMclear clears multi sets.
2014-08-13 17:54:27 +04:00
func (db *DB) SMclear(keys ...[]byte) (int64, error) {
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
t.Lock()
defer t.Unlock()
for _, key := range keys {
if err := checkKeySize(key); err != nil {
return 0, err
}
db.sDelete(t, key)
db.rmExpire(t, SetType, key)
}
err := t.Commit()
return int64(len(keys)), err
}
2018-03-29 15:33:36 +03:00
// SExpire expries the set.
2014-08-13 17:54:27 +04:00
func (db *DB) SExpire(key []byte, duration int64) (int64, error) {
if duration <= 0 {
return 0, errExpireValue
}
return db.sExpireAt(key, time.Now().Unix()+duration)
}
2018-03-29 15:33:36 +03:00
// SExpireAt expires the set at when.
2014-08-13 17:54:27 +04:00
func (db *DB) SExpireAt(key []byte, when int64) (int64, error) {
if when <= time.Now().Unix() {
return 0, errExpireValue
}
return db.sExpireAt(key, when)
}
2018-03-29 15:33:36 +03:00
// STTL gets the TTL of set.
2014-08-13 17:54:27 +04:00
func (db *DB) STTL(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return -1, err
}
return db.ttl(SetType, key)
}
2018-03-29 15:33:36 +03:00
// SPersist removes the TTL of set.
2014-08-13 17:54:27 +04:00
func (db *DB) SPersist(key []byte) (int64, error) {
if err := checkKeySize(key); err != nil {
return 0, err
}
2014-08-25 10:18:23 +04:00
t := db.setBatch
2014-08-13 17:54:27 +04:00
t.Lock()
defer t.Unlock()
n, err := db.rmExpire(t, SetType, key)
if err != nil {
return 0, err
}
err = t.Commit()
return n, err
}