0
0
bleve/index/store/forestdb/batch.go

87 lines
1.9 KiB
Go
Raw Normal View History

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 (
"fmt"
2014-10-31 14:42:32 +01:00
)
type op struct {
k []byte
v []byte
}
type Batch struct {
s *Store
ops []op
merges map[string][][]byte
2014-10-31 14:42:32 +01:00
}
func (b *Batch) Set(k, v []byte) {
b.ops = append(b.ops, op{k, v})
2014-10-31 14:42:32 +01:00
}
func (b *Batch) Delete(k []byte) {
b.ops = append(b.ops, op{k, nil})
2014-10-31 14:42:32 +01:00
}
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}
2014-10-31 14:42:32 +01:00
}
b.merges[string(key)] = ops
2014-10-31 14:42:32 +01:00
}
func (b *Batch) Execute() (err error) {
2014-10-31 14:42:32 +01:00
for k, mergeOps := range b.merges {
kb := []byte(k)
existingVal, err := b.s.get(kb)
2014-10-31 14:42:32 +01:00
if err != nil {
return err
}
mergedVal, fullMergeOk := b.s.mo.FullMerge(kb, existingVal, mergeOps)
if !fullMergeOk {
return fmt.Errorf("merge operator returned failure")
}
err = b.s.setlocked(kb, mergedVal)
2014-10-31 14:42:32 +01:00
if err != nil {
return err
}
}
for _, op := range b.ops {
if op.v != nil {
b.s.setlocked(op.k, op.v)
2014-10-31 14:42:32 +01:00
} else {
b.s.deletelocked(op.k)
2014-10-31 14:42:32 +01:00
}
}
return b.s.commit()
2014-10-31 14:42:32 +01:00
}
func (b *Batch) Close() error {
return nil
}