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

« back to all changes in this revision

Viewing changes to index/store/kvstore.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) 2014 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 store
 
16
 
 
17
import "encoding/json"
 
18
 
 
19
// KVStore is an abstraction for working with KV stores.  Note that
 
20
// in order to be used with the bleve.registry, it must also implement
 
21
// a constructor function of the registry.KVStoreConstructor type.
 
22
type KVStore interface {
 
23
 
 
24
        // Writer returns a KVWriter which can be used to
 
25
        // make changes to the KVStore.  If a writer cannot
 
26
        // be obtained a non-nil error is returned.
 
27
        Writer() (KVWriter, error)
 
28
 
 
29
        // Reader returns a KVReader which can be used to
 
30
        // read data from the KVStore.  If a reader cannot
 
31
        // be obtained a non-nil error is returned.
 
32
        Reader() (KVReader, error)
 
33
 
 
34
        // Close closes the KVStore
 
35
        Close() error
 
36
}
 
37
 
 
38
// KVReader is an abstraction of an **ISOLATED** reader
 
39
// In this context isolated is defined to mean that
 
40
// writes/deletes made after the KVReader is opened
 
41
// are not observed.
 
42
// Because there is usually a cost associated with
 
43
// keeping isolated readers active, users should
 
44
// close them as soon as they are no longer needed.
 
45
type KVReader interface {
 
46
 
 
47
        // Get returns the value associated with the key
 
48
        // If the key does not exist, nil is returned.
 
49
        // The caller owns the bytes returned.
 
50
        Get(key []byte) ([]byte, error)
 
51
 
 
52
        // MultiGet retrieves multiple values in one call.
 
53
        MultiGet(keys [][]byte) ([][]byte, error)
 
54
 
 
55
        // PrefixIterator returns a KVIterator that will
 
56
        // visit all K/V pairs with the provided prefix
 
57
        PrefixIterator(prefix []byte) KVIterator
 
58
 
 
59
        // RangeIterator returns a KVIterator that will
 
60
        // visit all K/V pairs >= start AND < end
 
61
        RangeIterator(start, end []byte) KVIterator
 
62
 
 
63
        // Close closes the iterator
 
64
        Close() error
 
65
}
 
66
 
 
67
// KVIterator is an abstraction around key iteration
 
68
type KVIterator interface {
 
69
 
 
70
        // Seek will advance the iterator to the specified key
 
71
        Seek(key []byte)
 
72
 
 
73
        // Next will advance the iterator to the next key
 
74
        Next()
 
75
 
 
76
        // Key returns the key pointed to by the iterator
 
77
        // The bytes returned are **ONLY** valid until the next call to Seek/Next/Close
 
78
        // Continued use after that requires that they be copied.
 
79
        Key() []byte
 
80
 
 
81
        // Value returns the value pointed to by the iterator
 
82
        // The bytes returned are **ONLY** valid until the next call to Seek/Next/Close
 
83
        // Continued use after that requires that they be copied.
 
84
        Value() []byte
 
85
 
 
86
        // Valid returns whether or not the iterator is in a valid state
 
87
        Valid() bool
 
88
 
 
89
        // Current returns Key(),Value(),Valid() in a single operation
 
90
        Current() ([]byte, []byte, bool)
 
91
 
 
92
        // Close closes the iterator
 
93
        Close() error
 
94
}
 
95
 
 
96
// KVWriter is an abstraction for mutating the KVStore
 
97
// KVWriter does **NOT** enforce restrictions of a single writer
 
98
// if the underlying KVStore allows concurrent writes, the
 
99
// KVWriter interface should also do so, it is up to the caller
 
100
// to do this in a way that is safe and makes sense
 
101
type KVWriter interface {
 
102
 
 
103
        // NewBatch returns a KVBatch for performing batch operations on this kvstore
 
104
        NewBatch() KVBatch
 
105
 
 
106
        // NewBatchEx returns a KVBatch and an associated byte array
 
107
        // that's pre-sized based on the KVBatchOptions.  The caller can
 
108
        // use the returned byte array for keys and values associated with
 
109
        // the batch.  Once the batch is either executed or closed, the
 
110
        // associated byte array should no longer be accessed by the
 
111
        // caller.
 
112
        NewBatchEx(KVBatchOptions) ([]byte, KVBatch, error)
 
113
 
 
114
        // ExecuteBatch will execute the KVBatch, the provided KVBatch **MUST** have
 
115
        // been created by the same KVStore (though not necessarily the same KVWriter)
 
116
        // Batch execution is atomic, either all the operations or none will be performed
 
117
        ExecuteBatch(batch KVBatch) error
 
118
 
 
119
        // Close closes the writer
 
120
        Close() error
 
121
}
 
122
 
 
123
// KVBatchOptions provides the KVWriter.NewBatchEx() method with batch
 
124
// preparation and preallocation information.
 
125
type KVBatchOptions struct {
 
126
        // TotalBytes is the sum of key and value bytes needed by the
 
127
        // caller for the entire batch.  It affects the size of the
 
128
        // returned byte array of KVWrite.NewBatchEx().
 
129
        TotalBytes int
 
130
 
 
131
        // NumSets is the number of Set() calls the caller will invoke on
 
132
        // the KVBatch.
 
133
        NumSets int
 
134
 
 
135
        // NumDeletes is the number of Delete() calls the caller will invoke
 
136
        // on the KVBatch.
 
137
        NumDeletes int
 
138
 
 
139
        // NumMerges is the number of Merge() calls the caller will invoke
 
140
        // on the KVBatch.
 
141
        NumMerges int
 
142
}
 
143
 
 
144
// KVBatch is an abstraction for making multiple KV mutations at once
 
145
type KVBatch interface {
 
146
 
 
147
        // Set updates the key with the specified value
 
148
        // both key and value []byte may be reused as soon as this call returns
 
149
        Set(key, val []byte)
 
150
 
 
151
        // Delete removes the specified key
 
152
        // the key []byte may be reused as soon as this call returns
 
153
        Delete(key []byte)
 
154
 
 
155
        // Merge merges old value with the new value at the specified key
 
156
        // as prescribed by the KVStores merge operator
 
157
        // both key and value []byte may be reused as soon as this call returns
 
158
        Merge(key, val []byte)
 
159
 
 
160
        // Reset frees resources for this batch and allows reuse
 
161
        Reset()
 
162
 
 
163
        // Close frees resources
 
164
        Close() error
 
165
}
 
166
 
 
167
// KVStoreStats is an optional interface that KVStores can implement
 
168
// if they're able to report any useful stats
 
169
type KVStoreStats interface {
 
170
        // Stats returns a JSON serializable object representing stats for this KVStore
 
171
        Stats() json.Marshaler
 
172
 
 
173
        StatsMap() map[string]interface{}
 
174
}