forked from kaspanet/kaspad
-
Notifications
You must be signed in to change notification settings - Fork 26
/
database.go
99 lines (79 loc) · 2.9 KB
/
database.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
package model
// DBCursor iterates over database entries given some bucket.
type DBCursor interface {
// Next moves the iterator to the next key/value pair. It returns whether the
// iterator is exhausted. Panics if the cursor is closed.
Next() bool
// First moves the iterator to the first key/value pair. It returns false if
// such a pair does not exist. Panics if the cursor is closed.
First() bool
// Seek moves the iterator to the first key/value pair whose key is greater
// than or equal to the given key. It returns ErrNotFound if such pair does not
// exist.
Seek(key DBKey) error
// Key returns the key of the current key/value pair, or ErrNotFound if done.
// The caller should not modify the contents of the returned key, and
// its contents may change on the next call to Next.
Key() (DBKey, error)
// Value returns the value of the current key/value pair, or ErrNotFound if done.
// The caller should not modify the contents of the returned slice, and its
// contents may change on the next call to Next.
Value() ([]byte, error)
// Close releases associated resources.
Close() error
}
// DBReader defines a proxy over domain data access
type DBReader interface {
// Get gets the value for the given key. It returns
// ErrNotFound if the given key does not exist.
Get(key DBKey) ([]byte, error)
// Has returns true if the database does contains the
// given key.
Has(key DBKey) (bool, error)
// Cursor begins a new cursor over the given bucket.
Cursor(bucket DBBucket) (DBCursor, error)
}
// DBWriter is an interface to write to the database
type DBWriter interface {
DBReader
// Put sets the value for the given key. It overwrites
// any previous value for that key.
Put(key DBKey, value []byte) error
// Delete deletes the value for the given key. Will not
// return an error if the key doesn't exist.
Delete(key DBKey) error
}
// DBTransaction is a proxy over domain data
// access that requires an open database transaction
type DBTransaction interface {
DBWriter
// Rollback rolls back whatever changes were made to the
// database within this transaction.
Rollback() error
// Commit commits whatever changes were made to the database
// within this transaction.
Commit() error
// RollbackUnlessClosed rolls back changes that were made to
// the database within the transaction, unless the transaction
// had already been closed using either Rollback or Commit.
RollbackUnlessClosed() error
}
// DBManager defines the interface of a database that can begin
// transactions and read data.
type DBManager interface {
DBWriter
// Begin begins a new database transaction.
Begin() (DBTransaction, error)
}
// DBKey is an interface for a database key
type DBKey interface {
Bytes() []byte
Bucket() DBBucket
Suffix() []byte
}
// DBBucket is an interface for a database bucket
type DBBucket interface {
Bucket(bucketBytes []byte) DBBucket
Key(suffix []byte) DBKey
Path() []byte
}