feature-21, ajoute de la gestion des subnets #24
1 changed files with 33 additions and 0 deletions
f-21: code: add list prefix
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
commit
906201a1b6
33
pkg/db/kv/listByPrefix.go
Normal file
33
pkg/db/kv/listByPrefix.go
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
package kv
|
||||
|
||||
import (
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
// ListByPrefix returns all key-value pairs whose key starts with prefix.
|
||||
func ListByPrefix(db *badger.DB, prefix string) (map[string]string, error) {
|
||||
result := make(map[string]string)
|
||||
p := []byte(prefix)
|
||||
|
||||
err := db.View(func(txn *badger.Txn) error {
|
||||
opts := badger.DefaultIteratorOptions
|
||||
opts.PrefetchSize = 10
|
||||
|
||||
it := txn.NewIterator(opts)
|
||||
defer it.Close()
|
||||
|
||||
for it.Seek(p); it.ValidForPrefix(p); it.Next() {
|
||||
item := it.Item()
|
||||
key := string(item.Key())
|
||||
|
||||
val, err := item.ValueCopy(nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
result[key] = string(val)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
return result, err
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue