/
session.go
104 lines (89 loc) · 2.74 KB
/
session.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
package kv
import (
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble"
)
// Common errors returned by the engine.
var (
// ErrKeyNotFound is returned when the targeted key doesn't exist.
ErrKeyNotFound = errors.New("key not found")
// ErrKeyAlreadyExists is returned when the targeted key already exists.
ErrKeyAlreadyExists = errors.New("key already exists")
)
type Session interface {
Commit() error
Close() error
// Insert inserts a key-value pair. If it already exists, it returns ErrKeyAlreadyExists.
Insert(k, v []byte) error
// Put stores a key-value pair. If it already exists, it overrides it.
Put(k, v []byte) error
// Get returns a value associated with the given key. If not found, returns ErrKeyNotFound.
Get(k []byte) ([]byte, error)
// Exists returns whether a key exists and is visible by the current session.
Exists(k []byte) (bool, error)
// Delete a record by key. If not found, returns ErrKeyNotFound.
Delete(k []byte) error
DeleteRange(start []byte, end []byte) error
Iterator(opts *IterOptions) (Iterator, error)
}
type Iterator interface {
Close() error
First() bool
Last() bool
Valid() bool
Next() bool
Prev() bool
Error() error
Key() []byte
Value() ([]byte, error)
}
type IterOptions struct {
// LowerBound specifies the smallest key (inclusive) that the iterator will
// return during iteration. If the iterator is seeked or iterated past this
// boundary the iterator will return Valid()==false. Setting LowerBound
// effectively truncates the key space visible to the iterator.
LowerBound []byte
// UpperBound specifies the largest key (exclusive) that the iterator will
// return during iteration. If the iterator is seeked or iterated past this
// boundary the iterator will return Valid()==false. Setting UpperBound
// effectively truncates the key space visible to the iterator.
UpperBound []byte
}
type iterator struct {
*pebble.Iterator
}
func (i *iterator) Value() ([]byte, error) {
return i.Iterator.ValueAndErr()
}
// Get returns a value associated with the given key. If not found, returns ErrKeyNotFound.
func get(r pebble.Reader, k []byte) ([]byte, error) {
value, closer, err := r.Get(k)
if err != nil {
if errors.Is(err, pebble.ErrNotFound) {
return nil, errors.WithStack(ErrKeyNotFound)
}
return nil, err
}
cp := make([]byte, len(value))
copy(cp, value)
err = closer.Close()
if err != nil {
return nil, err
}
return cp, nil
}
// Exists returns whether a key exists and is visible by the current session.
func exists(r pebble.Reader, k []byte) (bool, error) {
_, closer, err := r.Get(k)
if err != nil {
if errors.Is(err, pebble.ErrNotFound) {
return false, nil
}
return false, err
}
err = closer.Close()
if err != nil {
return false, err
}
return true, nil
}