~ubuntu-branches/debian/sid/golang-github-blevesearch-bleve/sid

« back to all changes in this revision

Viewing changes to index/store/moss/batch.go

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-30 16:06:03 UTC
  • Revision ID: package-import@ubuntu.com-20170330160603-0oogmb960l7918jx
Tags: upstream-0.5.0+git20170324.202.4702785f
ImportĀ upstreamĀ versionĀ 0.5.0+git20170324.202.4702785f

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright (c) 2016 Couchbase, Inc.
 
2
//
 
3
// Licensed under the Apache License, Version 2.0 (the "License");
 
4
// you may not use this file except in compliance with the License.
 
5
// You may obtain a copy of the License at
 
6
//
 
7
//              http://www.apache.org/licenses/LICENSE-2.0
 
8
//
 
9
// Unless required by applicable law or agreed to in writing, software
 
10
// distributed under the License is distributed on an "AS IS" BASIS,
 
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
// See the License for the specific language governing permissions and
 
13
// limitations under the License.
 
14
 
 
15
package moss
 
16
 
 
17
import (
 
18
        "github.com/couchbase/moss"
 
19
 
 
20
        "github.com/blevesearch/bleve/index/store"
 
21
)
 
22
 
 
23
type Batch struct {
 
24
        store   *Store
 
25
        merge   *store.EmulatedMerge
 
26
        batch   moss.Batch
 
27
        buf     []byte // Non-nil when using pre-alloc'ed / NewBatchEx().
 
28
        bufUsed int
 
29
}
 
30
 
 
31
func (b *Batch) Set(key, val []byte) {
 
32
        var err error
 
33
        if b.buf != nil {
 
34
                b.bufUsed += len(key) + len(val)
 
35
                err = b.batch.AllocSet(key, val)
 
36
        } else {
 
37
                err = b.batch.Set(key, val)
 
38
        }
 
39
 
 
40
        if err != nil {
 
41
                b.store.Logf("bleve moss batch.Set err: %v", err)
 
42
        }
 
43
}
 
44
 
 
45
func (b *Batch) Delete(key []byte) {
 
46
        var err error
 
47
        if b.buf != nil {
 
48
                b.bufUsed += len(key)
 
49
                err = b.batch.AllocDel(key)
 
50
        } else {
 
51
                err = b.batch.Del(key)
 
52
        }
 
53
 
 
54
        if err != nil {
 
55
                b.store.Logf("bleve moss batch.Delete err: %v", err)
 
56
        }
 
57
}
 
58
 
 
59
func (b *Batch) Merge(key, val []byte) {
 
60
        if b.buf != nil {
 
61
                b.bufUsed += len(key) + len(val)
 
62
        }
 
63
        b.merge.Merge(key, val)
 
64
}
 
65
 
 
66
func (b *Batch) Reset() {
 
67
        err := b.Close()
 
68
        if err != nil {
 
69
                b.store.Logf("bleve moss batch.Close err: %v", err)
 
70
                return
 
71
        }
 
72
 
 
73
        batch, err := b.store.ms.NewBatch(0, 0)
 
74
        if err == nil {
 
75
                b.batch = batch
 
76
                b.merge = store.NewEmulatedMerge(b.store.mo)
 
77
                b.buf = nil
 
78
                b.bufUsed = 0
 
79
        }
 
80
}
 
81
 
 
82
func (b *Batch) Close() error {
 
83
        b.merge = nil
 
84
        err := b.batch.Close()
 
85
        b.batch = nil
 
86
        return err
 
87
}