Switch from Godep to go vendoring

This commit is contained in:
Ken-Håvard Lieng 2016-03-01 01:51:26 +01:00
parent 6b37713bc0
commit cd317761c5
1504 changed files with 263076 additions and 34441 deletions

View file

@ -0,0 +1,88 @@
// 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.
// +build go1.4
package cznicb
import ()
type op struct {
k []byte
v []byte
}
type Batch struct {
s *Store
ops []op
merges map[string][][]byte
}
func (b *Batch) Set(k, v []byte) {
b.ops = append(b.ops, op{k, v})
}
func (b *Batch) Delete(k []byte) {
b.ops = append(b.ops, op{k, nil})
}
func (b *Batch) Merge(key, val []byte) {
ops, ok := b.merges[string(key)]
if ok && len(ops) > 0 {
last := ops[len(ops)-1]
mergedVal, partialMergeOk := b.s.mo.PartialMerge(key, last, val)
if partialMergeOk {
// replace last entry with the result of the merge
ops[len(ops)-1] = mergedVal
} else {
// could not partial merge, append this to the end
ops = append(ops, val)
}
} else {
ops = [][]byte{val}
}
b.merges[string(key)] = ops
}
func (b *Batch) Execute() (err error) {
b.s.m.Lock()
defer b.s.m.Unlock()
t := b.s.t
for key, mergeOps := range b.merges {
k := []byte(key)
t.Put(k, func(oldV interface{}, exists bool) (newV interface{}, write bool) {
ob := []byte(nil)
if exists && oldV != nil {
ob = oldV.([]byte)
}
mergedVal, fullMergeOk := b.s.mo.FullMerge(k, ob, mergeOps)
if !fullMergeOk {
return nil, false
}
return mergedVal, true
})
}
for _, op := range b.ops {
if op.v != nil {
t.Set(op.k, op.v)
} else {
t.Delete(op.k)
}
}
return nil
}
func (b *Batch) Close() error {
return nil
}

View file

@ -0,0 +1,112 @@
// 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.
// +build go1.4
// 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"
"fmt"
"sync"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/registry"
"github.com/cznic/b"
)
const Name = "cznicb"
const MAX_CONCURRENT_WRITERS = 1
func init() {
registry.RegisterKVStore(Name, StoreConstructor)
}
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
}
func itemCompare(a, b interface{}) int {
return bytes.Compare(a.([]byte), b.([]byte))
}
type Store struct {
availableWriters chan bool
m sync.RWMutex
t *b.Tree
mo store.MergeOperator
}
func (s *Store) Open() error {
return nil
}
func (s *Store) SetMergeOperator(mo store.MergeOperator) {
s.mo = mo
}
func (s *Store) Reader() (store.KVReader, error) {
return &Reader{s: s}, nil
}
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
}
func (s *Store) Close() error {
return nil
}
func (s *Store) get(k []byte) ([]byte, error) {
s.m.RLock()
defer s.m.RUnlock()
v, ok := s.t.Get(k)
if !ok || v == nil {
return nil, nil
}
return v.([]byte), nil
}
func (s *Store) iterator(k []byte) store.KVIterator {
iter := &Iterator{s: s}
iter.Seek(k)
return iter
}
func (s *Store) set(k, v []byte) (err error) {
s.m.Lock()
defer s.m.Unlock()
s.t.Set(k, v)
return nil
}
func (s *Store) delete(k []byte) (err error) {
s.m.Lock()
defer s.m.Unlock()
s.t.Delete(k)
return nil
}

View file

@ -0,0 +1,133 @@
// 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.
package cznicb
import (
"testing"
"github.com/blevesearch/bleve/index/store"
)
func TestCznicBStore(t *testing.T) {
s, err := StoreConstructor(nil)
if err != nil {
t.Fatal(err)
}
CommonTestKVStore(t, s)
}
func CommonTestKVStore(t *testing.T, s store.KVStore) {
writer, err := s.Writer()
if err != nil {
t.Error(err)
}
err = writer.Set([]byte("a"), []byte("val-a"))
if err != nil {
t.Fatal(err)
}
v, err := writer.Get([]byte("a"))
if err != nil {
t.Fatal(err)
}
if string(v) != "val-a" {
t.Errorf("expected val-a")
}
v, err = writer.Get([]byte("not-there"))
if err != nil {
t.Fatal(err)
}
if v != nil {
t.Errorf("expected nil v")
}
err = writer.Set([]byte("z"), []byte("val-z"))
if err != nil {
t.Fatal(err)
}
err = writer.Delete([]byte("z"))
if err != nil {
t.Fatal(err)
}
batch := writer.NewBatch()
batch.Set([]byte("b"), []byte("val-b"))
batch.Set([]byte("c"), []byte("val-c"))
batch.Set([]byte("d"), []byte("val-d"))
batch.Set([]byte("e"), []byte("val-e"))
batch.Set([]byte("f"), []byte("val-f"))
batch.Set([]byte("g"), []byte("val-g"))
batch.Set([]byte("h"), []byte("val-h"))
batch.Set([]byte("i"), []byte("val-i"))
batch.Set([]byte("j"), []byte("val-j"))
err = batch.Execute()
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
reader, err := s.Reader()
if err != nil {
t.Error(err)
}
defer func() {
err := reader.Close()
if err != nil {
t.Fatal(err)
}
}()
it := reader.Iterator([]byte("b"))
key, val, valid := it.Current()
if !valid {
t.Fatalf("valid false, expected true")
}
if string(key) != "b" {
t.Fatalf("expected key b, got %s", key)
}
if string(val) != "val-b" {
t.Fatalf("expected value val-b, got %s", val)
}
it.Next()
key, val, valid = it.Current()
if !valid {
t.Fatalf("valid false, expected true")
}
if string(key) != "c" {
t.Fatalf("expected key c, got %s", key)
}
if string(val) != "val-c" {
t.Fatalf("expected value val-c, got %s", val)
}
it.Seek([]byte("i"))
key, val, valid = it.Current()
if !valid {
t.Fatalf("valid false, expected true")
}
if string(key) != "i" {
t.Fatalf("expected key i, got %s", key)
}
if string(val) != "val-i" {
t.Fatalf("expected value val-i, got %s", val)
}
err = it.Close()
if err != nil {
t.Fatal(err)
}
}

