@@ -55,7 +55,7 @@ func NewNode(cs cbor.IpldStore, options ...Option) *Node {
55
55
}
56
56
57
57
type KV struct {
58
- Key string
58
+ Key [] byte
59
59
Value * cbg.Deferred
60
60
}
61
61
@@ -68,7 +68,7 @@ type Pointer struct {
68
68
}
69
69
70
70
func (n * Node ) Find (ctx context.Context , k string , out interface {}) error {
71
- return n .getValue (ctx , & hashBits {b : hash (k )}, k , func (kv * KV ) error {
71
+ return n .getValue (ctx , & hashBits {b : hash ([] byte ( k ) )}, k , func (kv * KV ) error {
72
72
// used to just see if the thing exists in the set
73
73
if out == nil {
74
74
return nil
@@ -88,15 +88,16 @@ func (n *Node) Find(ctx context.Context, k string, out interface{}) error {
88
88
89
89
func (n * Node ) FindRaw (ctx context.Context , k string ) ([]byte , error ) {
90
90
var ret []byte
91
- err := n .getValue (ctx , & hashBits {b : hash (k )}, k , func (kv * KV ) error {
91
+ err := n .getValue (ctx , & hashBits {b : hash ([] byte ( k ) )}, k , func (kv * KV ) error {
92
92
ret = kv .Value .Raw
93
93
return nil
94
94
})
95
95
return ret , err
96
96
}
97
97
98
98
func (n * Node ) Delete (ctx context.Context , k string ) error {
99
- return n .modifyValue (ctx , & hashBits {b : hash (k )}, k , nil )
99
+ kb := []byte (k )
100
+ return n .modifyValue (ctx , & hashBits {b : hash (kb )}, kb , nil )
100
101
}
101
102
102
103
var ErrNotFound = fmt .Errorf ("not found" )
@@ -125,7 +126,7 @@ func (n *Node) getValue(ctx context.Context, hv *hashBits, k string, cb func(*KV
125
126
}
126
127
127
128
for _ , kv := range c .KVs {
128
- if kv .Key == k {
129
+ if string ( kv .Key ) == k {
129
130
return cb (kv )
130
131
}
131
132
}
@@ -215,12 +216,15 @@ func (n *Node) Flush(ctx context.Context) error {
215
216
// SetRaw sets key k to cbor bytes raw
216
217
func (n * Node ) SetRaw (ctx context.Context , k string , raw []byte ) error {
217
218
d := & cbg.Deferred {Raw : raw }
218
- return n .modifyValue (ctx , & hashBits {b : hash (k )}, k , d )
219
+ kb := []byte (k )
220
+ return n .modifyValue (ctx , & hashBits {b : hash (kb )}, kb , d )
219
221
}
220
222
221
223
func (n * Node ) Set (ctx context.Context , k string , v interface {}) error {
222
224
var d * cbg.Deferred
223
225
226
+ kb := []byte (k )
227
+
224
228
cm , ok := v .(cbg.CBORMarshaler )
225
229
if ok {
226
230
buf := new (bytes.Buffer )
@@ -236,7 +240,7 @@ func (n *Node) Set(ctx context.Context, k string, v interface{}) error {
236
240
d = & cbg.Deferred {Raw : b }
237
241
}
238
242
239
- return n .modifyValue (ctx , & hashBits {b : hash (k )}, k , d )
243
+ return n .modifyValue (ctx , & hashBits {b : hash (kb )}, kb , d )
240
244
}
241
245
242
246
func (n * Node ) cleanChild (chnd * Node , cindex byte ) error {
@@ -273,7 +277,7 @@ func (n *Node) cleanChild(chnd *Node, cindex byte) error {
273
277
}
274
278
}
275
279
276
- func (n * Node ) modifyValue (ctx context.Context , hv * hashBits , k string , v * cbg.Deferred ) error {
280
+ func (n * Node ) modifyValue (ctx context.Context , hv * hashBits , k [] byte , v * cbg.Deferred ) error {
277
281
idx , err := hv .Next (n .bitWidth )
278
282
if err != nil {
279
283
return ErrMaxDepth
@@ -308,7 +312,7 @@ func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k string, v *cbg.D
308
312
309
313
if v == nil {
310
314
for i , p := range child .KVs {
311
- if p .Key == k {
315
+ if bytes . Equal ( p .Key , k ) {
312
316
if len (child .KVs ) == 1 {
313
317
return n .rmChild (cindex , idx )
314
318
}
@@ -323,7 +327,7 @@ func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k string, v *cbg.D
323
327
324
328
// check if key already exists
325
329
for _ , p := range child .KVs {
326
- if p .Key == k {
330
+ if bytes . Equal ( p .Key , k ) {
327
331
p .Value = v
328
332
return nil
329
333
}
@@ -356,7 +360,7 @@ func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k string, v *cbg.D
356
360
// otherwise insert the new element into the array in order
357
361
np := & KV {Key : k , Value : v }
358
362
for i := 0 ; i < len (child .KVs ); i ++ {
359
- if k < child .KVs [i ].Key {
363
+ if bytes . Compare ( k , child .KVs [i ].Key ) < 0 {
360
364
child .KVs = append (child .KVs [:i ], append ([]* KV {np }, child .KVs [i :]... )... )
361
365
return nil
362
366
}
@@ -365,7 +369,7 @@ func (n *Node) modifyValue(ctx context.Context, hv *hashBits, k string, v *cbg.D
365
369
return nil
366
370
}
367
371
368
- func (n * Node ) insertChild (idx int , k string , v * cbg.Deferred ) error {
372
+ func (n * Node ) insertChild (idx int , k [] byte , v * cbg.Deferred ) error {
369
373
if v == nil {
370
374
return ErrNotFound
371
375
}
@@ -441,7 +445,8 @@ func (n *Node) ForEach(ctx context.Context, f func(k string, val interface{}) er
441
445
}
442
446
} else {
443
447
for _ , kv := range p .KVs {
444
- if err := f (kv .Key , kv .Value ); err != nil {
448
+ // TODO: consider removing 'strings as keys' from every interface, go full-on bytes everywhere
449
+ if err := f (string (kv .Key ), kv .Value ); err != nil {
445
450
return err
446
451
}
447
452
}
0 commit comments