2017-11-09 14:42:32 -08:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-12-12 18:08:38 -08:00
|
|
|
"sort"
|
2017-11-09 14:42:32 -08:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2017-12-15 01:48:40 -08:00
|
|
|
cmn "github.com/tendermint/tmlibs/common"
|
2017-11-09 14:42:32 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyPerm = os.FileMode(0600)
|
|
|
|
dirPerm = os.FileMode(0700)
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2018-01-24 15:55:31 -08:00
|
|
|
registerDBCreator(FSDBBackend, func(name string, dir string) (DB, error) {
|
2017-11-09 14:42:32 -08:00
|
|
|
dbPath := filepath.Join(dir, name+".db")
|
|
|
|
return NewFSDB(dbPath), nil
|
|
|
|
}, false)
|
|
|
|
}
|
|
|
|
|
2017-12-12 18:08:38 -08:00
|
|
|
var _ DB = (*FSDB)(nil)
|
|
|
|
|
2017-11-09 14:42:32 -08:00
|
|
|
// It's slow.
|
|
|
|
type FSDB struct {
|
|
|
|
mtx sync.Mutex
|
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFSDB(dir string) *FSDB {
|
|
|
|
err := os.MkdirAll(dir, dirPerm)
|
|
|
|
if err != nil {
|
|
|
|
panic(errors.Wrap(err, "Creating FSDB dir "+dir))
|
|
|
|
}
|
|
|
|
database := &FSDB{
|
2017-12-12 14:00:03 -08:00
|
|
|
dir: dir,
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
return database
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) Get(key []byte) []byte {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
2017-12-17 13:11:28 -08:00
|
|
|
key = escapeKey(key)
|
2017-11-09 14:42:32 -08:00
|
|
|
|
|
|
|
path := db.nameToPath(key)
|
|
|
|
value, err := read(path)
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
2017-12-13 17:11:11 -08:00
|
|
|
panic(errors.Wrapf(err, "Getting key %s (0x%X)", string(key), key))
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:00:03 -08:00
|
|
|
func (db *FSDB) Has(key []byte) bool {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
2017-12-17 13:11:28 -08:00
|
|
|
key = escapeKey(key)
|
2017-12-12 14:00:03 -08:00
|
|
|
|
|
|
|
path := db.nameToPath(key)
|
2017-12-15 01:48:40 -08:00
|
|
|
return cmn.FileExists(path)
|
2017-12-12 14:00:03 -08:00
|
|
|
}
|
|
|
|
|
2017-11-09 14:42:32 -08:00
|
|
|
func (db *FSDB) Set(key []byte, value []byte) {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
db.SetNoLock(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) SetSync(key []byte, value []byte) {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
db.SetNoLock(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Implements atomicSetDeleter.
|
|
|
|
func (db *FSDB) SetNoLock(key []byte, value []byte) {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = escapeKey(key)
|
|
|
|
value = nonNilBytes(value)
|
2017-11-09 14:42:32 -08:00
|
|
|
path := db.nameToPath(key)
|
|
|
|
err := write(path, value)
|
|
|
|
if err != nil {
|
2017-12-13 17:11:11 -08:00
|
|
|
panic(errors.Wrapf(err, "Setting key %s (0x%X)", string(key), key))
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) Delete(key []byte) {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
db.DeleteNoLock(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) DeleteSync(key []byte) {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
db.DeleteNoLock(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: Implements atomicSetDeleter.
|
|
|
|
func (db *FSDB) DeleteNoLock(key []byte) {
|
2017-12-17 13:11:28 -08:00
|
|
|
key = escapeKey(key)
|
2017-12-12 16:22:08 -08:00
|
|
|
path := db.nameToPath(key)
|
|
|
|
err := remove(path)
|
2017-11-09 14:42:32 -08:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
2017-12-13 17:11:11 -08:00
|
|
|
panic(errors.Wrapf(err, "Removing key %s (0x%X)", string(key), key))
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) Close() {
|
|
|
|
// Nothing to do.
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) Print() {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
panic("FSDB.Print not yet implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) Stats() map[string]string {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
panic("FSDB.Stats not yet implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) NewBatch() Batch {
|
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
// Not sure we would ever want to try...
|
|
|
|
// It doesn't seem easy for general filesystems.
|
|
|
|
panic("FSDB.NewBatch not yet implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) Mutex() *sync.Mutex {
|
|
|
|
return &(db.mtx)
|
|
|
|
}
|
|
|
|
|
2017-12-12 14:00:03 -08:00
|
|
|
func (db *FSDB) Iterator(start, end []byte) Iterator {
|
2017-12-12 18:08:38 -08:00
|
|
|
db.mtx.Lock()
|
|
|
|
defer db.mtx.Unlock()
|
|
|
|
|
|
|
|
// We need a copy of all of the keys.
|
|
|
|
// Not the best, but probably not a bottleneck depending.
|
|
|
|
keys, err := list(db.dir, start, end)
|
|
|
|
if err != nil {
|
2017-12-13 17:11:11 -08:00
|
|
|
panic(errors.Wrapf(err, "Listing keys in %s", db.dir))
|
2017-12-12 18:08:38 -08:00
|
|
|
}
|
|
|
|
sort.Strings(keys)
|
2017-12-17 13:11:28 -08:00
|
|
|
return newMemDBIterator(db, keys, start, end)
|
2017-12-12 14:00:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) ReverseIterator(start, end []byte) Iterator {
|
2017-12-17 13:11:28 -08:00
|
|
|
panic("not implemented yet") // XXX
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *FSDB) nameToPath(name []byte) string {
|
|
|
|
n := url.PathEscape(string(name))
|
2017-12-13 17:11:11 -08:00
|
|
|
return filepath.Join(db.dir, n)
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read some bytes to a file.
|
|
|
|
// CONTRACT: returns os errors directly without wrapping.
|
|
|
|
func read(path string) ([]byte, error) {
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
d, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return d, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write some bytes from a file.
|
|
|
|
// CONTRACT: returns os errors directly without wrapping.
|
|
|
|
func write(path string, d []byte) error {
|
2017-12-12 16:22:08 -08:00
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, keyPerm)
|
2017-11-09 14:42:32 -08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
_, err = f.Write(d)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = f.Sync()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove a file.
|
|
|
|
// CONTRACT: returns os errors directly without wrapping.
|
|
|
|
func remove(path string) error {
|
|
|
|
return os.Remove(path)
|
|
|
|
}
|
|
|
|
|
2017-12-17 13:11:28 -08:00
|
|
|
// List keys in a directory, stripping of escape sequences and dir portions.
|
2017-11-09 14:42:32 -08:00
|
|
|
// CONTRACT: returns os errors directly without wrapping.
|
2017-12-12 18:08:38 -08:00
|
|
|
func list(dirPath string, start, end []byte) ([]string, error) {
|
2017-11-09 14:42:32 -08:00
|
|
|
dir, err := os.Open(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer dir.Close()
|
|
|
|
|
|
|
|
names, err := dir.Readdirnames(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-17 13:11:28 -08:00
|
|
|
var keys []string
|
2017-12-12 18:08:38 -08:00
|
|
|
for _, name := range names {
|
2017-11-09 14:42:32 -08:00
|
|
|
n, err := url.PathUnescape(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed to unescape %s while listing", name)
|
|
|
|
}
|
2017-12-17 13:11:28 -08:00
|
|
|
key := unescapeKey([]byte(n))
|
|
|
|
if IsKeyInDomain(key, start, end, false) {
|
|
|
|
keys = append(keys, string(key))
|
2017-12-12 18:08:38 -08:00
|
|
|
}
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|
2017-12-17 13:11:28 -08:00
|
|
|
return keys, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// To support empty or nil keys, while the file system doesn't allow empty
|
|
|
|
// filenames.
|
|
|
|
func escapeKey(key []byte) []byte {
|
|
|
|
return []byte("k_" + string(key))
|
|
|
|
}
|
|
|
|
func unescapeKey(escKey []byte) []byte {
|
|
|
|
if len(escKey) < 2 {
|
|
|
|
panic(fmt.Sprintf("Invalid esc key: %x", escKey))
|
|
|
|
}
|
|
|
|
if string(escKey[:2]) != "k_" {
|
|
|
|
panic(fmt.Sprintf("Invalid esc key: %x", escKey))
|
|
|
|
}
|
|
|
|
return escKey[2:]
|
2017-11-09 14:42:32 -08:00
|
|
|
}
|