2019-02-25 22:44:14 +00:00
|
|
|
package database
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
//Table is an abstract data structure built on top of a db
|
2019-02-25 22:44:14 +00:00
|
|
|
type Table struct {
|
|
|
|
prefix []byte
|
|
|
|
db Database
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
//NewTable creates a new table on the given database
|
2019-02-25 22:44:14 +00:00
|
|
|
func NewTable(db Database, prefix []byte) *Table {
|
|
|
|
return &Table{
|
|
|
|
prefix,
|
|
|
|
db,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
// Has implements the database interface
|
2019-02-25 22:44:14 +00:00
|
|
|
func (t *Table) Has(key []byte) (bool, error) {
|
|
|
|
key = append(t.prefix, key...)
|
|
|
|
return t.db.Has(key)
|
|
|
|
}
|
|
|
|
|
2019-03-17 18:26:35 +00:00
|
|
|
// Put implements the database interface
|
2019-02-25 22:44:14 +00:00
|
|
|
func (t *Table) Put(key []byte, value []byte) error {
|
|
|
|
key = append(t.prefix, key...)
|
|
|
|
return t.db.Put(key, value)
|
|
|
|
}
|
2019-03-17 18:26:35 +00:00
|
|
|
|
|
|
|
// Get implements the database interface
|
2019-02-25 22:44:14 +00:00
|
|
|
func (t *Table) Get(key []byte) ([]byte, error) {
|
|
|
|
key = append(t.prefix, key...)
|
|
|
|
return t.db.Get(key)
|
|
|
|
}
|
2019-03-17 18:26:35 +00:00
|
|
|
|
|
|
|
// Delete implements the database interface
|
2019-02-25 22:44:14 +00:00
|
|
|
func (t *Table) Delete(key []byte) error {
|
|
|
|
key = append(t.prefix, key...)
|
|
|
|
return t.db.Delete(key)
|
|
|
|
}
|
2019-03-17 18:26:35 +00:00
|
|
|
|
|
|
|
// Close implements the database interface
|
2019-02-25 22:44:14 +00:00
|
|
|
func (t *Table) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|