2014-07-30 18:30:38 +02:00
|
|
|
// Copyright (c) 2014 Couchbase, Inc.
|
|
|
|
// 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.
|
2014-08-25 21:18:24 +02:00
|
|
|
|
|
|
|
// +build leveldb full
|
|
|
|
|
2014-05-09 22:37:04 +02:00
|
|
|
package leveldb
|
|
|
|
|
|
|
|
import (
|
2014-08-20 22:58:20 +02:00
|
|
|
"fmt"
|
2014-09-12 23:21:35 +02:00
|
|
|
"sync"
|
2014-08-20 22:58:20 +02:00
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
"github.com/blevesearch/bleve/registry"
|
2014-05-09 22:37:04 +02:00
|
|
|
"github.com/jmhodges/levigo"
|
|
|
|
)
|
|
|
|
|
2014-08-20 22:58:20 +02:00
|
|
|
const Name = "leveldb"
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
type Store struct {
|
|
|
|
path string
|
|
|
|
opts *levigo.Options
|
|
|
|
db *levigo.DB
|
|
|
|
writer sync.Mutex
|
2014-05-09 22:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func Open(path string, createIfMissing bool, errorIfExists bool) (*Store, error) {
|
|
|
|
rv := Store{
|
2014-05-09 22:37:04 +02:00
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := levigo.NewOptions()
|
2014-07-30 20:29:26 +02:00
|
|
|
opts.SetCreateIfMissing(createIfMissing)
|
2014-08-20 22:58:20 +02:00
|
|
|
opts.SetErrorIfExists(errorIfExists)
|
2014-05-09 22:37:04 +02:00
|
|
|
rv.opts = opts
|
|
|
|
|
|
|
|
var err error
|
|
|
|
rv.db, err = levigo.Open(rv.path, rv.opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rv, nil
|
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func (ldbs *Store) get(key []byte) ([]byte, error) {
|
2014-05-09 22:37:04 +02:00
|
|
|
return ldbs.db.Get(defaultReadOptions(), key)
|
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func (ldbs *Store) getWithSnapshot(key []byte, snapshot *levigo.Snapshot) ([]byte, error) {
|
|
|
|
options := defaultReadOptions()
|
|
|
|
options.SetSnapshot(snapshot)
|
|
|
|
return ldbs.db.Get(options, key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ldbs *Store) set(key, val []byte) error {
|
|
|
|
ldbs.writer.Lock()
|
|
|
|
defer ldbs.writer.Unlock()
|
|
|
|
return ldbs.setlocked(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ldbs *Store) setlocked(key, val []byte) error {
|
2014-05-09 22:37:04 +02:00
|
|
|
return ldbs.db.Put(defaultWriteOptions(), key, val)
|
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func (ldbs *Store) delete(key []byte) error {
|
|
|
|
ldbs.writer.Lock()
|
|
|
|
defer ldbs.writer.Unlock()
|
|
|
|
return ldbs.deletelocked(key)
|
2014-05-09 22:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func (ldbs *Store) deletelocked(key []byte) error {
|
|
|
|
return ldbs.db.Delete(defaultWriteOptions(), key)
|
2014-05-09 22:37:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func (ldbs *Store) Close() error {
|
2014-05-09 22:37:04 +02:00
|
|
|
ldbs.db.Close()
|
2015-01-13 03:25:56 +01:00
|
|
|
ldbs.opts.Close()
|
2014-05-09 22:37:04 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-09-12 23:21:35 +02:00
|
|
|
func (ldbs *Store) iterator(key []byte) store.KVIterator {
|
|
|
|
rv := newIterator(ldbs)
|
2014-05-09 22:37:04 +02:00
|
|
|
rv.Seek(key)
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
2014-10-31 14:40:23 +01:00
|
|
|
func (ldbs *Store) Reader() (store.KVReader, error) {
|
2014-09-12 23:21:35 +02:00
|
|
|
return newReader(ldbs)
|
|
|
|
}
|
|
|
|
|
2014-10-31 14:40:23 +01:00
|
|
|
func (ldbs *Store) Writer() (store.KVWriter, error) {
|
2014-09-12 23:21:35 +02:00
|
|
|
return newWriter(ldbs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ldbs *Store) newBatch() store.KVBatch {
|
|
|
|
return newBatch(ldbs)
|
2014-05-09 22:37:04 +02:00
|
|
|
}
|
2014-08-20 22:58:20 +02:00
|
|
|
|
|
|
|
func StoreConstructor(config map[string]interface{}) (store.KVStore, error) {
|
|
|
|
path, ok := config["path"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("must specify path")
|
|
|
|
}
|
|
|
|
createIfMissing := false
|
|
|
|
cim, ok := config["create_if_missing"].(bool)
|
|
|
|
if ok {
|
|
|
|
createIfMissing = cim
|
|
|
|
}
|
|
|
|
errorIfExists := true
|
|
|
|
eie, ok := config["error_if_exists"].(bool)
|
|
|
|
if ok {
|
|
|
|
errorIfExists = eie
|
|
|
|
}
|
|
|
|
return Open(path, createIfMissing, errorIfExists)
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.RegisterKVStore(Name, StoreConstructor)
|
|
|
|
}
|