@@ -87,17 +87,14 @@ type Iterator interface {
87
87
ComputeStats (start , end MVCCKey , nowNanos int64 ) (MVCCStats , error )
88
88
}
89
89
90
- // Engine is the interface that wraps the core operations of a
91
- // key/value store.
92
- type Engine interface {
93
- // Open initializes the engine.
94
- Open () error
95
- // Close closes the engine, freeing up any outstanding resources.
90
+ // Reader is the read interface to an engine's data.
91
+ type Reader interface {
92
+ // Close closes the reader, freeing up any outstanding resources.
96
93
Close ()
97
- // Attrs returns the engine/store attributes .
98
- Attrs () roachpb. Attributes
99
- // Put sets the given key to the value provided .
100
- Put ( key MVCCKey , value [] byte ) error
94
+ // closed returns true if the reader has been closed or is not usable .
95
+ // Objects backed by this reader (e.g. Iterators) can check this to ensure
96
+ // that they are not using a closed engine .
97
+ closed () bool
101
98
// Get returns the value for the given key, nil otherwise.
102
99
Get (key MVCCKey ) ([]byte , error )
103
100
// GetProto fetches the value at the specified key and unmarshals it
@@ -111,6 +108,21 @@ type Engine interface {
111
108
// an error, the iteration will stop and return the error.
112
109
// If the first result of f is true, the iteration stops.
113
110
Iterate (start , end MVCCKey , f func (MVCCKeyValue ) (bool , error )) error
111
+ // NewIterator returns a new instance of an Iterator over this engine. When
112
+ // prefix is true, Seek will use the user-key prefix of the supplied MVCC key
113
+ // to restrict which sstables are searched, but iteration (using Next) over
114
+ // keys without the same user-key prefix will not work correctly (keys may be
115
+ // skipped). The caller must invoke Iterator.Close() when finished with the
116
+ // iterator to free resources.
117
+ NewIterator (prefix bool ) Iterator
118
+ }
119
+
120
+ // Writer is the write interface to an engine's data.
121
+ type Writer interface {
122
+ // ApplyBatchRepr atomically applies a set of batched updates. Created by
123
+ // calling Repr() on a batch. Using this method is equivalent to constructing
124
+ // and committing a batch whose Repr() equals repr.
125
+ ApplyBatchRepr (repr []byte ) error
114
126
// Clear removes the item from the db with the given key.
115
127
// Note that clear actually removes entries from the storage
116
128
// engine, rather than inserting tombstones.
@@ -129,67 +141,63 @@ type Engine interface {
129
141
//
130
142
// The logic for merges is written in db.cc in order to be compatible with RocksDB.
131
143
Merge (key MVCCKey , value []byte ) error
144
+ // Put sets the given key to the value provided.
145
+ Put (key MVCCKey , value []byte ) error
146
+ }
147
+
148
+ // ReadWriter is the read/write interface to an engine's data.
149
+ type ReadWriter interface {
150
+ Reader
151
+ Writer
152
+ }
153
+
154
+ // Engine is the interface that wraps the core operations of a key/value store.
155
+ type Engine interface {
156
+ ReadWriter
157
+ // Attrs returns the engine/store attributes.
158
+ Attrs () roachpb.Attributes
132
159
// Capacity returns capacity details for the engine's available storage.
133
160
Capacity () (roachpb.StoreCapacity , error )
134
161
// Flush causes the engine to write all in-memory data to disk
135
162
// immediately.
136
163
Flush () error
137
- // NewIterator returns a new instance of an Iterator over this engine. When
138
- // prefix is true, Seek will use the user-key prefix of the supplied MVCC key
139
- // to restrict which sstables are searched, but iteration (using Next) over
140
- // keys without the same user-key prefix will not work correctly (keys may be
141
- // skipped). The caller must invoke Iterator.Close() when finished with the
142
- // iterator to free resources.
143
- NewIterator (prefix bool ) Iterator
164
+ // GetStats retrieves stats from the engine.
165
+ GetStats () (* Stats , error )
166
+ // NewBatch returns a new instance of a batched engine which wraps
167
+ // this engine. Batched engines accumulate all mutations and apply
168
+ // them atomically on a call to Commit().
169
+ NewBatch () Batch
144
170
// NewSnapshot returns a new instance of a read-only snapshot
145
171
// engine. Snapshots are instantaneous and, as long as they're
146
172
// released relatively quickly, inexpensive. Snapshots are released
147
173
// by invoking Close(). Note that snapshots must not be used after the
148
174
// original engine has been stopped.
149
- NewSnapshot () Engine
150
- // NewBatch returns a new instance of a batched engine which wraps
151
- // this engine. Batched engines accumulate all mutations and apply
152
- // them atomically on a call to Commit().
153
- NewBatch () Batch
154
- // Commit atomically applies any batched updates to the underlying
155
- // engine. This is a noop unless the engine was created via NewBatch().
156
- Commit () error
157
- // ApplyBatchRepr atomically applies a set of batched updates. Created by
158
- // calling Repr() on a batch. Using this method is equivalent to constructing
159
- // and committing a batch whose Repr() equals repr.
160
- ApplyBatchRepr (repr []byte ) error
161
- // Repr returns the underlying representation of the batch and can be used to
162
- // reconstitute the batch on a remote node using
163
- // Engine.NewBatchFromRepr(). This method is only valid on engines created
164
- // via NewBatch().
165
- Repr () []byte
166
- // Defer adds a callback to be run after the batch commits
167
- // successfully. If Commit() fails (or if this engine was not
168
- // created via NewBatch()), deferred callbacks are not called. As
169
- // with the defer statement, the last callback to be deferred is the
170
- // first to be executed.
171
- Defer (fn func ())
172
- // Closed returns true if the engine has been close or not usable.
173
- // Objects backed by this engine (e.g. Iterators) can check this to ensure
174
- // that they are not using an closed engine.
175
- Closed () bool
176
- // GetStats retrieves stats from the engine.
177
- GetStats () (* Stats , error )
175
+ NewSnapshot () Reader
176
+ // Open initializes the engine.
177
+ Open () error
178
178
}
179
179
180
180
// Batch is the interface for batch specific operations.
181
- //
182
- // TODO(peter): Move various methods of Engine to Batch, such as Commit() and Repr().
183
181
type Batch interface {
184
- Engine
182
+ ReadWriter
183
+ // Commit atomically applies any batched updates to the underlying
184
+ // engine. This is a noop unless the engine was created via NewBatch().
185
+ Commit () error
186
+ // Defer adds a callback to be run after the batch commits successfully. If
187
+ // Commit() fails deferred callbacks are not called. As with the defer
188
+ // statement, the last callback to be deferred is the first to be executed.
189
+ Defer (fn func ())
185
190
// Distinct returns a view of the existing batch which passes reads directly
186
191
// to the underlying engine (the one the batch was created from). That is,
187
192
// the returned batch will not read its own writes. This is used as an
188
193
// optimization to avoid flushing mutations buffered by the batch in
189
194
// situations where we know all of the batched operations are for distinct
190
195
// keys. Closing/committing the returned engine is equivalent to
191
196
// closing/committing the original batch.
192
- Distinct () Engine
197
+ Distinct () ReadWriter
198
+ // Repr returns the underlying representation of the batch and can be used to
199
+ // reconstitute the batch on a remote node using Writer.ApplyBatchRepr().
200
+ Repr () []byte
193
201
}
194
202
195
203
// Stats is a set of RocksDB stats. These are all described in RocksDB
@@ -220,7 +228,7 @@ type Stats struct {
220
228
// PutProto sets the given key to the protobuf-serialized byte string
221
229
// of msg and the provided timestamp. Returns the length in bytes of
222
230
// key and the value.
223
- func PutProto (engine Engine , key MVCCKey , msg proto.Message ) (keyBytes , valBytes int64 , err error ) {
231
+ func PutProto (engine Writer , key MVCCKey , msg proto.Message ) (keyBytes , valBytes int64 , err error ) {
224
232
bytes , err := protoutil .Marshal (msg )
225
233
if err != nil {
226
234
return 0 , 0 , err
@@ -236,7 +244,7 @@ func PutProto(engine Engine, key MVCCKey, msg proto.Message) (keyBytes, valBytes
236
244
// Scan returns up to max key/value objects starting from
237
245
// start (inclusive) and ending at end (non-inclusive).
238
246
// Specify max=0 for unbounded scans.
239
- func Scan (engine Engine , start , end MVCCKey , max int64 ) ([]MVCCKeyValue , error ) {
247
+ func Scan (engine Reader , start , end MVCCKey , max int64 ) ([]MVCCKeyValue , error ) {
240
248
var kvs []MVCCKeyValue
241
249
err := engine .Iterate (start , end , func (kv MVCCKeyValue ) (bool , error ) {
242
250
if max != 0 && int64 (len (kvs )) >= max {
@@ -254,18 +262,16 @@ func Scan(engine Engine, start, end MVCCKey, max int64) ([]MVCCKeyValue, error)
254
262
// none, and an error will be returned. Note that this function
255
263
// actually removes entries from the storage engine, rather than
256
264
// inserting tombstones, as with deletion through the MVCC.
257
- func ClearRange (engine Engine , start , end MVCCKey ) (int , error ) {
258
- b := engine .NewBatch ()
259
- defer b .Close ()
265
+ func ClearRange (engine ReadWriter , start , end MVCCKey ) (int , error ) {
260
266
count := 0
261
267
if err := engine .Iterate (start , end , func (kv MVCCKeyValue ) (bool , error ) {
262
- if err := b .Clear (kv .Key ); err != nil {
268
+ if err := engine .Clear (kv .Key ); err != nil {
263
269
return false , err
264
270
}
265
271
count ++
266
272
return false , nil
267
273
}); err != nil {
268
274
return 0 , err
269
275
}
270
- return count , b . Commit ()
276
+ return count , nil
271
277
}
0 commit comments