-
Notifications
You must be signed in to change notification settings - Fork 15
/
store.go
79 lines (64 loc) · 2.38 KB
/
store.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
// store package represents the storage layer used by the nodes in the chain.
package store
import (
"bytes"
"encoding/gob"
"errors"
)
var (
// ErrNotFound should be returned by storage during a read operation if no
// item exists for the given key.
ErrNotFound = errors.New("that key does not exist")
// ErrDirtyItem should be returned by storage if the latest version for the
// key has not been committed yet.
ErrDirtyItem = errors.New("key has an uncommitted version")
)
type Storer interface {
// Read an item from the store by key. If there is an uncommitted (dirty)
// version of the item in the store, it returns a ErrDirtyItem error. If
// no item exists for that key it returns a ErrNotFound error.
Read(key string) (*Item, error)
// Write a new item to the store.
Write(key string, val []byte, version uint64) error
// Commit a version for the given key. All items with matching key and older
// than version are cleared.
Commit(key string, version uint64) error
// ReadVersion finds an item for the given key with the matching version. If
// no item is found for that version of key, ErrNotFound is returned
ReadVersion(key string, version uint64) (*Item, error)
// AllNewerCommitted returns all committed items who's key is not in
// versionsByKey or who's version is higher than the versions in
// versionsByKey.
AllNewerCommitted(versionsByKey map[string]uint64) ([]*Item, error)
// AllNewerDirty returns all uncommitted items who's key is not in
// versionsByKey or who's version is higher than the versions in
// versionsByKey.
AllNewerDirty(versionsByKey map[string]uint64) ([]*Item, error)
// AllDirty returns all uncommitted items.
AllDirty() ([]*Item, error)
// AllCommitted returns all committed items.
AllCommitted() ([]*Item, error)
}
// Item is an object in the Store. A key inside the store might have multiple
// versions.
type Item struct {
Version uint64
Committed bool
Value []byte
Key string
}
// Encode returns the byte representation of the interface given, using
// gob.NewEncoder.
func Encode(i interface{}) ([]byte, error) {
buf := bytes.Buffer{}
enc := gob.NewEncoder(&buf)
err := enc.Encode(i)
return buf.Bytes(), err
}
// DecodeMany decodes a byte slice into a slice of Items.
func DecodeMany(b []byte) ([]*Item, error) {
i := make([]*Item, 0)
dec := gob.NewDecoder(bytes.NewReader(b))
err := dec.Decode(&i)
return i, err
}