-
Notifications
You must be signed in to change notification settings - Fork 670
/
db.go
135 lines (117 loc) · 3.82 KB
/
db.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// (c) 2019-2020, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package mockdb
import (
"errors"
"github.com/ava-labs/gecko/database"
)
var errNoFunction = errors.New("user didn't specify what value(s) return")
// Database implements database.Database.
// This is a mock database meant to be used in tests.
// You specify the database's return value(s) for a given method call by
// assign value to the corresponding member.
// For example, to specify what should happen when Has is called,
// assign a value to OnHas.
// If no value is assigned to the corresponding member, the method returns an error or nil
// If you
type Database struct {
// Executed when Has is called
OnHas func([]byte) (bool, error)
OnGet func([]byte) ([]byte, error)
OnPut func([]byte, []byte) error
OnDelete func([]byte) error
OnNewBatch func() database.Batch
OnNewIterator func() database.Iterator
OnNewIteratorWithStart func([]byte) database.Iterator
OnNewIteratorWithPrefix func([]byte) database.Iterator
OnNewIteratorWithStartAndPrefix func([]byte, []byte) database.Iterator
OnStat func(string) (string, error)
OnCompact func([]byte, []byte) error
OnClose func() error
}
// Has implements the database.Database interface
func (db *Database) Has(k []byte) (bool, error) {
if db.OnHas == nil {
return false, errNoFunction
}
return db.OnHas(k)
}
// Get implements the database.Database interface
func (db *Database) Get(k []byte) ([]byte, error) {
if db.OnGet == nil {
return nil, errNoFunction
}
return db.OnGet(k)
}
// Put implements the database.Database interface
func (db *Database) Put(k, v []byte) error {
if db.OnPut == nil {
return errNoFunction
}
return db.OnPut(k, v)
}
// Delete implements the database.Database interface
func (db *Database) Delete(k []byte) error {
if db.OnDelete == nil {
return errNoFunction
}
return db.OnDelete(k)
}
// NewBatch implements the database.Database interface
func (db *Database) NewBatch() database.Batch {
if db.OnNewBatch == nil {
return nil
}
return db.OnNewBatch()
}
// NewIterator implements the database.Database interface
func (db *Database) NewIterator() database.Iterator {
if db.OnNewIterator == nil {
return nil
}
return db.OnNewIterator()
}
// NewIteratorWithStart implements the database.Database interface
func (db *Database) NewIteratorWithStart(start []byte) database.Iterator {
if db.OnNewIteratorWithStart == nil {
return nil
}
return db.OnNewIteratorWithStart(start)
}
// NewIteratorWithPrefix implements the database.Database interface
func (db *Database) NewIteratorWithPrefix(prefix []byte) database.Iterator {
if db.OnNewIteratorWithPrefix == nil {
return nil
}
return db.OnNewIteratorWithPrefix(prefix)
}
// NewIteratorWithStartAndPrefix implements the database.Database interface
func (db *Database) NewIteratorWithStartAndPrefix(start, prefix []byte) database.Iterator {
if db.OnNewIteratorWithStartAndPrefix == nil {
return nil
}
return db.OnNewIteratorWithStartAndPrefix(start, prefix)
}
// Stat implements the database.Database interface
func (db *Database) Stat(stat string) (string, error) {
if db.OnStat == nil {
return "", errNoFunction
}
return db.OnStat(stat)
}
// Compact implements the database.Database interface
func (db *Database) Compact(start []byte, limit []byte) error {
if db.OnCompact == nil {
return errNoFunction
}
return db.OnCompact(start, limit)
}
// Close implements the database.Database interface
func (db *Database) Close() error {
if db.OnClose == nil {
return errNoFunction
}
return db.OnClose()
}
// New returns a new mock database
func New() *Database { return &Database{} }