2014-10-31 14:42:32 +01: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.
|
|
|
|
|
|
|
|
// +build forestdb
|
|
|
|
|
|
|
|
package forestdb
|
|
|
|
|
|
|
|
import (
|
2014-11-04 14:34:49 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/binary"
|
2015-01-13 21:03:24 +01:00
|
|
|
"encoding/json"
|
2014-10-31 14:42:32 +01:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
"github.com/blevesearch/bleve/registry"
|
|
|
|
"github.com/couchbaselabs/goforestdb"
|
|
|
|
)
|
|
|
|
|
2015-01-13 21:03:24 +01:00
|
|
|
type ForestDBConfig struct {
|
|
|
|
BlockSize uint32
|
|
|
|
BufferCacheSize uint64
|
|
|
|
ChunkSize uint16
|
|
|
|
CleanupCacheOnClose bool
|
|
|
|
CompactionBufferSizeMax uint32
|
|
|
|
CompactionMinimumFilesize uint64
|
|
|
|
CompactionMode forestdb.CompactOpt
|
|
|
|
CompactionThreshold uint8
|
|
|
|
CompactorSleepDuration uint64
|
|
|
|
CompressDocumentBody bool
|
|
|
|
DurabilityOpt forestdb.DurabilityOpt
|
|
|
|
OpenFlags forestdb.OpenFlags
|
|
|
|
PurgingInterval uint32
|
|
|
|
SeqTreeOpt forestdb.SeqTreeOpt
|
|
|
|
WalFlushBeforeCommit bool
|
|
|
|
WalThreshold uint64
|
|
|
|
}
|
|
|
|
|
2015-01-07 02:35:57 +01:00
|
|
|
const Name = "forestdb"
|
2014-10-31 14:42:32 +01:00
|
|
|
|
|
|
|
type Store struct {
|
2014-11-26 03:52:35 +01:00
|
|
|
path string
|
|
|
|
config *forestdb.Config
|
|
|
|
kvconfig *forestdb.KVStoreConfig
|
|
|
|
dbfile *forestdb.File
|
|
|
|
dbkv *forestdb.KVStore
|
|
|
|
writer sync.Mutex
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
2015-01-13 20:04:02 +01:00
|
|
|
func Open(path string, createIfMissing bool,
|
|
|
|
config map[string]interface{}) (*Store, error) {
|
|
|
|
if config == nil {
|
|
|
|
config = map[string]interface{}{}
|
|
|
|
}
|
|
|
|
|
2015-01-16 19:33:35 +01:00
|
|
|
forestDBDefaultConfig := forestdb.DefaultConfig()
|
|
|
|
forestDBDefaultConfig.SetCompactionMode(forestdb.COMPACT_AUTO)
|
|
|
|
forestDBConfig, err := applyConfig(forestDBDefaultConfig, config)
|
2015-01-13 21:03:24 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-31 14:42:32 +01:00
|
|
|
rv := Store{
|
2014-11-26 03:52:35 +01:00
|
|
|
path: path,
|
2015-01-13 21:03:24 +01:00
|
|
|
config: forestDBConfig,
|
2014-11-26 03:52:35 +01:00
|
|
|
kvconfig: forestdb.DefaultKVStoreConfig(),
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if createIfMissing {
|
2014-11-26 03:52:35 +01:00
|
|
|
rv.kvconfig.SetCreateIfMissing(true)
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
2014-11-26 03:52:35 +01:00
|
|
|
rv.dbfile, err = forestdb.Open(rv.path, rv.config)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
rv.dbkv, err = rv.dbfile.OpenKVStoreDefault(rv.kvconfig)
|
2014-10-31 14:42:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &rv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) get(key []byte) ([]byte, error) {
|
2014-11-26 03:52:35 +01:00
|
|
|
res, err := s.dbkv.GetKV(key)
|
2014-10-31 14:42:32 +01:00
|
|
|
if err != nil && err != forestdb.RESULT_KEY_NOT_FOUND {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) set(key, val []byte) error {
|
|
|
|
s.writer.Lock()
|
|
|
|
defer s.writer.Unlock()
|
|
|
|
return s.setlocked(key, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) setlocked(key, val []byte) error {
|
2014-11-26 03:52:35 +01:00
|
|
|
return s.dbkv.SetKV(key, val)
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) delete(key []byte) error {
|
|
|
|
s.writer.Lock()
|
|
|
|
defer s.writer.Unlock()
|
|
|
|
return s.deletelocked(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) deletelocked(key []byte) error {
|
2014-11-26 03:52:35 +01:00
|
|
|
return s.dbkv.DeleteKV(key)
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) commit() error {
|
2014-11-26 03:52:35 +01:00
|
|
|
return s.dbfile.Commit(forestdb.COMMIT_NORMAL)
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Close() error {
|
2014-11-26 03:52:35 +01:00
|
|
|
err := s.dbkv.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return s.dbfile.Close()
|
|
|
|
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ldbs *Store) iterator(key []byte) store.KVIterator {
|
|
|
|
rv := newIterator(ldbs)
|
|
|
|
rv.Seek(key)
|
|
|
|
return rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Reader() (store.KVReader, error) {
|
|
|
|
return newReader(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ldbs *Store) Writer() (store.KVWriter, error) {
|
|
|
|
return newWriter(ldbs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ldbs *Store) newBatch() store.KVBatch {
|
|
|
|
return newBatch(ldbs)
|
|
|
|
}
|
|
|
|
|
2014-11-04 14:34:49 +01:00
|
|
|
func (s *Store) getSeqNum() (forestdb.SeqNum, error) {
|
2014-11-26 03:52:35 +01:00
|
|
|
dbinfo, err := s.dbkv.Info()
|
2014-11-04 14:34:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return dbinfo.LastSeqNum(), nil
|
|
|
|
}
|
|
|
|
|
2014-11-26 03:52:35 +01:00
|
|
|
func (s *Store) newSnapshot() (*forestdb.KVStore, error) {
|
2014-11-04 14:34:49 +01:00
|
|
|
seqNum, err := s.getSeqNum()
|
|
|
|
if err != nil {
|
2015-01-16 20:07:47 +01:00
|
|
|
return nil, fmt.Errorf("error getting snapshot seqnum %v", err)
|
2014-11-04 14:34:49 +01:00
|
|
|
}
|
2014-11-26 03:52:35 +01:00
|
|
|
return s.dbkv.SnapshotOpen(seqNum)
|
2014-11-04 14:34:49 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 03:59:02 +01:00
|
|
|
func (s *Store) GetRollbackID() ([]byte, error) {
|
2014-11-04 14:34:49 +01:00
|
|
|
seqNum, err := s.getSeqNum()
|
2014-10-31 14:42:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-04 14:34:49 +01:00
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
err = binary.Write(buf, binary.LittleEndian, seqNum)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2015-01-07 03:59:02 +01:00
|
|
|
func (s *Store) RollbackTo(rollbackId []byte) error {
|
2014-11-04 14:34:49 +01:00
|
|
|
s.writer.Lock()
|
|
|
|
defer s.writer.Unlock()
|
|
|
|
buf := bytes.NewReader(rollbackId)
|
|
|
|
var seqNum forestdb.SeqNum
|
|
|
|
err := binary.Read(buf, binary.LittleEndian, &seqNum)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-11-26 03:52:35 +01:00
|
|
|
err = s.dbkv.Rollback(seqNum)
|
2014-11-04 14:34:49 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2014-10-31 14:42:32 +01: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
|
|
|
|
}
|
2015-01-13 20:04:02 +01:00
|
|
|
return Open(path, createIfMissing, config)
|
2014-10-31 14:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.RegisterKVStore(Name, StoreConstructor)
|
|
|
|
}
|
2015-01-13 20:04:02 +01:00
|
|
|
|
2015-01-13 21:03:24 +01:00
|
|
|
func applyConfig(c *forestdb.Config, config map[string]interface{}) (
|
|
|
|
*forestdb.Config, error) {
|
|
|
|
v, exists := config["forestDBConfig"]
|
|
|
|
if !exists || v == nil {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
m, ok := v.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
// These extra steps of json.Marshal()/Unmarshal() help to convert
|
|
|
|
// to the types that we need for the setter calls.
|
|
|
|
b, err := json.Marshal(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
var f ForestDBConfig
|
|
|
|
err = json.Unmarshal(b, &f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, exists := m["blockSize"]; exists {
|
|
|
|
c.SetBlockSize(f.BlockSize)
|
|
|
|
}
|
|
|
|
if _, exists := m["bufferCacheSize"]; exists {
|
|
|
|
c.SetBufferCacheSize(f.BufferCacheSize)
|
|
|
|
}
|
|
|
|
if _, exists := m["chunkSize"]; exists {
|
|
|
|
c.SetChunkSize(f.ChunkSize)
|
|
|
|
}
|
|
|
|
if _, exists := m["cleanupCacheOnClose"]; exists {
|
|
|
|
c.SetCleanupCacheOnClose(f.CleanupCacheOnClose)
|
|
|
|
}
|
|
|
|
if _, exists := m["compactionBufferSizeMax"]; exists {
|
|
|
|
c.SetCompactionBufferSizeMax(f.CompactionBufferSizeMax)
|
|
|
|
}
|
|
|
|
if _, exists := m["compactionMinimumFilesize"]; exists {
|
|
|
|
c.SetCompactionMinimumFilesize(f.CompactionMinimumFilesize)
|
|
|
|
}
|
|
|
|
if _, exists := m["compactionMode"]; exists {
|
|
|
|
c.SetCompactionMode(f.CompactionMode)
|
|
|
|
}
|
|
|
|
if _, exists := m["compactionThreshold"]; exists {
|
|
|
|
c.SetCompactionThreshold(f.CompactionThreshold)
|
|
|
|
}
|
|
|
|
if _, exists := m["compactorSleepDuration"]; exists {
|
|
|
|
c.SetCompactorSleepDuration(f.CompactorSleepDuration)
|
|
|
|
}
|
|
|
|
if _, exists := m["compressDocumentBody"]; exists {
|
|
|
|
c.SetCompressDocumentBody(f.CompressDocumentBody)
|
|
|
|
}
|
|
|
|
if _, exists := m["durabilityOpt"]; exists {
|
|
|
|
c.SetDurabilityOpt(f.DurabilityOpt)
|
|
|
|
}
|
|
|
|
if _, exists := m["openFlags"]; exists {
|
|
|
|
c.SetOpenFlags(f.OpenFlags)
|
|
|
|
}
|
|
|
|
if _, exists := m["purgingInterval"]; exists {
|
|
|
|
c.SetPurgingInterval(f.PurgingInterval)
|
|
|
|
}
|
|
|
|
if _, exists := m["seqTreeOpt"]; exists {
|
|
|
|
c.SetSeqTreeOpt(f.SeqTreeOpt)
|
|
|
|
}
|
|
|
|
if _, exists := m["walFlushBeforeCommit"]; exists {
|
|
|
|
c.SetWalFlushBeforeCommit(f.WalFlushBeforeCommit)
|
|
|
|
}
|
|
|
|
if _, exists := m["walThreshold"]; exists {
|
|
|
|
c.SetWalThreshold(f.WalThreshold)
|
|
|
|
}
|
|
|
|
return c, nil
|
2015-01-13 20:04:02 +01:00
|
|
|
}
|