ethdb: Implement interface for prefixed operations to the DB

This commit is contained in:
Nick Johnson 2017-01-09 15:13:48 +00:00
parent 681b51aac4
commit ede0dc68f6

View file

@ -305,3 +305,48 @@ func (b *ldbBatch) Put(key, value []byte) error {
func (b *ldbBatch) Write() error {
return b.db.Write(b.b, nil)
}
type DatabaseTable struct {
db Database
prefix string
}
func NewDatabaseTable(db Database, prefix string) *DatabaseTable {
return &DatabaseTable{
db: db,
prefix: prefix,
}
}
func (dt *DatabaseTable) Put(key []byte, value []byte) error {
return dt.db.Put(append([]byte(dt.prefix), key...), value)
}
func (dt *DatabaseTable) Get(key []byte) ([]byte, error) {
return dt.db.Get(append([]byte(dt.prefix), key...))
}
func (dt *DatabaseTable) Delete(key []byte) error {
return dt.db.Delete(append([]byte(dt.prefix), key...))
}
func (dt *DatabaseTable) Close() {
// Do nothing; don't close the underlying DB.
}
type databaseTableBatch struct {
batch Batch
prefix string
}
func (dt *DatabaseTable) NewBatch() Batch {
return &databaseTableBatch{dt.db.NewBatch(), dt.prefix}
}
func (dtb *databaseTableBatch) Put(key, value []byte) error {
return dtb.batch.Put(append([]byte(dtb.prefix), key...), value)
}
func (dtb *databaseTableBatch) Write() error {
return dtb.batch.Write()
}