diff --git a/ledis/const.go b/ledis/const.go index ce5a4e6..9b2daaf 100644 --- a/ledis/const.go +++ b/ledis/const.go @@ -5,22 +5,39 @@ import ( ) const ( - noneType byte = 0 - kvType byte = 1 - hashType byte = 2 - hSizeType byte = 3 - listType byte = 4 - lMetaType byte = 5 - zsetType byte = 6 - zSizeType byte = 7 - zScoreType byte = 8 - binType byte = 9 - binMetaType byte = 10 + NoneType byte = 0 + KVType byte = 1 + HashType byte = 2 + HSizeType byte = 3 + ListType byte = 4 + LMetaType byte = 5 + ZSetType byte = 6 + ZSizeType byte = 7 + ZScoreType byte = 8 + BitType byte = 9 + BitMetaType byte = 10 maxDataType byte = 100 - expTimeType byte = 101 - expMetaType byte = 102 + ExpTimeType byte = 101 + ExpMetaType byte = 102 +) + +var ( + TypeName = map[byte]string{ + KVType: "kv", + HashType: "hash", + HSizeType: "hsize", + ListType: "list", + LMetaType: "lmeta", + ZSetType: "zset", + ZSizeType: "zsize", + ZScoreType: "zscore", + BitType: "bit", + BitMetaType: "bitmeta", + ExpTimeType: "exptime", + ExpMetaType: "expmeta", + } ) const ( diff --git a/ledis/ledis_db.go b/ledis/ledis_db.go index 58b93e8..83819ae 100644 --- a/ledis/ledis_db.go +++ b/ledis/ledis_db.go @@ -26,11 +26,11 @@ func (db *DB) FlushAll() (drop int64, err error) { func (db *DB) newEliminator() *elimination { eliminator := newEliminator(db) - eliminator.regRetireContext(kvType, db.kvTx, db.delete) - eliminator.regRetireContext(listType, db.listTx, db.lDelete) - eliminator.regRetireContext(hashType, db.hashTx, db.hDelete) - eliminator.regRetireContext(zsetType, db.zsetTx, db.zDelete) - eliminator.regRetireContext(binType, db.binTx, db.bDelete) + eliminator.regRetireContext(KVType, db.kvTx, db.delete) + eliminator.regRetireContext(ListType, db.listTx, db.lDelete) + eliminator.regRetireContext(HashType, db.hashTx, db.hDelete) + eliminator.regRetireContext(ZSetType, db.zsetTx, db.zDelete) + eliminator.regRetireContext(BitType, db.binTx, db.bDelete) return eliminator } diff --git a/ledis/t_bin.go b/ledis/t_bit.go similarity index 97% rename from ledis/t_bin.go rename to ledis/t_bit.go index 2d62047..4111df8 100644 --- a/ledis/t_bin.go +++ b/ledis/t_bit.go @@ -119,19 +119,27 @@ func (datas segBitInfoArray) Swap(i, j int) { func (db *DB) bEncodeMetaKey(key []byte) []byte { mk := make([]byte, len(key)+2) mk[0] = db.index - mk[1] = binMetaType + mk[1] = BitMetaType copy(mk, key) return mk } +func (db *DB) bDecodeMetaKey(bkey []byte) ([]byte, error) { + if len(bkey) < 2 || bkey[0] != db.index || bkey[1] != BitMetaType { + return nil, errBinKey + } + + return bkey[2:], nil +} + func (db *DB) bEncodeBinKey(key []byte, seq uint32) []byte { bk := make([]byte, len(key)+8) pos := 0 bk[pos] = db.index pos++ - bk[pos] = binType + bk[pos] = BitType pos++ binary.BigEndian.PutUint16(bk[pos:], uint16(len(key))) @@ -355,7 +363,7 @@ func (db *DB) bExpireAt(key []byte, when int64) (int64, error) { if seq, _, err := db.bGetMeta(key); err != nil || seq < 0 { return 0, err } else { - db.expireAt(t, binType, key, when) + db.expireAt(t, BitType, key, when) if err := t.Commit(); err != nil { return 0, err } @@ -407,7 +415,7 @@ func (db *DB) BDelete(key []byte) (drop int64, err error) { defer t.Unlock() drop = db.bDelete(t, key) - db.rmExpire(t, binType, key) + db.rmExpire(t, BitType, key) err = t.Commit() return @@ -736,7 +744,7 @@ func (db *DB) BOperation(op uint8, dstkey []byte, srckeys ...[]byte) (blen int32 // clear the old data in case db.bDelete(t, dstkey) - db.rmExpire(t, binType, dstkey) + db.rmExpire(t, BitType, dstkey) // set data db.bSetMeta(t, dstkey, maxDstSeq, maxDstOff) @@ -786,7 +794,7 @@ func (db *DB) BTTL(key []byte) (int64, error) { return -1, err } - return db.ttl(binType, key) + return db.ttl(BitType, key) } func (db *DB) BPersist(key []byte) (int64, error) { @@ -798,7 +806,7 @@ func (db *DB) BPersist(key []byte) (int64, error) { t.Lock() defer t.Unlock() - n, err := db.rmExpire(t, binType, key) + n, err := db.rmExpire(t, BitType, key) if err != nil { return 0, err } @@ -818,14 +826,14 @@ func (db *DB) bFlush() (drop int64, err error) { minKey := make([]byte, 2) minKey[0] = db.index - minKey[1] = binType + minKey[1] = BitType maxKey := make([]byte, 2) maxKey[0] = db.index - maxKey[1] = binMetaType + 1 + maxKey[1] = BitMetaType + 1 drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, binType) + err = db.expFlush(t, BitType) err = t.Commit() return diff --git a/ledis/t_bin_test.go b/ledis/t_bit_test.go similarity index 100% rename from ledis/t_bin_test.go rename to ledis/t_bit_test.go diff --git a/ledis/t_hash.go b/ledis/t_hash.go index 711d83b..5f6be64 100644 --- a/ledis/t_hash.go +++ b/ledis/t_hash.go @@ -33,14 +33,14 @@ func (db *DB) hEncodeSizeKey(key []byte) []byte { buf := make([]byte, len(key)+2) buf[0] = db.index - buf[1] = hSizeType + buf[1] = HSizeType copy(buf[2:], key) return buf } func (db *DB) hDecodeSizeKey(ek []byte) ([]byte, error) { - if len(ek) < 2 || ek[0] != db.index || ek[1] != hSizeType { + if len(ek) < 2 || ek[0] != db.index || ek[1] != HSizeType { return nil, errHSizeKey } @@ -53,7 +53,7 @@ func (db *DB) hEncodeHashKey(key []byte, field []byte) []byte { pos := 0 buf[pos] = db.index pos++ - buf[pos] = hashType + buf[pos] = HashType pos++ binary.BigEndian.PutUint16(buf[pos:], uint16(len(key))) @@ -70,7 +70,7 @@ func (db *DB) hEncodeHashKey(key []byte, field []byte) []byte { } func (db *DB) hDecodeHashKey(ek []byte) ([]byte, []byte, error) { - if len(ek) < 5 || ek[0] != db.index || ek[1] != hashType { + if len(ek) < 5 || ek[0] != db.index || ek[1] != HashType { return nil, nil, errHashKey } @@ -151,7 +151,7 @@ func (db *DB) hExpireAt(key []byte, when int64) (int64, error) { if hlen, err := db.HLen(key); err != nil || hlen == 0 { return 0, err } else { - db.expireAt(t, hashType, key, when) + db.expireAt(t, HashType, key, when) if err := t.Commit(); err != nil { return 0, err } @@ -304,7 +304,7 @@ func (db *DB) hIncrSize(key []byte, delta int64) (int64, error) { if size <= 0 { size = 0 t.Delete(sk) - db.rmExpire(t, hashType, key) + db.rmExpire(t, HashType, key) } else { t.Put(sk, PutInt64(size)) } @@ -428,7 +428,7 @@ func (db *DB) HClear(key []byte) (int64, error) { defer t.Unlock() num := db.hDelete(t, key) - db.rmExpire(t, hashType, key) + db.rmExpire(t, HashType, key) err := t.Commit() return num, err @@ -445,7 +445,7 @@ func (db *DB) HMclear(keys ...[]byte) (int64, error) { } db.hDelete(t, key) - db.rmExpire(t, hashType, key) + db.rmExpire(t, HashType, key) } err := t.Commit() @@ -455,18 +455,18 @@ func (db *DB) HMclear(keys ...[]byte) (int64, error) { func (db *DB) hFlush() (drop int64, err error) { minKey := make([]byte, 2) minKey[0] = db.index - minKey[1] = hashType + minKey[1] = HashType maxKey := make([]byte, 2) maxKey[0] = db.index - maxKey[1] = hSizeType + 1 + maxKey[1] = HSizeType + 1 t := db.kvTx t.Lock() defer t.Unlock() drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, hashType) + err = db.expFlush(t, HashType) err = t.Commit() return @@ -530,7 +530,7 @@ func (db *DB) HTTL(key []byte) (int64, error) { return -1, err } - return db.ttl(hashType, key) + return db.ttl(HashType, key) } func (db *DB) HPersist(key []byte) (int64, error) { @@ -542,7 +542,7 @@ func (db *DB) HPersist(key []byte) (int64, error) { t.Lock() defer t.Unlock() - n, err := db.rmExpire(t, hashType, key) + n, err := db.rmExpire(t, HashType, key) if err != nil { return 0, err } diff --git a/ledis/t_kv.go b/ledis/t_kv.go index b62700b..116aa87 100644 --- a/ledis/t_kv.go +++ b/ledis/t_kv.go @@ -31,13 +31,13 @@ func checkValueSize(value []byte) error { func (db *DB) encodeKVKey(key []byte) []byte { ek := make([]byte, len(key)+2) ek[0] = db.index - ek[1] = kvType + ek[1] = KVType copy(ek[2:], key) return ek } func (db *DB) decodeKVKey(ek []byte) ([]byte, error) { - if len(ek) < 2 || ek[0] != db.index || ek[1] != kvType { + if len(ek) < 2 || ek[0] != db.index || ek[1] != KVType { return nil, errKVKey } @@ -51,7 +51,7 @@ func (db *DB) encodeKVMinKey() []byte { func (db *DB) encodeKVMaxKey() []byte { ek := db.encodeKVKey(nil) - ek[len(ek)-1] = kvType + 1 + ek[len(ek)-1] = KVType + 1 return ek } @@ -100,7 +100,7 @@ func (db *DB) setExpireAt(key []byte, when int64) (int64, error) { if exist, err := db.Exists(key); err != nil || exist == 0 { return 0, err } else { - db.expireAt(t, kvType, key, when) + db.expireAt(t, KVType, key, when) if err := t.Commit(); err != nil { return 0, err } @@ -132,7 +132,7 @@ func (db *DB) Del(keys ...[]byte) (int64, error) { for i, k := range keys { t.Delete(codedKeys[i]) - db.rmExpire(t, kvType, k) + db.rmExpire(t, KVType, k) } err := t.Commit() @@ -317,7 +317,7 @@ func (db *DB) flush() (drop int64, err error) { defer t.Unlock() drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, kvType) + err = db.expFlush(t, KVType) err = t.Commit() return @@ -382,7 +382,7 @@ func (db *DB) TTL(key []byte) (int64, error) { return -1, err } - return db.ttl(kvType, key) + return db.ttl(KVType, key) } func (db *DB) Persist(key []byte) (int64, error) { @@ -393,7 +393,7 @@ func (db *DB) Persist(key []byte) (int64, error) { t := db.kvTx t.Lock() defer t.Unlock() - n, err := db.rmExpire(t, kvType, key) + n, err := db.rmExpire(t, KVType, key) if err != nil { return 0, err } diff --git a/ledis/t_list.go b/ledis/t_list.go index 2f17609..a7ab1ac 100644 --- a/ledis/t_list.go +++ b/ledis/t_list.go @@ -23,14 +23,14 @@ var errListSeq = errors.New("invalid list sequence, overflow") func (db *DB) lEncodeMetaKey(key []byte) []byte { buf := make([]byte, len(key)+2) buf[0] = db.index - buf[1] = lMetaType + buf[1] = LMetaType copy(buf[2:], key) return buf } func (db *DB) lDecodeMetaKey(ek []byte) ([]byte, error) { - if len(ek) < 2 || ek[0] != db.index || ek[1] != lMetaType { + if len(ek) < 2 || ek[0] != db.index || ek[1] != LMetaType { return nil, errLMetaKey } @@ -43,7 +43,7 @@ func (db *DB) lEncodeListKey(key []byte, seq int32) []byte { pos := 0 buf[pos] = db.index pos++ - buf[pos] = listType + buf[pos] = ListType pos++ binary.BigEndian.PutUint16(buf[pos:], uint16(len(key))) @@ -58,7 +58,7 @@ func (db *DB) lEncodeListKey(key []byte, seq int32) []byte { } func (db *DB) lDecodeListKey(ek []byte) (key []byte, seq int32, err error) { - if len(ek) < 8 || ek[0] != db.index || ek[1] != listType { + if len(ek) < 8 || ek[0] != db.index || ek[1] != ListType { err = errListKey return } @@ -175,7 +175,7 @@ func (db *DB) lpop(key []byte, whereSeq int32) ([]byte, error) { t.Delete(itemKey) size := db.lSetMeta(metaKey, headSeq, tailSeq) if size == 0 { - db.rmExpire(t, hashType, key) + db.rmExpire(t, HashType, key) } err = t.Commit() @@ -264,7 +264,7 @@ func (db *DB) lExpireAt(key []byte, when int64) (int64, error) { if llen, err := db.LLen(key); err != nil || llen == 0 { return 0, err } else { - db.expireAt(t, listType, key, when) + db.expireAt(t, ListType, key, when) if err := t.Commit(); err != nil { return 0, err } @@ -398,7 +398,7 @@ func (db *DB) LClear(key []byte) (int64, error) { defer t.Unlock() num := db.lDelete(t, key) - db.rmExpire(t, listType, key) + db.rmExpire(t, ListType, key) err := t.Commit() return num, err @@ -415,7 +415,7 @@ func (db *DB) LMclear(keys ...[]byte) (int64, error) { } db.lDelete(t, key) - db.rmExpire(t, listType, key) + db.rmExpire(t, ListType, key) } @@ -426,18 +426,18 @@ func (db *DB) LMclear(keys ...[]byte) (int64, error) { func (db *DB) lFlush() (drop int64, err error) { minKey := make([]byte, 2) minKey[0] = db.index - minKey[1] = listType + minKey[1] = ListType maxKey := make([]byte, 2) maxKey[0] = db.index - maxKey[1] = lMetaType + 1 + maxKey[1] = LMetaType + 1 t := db.listTx t.Lock() defer t.Unlock() drop, err = db.flushRegion(t, minKey, maxKey) - err = db.expFlush(t, listType) + err = db.expFlush(t, ListType) err = t.Commit() return @@ -464,7 +464,7 @@ func (db *DB) LTTL(key []byte) (int64, error) { return -1, err } - return db.ttl(listType, key) + return db.ttl(ListType, key) } func (db *DB) LPersist(key []byte) (int64, error) { @@ -476,7 +476,7 @@ func (db *DB) LPersist(key []byte) (int64, error) { t.Lock() defer t.Unlock() - n, err := db.rmExpire(t, listType, key) + n, err := db.rmExpire(t, ListType, key) if err != nil { return 0, err } diff --git a/ledis/t_ttl.go b/ledis/t_ttl.go index f1614bd..aa34b73 100644 --- a/ledis/t_ttl.go +++ b/ledis/t_ttl.go @@ -26,7 +26,7 @@ func (db *DB) expEncodeTimeKey(dataType byte, key []byte, when int64) []byte { buf := make([]byte, len(key)+11) buf[0] = db.index - buf[1] = expTimeType + buf[1] = ExpTimeType buf[2] = dataType pos := 3 @@ -42,7 +42,7 @@ func (db *DB) expEncodeMetaKey(dataType byte, key []byte) []byte { buf := make([]byte, len(key)+3) buf[0] = db.index - buf[1] = expMetaType + buf[1] = ExpMetaType buf[2] = dataType pos := 3 @@ -52,7 +52,7 @@ func (db *DB) expEncodeMetaKey(dataType byte, key []byte) []byte { } func (db *DB) expDecodeMetaKey(mk []byte) (byte, []byte, error) { - if len(mk) <= 3 || mk[0] != db.index || mk[1] != expMetaType { + if len(mk) <= 3 || mk[0] != db.index || mk[1] != ExpMetaType { return 0, nil, errExpMetaKey } @@ -60,7 +60,7 @@ func (db *DB) expDecodeMetaKey(mk []byte) (byte, []byte, error) { } func (db *DB) expDecodeTimeKey(tk []byte) (byte, []byte, int64, error) { - if len(tk) < 11 || tk[0] != db.index || tk[1] != expTimeType { + if len(tk) < 11 || tk[0] != db.index || tk[1] != ExpTimeType { return 0, nil, 0, errExpTimeKey } @@ -114,12 +114,12 @@ func (db *DB) rmExpire(t *tx, dataType byte, key []byte) (int64, error) { func (db *DB) expFlush(t *tx, dataType byte) (err error) { minKey := make([]byte, 3) minKey[0] = db.index - minKey[1] = expTimeType + minKey[1] = ExpTimeType minKey[2] = dataType maxKey := make([]byte, 3) maxKey[0] = db.index - maxKey[1] = expMetaType + maxKey[1] = ExpMetaType maxKey[2] = dataType + 1 _, err = db.flushRegion(t, minKey, maxKey) @@ -153,7 +153,7 @@ func (eli *elimination) active() { db := eli.db dbGet := db.db.Get - minKey := db.expEncodeTimeKey(noneType, nil, 0) + minKey := db.expEncodeTimeKey(NoneType, nil, 0) maxKey := db.expEncodeTimeKey(maxDataType, nil, now) it := db.db.RangeLimitIterator(minKey, maxKey, leveldb.RangeROpen, 0, -1) diff --git a/ledis/t_zset.go b/ledis/t_zset.go index 9a2f105..4931ded 100644 --- a/ledis/t_zset.go +++ b/ledis/t_zset.go @@ -45,14 +45,14 @@ func checkZSetKMSize(key []byte, member []byte) error { func (db *DB) zEncodeSizeKey(key []byte) []byte { buf := make([]byte, len(key)+2) buf[0] = db.index - buf[1] = zSizeType + buf[1] = ZSizeType copy(buf[2:], key) return buf } func (db *DB) zDecodeSizeKey(ek []byte) ([]byte, error) { - if len(ek) < 2 || ek[0] != db.index || ek[1] != zSizeType { + if len(ek) < 2 || ek[0] != db.index || ek[1] != ZSizeType { return nil, errZSizeKey } @@ -66,7 +66,7 @@ func (db *DB) zEncodeSetKey(key []byte, member []byte) []byte { buf[pos] = db.index pos++ - buf[pos] = zsetType + buf[pos] = ZSetType pos++ binary.BigEndian.PutUint16(buf[pos:], uint16(len(key))) @@ -84,7 +84,7 @@ func (db *DB) zEncodeSetKey(key []byte, member []byte) []byte { } func (db *DB) zDecodeSetKey(ek []byte) ([]byte, []byte, error) { - if len(ek) < 5 || ek[0] != db.index || ek[1] != zsetType { + if len(ek) < 5 || ek[0] != db.index || ek[1] != ZSetType { return nil, nil, errZSetKey } @@ -121,7 +121,7 @@ func (db *DB) zEncodeScoreKey(key []byte, member []byte, score int64) []byte { buf[pos] = db.index pos++ - buf[pos] = zScoreType + buf[pos] = ZScoreType pos++ binary.BigEndian.PutUint16(buf[pos:], uint16(len(key))) @@ -158,7 +158,7 @@ func (db *DB) zEncodeStopScoreKey(key []byte, score int64) []byte { } func (db *DB) zDecodeScoreKey(ek []byte) (key []byte, member []byte, score int64, err error) { - if len(ek) < 14 || ek[0] != db.index || ek[1] != zScoreType { + if len(ek) < 14 || ek[0] != db.index || ek[1] != ZScoreType { err = errZScoreKey return } @@ -260,7 +260,7 @@ func (db *DB) zExpireAt(key []byte, when int64) (int64, error) { if zcnt, err := db.ZCard(key); err != nil || zcnt == 0 { return 0, err } else { - db.expireAt(t, zsetType, key, when) + db.expireAt(t, ZSetType, key, when) if err := t.Commit(); err != nil { return 0, err } @@ -314,7 +314,7 @@ func (db *DB) zIncrSize(t *tx, key []byte, delta int64) (int64, error) { if size <= 0 { size = 0 t.Delete(sk) - db.rmExpire(t, zsetType, key) + db.rmExpire(t, ZSetType, key) } else { t.Put(sk, PutInt64(size)) } @@ -734,11 +734,11 @@ func (db *DB) zFlush() (drop int64, err error) { minKey := make([]byte, 2) minKey[0] = db.index - minKey[1] = zsetType + minKey[1] = ZSetType maxKey := make([]byte, 2) maxKey[0] = db.index - maxKey[1] = zScoreType + 1 + maxKey[1] = ZScoreType + 1 it := db.db.RangeLimitIterator(minKey, maxKey, leveldb.RangeROpen, 0, -1) defer it.Close() @@ -753,7 +753,7 @@ func (db *DB) zFlush() (drop int64, err error) { } } - db.expFlush(t, zsetType) + db.expFlush(t, ZSetType) err = t.Commit() return @@ -819,7 +819,7 @@ func (db *DB) ZTTL(key []byte) (int64, error) { return -1, err } - return db.ttl(zsetType, key) + return db.ttl(ZSetType, key) } func (db *DB) ZPersist(key []byte) (int64, error) { @@ -831,7 +831,7 @@ func (db *DB) ZPersist(key []byte) (int64, error) { t.Lock() defer t.Unlock() - n, err := db.rmExpire(t, zsetType, key) + n, err := db.rmExpire(t, ZSetType, key) if err != nil { return 0, err } diff --git a/server/cmd_bin.go b/server/cmd_bit.go similarity index 100% rename from server/cmd_bin.go rename to server/cmd_bit.go diff --git a/server/cmd_bin_test.go b/server/cmd_bit_test.go similarity index 100% rename from server/cmd_bin_test.go rename to server/cmd_bit_test.go