-
Notifications
You must be signed in to change notification settings - Fork 13
/
handle.go
189 lines (163 loc) · 4.51 KB
/
handle.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// SPDX-License-Identifier: ISC
// Copyright (c) 2014-2019 Bitmark Inc.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package storage
import (
"encoding/binary"
"github.com/syndtr/goleveldb/leveldb"
ldb_util "github.com/syndtr/goleveldb/leveldb/util"
"github.com/bitmark-inc/logger"
)
type Handle interface {
Begin()
Commit() error
Empty() bool
Get([]byte) []byte
GetN([]byte) (uint64, bool)
GetNB([]byte) (uint64, []byte)
Has([]byte) bool
put([]byte, []byte, []byte)
putN([]byte, uint64)
remove([]byte)
}
// PoolHandle - the structure of a pool handle
type PoolHandle struct {
Handle
prefix byte
limit []byte
dataAccess DataAccess
}
// Element - a binary data item
type Element struct {
Key []byte
Value []byte
}
// prepend the prefix onto the key
func (p *PoolHandle) prefixKey(key []byte) []byte {
prefixedKey := make([]byte, 1, len(key)+1)
prefixedKey[0] = p.prefix
return append(prefixedKey, key...)
}
// Put - store a key/value bytes pair to the database
func (p *PoolHandle) put(key []byte, value []byte, dummy []byte) {
poolData.RLock()
defer poolData.RUnlock()
if nil == p.dataAccess {
logger.Panic("pool.Put nil database")
return
}
p.dataAccess.Put(p.prefixKey(key), value)
}
// PutN - store a uint8 as an 8 byte sequence
func (p *PoolHandle) putN(key []byte, value uint64) {
buffer := make([]byte, 8)
binary.BigEndian.PutUint64(buffer, value)
p.put(key, buffer, []byte{})
}
func (p *PoolHandle) remove(key []byte) {
poolData.RLock()
defer poolData.RUnlock()
p.dataAccess.Delete(p.prefixKey(key))
}
// Get - read a value for a given key
//
// this returns the actual element - copy the result if it must be preserved
func (p *PoolHandle) Get(key []byte) []byte {
poolData.RLock()
defer poolData.RUnlock()
if nil == p.dataAccess {
return nil
}
value, err := p.dataAccess.Get(p.prefixKey(key))
if leveldb.ErrNotFound == err {
return nil
}
logger.PanicIfError("pool.GetB", err)
return value
}
// GetN - read a record and decode first 8 bytes as big endian uint64
//
// second parameter is false if record was not found
// panics if not 8 (or more) bytes in the record
func (p *PoolHandle) GetN(key []byte) (uint64, bool) {
buffer := p.Get(key)
if nil == buffer {
return 0, false
}
if len(buffer) < 8 {
logger.Panicf("pool.GetN truncated record for: %x: %s", key, buffer)
}
n := binary.BigEndian.Uint64(buffer[:8])
return n, true
}
// GetNB - read a record and decode first 8 bytes as big endian uint64
// and return the rest of the record as byte slice
//
// second parameter is nil if record was not found
// panics if not 9 (or more) bytes in the record
// this returns the actual element in the second parameter - copy the result if it must be preserved
func (p *PoolHandle) GetNB(key []byte) (uint64, []byte) {
buffer := p.Get(key)
if nil == buffer {
return 0, nil
}
if len(buffer) < 9 { // must have at least one byte after the N value
logger.Panicf("pool.GetNB truncated record for: %x: %s", key, buffer)
}
n := binary.BigEndian.Uint64(buffer[:8])
return n, buffer[8:]
}
// Has - Check if a key exists
func (p *PoolHandle) Has(key []byte) bool {
poolData.RLock()
defer poolData.RUnlock()
if nil == p.dataAccess {
return false
}
value, err := p.dataAccess.Has(p.prefixKey(key))
logger.PanicIfError("pool.Has", err)
return value
}
// LastElement - get the last element in a pool
func (p *PoolHandle) LastElement() (Element, bool) {
maxRange := ldb_util.Range{
Start: []byte{p.prefix}, // Start of key range, included in the range
Limit: p.limit, // Limit of key range, excluded from the range
}
poolData.RLock()
defer poolData.RUnlock()
if nil == p.dataAccess {
return Element{}, false
}
iter := p.dataAccess.Iterator(&maxRange)
found := false
result := Element{}
if iter.Last() {
// contents of the returned slice must not be modified, and are
// only valid until the next call to Next
key := iter.Key()
value := iter.Value()
dataKey := make([]byte, len(key)-1) // strip the prefix
copy(dataKey, key[1:]) // ...
dataValue := make([]byte, len(value))
copy(dataValue, value)
result.Key = dataKey
result.Value = dataValue
found = true
}
iter.Release()
err := iter.Error()
logger.PanicIfError("pool.LastElement", err)
return result, found
}
func (p *PoolHandle) Begin() {
p.dataAccess.Begin()
}
func (p *PoolHandle) Commit() error {
return p.dataAccess.Commit()
}
// Empty - check if struct is empty
func (p *PoolHandle) Empty() bool {
return nil == p || 0 == p.prefix
}