-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
store_helper.go
181 lines (157 loc) · 7.71 KB
/
store_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
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
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package transientstore
import (
"bytes"
"path/filepath"
"github.com/hyperledger/fabric/common/ledger/util"
"github.com/hyperledger/fabric/core/config"
)
var (
prwsetPrefix = []byte("P")[0] // key prefix for storing private write set in transient store.
purgeIndexByHeightPrefix = []byte("H")[0] // key prefix for storing index on private write set using received at block height.
purgeIndexByTxidPrefix = []byte("T")[0] // key prefix for storing index on private write set using txid
compositeKeySep = byte(0x00)
)
// createCompositeKeyForPvtRWSet creates a key for storing private write set
// in the transient store. The structure of the key is <prwsetPrefix>~txid~uuid~blockHeight.
func createCompositeKeyForPvtRWSet(txid string, uuid string, blockHeight uint64) []byte {
var compositeKey []byte
compositeKey = append(compositeKey, prwsetPrefix)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, createCompositeKeyWithoutPrefixForTxid(txid, uuid, blockHeight)...)
return compositeKey
}
// createCompositeKeyForPurgeIndexByTxid creates a key to index private write set based on
// txid such that purge based on txid can be achieved. The structure
// of the key is <purgeIndexByTxidPrefix>~txid~uuid~blockHeight.
func createCompositeKeyForPurgeIndexByTxid(txid string, uuid string, blockHeight uint64) []byte {
var compositeKey []byte
compositeKey = append(compositeKey, purgeIndexByTxidPrefix)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, createCompositeKeyWithoutPrefixForTxid(txid, uuid, blockHeight)...)
return compositeKey
}
// createCompositeKeyWithoutPrefixForTxid creates a composite key of structure txid~uuid~blockHeight.
func createCompositeKeyWithoutPrefixForTxid(txid string, uuid string, blockHeight uint64) []byte {
var compositeKey []byte
compositeKey = append(compositeKey, []byte(txid)...)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, []byte(uuid)...)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, util.EncodeOrderPreservingVarUint64(blockHeight)...)
return compositeKey
}
// createCompositeKeyForPurgeIndexByHeight creates a key to index private write set based on
// received at block height such that purge based on block height can be achieved. The structure
// of the key is <purgeIndexByHeightPrefix>~blockHeight~txid~uuid.
func createCompositeKeyForPurgeIndexByHeight(blockHeight uint64, txid string, uuid string) []byte {
var compositeKey []byte
compositeKey = append(compositeKey, purgeIndexByHeightPrefix)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, util.EncodeOrderPreservingVarUint64(blockHeight)...)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, []byte(txid)...)
compositeKey = append(compositeKey, compositeKeySep)
compositeKey = append(compositeKey, []byte(uuid)...)
return compositeKey
}
// splitCompositeKeyOfPvtRWSet splits the compositeKey (<prwsetPrefix>~txid~uuid~blockHeight)
// into uuid and blockHeight.
func splitCompositeKeyOfPvtRWSet(compositeKey []byte) (uuid string, blockHeight uint64) {
return splitCompositeKeyWithoutPrefixForTxid(compositeKey[2:])
}
// splitCompositeKeyOfPurgeIndexByTxid splits the compositeKey (<purgeIndexByTxidPrefix>~txid~uuid~blockHeight)
// into uuid and blockHeight.
func splitCompositeKeyOfPurgeIndexByTxid(compositeKey []byte) (uuid string, blockHeight uint64) {
return splitCompositeKeyWithoutPrefixForTxid(compositeKey[2:])
}
// splitCompositeKeyOfPurgeIndexByHeight splits the compositeKey (<purgeIndexByHeightPrefix>~blockHeight~txid~uuid)
// into txid, uuid and blockHeight.
func splitCompositeKeyOfPurgeIndexByHeight(compositeKey []byte) (txid string, uuid string, blockHeight uint64) {
var n int
blockHeight, n = util.DecodeOrderPreservingVarUint64(compositeKey[2:])
splits := bytes.Split(compositeKey[n+3:], []byte{compositeKeySep})
txid = string(splits[0])
uuid = string(splits[1])
return
}
// splitCompositeKeyWithoutPrefixForTxid splits the composite key txid~uuid~blockHeight into
// uuid and blockHeight
func splitCompositeKeyWithoutPrefixForTxid(compositeKey []byte) (uuid string, blockHeight uint64) {
// skip txid as all functions which requires split of composite key already has it
firstSepIndex := bytes.IndexByte(compositeKey, compositeKeySep)
secondSepIndex := firstSepIndex + bytes.IndexByte(compositeKey[firstSepIndex+1:], compositeKeySep) + 1
uuid = string(compositeKey[firstSepIndex+1 : secondSepIndex])
blockHeight, _ = util.DecodeOrderPreservingVarUint64(compositeKey[secondSepIndex+1:])
return
}
// createTxidRangeStartKey returns a startKey to do a range query on transient store using txid
func createTxidRangeStartKey(txid string) []byte {
var startKey []byte
startKey = append(startKey, prwsetPrefix)
startKey = append(startKey, compositeKeySep)
startKey = append(startKey, []byte(txid)...)
startKey = append(startKey, compositeKeySep)
return startKey
}
// createTxidRangeEndKey returns a endKey to do a range query on transient store using txid
func createTxidRangeEndKey(txid string) []byte {
var endKey []byte
endKey = append(endKey, prwsetPrefix)
endKey = append(endKey, compositeKeySep)
endKey = append(endKey, []byte(txid)...)
// As txid is a fixed length string (i.e., 128 bits long UUID), 0xff can be used as a stopper.
// Otherwise a super-string of a given txid would also fall under the end key of range query.
endKey = append(endKey, byte(0xff))
return endKey
}
// createPurgeIndexByHeightRangeStartKey returns a startKey to do a range query on index stored in transient store
// using blockHeight
func createPurgeIndexByHeightRangeStartKey(blockHeight uint64) []byte {
var startKey []byte
startKey = append(startKey, purgeIndexByHeightPrefix)
startKey = append(startKey, compositeKeySep)
startKey = append(startKey, util.EncodeOrderPreservingVarUint64(blockHeight)...)
startKey = append(startKey, compositeKeySep)
return startKey
}
// createPurgeIndexByHeightRangeEndKey returns a endKey to do a range query on index stored in transient store
// using blockHeight
func createPurgeIndexByHeightRangeEndKey(blockHeight uint64) []byte {
var endKey []byte
endKey = append(endKey, purgeIndexByHeightPrefix)
endKey = append(endKey, compositeKeySep)
endKey = append(endKey, util.EncodeOrderPreservingVarUint64(blockHeight)...)
endKey = append(endKey, byte(0xff))
return endKey
}
// createPurgeIndexByTxidRangeStartKey returns a startKey to do a range query on index stored in transient store
// using txid
func createPurgeIndexByTxidRangeStartKey(txid string) []byte {
var startKey []byte
startKey = append(startKey, purgeIndexByTxidPrefix)
startKey = append(startKey, compositeKeySep)
startKey = append(startKey, []byte(txid)...)
startKey = append(startKey, compositeKeySep)
return startKey
}
// createPurgeIndexByTxidRangeEndKey returns a endKey to do a range query on index stored in transient store
// using txid
func createPurgeIndexByTxidRangeEndKey(txid string) []byte {
var endKey []byte
endKey = append(endKey, purgeIndexByTxidPrefix)
endKey = append(endKey, compositeKeySep)
endKey = append(endKey, []byte(txid)...)
// As txid is a fixed length string (i.e., 128 bits long UUID), 0xff can be used as a stopper.
// Otherwise a super-string of a given txid would also fall under the end key of range query.
endKey = append(endKey, byte(0xff))
return endKey
}
// GetTransientStorePath returns the filesystem path for temporarily storing the private rwset
func GetTransientStorePath() string {
sysPath := config.GetPath("peer.fileSystemPath")
return filepath.Join(sysPath, "transientStore")
}