github.com/letsencrypt/trillian@v1.1.2-0.20180615153820-ae375a99d36a/storage/tree_storage.go (about) 1 // Copyright 2016 Google Inc. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package storage 16 17 import ( 18 "context" 19 20 "google.golang.org/grpc/codes" 21 "google.golang.org/grpc/status" 22 ) 23 24 // ErrTreeNeedsInit is returned when calling methods on an uninitialised tree. 25 var ErrTreeNeedsInit = status.Error(codes.FailedPrecondition, "tree needs initialising") 26 27 // ReadOnlyTreeTX represents a read-only transaction on a TreeStorage. 28 // A ReadOnlyTreeTX can only modify the tree specified in its creation. 29 type ReadOnlyTreeTX interface { 30 NodeReader 31 32 // ReadRevision returns the tree revision that was current at the time this 33 // transaction was started. 34 ReadRevision() int64 35 36 // Commit attempts to commit any reads performed under this transaction. 37 Commit() error 38 39 // Rollback aborts this transaction. 40 Rollback() error 41 42 // Close attempts to Rollback the TX if it's open, it's a noop otherwise. 43 Close() error 44 45 // Open indicates if this transaction is open. An open transaction is one for which 46 // Commit() or Rollback() has never been called. Implementations must do all clean up 47 // in these methods so transactions are assumed closed regardless of the reported success. 48 IsOpen() bool 49 } 50 51 // TreeTX represents an in-process tree-modifying transaction. 52 // The transaction must end with a call to Commit or Rollback. 53 // After a call to Commit or Rollback, all operations on the transaction will fail. 54 // After a call to Commit or Rollback implementations must be in a clean state and have 55 // released any resources owned by the TreeTX. 56 // A TreeTX can only modify the tree specified in its creation. 57 type TreeTX interface { 58 ReadOnlyTreeTX 59 TreeWriter 60 } 61 62 // TreeWriter represents additional transaction methods that modify the tree. 63 type TreeWriter interface { 64 // SetMerkleNodes stores the provided nodes, at the transaction's writeRevision. 65 SetMerkleNodes(ctx context.Context, nodes []Node) error 66 67 // WriteRevision returns the tree revision that any writes through this TreeTX will be stored at. 68 WriteRevision() int64 69 } 70 71 // DatabaseChecker performs connectivity checks on the database. 72 type DatabaseChecker interface { 73 // CheckDatabaseAccessible returns nil if the database is accessible, error otherwise. 74 CheckDatabaseAccessible(context.Context) error 75 } 76 77 // NodeReader provides read-only access to the stored tree nodes, as an interface to allow easier 78 // testing of node manipulation. 79 type NodeReader interface { 80 // GetMerkleNodes looks up the set of nodes identified by ids, at treeRevision, and returns them. 81 GetMerkleNodes(ctx context.Context, treeRevision int64, ids []NodeID) ([]Node, error) 82 }