neoneo-go/_pkg.dev/database/table.go
Roman Khimov ddd1d92ff1 pkg: hide it by moving to _pkg.dev
The idea here is to preserve the history of `dev` branch development and its
code when merging with the `master`. Later this code could be moved into the
masters code where appropriate.
2019-08-20 18:39:50 +03:00

50 lines
1.2 KiB
Go

package database
//Table is an abstract data structure built on top of a db
type Table struct {
prefix []byte
db Database
}
//NewTable creates a new table on the given database
func NewTable(db Database, prefix []byte) *Table {
return &Table{
prefix,
db,
}
}
// Has implements the database interface
func (t *Table) Has(key []byte) (bool, error) {
prefixedKey := append(t.prefix, key...)
return t.db.Has(prefixedKey)
}
// Put implements the database interface
func (t *Table) Put(key []byte, value []byte) error {
prefixedKey := append(t.prefix, key...)
return t.db.Put(prefixedKey, value)
}
// Get implements the database interface
func (t *Table) Get(key []byte) ([]byte, error) {
prefixedKey := append(t.prefix, key...)
return t.db.Get(prefixedKey)
}
// Delete implements the database interface
func (t *Table) Delete(key []byte) error {
prefixedKey := append(t.prefix, key...)
return t.db.Delete(prefixedKey)
}
// Close implements the database interface
func (t *Table) Close() error {
return nil
}
// Prefix implements the database interface
func (t *Table) Prefix(key []byte) ([][]byte, error) {
prefixedKey := append(t.prefix, key...)
return t.db.Prefix(prefixedKey)
}