Skip to content

Commit

Permalink
ethdb: Implement interface for prefixed operations to the DB (ethereu…
Browse files Browse the repository at this point in the history
  • Loading branch information
Arachnid authored and fjl committed Jan 11, 2017
1 parent 8820d97 commit d30d780
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions ethdb/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit d30d780

Please sign in to comment.