diff --git a/ledis/t_bit.go b/ledis/t_bit.go index bc208a0..f7edddd 100644 --- a/ledis/t_bit.go +++ b/ledis/t_bit.go @@ -231,15 +231,20 @@ func (db *DB) bSetMeta(t *tx, key []byte, tailSeq uint32, tailOff uint32) { } func (db *DB) bUpdateMeta(t *tx, key []byte, seq uint32, off uint32) (tailSeq uint32, tailOff uint32, err error) { - var ts, to int32 - if ts, to, err = db.bGetMeta(key); err != nil { + var tseq, toff int32 + var update bool = false + + if tseq, toff, err = db.bGetMeta(key); err != nil { return + } else if tseq < 0 { + update = true } else { - tailSeq = uint32(MaxInt32(ts, 0)) - tailOff = uint32(MaxInt32(to, 0)) + tailSeq = uint32(MaxInt32(tseq, 0)) + tailOff = uint32(MaxInt32(toff, 0)) + update = (seq > tailSeq || (seq == tailSeq && off > tailOff)) } - if seq > tailSeq || (seq == tailSeq && off > tailOff) { + if update { db.bSetMeta(t, key, seq, off) tailSeq = seq tailOff = off diff --git a/ledis/t_bit_test.go b/ledis/t_bit_test.go index a356539..9103523 100644 --- a/ledis/t_bit_test.go +++ b/ledis/t_bit_test.go @@ -41,6 +41,7 @@ func TestBinary(t *testing.T) { testOpXor(t) testOpNot(t) testMSetBit(t) + testBitExpire(t) } func testSimple(t *testing.T) { @@ -518,3 +519,20 @@ func testMSetBit(t *testing.T) { return } + +func testBitExpire(t *testing.T) { + db := getTestDB() + db.FlushAll() + + key := []byte("test_b_ttl") + + db.BSetBit(key, 0, 1) + + if res, err := db.BExpire(key, 100); res != 1 || err != nil { + t.Fatal(false) + } + + if ttl, err := db.BTTL(key); ttl != 100 || err != nil { + t.Fatal(false) + } +}