2018-04-10 06:44:09 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
2018-04-20 11:26:54 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/errors"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
2018-04-10 06:44:09 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
2018-04-20 11:26:54 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/storage"
|
2019-04-30 06:46:12 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
2020-01-02 09:10:19 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-04-20 11:26:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type storagePrefix byte
|
|
|
|
|
|
|
|
const (
|
|
|
|
// PeersCache is used for the db entries used for peers DB
|
|
|
|
PeersCache storagePrefix = iota
|
|
|
|
// DeduplicatorCache is used for the db entries used for messages
|
|
|
|
// deduplication cache
|
|
|
|
DeduplicatorCache
|
2018-12-12 09:39:00 +00:00
|
|
|
// MailserversCache is a list of mail servers provided by users.
|
|
|
|
MailserversCache
|
2019-04-30 06:46:12 +00:00
|
|
|
// TopicHistoryBucket isolated bucket for storing history metadata.
|
|
|
|
TopicHistoryBucket
|
|
|
|
// HistoryRequestBucket isolated bucket for storing list of pending requests.
|
|
|
|
HistoryRequestBucket
|
2018-04-10 06:44:09 +00:00
|
|
|
)
|
|
|
|
|
2019-04-30 06:46:12 +00:00
|
|
|
// NewMemoryDB returns leveldb with memory backend prefixed with a bucket.
|
|
|
|
func NewMemoryDB() (*leveldb.DB, error) {
|
|
|
|
return leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewDBNamespace returns instance that ensures isolated operations.
|
2019-05-06 06:33:19 +00:00
|
|
|
func NewDBNamespace(db Storage, prefix storagePrefix) LevelDBNamespace {
|
2019-04-30 06:46:12 +00:00
|
|
|
return LevelDBNamespace{
|
|
|
|
db: db,
|
|
|
|
prefix: prefix,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoryDBNamespace wraps in memory leveldb with provided bucket.
|
|
|
|
// Mostly used for tests. Including tests in other packages.
|
|
|
|
func NewMemoryDBNamespace(prefix storagePrefix) (pdb LevelDBNamespace, err error) {
|
|
|
|
db, err := NewMemoryDB()
|
|
|
|
if err != nil {
|
|
|
|
return pdb, err
|
|
|
|
}
|
2019-05-06 06:33:19 +00:00
|
|
|
return NewDBNamespace(LevelDBStorage{db: db}, prefix), nil
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 11:26:54 +00:00
|
|
|
// Key creates a DB key for a specified service with specified data
|
|
|
|
func Key(prefix storagePrefix, data ...[]byte) []byte {
|
|
|
|
keyLength := 1
|
|
|
|
for _, d := range data {
|
|
|
|
keyLength += len(d)
|
|
|
|
}
|
|
|
|
key := make([]byte, keyLength)
|
|
|
|
key[0] = byte(prefix)
|
|
|
|
startPos := 1
|
|
|
|
for _, d := range data {
|
|
|
|
copy(key[startPos:], d[:])
|
|
|
|
startPos += len(d)
|
|
|
|
}
|
|
|
|
|
|
|
|
return key
|
|
|
|
}
|
|
|
|
|
2018-04-10 06:44:09 +00:00
|
|
|
// Create returns status pointer to leveldb.DB.
|
2018-04-20 11:26:54 +00:00
|
|
|
func Create(path, dbName string) (*leveldb.DB, error) {
|
|
|
|
// Create euphemeral storage if the node config path isn't provided
|
|
|
|
if path == "" {
|
|
|
|
return leveldb.Open(storage.NewMemStorage(), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
path = filepath.Join(path, dbName)
|
2018-07-02 08:42:16 +00:00
|
|
|
return Open(path, &opt.Options{OpenFilesCacheCapacity: 5})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open opens an existing leveldb database
|
|
|
|
func Open(path string, opts *opt.Options) (db *leveldb.DB, err error) {
|
|
|
|
db, err = leveldb.OpenFile(path, opts)
|
2018-04-10 06:44:09 +00:00
|
|
|
if _, iscorrupted := err.(*errors.ErrCorrupted); iscorrupted {
|
|
|
|
log.Info("database is corrupted trying to recover", "path", path)
|
|
|
|
db, err = leveldb.RecoverFile(path, nil)
|
|
|
|
}
|
2018-07-02 08:42:16 +00:00
|
|
|
return
|
2018-04-10 06:44:09 +00:00
|
|
|
}
|
2019-04-30 06:46:12 +00:00
|
|
|
|
|
|
|
// LevelDBNamespace database where all operations will be prefixed with a certain bucket.
|
|
|
|
type LevelDBNamespace struct {
|
2019-05-06 06:33:19 +00:00
|
|
|
db Storage
|
2019-04-30 06:46:12 +00:00
|
|
|
prefix storagePrefix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db LevelDBNamespace) prefixedKey(key []byte) []byte {
|
|
|
|
endkey := make([]byte, len(key)+1)
|
|
|
|
endkey[0] = byte(db.prefix)
|
|
|
|
copy(endkey[1:], key)
|
|
|
|
return endkey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db LevelDBNamespace) Put(key, value []byte) error {
|
2019-05-06 06:33:19 +00:00
|
|
|
return db.db.Put(db.prefixedKey(key), value)
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db LevelDBNamespace) Get(key []byte) ([]byte, error) {
|
2019-05-06 06:33:19 +00:00
|
|
|
return db.db.Get(db.prefixedKey(key))
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Range returns leveldb util.Range prefixed with a single byte.
|
|
|
|
// If prefix is nil range will iterate over all records in a given bucket.
|
|
|
|
func (db LevelDBNamespace) Range(prefix, limit []byte) *util.Range {
|
|
|
|
if limit == nil {
|
|
|
|
return util.BytesPrefix(db.prefixedKey(prefix))
|
|
|
|
}
|
|
|
|
return &util.Range{Start: db.prefixedKey(prefix), Limit: db.prefixedKey(limit)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes key from database.
|
|
|
|
func (db LevelDBNamespace) Delete(key []byte) error {
|
2019-05-06 06:33:19 +00:00
|
|
|
return db.db.Delete(db.prefixedKey(key))
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewIterator returns iterator for a given slice.
|
|
|
|
func (db LevelDBNamespace) NewIterator(slice *util.Range) NamespaceIterator {
|
2019-05-06 06:33:19 +00:00
|
|
|
return NamespaceIterator{db.db.NewIterator(slice)}
|
2019-04-30 06:46:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NamespaceIterator wraps leveldb iterator, works mostly the same way.
|
|
|
|
// The only difference is that first byte of the key is dropped.
|
|
|
|
type NamespaceIterator struct {
|
|
|
|
iter iterator.Iterator
|
|
|
|
}
|
|
|
|
|
|
|
|
// Key returns key of the current item.
|
|
|
|
func (iter NamespaceIterator) Key() []byte {
|
|
|
|
return iter.iter.Key()[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Value returns actual value of the current item.
|
|
|
|
func (iter NamespaceIterator) Value() []byte {
|
|
|
|
return iter.iter.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns accumulated error.
|
|
|
|
func (iter NamespaceIterator) Error() error {
|
|
|
|
return iter.iter.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prev moves cursor backward.
|
|
|
|
func (iter NamespaceIterator) Prev() bool {
|
|
|
|
return iter.iter.Prev()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next moves cursor forward.
|
|
|
|
func (iter NamespaceIterator) Next() bool {
|
|
|
|
return iter.iter.Next()
|
|
|
|
}
|