@@ -305,3 +305,55 @@ func (b *ldbBatch) Put(key, value []byte) error {
305
305
func (b * ldbBatch ) Write () error {
306
306
return b .db .Write (b .b , nil )
307
307
}
308
+
309
+ type table struct {
310
+ db Database
311
+ prefix string
312
+ }
313
+
314
+ // NewTable returns a Database object that prefixes all keys with a given
315
+ // string.
316
+ func NewTable (db Database , prefix string ) Database {
317
+ return & table {
318
+ db : db ,
319
+ prefix : prefix ,
320
+ }
321
+ }
322
+
323
+ func (dt * table ) Put (key []byte , value []byte ) error {
324
+ return dt .db .Put (append ([]byte (dt .prefix ), key ... ), value )
325
+ }
326
+
327
+ func (dt * table ) Get (key []byte ) ([]byte , error ) {
328
+ return dt .db .Get (append ([]byte (dt .prefix ), key ... ))
329
+ }
330
+
331
+ func (dt * table ) Delete (key []byte ) error {
332
+ return dt .db .Delete (append ([]byte (dt .prefix ), key ... ))
333
+ }
334
+
335
+ func (dt * table ) Close () {
336
+ // Do nothing; don't close the underlying DB.
337
+ }
338
+
339
+ type tableBatch struct {
340
+ batch Batch
341
+ prefix string
342
+ }
343
+
344
+ // NewTableBatch returns a Batch object which prefixes all keys with a given string.
345
+ func NewTableBatch (db Database , prefix string ) Batch {
346
+ return & tableBatch {db .NewBatch (), prefix }
347
+ }
348
+
349
+ func (dt * table ) NewBatch () Batch {
350
+ return & tableBatch {dt .db .NewBatch (), dt .prefix }
351
+ }
352
+
353
+ func (tb * tableBatch ) Put (key , value []byte ) error {
354
+ return tb .batch .Put (append ([]byte (tb .prefix ), key ... ), value )
355
+ }
356
+
357
+ func (tb * tableBatch ) Write () error {
358
+ return tb .batch .Write ()
359
+ }
0 commit comments