-
Notifications
You must be signed in to change notification settings - Fork 172
/
storage_snapshot.go
64 lines (53 loc) · 1.31 KB
/
storage_snapshot.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
package snapshot
import (
"github.com/onflow/flow-go/model/flow"
)
// Note: StorageSnapshot must be thread safe (or immutable).
type StorageSnapshot interface {
// Get returns the register id's value, or an empty RegisterValue if the id
// is not found. Get should be idempotent (i.e., the same value is returned
// for the same id).
Get(id flow.RegisterID) (flow.RegisterValue, error)
}
type EmptyStorageSnapshot struct{}
func (EmptyStorageSnapshot) Get(
id flow.RegisterID,
) (
flow.RegisterValue,
error,
) {
return nil, nil
}
type ReadFuncStorageSnapshot struct {
ReadFunc func(flow.RegisterID) (flow.RegisterValue, error)
}
func NewReadFuncStorageSnapshot(
readFunc func(flow.RegisterID) (flow.RegisterValue, error),
) StorageSnapshot {
return &ReadFuncStorageSnapshot{
ReadFunc: readFunc,
}
}
func (storage ReadFuncStorageSnapshot) Get(
id flow.RegisterID,
) (
flow.RegisterValue,
error,
) {
return storage.ReadFunc(id)
}
type Peeker interface {
Peek(id flow.RegisterID) (flow.RegisterValue, error)
}
func NewPeekerStorageSnapshot(peeker Peeker) StorageSnapshot {
return NewReadFuncStorageSnapshot(peeker.Peek)
}
type MapStorageSnapshot map[flow.RegisterID]flow.RegisterValue
func (storage MapStorageSnapshot) Get(
id flow.RegisterID,
) (
flow.RegisterValue,
error,
) {
return storage[id], nil
}