30e5aa8f48
* [database] - Add Prefix method to interface - Convert leveldb error to `database error` - Be explicit with prefixedKey in `Table` as slices can be pointers * [protocol] - Add stringer method to protocol * [Chaindb] - Added saveBlock() which will allow us to save a block into the database. The block is broken up into transactions and Headers. The headers are saved as is. The transactions are saved as is, then the utxos in the transactions are collected to make the utxo db. - Verification for blocks and transactions will reside in the same package. Note that the save methods are all unexported, while the Get methods are exported. Making it so that any can call a get method, but only code in this package may save to the database. The other code which will reside in this package will be code verification logic. * [chaindb] - Added saveHeader function which saveHeaders uses - Update the latest header, each time we save a header instead of after a batch. This is so that we can call saveHeader without saveHeaders. This functionality can be rolled back if the performance of updating the header after a batch is significant - small refactor in test code
50 lines
1.2 KiB
Go
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)
|
|
}
|