aboutsummaryrefslogtreecommitdiffstats
path: root/ethdb/database.go
diff options
context:
space:
mode:
authorNick Johnson <arachnid@notdot.net>2017-01-11 20:26:09 +0800
committerFelix Lange <fjl@users.noreply.github.com>2017-01-11 20:26:09 +0800
commitd30d7800e01def081a5f2e37846f17ae0d5c6910 (patch)
tree7fac2488b30f20f0d3b661504d8ca13a82479870 /ethdb/database.go
parent8820d97039cffa66411062df4b23ff74d659a220 (diff)
downloaddexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.tar
dexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.tar.gz
dexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.tar.bz2
dexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.tar.lz
dexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.tar.xz
dexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.tar.zst
dexon-d30d7800e01def081a5f2e37846f17ae0d5c6910.zip
ethdb: Implement interface for prefixed operations to the DB (#3536)
Diffstat (limited to 'ethdb/database.go')
-rw-r--r--ethdb/database.go52
1 files changed, 52 insertions, 0 deletions
diff --git a/ethdb/database.go b/ethdb/database.go
index 96d9a5982..c0e92a87b 100644
--- a/ethdb/database.go
+++ b/ethdb/database.go
@@ -305,3 +305,55 @@ func (b *ldbBatch) Put(key, value []byte) error {
func (b *ldbBatch) Write() error {
return b.db.Write(b.b, nil)
}
+
+type table struct {
+ db Database
+ prefix string
+}
+
+// NewTable returns a Database object that prefixes all keys with a given
+// string.
+func NewTable(db Database, prefix string) Database {
+ return &table{
+ db: db,
+ prefix: prefix,
+ }
+}
+
+func (dt *table) Put(key []byte, value []byte) error {
+ return dt.db.Put(append([]byte(dt.prefix), key...), value)
+}
+
+func (dt *table) Get(key []byte) ([]byte, error) {
+ return dt.db.Get(append([]byte(dt.prefix), key...))
+}
+
+func (dt *table) Delete(key []byte) error {
+ return dt.db.Delete(append([]byte(dt.prefix), key...))
+}
+
+func (dt *table) Close() {
+ // Do nothing; don't close the underlying DB.
+}
+
+type tableBatch struct {
+ batch Batch
+ prefix string
+}
+
+// NewTableBatch returns a Batch object which prefixes all keys with a given string.
+func NewTableBatch(db Database, prefix string) Batch {
+ return &tableBatch{db.NewBatch(), prefix}
+}
+
+func (dt *table) NewBatch() Batch {
+ return &tableBatch{dt.db.NewBatch(), dt.prefix}
+}
+
+func (tb *tableBatch) Put(key, value []byte) error {
+ return tb.batch.Put(append([]byte(tb.prefix), key...), value)
+}
+
+func (tb *tableBatch) Write() error {
+ return tb.batch.Write()
+}