2014-08-24 09:06:44 +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-09-02 16:54:50 +02:00
|
|
|
|
2015-10-03 14:18:53 +02:00
|
|
|
// Package boltdb implements a store.KVStore on top of BoltDB. It supports the
|
|
|
|
// following options:
|
|
|
|
//
|
|
|
|
// "bucket" (string): the name of BoltDB bucket to use, defaults to "bleve".
|
|
|
|
//
|
|
|
|
// "nosync" (bool): if true, set boltdb.DB.NoSync to true. It speeds up index
|
|
|
|
// operations in exchange of losing integrity guarantees if indexation aborts
|
|
|
|
// without closing the index. Use it when rebuilding indexes from zero.
|
2014-08-24 09:06:44 +02:00
|
|
|
package boltdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2014-09-12 23:21:35 +02:00
|
|
|
"sync"
|
2014-08-24 09:06:44 +02:00
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
"github.com/blevesearch/bleve/registry"
|
2014-08-24 09:06:44 +02:00
|
|
|
"github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "boltdb"
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
type Store struct {
|
2014-08-24 09:06:44 +02:00
|
|
|
path string
|
|
|
|
bucket string
|
|
|
|
db *bolt.DB
|
2015-10-03 14:18:53 +02:00
|
|
|
noSync bool
|
2014-09-12 23:21:35 +02:00
|
|
|
writer sync.Mutex
|
2015-04-24 23:11:47 +02:00
|
|
|
mo store.MergeOperator
|
2014-08-24 09:06:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func New(path string, bucket string) *Store {
|
2014-09-04 01:16:46 +02:00
|
|
|
rv := Store{
|
2014-08-24 09:06:44 +02:00
|
|
|
path: path,
|
|
|
|
bucket: bucket,
|
|
|
|
}
|
2015-04-24 23:11:47 +02:00
|
|
|
return &rv
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *Store) Open() error {
|
2014-08-24 09:06:44 +02:00
|
|
|
|
|
|
|
var err error
|
2015-04-24 23:11:47 +02:00
|
|
|
bs.db, err = bolt.Open(bs.path, 0600, nil)
|
2014-08-24 09:06:44 +02:00
|
|
|
if err != nil {
|
2015-04-24 23:11:47 +02:00
|
|
|
return err
|
2014-08-24 09:06:44 +02:00
|
|
|
}
|
2015-10-03 14:18:53 +02:00
|
|
|
bs.db.NoSync = bs.noSync
|
2014-08-24 09:06:44 +02:00
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
err = bs.db.Update(func(tx *bolt.Tx) error {
|
|
|
|
_, err := tx.CreateBucketIfNotExists([]byte(bs.bucket))
|
2014-08-24 09:06:44 +02:00
|
|
|
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil {
|
2015-04-24 23:11:47 +02:00
|
|
|
return err
|
2014-08-24 09:06:44 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *Store) SetMergeOperator(mo store.MergeOperator) {
|
|
|
|
bs.mo = mo
|
2014-08-24 09:06:44 +02:00
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (bs *Store) Close() error {
|
2014-08-24 09:06:44 +02:00
|
|
|
return bs.db.Close()
|
|
|
|
}
|
|
|
|
|
2014-10-31 14:40:23 +01:00
|
|
|
func (bs *Store) Reader() (store.KVReader, error) {
|
2015-04-03 22:50:48 +02:00
|
|
|
tx, err := bs.db.Begin(false)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Reader{
|
|
|
|
store: bs,
|
|
|
|
tx: tx,
|
|
|
|
}, nil
|
2014-09-12 23:21:35 +02:00
|
|
|
}
|
|
|
|
|
2014-10-31 14:40:23 +01:00
|
|
|
func (bs *Store) Writer() (store.KVWriter, error) {
|
2015-04-03 22:50:48 +02:00
|
|
|
bs.writer.Lock()
|
|
|
|
tx, err := bs.db.Begin(true)
|
|
|
|
if err != nil {
|
|
|
|
bs.writer.Unlock()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reader := &Reader{
|
|
|
|
store: bs,
|
|
|
|
tx: tx,
|
|
|
|
}
|
|
|
|
return &Writer{
|
|
|
|
store: bs,
|
|
|
|
tx: tx,
|
|
|
|
reader: reader,
|
|
|
|
}, nil
|
2014-08-24 09:06:44 +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")
|
|
|
|
}
|
|
|
|
|
|
|
|
bucket, ok := config["bucket"].(string)
|
|
|
|
if !ok {
|
2014-08-25 21:11:04 +02:00
|
|
|
bucket = "bleve"
|
2014-08-24 09:06:44 +02:00
|
|
|
}
|
|
|
|
|
2015-10-03 14:18:53 +02:00
|
|
|
noSync, _ := config["nosync"].(bool)
|
|
|
|
|
|
|
|
store := New(path, bucket)
|
|
|
|
store.noSync = noSync
|
|
|
|
return store, nil
|
2014-08-24 09:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.RegisterKVStore(Name, StoreConstructor)
|
|
|
|
}
|