Skip to content

Commit

Permalink
add kv isEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
asabya committed May 9, 2023
1 parent d65e581 commit 31abc64
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/collection/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ func (idx *Index) DeleteIndex(encryptionPassword string) error {
return nil
}

func (idx *Index) IsEmpty(encryptionPassword string) (bool, error) {
if idx.memDB == nil || idx.memDB.Entries == nil {
manifest, err := idx.loadManifest(idx.name, encryptionPassword)
if err != nil {
return true, err
}
idx.memDB = manifest
}

return len(idx.memDB.Entries) == 0, nil
}

// CountIndex counts the entries in an index.
func (idx *Index) CountIndex(encryptionPassword string) (uint64, error) {
if idx.memDB == nil || idx.memDB.Entries == nil {
Expand Down
15 changes: 15 additions & 0 deletions pkg/collection/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,21 @@ func (kv *KeyValue) KVCount(name, encryptionPassword string) (*TableKeyCount, er
}
}

// IsEmpty checks if the given key value table is empty.
func (kv *KeyValue) IsEmpty(name, encryptionPassword string) (bool, error) {
kv.openKVTMu.Lock()
defer kv.openKVTMu.Unlock()
if table, ok := kv.openKVTables[name]; ok {
return table.index.IsEmpty(table.index.encryptionPassword)
} else {
idx, err := OpenIndex(kv.podName, defaultCollectionName, name, encryptionPassword, kv.fd, kv.ai, kv.user, kv.client, kv.logger)
if err != nil {
return true, err
}
return idx.IsEmpty(idx.encryptionPassword)
}
}

// KVPut inserts a given key and value in to the KV table.
func (kv *KeyValue) KVPut(name, key string, value []byte) error {
if kv.fd.IsReadOnlyFeed() { // skipcq: TCV-001
Expand Down

0 comments on commit 31abc64

Please sign in to comment.