View file

@ -0,0 +1,113 @@
// 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.
// +build go1.4
package cznicb
import (
"errors"
"github.com/cznic/b"
)
var iteratorDoneErr = errors.New("iteratorDoneErr") // A sentinel value.
type Iterator struct { // Assuming that iterators are used single-threaded.
s *Store
e *b.Enumerator
currK interface{}
currV interface{}
currErr error
}
func (i *Iterator) SeekFirst() {
i.currK = nil
i.currV = nil
i.currErr = nil
var err error
i.s.m.RLock()
i.e, err = i.s.t.SeekFirst()
i.s.m.RUnlock() // cannot defer, must unlock before Next
if err != nil {
i.currK = nil
i.currV = nil
i.currErr = iteratorDoneErr
}
i.Next()
}
func (i *Iterator) Seek(k []byte) {
i.currK = nil
i.currV = nil
i.currErr = nil
i.s.m.RLock()
i.e, _ = i.s.t.Seek(k)
i.s.m.RUnlock() // cannot defer, must unlock before Next
i.Next()
}
func (i *Iterator) Next() {
if i.currErr != nil {
i.currK = nil
i.currV = nil
i.currErr = iteratorDoneErr
return
}
i.s.m.RLock()
defer i.s.m.RUnlock()
i.currK, i.currV, i.currErr = i.e.Next()
}
func (i *Iterator) Current() ([]byte, []byte, bool) {
if i.currErr == iteratorDoneErr ||
i.currK == nil ||
i.currV == nil {
return nil, nil, false
}
return i.currK.([]byte), i.currV.([]byte), true
}
func (i *Iterator) Key() []byte {
k, _, ok := i.Current()
if !ok {
return nil
}
return k
}
func (i *Iterator) Value() []byte {
_, v, ok := i.Current()
if !ok {
return nil
}
return v
}
func (i *Iterator) Valid() bool {
_, _, ok := i.Current()
return ok
}
func (i *Iterator) Close() error {
if i.e != nil {
i.e.Close()
}
i.e = nil
return nil
}

View file

@ -0,0 +1,44 @@
// 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.
// +build go1.4
package cznicb
import (
"github.com/blevesearch/bleve/index/store"
)
type Reader struct {
s *Store
}
func newReader(s *Store) (*Reader, error) {
return &Reader{
s: s,
}, nil
}
func (r *Reader) BytesSafeAfterClose() bool {
return false
}
func (r *Reader) Get(key []byte) ([]byte, error) {
return r.s.get(key)
}
func (r *Reader) Iterator(key []byte) store.KVIterator {
return r.s.iterator(key)
}
func (r *Reader) Close() error {
return nil
}

View file

@ -0,0 +1,55 @@
// 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 go1.4
package cznicb
import (
"github.com/blevesearch/bleve/index/store"
)
type Writer struct {
s *Store
r *Reader
}
func (w *Writer) BytesSafeAfterClose() bool {
return false
}
func (w *Writer) Set(key, val []byte) error {
return w.s.set(key, val)
}
func (w *Writer) Delete(key []byte) error {
return w.s.delete(key)
}
func (w *Writer) NewBatch() store.KVBatch {
return &Batch{
s: w.s,
ops: make([]op, 0, 1000),
merges: make(map[string][][]byte),
}
}
func (w *Writer) Close() error {
w.s.availableWriters <- true
w.s = nil
return nil
}
func (w *Writer) Get(key []byte) ([]byte, error) {
return w.r.s.get(key)
}
func (w *Writer) Iterator(key []byte) store.KVIterator {
return w.r.s.iterator(key)
}