-
Notifications
You must be signed in to change notification settings - Fork 2
/
engine.go
115 lines (103 loc) · 4.32 KB
/
engine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2014 The Cockroach Authors.
//
// 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 engine
// Reader is the read interface to an engine's data.
type Reader interface {
// Close closes the reader, freeing up any outstanding resources. Note that
// various implementations have slightly different behaviors. In particular,
// Distinct() batches release their parent batch for future use while
// Engines, Snapshots and Batches free the associated C++ resources.
Close()
// Get returns the value for the given key, nil otherwise.
Get(key []byte) ([]byte, error)
// NewIterator returns a new instance of an Iterator over this engine. When
// prefix is true, Seek will use the user-key prefix of the supplied MVCC key
// to restrict which sstables are searched, but iteration (using Next) over
// keys without the same user-key prefix will not work correctly (keys may be
// skipped). The caller must invoke Iterator.Close() when finished with the
// iterator to free resources.
NewIterator() Iterator
}
// Writer is the write interface to an engine's data.
type Writer interface {
//// Clear removes the item from the db with the given key.
//// Note that clear actually removes entries from the storage
//// engine, rather than inserting tombstones.
Clear(key []byte) error
//// The logic for merges is written in db.cc in order to be compatible with RocksDB.
//Merge(key MVCCKey, value []byte) error
// Put sets the given key to the value provided.
Put(key []byte, value []byte) error
}
// ReadWriter is the read/write interface to an engine's data.
type ReadWriter interface {
Reader
Writer
}
// Engine is the interface that wraps the core operations of a key/value store.
type Engine interface {
ReadWriter
//// Not thread safe.
//GetAuxiliaryDir() string
// NewBatch returns a new instance of a batched engine which wraps
// this engine. Batched engines accumulate all mutations and apply
// them atomically on a call to Commit().
NewBatch(update bool) Batch
// NewSnapshot returns a new instance of a read-only snapshot
// engine. Snapshots are instantaneous and, as long as they're
// released relatively quickly, inexpensive. Snapshots are released
// by invoking Close(). Note that snapshots must not be used after the
// original engine has been stopped.
NewSnapshot() Reader
}
// Batch is the interface for batch specific operations.
type Batch interface {
ReadWriter
// Commit atomically applies any batched updates to the underlying
// engine. This is a noop unless the engine was created via NewBatch(). If
// sync is true, the batch is synchronously committed to disk.
Commit() error
}
// SimpleIterator is an interface for iterating over key/value pairs in an
// engine. SimpleIterator implementations are thread safe unless otherwise
// noted. SimpleIterator is a subset of the functionality offered by Iterator.
type SimpleIterator interface {
// Close frees up resources held by the iterator.
Close()
// Seek advances the iterator to the first key in the engine which
// is >= the provided key.
Seek(key []byte)
// Valid must be called after any call to Seek(), Next(), Prev(), or
// similar methods. It returns (true, nil) if the iterator points to
// a valid key (it is undefined to call Key(), Value(), or similar
// methods unless Valid() has returned (true, nil)). It returns
// (false, nil) if the iterator has moved past the end of the valid
// range, or (false, err) if an error has occurred. Valid() will
// never return true with a non-nil error.
Valid() bool
ValidForPrefix(prefix []byte) bool
// Next advances the iterator to the next key/value in the
// iteration. After this call, Valid() will be true if the
// iterator was not positioned at the last key.
Next()
Rewind()
}
type ItemIntf interface {
Key() []byte
Value() ([]byte, error)
}
type Iterator interface {
SimpleIterator
Item() ItemIntf
}