forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_helper.go
121 lines (100 loc) · 2.92 KB
/
db_helper.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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package confighistory
import (
"bytes"
"encoding/binary"
"fmt"
"math"
"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
)
const (
keyPrefix = "s"
separatorByte = byte(0)
)
type compositeKey struct {
ns, key string
blockNum uint64
}
type compositeKV struct {
*compositeKey
value []byte
}
type dbProvider struct {
*leveldbhelper.Provider
}
type db struct {
*leveldbhelper.DBHandle
}
type batch struct {
*leveldbhelper.UpdateBatch
}
func newDBProvider(dbPath string) *dbProvider {
logger.Debugf("Opening db for config history: db path = %s", dbPath)
return &dbProvider{leveldbhelper.NewProvider(&leveldbhelper.Conf{DBPath: dbPath})}
}
func newBatch() *batch {
return &batch{leveldbhelper.NewUpdateBatch()}
}
func (p *dbProvider) getDB(id string) *db {
return &db{p.GetDBHandle(id)}
}
func (b *batch) add(ns, key string, blockNum uint64, value []byte) {
logger.Debugf("add() - {%s, %s, %d}", ns, key, blockNum)
k, v := encodeCompositeKey(ns, key, blockNum), value
b.Put(k, v)
}
func (d *db) writeBatch(batch *batch, sync bool) error {
return d.WriteBatch(batch.UpdateBatch, sync)
}
func (d *db) mostRecentEntryBelow(blockNum uint64, ns, key string) (*compositeKV, error) {
logger.Debugf("mostRecentEntryBelow() - {%s, %s, %d}", ns, key, blockNum)
if blockNum == 0 {
return nil, fmt.Errorf("blockNum should be greater than 0")
}
startKey := encodeCompositeKey(ns, key, blockNum-1)
itr := d.GetIterator(startKey, nil)
defer itr.Release()
if !itr.Next() {
logger.Debugf("Key no entry found. Returning nil")
return nil, nil
}
k, v := decodeCompositeKey(itr.Key()), itr.Value()
return &compositeKV{k, v}, nil
}
func (d *db) entryAt(blockNum uint64, ns, key string) (*compositeKV, error) {
logger.Debugf("entryAt() - {%s, %s, %d}", ns, key, blockNum)
keyBytes := encodeCompositeKey(ns, key, blockNum)
valBytes, err := d.Get(keyBytes)
if err != nil {
return nil, err
}
if valBytes == nil {
return nil, nil
}
k, v := decodeCompositeKey(keyBytes), valBytes
return &compositeKV{k, v}, nil
}
func encodeCompositeKey(ns, key string, blockNum uint64) []byte {
b := []byte(keyPrefix + ns)
b = append(b, separatorByte)
b = append(b, []byte(key)...)
return append(b, encodeBlockNum(blockNum)...)
}
func decodeCompositeKey(b []byte) *compositeKey {
blockNumStartIndex := len(b) - 8
nsKeyBytes, blockNumBytes := b[1:blockNumStartIndex], b[blockNumStartIndex:]
separatorIndex := bytes.Index(nsKeyBytes, []byte{separatorByte})
ns, key := nsKeyBytes[0:separatorIndex], nsKeyBytes[separatorIndex+1:]
return &compositeKey{string(ns), string(key), decodeBlockNum(blockNumBytes)}
}
func encodeBlockNum(blockNum uint64) []byte {
b := make([]byte, 8)
binary.BigEndian.PutUint64(b, math.MaxUint64-blockNum)
return b
}
func decodeBlockNum(blockNumBytes []byte) uint64 {
return math.MaxUint64 - binary.BigEndian.Uint64(blockNumBytes)
}