forked from google/trillian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_storage.go
118 lines (101 loc) · 4.31 KB
/
map_storage.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
116
117
118
// Copyright 2016 Google Inc. All Rights Reserved.
//
// 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 storage
import (
"context"
"errors"
"github.com/google/trillian"
)
// ReadOnlyMapTX provides a read-only view into log data.
// A ReadOnlyMapTX, unlike ReadOnlyMapTreeTX, is not tied to a particular tree.
type ReadOnlyMapTX interface {
// Commit ensures the data read by the TX is consistent in the database. Only after Commit the
// data read should be regarded as valid.
Commit() error
// Rollback discards the read-only TX.
Rollback() error
// Close attempts to Rollback the TX if it's open, it's a noop otherwise.
Close() error
}
// ReadOnlyMapTreeTX provides a read-only view into the Map data.
// A ReadOnlyMapTreeTX can only read from the tree specified in its creation.
type ReadOnlyMapTreeTX interface {
ReadOnlyTreeTX
MapRootReader
Getter
}
// MapTreeTX is the transactional interface for reading/modifying a Map.
// It extends the basic TreeTX interface with Map specific methods.
// After a call to Commit or Rollback implementations must be in a clean state and have
// released any resources owned by the MapTX.
// A MapTreeTX can only read from the tree specified in its creation.
type MapTreeTX interface {
TreeTX
MapRootReader
MapRootWriter
Getter
Setter
}
// ErrMapNeedsInit is an error returned from SnapshotForTree / BeginForTree when used
// on a uninitialized map storage - i.e. a new, empty map in which the Revision 0 SMH
// hasn't yet been created.
var ErrMapNeedsInit = errors.New("uninitialized map")
// ReadOnlyMapStorage provides a narrow read-only view into a MapStorage.
type ReadOnlyMapStorage interface {
DatabaseChecker
// Snapshot starts a read-only transaction not tied to any particular tree.
Snapshot(ctx context.Context) (ReadOnlyMapTX, error)
// SnapshotForTree starts a new read-only transaction.
// Commit must be called when the caller is finished with the returned object,
// and values read through it should only be propagated if Commit returns
// without error.
SnapshotForTree(ctx context.Context, treeID int64) (ReadOnlyMapTreeTX, error)
}
// MapStorage should be implemented by concrete storage mechanisms which want to support Maps
type MapStorage interface {
ReadOnlyMapStorage
// BeginForTree starts a new Map transaction.
// Either Commit or Rollback must be called when the caller is finished with
// the returned object, and values read through it should only be propagated
// if Commit returns without error.
BeginForTree(ctx context.Context, treeID int64) (MapTreeTX, error)
}
// Setter allows the setting of key->value pairs on the map.
type Setter interface {
// Set sets key to leaf
Set(ctx context.Context, keyHash []byte, value trillian.MapLeaf) error
}
// Getter allows access to the values stored in the map.
type Getter interface {
// Get retrieves the values associates with the keyHashes, if any, at the
// specified revision.
// Setting revision to -1 will fetch the latest revision.
// The returned array of MapLeaves will only contain entries for which values
// exist. i.e. requesting a set of unknown keys would result in a
// zero-length array being returned.
Get(ctx context.Context, revision int64, keyHashes [][]byte) ([]trillian.MapLeaf, error)
}
// MapRootReader provides access to the map roots.
type MapRootReader interface {
// GetSignedMapRoot returns the SignedMapRoot associated with the
// specified revision.
GetSignedMapRoot(ctx context.Context, revision int64) (trillian.SignedMapRoot, error)
// LatestSignedMapRoot returns the most recently created SignedMapRoot.
LatestSignedMapRoot(ctx context.Context) (trillian.SignedMapRoot, error)
}
// MapRootWriter allows the storage of new SignedMapRoots
type MapRootWriter interface {
// StoreSignedMapRoot stores root.
StoreSignedMapRoot(ctx context.Context, root trillian.SignedMapRoot) error
}