-
Notifications
You must be signed in to change notification settings - Fork 182
/
prefixdb.go
147 lines (125 loc) · 3.18 KB
/
prefixdb.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
136
137
138
139
140
141
142
143
144
145
146
147
package db
import (
"fmt"
)
// PrefixDB wraps a namespace of another database as a logical database.
type PrefixDB struct {
prefix []byte
db DB
}
var _ DB = (*PrefixDB)(nil)
// NewPrefixDB lets you namespace multiple DBs within a single DB.
func NewPrefixDB(db DB, prefix []byte) *PrefixDB {
return &PrefixDB{
prefix: prefix,
db: db,
}
}
// Get implements DB.
func (pdb *PrefixDB) Get(key []byte) ([]byte, error) {
pkey := pdb.prefixed(key)
value, err := pdb.db.Get(pkey)
if err != nil {
return nil, err
}
return value, nil
}
func (pdb *PrefixDB) GetUnsafeValue(key []byte, processor UnsafeValueProcessor) (retv interface{}, err error) {
pkey := pdb.prefixed(key)
return pdb.db.GetUnsafeValue(pkey, processor)
}
// Has implements DB.
func (pdb *PrefixDB) Has(key []byte) (bool, error) {
ok, err := pdb.db.Has(pdb.prefixed(key))
if err != nil {
return ok, err
}
return ok, nil
}
// Set implements DB.
func (pdb *PrefixDB) Set(key []byte, value []byte) error {
pkey := pdb.prefixed(key)
if err := pdb.db.Set(pkey, value); err != nil {
return err
}
return nil
}
// SetSync implements DB.
func (pdb *PrefixDB) SetSync(key []byte, value []byte) error {
return pdb.db.SetSync(pdb.prefixed(key), value)
}
// Delete implements DB.
func (pdb *PrefixDB) Delete(key []byte) error {
return pdb.db.Delete(pdb.prefixed(key))
}
// DeleteSync implements DB.
func (pdb *PrefixDB) DeleteSync(key []byte) error {
return pdb.db.DeleteSync(pdb.prefixed(key))
}
// Iterator implements DB.
func (pdb *PrefixDB) Iterator(start, end []byte) (Iterator, error) {
var pstart, pend []byte
pstart = concat(pdb.prefix, start)
if end == nil {
pend = cpIncr(pdb.prefix)
} else {
pend = concat(pdb.prefix, end)
}
itr, err := pdb.db.Iterator(pstart, pend)
if err != nil {
return nil, err
}
return newPrefixIterator(pdb.prefix, start, end, itr)
}
// ReverseIterator implements DB.
func (pdb *PrefixDB) ReverseIterator(start, end []byte) (Iterator, error) {
var pstart, pend []byte
pstart = concat(pdb.prefix, start)
if end == nil {
pend = cpIncr(pdb.prefix)
} else {
pend = concat(pdb.prefix, end)
}
ritr, err := pdb.db.ReverseIterator(pstart, pend)
if err != nil {
return nil, err
}
return newPrefixIterator(pdb.prefix, start, end, ritr)
}
// NewBatch implements DB.
func (pdb *PrefixDB) NewBatch() Batch {
return newPrefixBatch(pdb.prefix, pdb.db.NewBatch())
}
// Close implements DB.
func (pdb *PrefixDB) Close() error {
return pdb.db.Close()
}
// Print implements DB.
func (pdb *PrefixDB) Print() error {
fmt.Printf("prefix: %X\n", pdb.prefix)
itr, err := pdb.Iterator(nil, nil)
if err != nil {
return err
}
defer itr.Close()
for ; itr.Valid(); itr.Next() {
key := itr.Key()
value := itr.Value()
fmt.Printf("[%X]:\t[%X]\n", key, value)
}
return nil
}
// Stats implements DB.
func (pdb *PrefixDB) Stats() map[string]string {
stats := make(map[string]string)
stats["prefixdb.prefix.string"] = string(pdb.prefix)
stats["prefixdb.prefix.hex"] = fmt.Sprintf("%X", pdb.prefix)
source := pdb.db.Stats()
for key, value := range source {
stats["prefixdb.source."+key] = value
}
return stats
}
func (pdb *PrefixDB) prefixed(key []byte) []byte {
return concat(pdb.prefix, key)
}