@@ -55,7 +55,7 @@ func TestGetValue(t *testing.T) {
55
55
_ , err := ts .GetValue (ctx , TestKey )
56
56
require .ErrorIs (err , ErrKeyNotSpecified , "No error thrown." )
57
57
// SetScope
58
- ts .SetScope (ctx , [][]byte {TestKey }, map [Key ][]byte {ToStateKeyArray (TestKey ): TestVal })
58
+ ts .SetScope (ctx , [][]byte {TestKey }, map [* Key ][]byte {ToStateKeyArray (TestKey ): TestVal })
59
59
val , err := ts .GetValue (ctx , TestKey )
60
60
require .NoError (err , "Error getting value." )
61
61
require .Equal (TestVal , val , "Value was not saved correctly." )
@@ -66,7 +66,7 @@ func TestGetValueNoStorage(t *testing.T) {
66
66
ctx := context .TODO ()
67
67
ts := New (10 )
68
68
// SetScope but dont add to storage
69
- ts .SetScope (ctx , [][]byte {TestKey }, map [Key ][]byte {})
69
+ ts .SetScope (ctx , [][]byte {TestKey }, map [* Key ][]byte {})
70
70
_ , err := ts .GetValue (ctx , TestKey )
71
71
require .ErrorIs (database .ErrNotFound , err , "No error thrown." )
72
72
}
@@ -79,7 +79,7 @@ func TestInsertNew(t *testing.T) {
79
79
err := ts .Insert (ctx , TestKey , TestVal )
80
80
require .ErrorIs (ErrKeyNotSpecified , err , "No error thrown." )
81
81
// SetScope
82
- ts .SetScope (ctx , [][]byte {TestKey }, map [Key ][]byte {})
82
+ ts .SetScope (ctx , [][]byte {TestKey }, map [* Key ][]byte {})
83
83
// Insert key
84
84
err = ts .Insert (ctx , TestKey , TestVal )
85
85
require .NoError (err , "Error thrown." )
@@ -94,7 +94,7 @@ func TestInsertUpdate(t *testing.T) {
94
94
ctx := context .TODO ()
95
95
ts := New (10 )
96
96
// SetScope and add
97
- ts .SetScope (ctx , [][]byte {TestKey }, map [Key ][]byte {ToStateKeyArray (TestKey ): TestVal })
97
+ ts .SetScope (ctx , [][]byte {TestKey }, map [* Key ][]byte {ToStateKeyArray (TestKey ): TestVal })
98
98
require .Equal (0 , ts .OpIndex (), "SetStorage operation was not added." )
99
99
// Insert key
100
100
newVal := []byte ("newVal" )
@@ -163,15 +163,15 @@ func TestSetScope(t *testing.T) {
163
163
ts := New (10 )
164
164
ctx := context .TODO ()
165
165
keys := [][]byte {[]byte ("key1" ), []byte ("key2" ), []byte ("key3" )}
166
- ts .SetScope (ctx , keys , map [Key ][]byte {})
166
+ ts .SetScope (ctx , keys , map [* Key ][]byte {})
167
167
require .Equal (keys , ts .scope , "Scope not updated correctly." )
168
168
}
169
169
170
170
func TestRemoveInsertRollback (t * testing.T ) {
171
171
require := require .New (t )
172
172
ts := New (10 )
173
173
ctx := context .TODO ()
174
- ts .SetScope (ctx , [][]byte {TestKey }, map [Key ][]byte {})
174
+ ts .SetScope (ctx , [][]byte {TestKey }, map [* Key ][]byte {})
175
175
// Insert
176
176
err := ts .Insert (ctx , TestKey , TestVal )
177
177
require .NoError (err , "Error from insert." )
@@ -219,7 +219,7 @@ func TestRestoreInsert(t *testing.T) {
219
219
ctx := context .TODO ()
220
220
keys := [][]byte {[]byte ("key1" ), []byte ("key2" ), []byte ("key3" )}
221
221
vals := [][]byte {[]byte ("val1" ), []byte ("val2" ), []byte ("val3" )}
222
- ts .SetScope (ctx , keys , map [Key ][]byte {})
222
+ ts .SetScope (ctx , keys , map [* Key ][]byte {})
223
223
for i , key := range keys {
224
224
err := ts .Insert (ctx , key , vals [i ])
225
225
require .NoError (err , "Error inserting." )
@@ -249,7 +249,7 @@ func TestRestoreDelete(t *testing.T) {
249
249
ctx := context .TODO ()
250
250
keys := [][]byte {[]byte ("key1" ), []byte ("key2" ), []byte ("key3" )}
251
251
vals := [][]byte {[]byte ("val1" ), []byte ("val2" ), []byte ("val3" )}
252
- ts .SetScope (ctx , keys , map [Key ][]byte {
252
+ ts .SetScope (ctx , keys , map [* Key ][]byte {
253
253
ToStateKeyArray (keys [0 ]): vals [0 ],
254
254
ToStateKeyArray (keys [1 ]): vals [1 ],
255
255
ToStateKeyArray (keys [2 ]): vals [2 ],
@@ -288,7 +288,7 @@ func TestWriteChanges(t *testing.T) {
288
288
tracer , _ := trace .New (& trace.Config {Enabled : false })
289
289
keys := [][]byte {[]byte ("key1" ), []byte ("key2" ), []byte ("key3" )}
290
290
vals := [][]byte {[]byte ("val1" ), []byte ("val2" ), []byte ("val3" )}
291
- ts .SetScope (ctx , keys , map [Key ][]byte {})
291
+ ts .SetScope (ctx , keys , map [* Key ][]byte {})
292
292
// Add
293
293
for i , key := range keys {
294
294
err := ts .Insert (ctx , key , vals [i ])
@@ -306,7 +306,7 @@ func TestWriteChanges(t *testing.T) {
306
306
}
307
307
// Remove
308
308
ts = New (10 )
309
- ts .SetScope (ctx , keys , map [Key ][]byte {
309
+ ts .SetScope (ctx , keys , map [* Key ][]byte {
310
310
ToStateKeyArray (keys [0 ]): vals [0 ],
311
311
ToStateKeyArray (keys [1 ]): vals [1 ],
312
312
ToStateKeyArray (keys [2 ]): vals [2 ],
@@ -334,21 +334,21 @@ func BenchmarkFetchAndSetScope(b *testing.B) {
334
334
}
335
335
}
336
336
337
+ func BenchmarkInsert (b * testing.B ) {
338
+ for _ , size := range []int {4 , 8 , 16 , 32 , 64 , 128 } {
339
+ b .Run (fmt .Sprintf ("insert_%d_keys" , size ), func (b * testing.B ) {
340
+ benchmarkInsert (b , size )
341
+ })
342
+ }
343
+ }
344
+
337
345
func benchmarkFetchAndSetScope (b * testing.B , size int ) {
338
346
require := require .New (b )
339
- ts := New (10 )
347
+ ts := New (size )
340
348
db := NewTestDB ()
341
349
ctx := context .TODO ()
342
350
343
- keys := [][]byte {}
344
- vals := [][]byte {}
345
-
346
- // each k/v is unique to simulate worst case
347
- for i := 0 ; i <= size ; i ++ {
348
- keys = append (keys , randomBytes (MapKeyLength ))
349
- vals = append (vals , randomBytes (8 ))
350
- }
351
-
351
+ keys , vals := initializeSet (size )
352
352
for i , key := range keys {
353
353
err := db .Insert (ctx , key , vals [i ])
354
354
require .NoError (err , "Error during insert." )
@@ -363,6 +363,43 @@ func benchmarkFetchAndSetScope(b *testing.B, size int) {
363
363
b .StopTimer ()
364
364
}
365
365
366
+ func benchmarkInsert (b * testing.B , size int ) {
367
+ require := require .New (b )
368
+ ts := New (size )
369
+ ctx := context .TODO ()
370
+
371
+ keys , vals := initializeSet (size )
372
+
373
+ storage := map [* Key ][]byte {}
374
+ for i , key := range keys {
375
+ storage [ToStateKeyArray (key )] = vals [i ]
376
+ }
377
+
378
+ ts .SetScope (ctx , keys , storage )
379
+
380
+ b .ResetTimer ()
381
+ for i := 0 ; i < b .N ; i ++ {
382
+ for i , key := range keys {
383
+ err := ts .Insert (ctx , key , vals [i ])
384
+ require .NoError (err , "Error during insert." )
385
+ }
386
+ }
387
+ b .ReportAllocs ()
388
+ b .StopTimer ()
389
+ }
390
+
391
+ func initializeSet (size int ) ([][]byte , [][]byte ) {
392
+ keys := [][]byte {}
393
+ vals := [][]byte {}
394
+
395
+ for i := 0 ; i <= size ; i ++ {
396
+ keys = append (keys , randomBytes (MapKeyLength ))
397
+ vals = append (vals , randomBytes (8 ))
398
+ }
399
+
400
+ return keys , vals
401
+ }
402
+
366
403
func randomBytes (size int ) []byte {
367
404
bytes := make ([]byte , size )
368
405
rand .Read (bytes )
0 commit comments