Skip to content

Commit

Permalink
feat: complete memory keys method
Browse files Browse the repository at this point in the history
  • Loading branch information
qishenonly committed Sep 10, 2023
1 parent c0214bf commit 1e31cbc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
31 changes: 30 additions & 1 deletion db/memory/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,36 @@ func (d *Db) Delete(key []byte) error {
}

func (d *Db) Keys() ([][]byte, error) {
panic("implement me")
d.mux.RLock()
defer d.mux.RUnlock()

// Create a slice to hold the keys
keys := make([][]byte, 0)

// Collect keys from the active MemTable
for key := range d.mem.table {
keys = append(keys, []byte(key))
}

// Collect keys from the immutable MemTables (WAL data MemTables)
for _, mt := range d.walDataMtList {
for key := range mt.table {
keys = append(keys, []byte(key))
}
}

// Collect keys from the old MemTables (flushed MemTables)
for _, mt := range d.oldList {
for key := range mt.table {
keys = append(keys, []byte(key))
}
}

// Collect keys from the persistent storage (your DB implementation)
persistentKeys := d.db.GetListKeys()
keys = append(keys, persistentKeys...)

return keys, nil
}

func (d *Db) Close() error {
Expand Down
24 changes: 24 additions & 0 deletions db/memory/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,27 @@ func TestPutAndGet(t *testing.T) {
end = time.Now()
fmt.Println("get time: ", end.Sub(start).String())
}

func TestDb_Keys(t *testing.T) {
err := os.Mkdir("./flydb", os.ModePerm)
memOpt := config.DefaultDbMemoryOptions
memOpt.LogNum = 100
memOpt.FileSize = 256 * 1024 * 1024
memOpt.TotalMemSize = 2 * 1024 * 1024 * 1024
memOpt.Option.DirPath = "./"

db, err := NewDB(memOpt)
defer db.Clean()
assert.Nil(t, err)
assert.NotNil(t, db)

for n := 0; n < 100; n++ {
err = db.Put(randkv.GetTestKey(n), randkv.RandomValue(24))
assert.Nil(t, err)
}

keys, err := db.Keys()
assert.Nil(t, err)
assert.Equal(t, 100, len(keys))
t.Log(keys)
}

0 comments on commit 1e31cbc

Please sign in to comment.