@@ -44,6 +44,7 @@ import (
4444// slash is required because of: invalid datastore key: NODES:/peers/keys/AASAQAISEAXNRKHMX2O3AA26JM7NGIWUPOGIITJ2UHHXGX4OWIEKPNAW6YCSK/priv
4545const (
4646 requiredPrefixSlash = "/"
47+ nodesPrefix = "NODES"
4748
4849 ErrNilNodeRepo = local_store .DBError ("node repo is nil" )
4950)
@@ -71,7 +72,8 @@ type NodeRepo struct {
7172 BootstrapSelfHashHex string
7273}
7374
74- func NewNodeRepo (db NodeStorer , prefix string ) * NodeRepo {
75+ func NewNodeRepo (db NodeStorer ) * NodeRepo {
76+ prefix := nodesPrefix
7577 if ! strings .HasPrefix (prefix , requiredPrefixSlash ) {
7678 prefix = requiredPrefixSlash + prefix
7779 }
@@ -83,10 +85,6 @@ func NewNodeRepo(db NodeStorer, prefix string) *NodeRepo {
8385 return nr
8486}
8587
86- func (d * NodeRepo ) Prefix () string {
87- return d .prefix
88- }
89-
9088func (d * NodeRepo ) Put (ctx context.Context , key datastore.Key , value []byte ) error {
9189 if d == nil || d .db == nil {
9290 return ErrNilNodeRepo
@@ -345,9 +343,7 @@ func (d *NodeRepo) query(tx *local_store.Txn, q datastore.Query) (_ datastore.Re
345343 opt := local_store .DefaultIteratorOptions
346344 opt .PrefetchValues = ! q .KeysOnly
347345
348- key := strings .TrimPrefix (q .Prefix , "/" )
349- prefix := local_store .NewPrefixBuilder (d .prefix ).AddRootID (key ).Build ().Bytes ()
350- opt .Prefix = prefix
346+ opt .Prefix = d .storageQueryPrefix (q .Prefix )
351347
352348 // Handle ordering
353349 if len (q .Orders ) > 0 {
@@ -405,7 +401,7 @@ func (d *NodeRepo) query(tx *local_store.Txn, q datastore.Query) (_ datastore.Re
405401 matches := true
406402 check := func (value []byte ) error {
407403 e := datastore.DsEntry {
408- Key : string (item .Key ()),
404+ Key : d . resultKeyFromStorageKey ( string (item .Key () )),
409405 Value : value ,
410406 Size : int (item .ValueSize ()),
411407 }
@@ -443,7 +439,7 @@ func (d *NodeRepo) query(tx *local_store.Txn, q datastore.Query) (_ datastore.Re
443439 return
444440 }
445441 item := it .Item ()
446- e := datastore.DsEntry {Key : string (item .Key ())}
442+ e := datastore.DsEntry {Key : d . resultKeyFromStorageKey ( string (item .Key () ))}
447443
448444 var result datastore.Result
449445 if ! q .KeysOnly {
@@ -481,6 +477,30 @@ func (d *NodeRepo) query(tx *local_store.Txn, q datastore.Query) (_ datastore.Re
481477 return results , nil
482478}
483479
480+ func (d * NodeRepo ) storageQueryPrefix (queryPrefix string ) []byte {
481+ prefix := strings .TrimSuffix (datastore .NewKey (queryPrefix ).String (), requiredPrefixSlash )
482+ base := strings .TrimSuffix (d .prefix , requiredPrefixSlash )
483+
484+ if prefix == "" {
485+ return []byte (base + requiredPrefixSlash )
486+ }
487+
488+ return []byte (base + prefix + requiredPrefixSlash )
489+ }
490+
491+ func (d * NodeRepo ) resultKeyFromStorageKey (storageKey string ) string {
492+ if storageKey == d .prefix {
493+ return requiredPrefixSlash
494+ }
495+
496+ trimPrefix := d .prefix + requiredPrefixSlash
497+ if strings .HasPrefix (storageKey , trimPrefix ) { //nolint:modernize
498+ return requiredPrefixSlash + strings .TrimPrefix (storageKey , trimPrefix )
499+ }
500+
501+ return storageKey
502+ }
503+
484504func filter (filters []datastore.Filter , entry datastore.DsEntry ) bool {
485505 for _ , f := range filters {
486506 if ! f .Filter (entry ) {
0 commit comments