2014-09-19 19:09:07 +00:00
|
|
|
/*
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
Copyright 2014 The Perkeep Authors.
|
2014-09-19 19:09:07 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Package leveldb provides an implementation of sorted.KeyValue
|
|
|
|
// on top of a single mutable database file on disk using
|
|
|
|
// github.com/syndtr/goleveldb.
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
package leveldb // import "perkeep.org/pkg/sorted/leveldb"
|
2014-09-19 19:09:07 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2016-08-31 15:51:43 +00:00
|
|
|
"log"
|
2014-09-19 19:09:07 +00:00
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
|
2015-12-01 16:19:49 +00:00
|
|
|
"go4.org/jsonconfig"
|
Rename import paths from camlistore.org to perkeep.org.
Part of the project renaming, issue #981.
After this, users will need to mv their $GOPATH/src/camlistore.org to
$GOPATH/src/perkeep.org. Sorry.
This doesn't yet rename the tools like camlistored, camput, camget,
camtool, etc.
Also, this only moves the lru package to internal. More will move to
internal later.
Also, this doesn't yet remove the "/pkg/" directory. That'll likely
happen later.
This updates some docs, but not all.
devcam test now passes again, even with Go 1.10 (which requires vet
checks are clean too). So a bunch of vet tests are fixed in this CL
too, and a bunch of other broken tests are now fixed (introduced from
the past week of merging the CL backlog).
Change-Id: If580db1691b5b99f8ed6195070789b1f44877dd4
2018-01-01 22:41:41 +00:00
|
|
|
"perkeep.org/pkg/env"
|
|
|
|
"perkeep.org/pkg/sorted"
|
2014-09-19 19:09:07 +00:00
|
|
|
|
2016-04-20 23:43:47 +00:00
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/filter"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/iterator"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/opt"
|
|
|
|
"github.com/syndtr/goleveldb/leveldb/util"
|
2014-09-19 19:09:07 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ sorted.Wiper = (*kvis)(nil)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
sorted.RegisterKeyValue("leveldb", newKeyValueFromJSONConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStorage is a convenience that calls newKeyValueFromJSONConfig
|
2016-05-04 14:50:08 +00:00
|
|
|
// with file as the leveldb storage directory.
|
2014-09-19 19:09:07 +00:00
|
|
|
func NewStorage(file string) (sorted.KeyValue, error) {
|
|
|
|
return newKeyValueFromJSONConfig(jsonconfig.Obj{"file": file})
|
|
|
|
}
|
|
|
|
|
|
|
|
// newKeyValueFromJSONConfig returns a KeyValue implementation on top of a
|
|
|
|
// github.com/syndtr/goleveldb/leveldb file.
|
|
|
|
func newKeyValueFromJSONConfig(cfg jsonconfig.Obj) (sorted.KeyValue, error) {
|
|
|
|
file := cfg.RequiredString("file")
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
strictness := opt.DefaultStrict
|
2015-04-02 12:55:01 +00:00
|
|
|
if env.IsDev() {
|
2014-09-19 19:09:07 +00:00
|
|
|
// Be more strict in dev mode.
|
|
|
|
strictness = opt.StrictAll
|
|
|
|
}
|
|
|
|
opts := &opt.Options{
|
|
|
|
// The default is 10,
|
|
|
|
// 8 means 2.126% or 1/47th disk check rate,
|
|
|
|
// 10 means 0.812% error rate (1/2^(bits/1.44)) or 1/123th disk check rate,
|
|
|
|
// 12 means 0.31% or 1/322th disk check rate.
|
|
|
|
// TODO(tgulacsi): decide which number is the best here. Till that go with the default.
|
|
|
|
Filter: filter.NewBloomFilter(10),
|
|
|
|
Strict: strictness,
|
|
|
|
}
|
|
|
|
db, err := leveldb.OpenFile(file, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
is := &kvis{
|
|
|
|
db: db,
|
|
|
|
path: file,
|
|
|
|
opts: opts,
|
|
|
|
readOpts: &opt.ReadOptions{Strict: strictness},
|
|
|
|
// On machine crash we want to reindex anyway, and
|
|
|
|
// fsyncs may impose great performance penalty.
|
|
|
|
writeOpts: &opt.WriteOptions{Sync: false},
|
|
|
|
}
|
|
|
|
return is, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type kvis struct {
|
|
|
|
path string
|
|
|
|
db *leveldb.DB
|
|
|
|
opts *opt.Options
|
|
|
|
readOpts *opt.ReadOptions
|
|
|
|
writeOpts *opt.WriteOptions
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) Get(key string) (string, error) {
|
|
|
|
val, err := is.db.Get([]byte(key), is.readOpts)
|
|
|
|
if err != nil {
|
|
|
|
if err == leveldb.ErrNotFound {
|
|
|
|
return "", sorted.ErrNotFound
|
|
|
|
}
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if val == nil {
|
|
|
|
return "", sorted.ErrNotFound
|
|
|
|
}
|
|
|
|
return string(val), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) Set(key, value string) error {
|
2015-01-19 18:55:05 +00:00
|
|
|
if err := sorted.CheckSizes(key, value); err != nil {
|
2016-08-31 15:51:43 +00:00
|
|
|
log.Printf("Skipping storing (%q:%q): %v", key, value, err)
|
|
|
|
return nil
|
2015-01-19 18:55:05 +00:00
|
|
|
}
|
2014-09-19 19:09:07 +00:00
|
|
|
return is.db.Put([]byte(key), []byte(value), is.writeOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) Delete(key string) error {
|
|
|
|
return is.db.Delete([]byte(key), is.writeOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) Find(start, end string) sorted.Iterator {
|
|
|
|
var startB, endB []byte
|
|
|
|
// A nil Range.Start is treated as a key before all keys in the DB.
|
|
|
|
if start != "" {
|
|
|
|
startB = []byte(start)
|
|
|
|
}
|
|
|
|
// A nil Range.Limit is treated as a key after all keys in the DB.
|
|
|
|
if end != "" {
|
|
|
|
endB = []byte(end)
|
|
|
|
}
|
|
|
|
it := &iter{
|
|
|
|
it: is.db.NewIterator(
|
|
|
|
&util.Range{Start: startB, Limit: endB},
|
|
|
|
is.readOpts,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
return it
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) Wipe() error {
|
|
|
|
// Close the already open DB.
|
|
|
|
if err := is.db.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(is.path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
db, err := leveldb.OpenFile(is.path, is.opts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating %s: %v", is.path, err)
|
|
|
|
}
|
|
|
|
is.db = db
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) BeginBatch() sorted.BatchMutation {
|
|
|
|
return &lvbatch{batch: new(leveldb.Batch)}
|
|
|
|
}
|
|
|
|
|
|
|
|
type lvbatch struct {
|
2015-01-19 18:55:05 +00:00
|
|
|
errMu sync.Mutex
|
|
|
|
err error // Set if one of the mutations had too large a key or value. Sticky.
|
|
|
|
|
2014-09-19 19:09:07 +00:00
|
|
|
batch *leveldb.Batch
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lvb *lvbatch) Set(key, value string) {
|
2015-01-19 18:55:05 +00:00
|
|
|
lvb.errMu.Lock()
|
|
|
|
defer lvb.errMu.Unlock()
|
|
|
|
if lvb.err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := sorted.CheckSizes(key, value); err != nil {
|
2016-08-31 15:51:43 +00:00
|
|
|
log.Printf("Skipping storing (%q:%q): %v", key, value, err)
|
2015-01-19 18:55:05 +00:00
|
|
|
return
|
|
|
|
}
|
2014-09-19 19:09:07 +00:00
|
|
|
lvb.batch.Put([]byte(key), []byte(value))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lvb *lvbatch) Delete(key string) {
|
|
|
|
lvb.batch.Delete([]byte(key))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) CommitBatch(bm sorted.BatchMutation) error {
|
|
|
|
b, ok := bm.(*lvbatch)
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid batch type")
|
|
|
|
}
|
2015-01-19 18:55:05 +00:00
|
|
|
b.errMu.Lock()
|
|
|
|
defer b.errMu.Unlock()
|
|
|
|
if b.err != nil {
|
|
|
|
return b.err
|
|
|
|
}
|
2014-09-19 19:09:07 +00:00
|
|
|
return is.db.Write(b.batch, is.writeOpts)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (is *kvis) Close() error {
|
|
|
|
return is.db.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
type iter struct {
|
|
|
|
it iterator.Iterator
|
|
|
|
|
|
|
|
skey, sval *string // for caching string values
|
|
|
|
|
2016-05-04 14:50:08 +00:00
|
|
|
// closed is not strictly necessary, but helps us detect programmer
|
|
|
|
// errors like calling Next on a Closed iterator (which panics).
|
2014-09-19 19:09:07 +00:00
|
|
|
closed bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *iter) Close() error {
|
|
|
|
it.closed = true
|
|
|
|
it.it.Release()
|
2016-05-04 14:50:08 +00:00
|
|
|
return it.it.Error()
|
2014-09-19 19:09:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (it *iter) KeyBytes() []byte {
|
|
|
|
return it.it.Key()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *iter) Key() string {
|
|
|
|
if it.skey != nil {
|
|
|
|
return *it.skey
|
|
|
|
}
|
|
|
|
str := string(it.it.Key())
|
|
|
|
it.skey = &str
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *iter) ValueBytes() []byte {
|
|
|
|
return it.it.Value()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *iter) Value() string {
|
|
|
|
if it.sval != nil {
|
|
|
|
return *it.sval
|
|
|
|
}
|
|
|
|
str := string(it.it.Value())
|
|
|
|
it.sval = &str
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it *iter) Next() bool {
|
|
|
|
if it.closed {
|
2016-05-04 14:50:08 +00:00
|
|
|
panic("Next called on closed iterator")
|
2014-09-19 19:09:07 +00:00
|
|
|
}
|
|
|
|
it.skey, it.sval = nil, nil
|
|
|
|
return it.it.Next()
|
|
|
|
}
|