2015-01-14 02:35:13 +01:00
|
|
|
// Copyright (c) 2015 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.
|
|
|
|
|
2015-01-16 00:54:22 +01:00
|
|
|
// +build go1.4
|
|
|
|
|
2015-01-14 02:35:13 +01:00
|
|
|
// Package cznicb provides an in-memory implementation of the KVStore
|
|
|
|
// interfaces using the cznic/b in-memory btree. Of note: this
|
|
|
|
// implementation does not have reader isolation.
|
|
|
|
package cznicb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2015-04-24 23:11:47 +02:00
|
|
|
"fmt"
|
2015-01-14 02:35:13 +01:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/blevesearch/bleve/index/store"
|
|
|
|
"github.com/blevesearch/bleve/registry"
|
|
|
|
|
|
|
|
"github.com/cznic/b"
|
|
|
|
)
|
|
|
|
|
|
|
|
const Name = "cznicb"
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
const MAX_CONCURRENT_WRITERS = 1
|
2015-01-14 02:35:13 +01:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.RegisterKVStore(Name, StoreConstructor)
|
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func StoreConstructor(config map[string]interface{}) (store.KVStore, error) {
|
|
|
|
s := &Store{
|
|
|
|
t: b.TreeNew(itemCompare),
|
|
|
|
availableWriters: make(chan bool, MAX_CONCURRENT_WRITERS),
|
|
|
|
}
|
|
|
|
for i := 0; i < MAX_CONCURRENT_WRITERS; i++ {
|
|
|
|
s.availableWriters <- true
|
|
|
|
}
|
|
|
|
return s, nil
|
2015-01-14 02:35:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func itemCompare(a, b interface{}) int {
|
|
|
|
return bytes.Compare(a.([]byte), b.([]byte))
|
|
|
|
}
|
|
|
|
|
|
|
|
type Store struct {
|
2015-04-24 23:11:47 +02:00
|
|
|
availableWriters chan bool
|
|
|
|
m sync.RWMutex
|
|
|
|
t *b.Tree
|
|
|
|
mo store.MergeOperator
|
2015-01-14 02:35:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) Open() error {
|
|
|
|
return nil
|
2015-01-29 19:22:13 +01:00
|
|
|
}
|
2015-01-14 02:35:13 +01:00
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) SetMergeOperator(mo store.MergeOperator) {
|
|
|
|
s.mo = mo
|
2015-01-14 02:35:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Store) Reader() (store.KVReader, error) {
|
2015-04-24 23:11:47 +02:00
|
|
|
return &Reader{s: s}, nil
|
2015-01-14 02:35:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) Writer() (store.KVWriter, error) {
|
|
|
|
available, ok := <-s.availableWriters
|
|
|
|
if !ok || !available {
|
|
|
|
return nil, fmt.Errorf("no available writers")
|
|
|
|
}
|
|
|
|
return &Writer{s: s, r: &Reader{s: s}}, nil
|
2015-04-03 22:50:48 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) Close() error {
|
|
|
|
return nil
|
2015-01-14 02:35:13 +01:00
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) get(k []byte) ([]byte, error) {
|
|
|
|
s.m.RLock()
|
|
|
|
defer s.m.RUnlock()
|
2015-01-14 02:35:13 +01:00
|
|
|
v, ok := s.t.Get(k)
|
|
|
|
if !ok || v == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return v.([]byte), nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) iterator(k []byte) store.KVIterator {
|
2015-01-14 02:35:13 +01:00
|
|
|
iter := &Iterator{s: s}
|
|
|
|
iter.Seek(k)
|
|
|
|
return iter
|
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) set(k, v []byte) (err error) {
|
2015-01-14 02:35:13 +01:00
|
|
|
s.m.Lock()
|
2015-04-24 23:11:47 +02:00
|
|
|
defer s.m.Unlock()
|
2015-01-14 02:35:13 +01:00
|
|
|
s.t.Set(k, v)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-04-24 23:11:47 +02:00
|
|
|
func (s *Store) delete(k []byte) (err error) {
|
2015-01-14 02:35:13 +01:00
|
|
|
s.m.Lock()
|
2015-04-24 23:11:47 +02:00
|
|
|
defer s.m.Unlock()
|
2015-01-14 02:35:13 +01:00
|
|
|
s.t.Delete(k)
|
|
|
|
return nil
|
|
|
|
}
|