Skip to content

Commit

Permalink
cover bitFlip
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshi-099 committed Mar 27, 2024
1 parent 0929cf6 commit f2a5454
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
8 changes: 4 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ var cmdTable = []Cmd{
}},
{OpBitFlip, func(db *DB, reader *codeman.Reader) error {
// key, offset
return db.BitFlip(reader.Str(), reader.Uint32())
return db.BitFlip(reader.Str(), reader.Uint32(), reader.Uint32())
}},
{OpBitMerge, func(db *DB, reader *codeman.Reader) error {
// op, key, items
Expand Down Expand Up @@ -709,13 +709,13 @@ func (db *DB) BitSet(key string, val bool, offsets ...uint32) (int, error) {
}

// BitFlip
func (db *DB) BitFlip(key string, offset uint32) error {
func (db *DB) BitFlip(key string, start, end uint32) error {
bm, err := db.fetchBitMap(key)
if err != nil {
return err
}
db.encode(newCodec(OpBitFlip).Str(key).Uint32(offset))
bm.Flip(uint64(offset))
db.encode(newCodec(OpBitFlip).Str(key).Uint32(start).Uint32(end))
bm.Flip(uint64(start), uint64(end))

return nil
}
Expand Down
32 changes: 22 additions & 10 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ func createDB() (*DB, error) {
func TestDB(t *testing.T) {
println("===== TestDB =====")
assert := assert.New(t)
const N = 5000

db, err := createDB()
assert.Nil(err)

// Test db operations
for i := 0; i < 20000; i++ {
for i := 0; i < N; i++ {
key := strconv.Itoa(i)
val := []byte(strconv.Itoa(i))
db.Set("set-"+key, val)
Expand All @@ -47,7 +48,7 @@ func TestDB(t *testing.T) {
var ts int64
now := time.Now().UnixNano()

for i := 0; i < 10000; i++ {
for i := 0; i < N; i++ {
key := strconv.Itoa(i)
// set
val, _, err := db.Get("set-" + key)
Expand Down Expand Up @@ -76,8 +77,8 @@ func TestDB(t *testing.T) {
count++
return true
})
assert.Equal(count, 40000)
assert.Equal(int(db.Len()), 60000)
assert.Equal(count, N*2)
assert.Equal(int(db.Len()), N*3)

// GC
db.GC()
Expand All @@ -86,8 +87,8 @@ func TestDB(t *testing.T) {
count++
return true
})
assert.Equal(count, 40000)
assert.Equal(int(db.Len()), 40000)
assert.Equal(count, N*2)
assert.Equal(int(db.Len()), N*2)

// Error
val, _, err := db.Get("map")
Expand Down Expand Up @@ -459,9 +460,6 @@ func TestBitmap(t *testing.T) {
assert.True(ok)
assert.Nil(err)

// TODO
db.BitFlip(key, uint32(i))

// Error
db.BitSet("my-bitset", true, 1)
db.Set("none", []byte("1"))
Expand All @@ -474,7 +472,7 @@ func TestBitmap(t *testing.T) {
assert.False(ok)
assert.ErrorContains(err, ErrWrongType.Error())

err = db.BitFlip("none", uint32(i))
err = db.BitFlip("none", uint32(i), uint32(i+1))
assert.ErrorContains(err, ErrWrongType.Error())

m, err := db.BitArray("none")
Expand Down Expand Up @@ -533,6 +531,20 @@ func TestBitmap(t *testing.T) {
assert.ElementsMatch(m1, m2)
}

// flip
for i := 0; i < 1000; i++ {
db.BitSet("bf", true, uint32(i))
}
db.BitFlip("bf", 500, 1500)
for i := 500; i < 1000; i++ {
ok, _ := db.BitTest("bf", uint32(i))
assert.False(ok)
}
for i := 1000; i < 1500; i++ {
ok, _ := db.BitTest("bf", uint32(i))
assert.True(ok)
}

// reload
db.Close()
db, err = Open(db.GetOptions())
Expand Down
4 changes: 2 additions & 2 deletions structx/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ func (b *Bitmap) Test(i uint32) bool {
}

// Flip
func (b *Bitmap) Flip(i uint64) {
func (b *Bitmap) Flip(start, end uint64) {
b.Lock()
b.bm.Flip(i, i)
b.bm.Flip(start, end)
b.Unlock()
}

Expand Down

0 comments on commit f2a5454

Please sign in to comment.