-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from manosriram/feat/reader
add keyreader and keyvaluereader
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package nimbusdb | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/google/btree" | ||
) | ||
|
||
// KeyReader iterates through each key matching given prefix. | ||
// If prefix is an empty string, all keys are matched. | ||
// The second argument is a callback function which contains the key. | ||
func (db *Db) KeyReader(prefix string, handler func(k []byte)) { | ||
db.keyDir.tree.Ascend(func(it btree.Item) bool { | ||
key := it.(*item).key | ||
if strings.HasPrefix(string(key), prefix) { | ||
handler(key) | ||
} | ||
return true | ||
}) | ||
} | ||
|
||
// KeyValueReader iterates through each key-value pair matching given key's prefix. | ||
// If prefix is an empty string, all key-value pairs are matched. | ||
// The second argument is a callback function which contains key and the value. | ||
func (db *Db) KeyValueReader(keyPrefix string, handler func(k []byte, v []byte)) (bool, error) { | ||
db.keyDir.tree.Ascend(func(it btree.Item) bool { | ||
key := it.(*item).key | ||
if strings.HasPrefix(string(key), keyPrefix) { | ||
v, err := db.getKeyDir(key) | ||
if err != nil { | ||
return false | ||
} | ||
handler(it.(*item).key, v.v) | ||
} | ||
return true | ||
}) | ||
return false, nil | ||
} |