File tree Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Expand file tree Collapse file tree 3 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -268,3 +268,23 @@ func (self *LDBDatabase) meter(refresh time.Duration) {
268
268
}
269
269
}
270
270
}
271
+
272
+ // TODO: remove this stuff and expose leveldb directly
273
+
274
+ func (db * LDBDatabase ) NewBatch () Batch {
275
+ return & ldbBatch {db : db .db , b : new (leveldb.Batch )}
276
+ }
277
+
278
+ type ldbBatch struct {
279
+ db * leveldb.DB
280
+ b * leveldb.Batch
281
+ }
282
+
283
+ func (b * ldbBatch ) Put (key , value []byte ) error {
284
+ b .b .Put (key , value )
285
+ return nil
286
+ }
287
+
288
+ func (b * ldbBatch ) Write () error {
289
+ return b .db .Write (b .b , nil )
290
+ }
Original file line number Diff line number Diff line change @@ -22,4 +22,10 @@ type Database interface {
22
22
Delete (key []byte ) error
23
23
Close ()
24
24
Flush () error
25
+ NewBatch () Batch
26
+ }
27
+
28
+ type Batch interface {
29
+ Put (key , value []byte ) error
30
+ Write () error
25
31
}
Original file line number Diff line number Diff line change @@ -95,3 +95,26 @@ func (db *MemDatabase) LastKnownTD() []byte {
95
95
func (db * MemDatabase ) Flush () error {
96
96
return nil
97
97
}
98
+
99
+ func (db * MemDatabase ) NewBatch () Batch {
100
+ return & memBatch {db : db }
101
+ }
102
+
103
+ type kv struct { k , v []byte }
104
+
105
+ type memBatch struct {
106
+ db * MemDatabase
107
+ writes []kv
108
+ }
109
+
110
+ func (w * memBatch ) Put (key , value []byte ) error {
111
+ w .writes = append (w .writes , kv {key , common .CopyBytes (value )})
112
+ return nil
113
+ }
114
+
115
+ func (w * memBatch ) Write () error {
116
+ for _ , kv := range w .writes {
117
+ w .db .db [string (kv .k )] = kv .v
118
+ }
119
+ return nil
120
+ }
You can’t perform that action at this time.
0 commit comments