drop _pkg.dev
The last piece left for the #334. I think this problem was solved by #517 and if we're to improve anything in this space we're gonna be building upon things done there, so this old code is completely useless and can be removed.
This commit is contained in:
parent
5f40b6a93c
commit
e4db0e5db4
4 changed files with 0 additions and 267 deletions
|
@ -1,12 +0,0 @@
|
|||
# ReadMe
|
||||
|
||||
Currently this package is in Development.
|
||||
|
||||
|
||||
## References
|
||||
|
||||
btcd https://github.com/btcsuite/btcd
|
||||
|
||||
geth https://github.com/ethereum/go-ethereum
|
||||
|
||||
aeternity https://github.com/aeternity/elixir-node
|
|
@ -1,114 +0,0 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"github.com/syndtr/goleveldb/leveldb"
|
||||
"github.com/syndtr/goleveldb/leveldb/errors"
|
||||
ldbutil "github.com/syndtr/goleveldb/leveldb/util"
|
||||
)
|
||||
|
||||
//DbDir is the folder which all database files will be put under
|
||||
// Structure /DbDir/net
|
||||
const DbDir = "db/"
|
||||
|
||||
// LDB represents a leveldb object
|
||||
type LDB struct {
|
||||
db *leveldb.DB
|
||||
Path string
|
||||
}
|
||||
|
||||
// ErrNotFound means that the value was not found in the db
|
||||
var ErrNotFound = errors.New("value not found for that key")
|
||||
|
||||
// Database contains all methods needed for an object to be a database
|
||||
type Database interface {
|
||||
// Has checks whether the key is in the database
|
||||
Has(key []byte) (bool, error)
|
||||
// Put adds the key value pair into the pair
|
||||
Put(key []byte, value []byte) error
|
||||
// Get returns the value for the given key
|
||||
Get(key []byte) ([]byte, error)
|
||||
// Delete deletes the given value for the key from the database
|
||||
Delete(key []byte) error
|
||||
//Prefix returns all values that start with key
|
||||
Prefix(key []byte) ([][]byte, error)
|
||||
// Close closes the underlying db object
|
||||
Close() error
|
||||
}
|
||||
|
||||
// New will return a new leveldb instance
|
||||
func New(path string) (*LDB, error) {
|
||||
dbPath := DbDir + path
|
||||
db, err := leveldb.OpenFile(dbPath, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, corrupted := err.(*errors.ErrCorrupted); corrupted {
|
||||
db, err = leveldb.RecoverFile(path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &LDB{
|
||||
db,
|
||||
dbPath,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Has implements the database interface
|
||||
func (l *LDB) Has(key []byte) (bool, error) {
|
||||
return l.db.Has(key, nil)
|
||||
}
|
||||
|
||||
// Put implements the database interface
|
||||
func (l *LDB) Put(key []byte, value []byte) error {
|
||||
return l.db.Put(key, value, nil)
|
||||
}
|
||||
|
||||
// Get implements the database interface
|
||||
func (l *LDB) Get(key []byte) ([]byte, error) {
|
||||
val, err := l.db.Get(key, nil)
|
||||
if err == nil {
|
||||
return val, nil
|
||||
}
|
||||
if err == leveldb.ErrNotFound {
|
||||
return val, ErrNotFound
|
||||
}
|
||||
return val, err
|
||||
|
||||
}
|
||||
|
||||
// Delete implements the database interface
|
||||
func (l *LDB) Delete(key []byte) error {
|
||||
return l.db.Delete(key, nil)
|
||||
}
|
||||
|
||||
// Close implements the database interface
|
||||
func (l *LDB) Close() error {
|
||||
return l.db.Close()
|
||||
}
|
||||
|
||||
// Prefix implements the database interface
|
||||
func (l *LDB) Prefix(key []byte) ([][]byte, error) {
|
||||
|
||||
var results [][]byte
|
||||
|
||||
iter := l.db.NewIterator(ldbutil.BytesPrefix(key), nil)
|
||||
for iter.Next() {
|
||||
|
||||
value := iter.Value()
|
||||
|
||||
// Copy the data, as we cannot modify it
|
||||
// Once the iter has been released
|
||||
deref := make([]byte, len(value))
|
||||
|
||||
copy(deref, value)
|
||||
|
||||
// Append result
|
||||
results = append(results, deref)
|
||||
|
||||
}
|
||||
iter.Release()
|
||||
err := iter.Error()
|
||||
return results, err
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
package database_test
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/CityOfZion/neo-go/pkg/database"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
const path = "temp"
|
||||
|
||||
func cleanup(db *database.LDB) {
|
||||
db.Close()
|
||||
os.RemoveAll(database.DbDir)
|
||||
}
|
||||
func TestDBCreate(t *testing.T) {
|
||||
|
||||
db, err := database.New(path)
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.NotEqual(t, nil, db)
|
||||
cleanup(db)
|
||||
}
|
||||
func TestPutGet(t *testing.T) {
|
||||
|
||||
db, err := database.New(path)
|
||||
assert.Nil(t, err)
|
||||
|
||||
key := []byte("Hello")
|
||||
value := []byte("World")
|
||||
|
||||
err = db.Put(key, value)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
res, err := db.Get(key)
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, value, res)
|
||||
cleanup(db)
|
||||
}
|
||||
func TestPutDelete(t *testing.T) {
|
||||
|
||||
db, err := database.New(path)
|
||||
assert.Nil(t, err)
|
||||
|
||||
key := []byte("Hello")
|
||||
value := []byte("World")
|
||||
|
||||
err = db.Put(key, value)
|
||||
|
||||
err = db.Delete(key)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
res, err := db.Get(key)
|
||||
|
||||
assert.Equal(t, database.ErrNotFound, err)
|
||||
assert.Equal(t, res, []byte{})
|
||||
cleanup(db)
|
||||
}
|
||||
|
||||
func TestHas(t *testing.T) {
|
||||
|
||||
db, err := database.New(path)
|
||||
assert.Nil(t, err)
|
||||
|
||||
res, err := db.Has([]byte("NotExist"))
|
||||
assert.Equal(t, res, false)
|
||||
assert.Equal(t, err, nil)
|
||||
|
||||
key := []byte("Hello")
|
||||
value := []byte("World")
|
||||
|
||||
err = db.Put(key, value)
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
res, err = db.Has(key)
|
||||
assert.Equal(t, res, true)
|
||||
assert.Equal(t, err, nil)
|
||||
cleanup(db)
|
||||
|
||||
}
|
||||
func TestDBClose(t *testing.T) {
|
||||
|
||||
db, err := database.New(path)
|
||||
assert.Nil(t, err)
|
||||
|
||||
err = db.Close()
|
||||
assert.Equal(t, nil, err)
|
||||
|
||||
cleanup(db)
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
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)
|
||||
}
|
Loading…
Reference in a new issue