@@ -27,10 +27,11 @@ import (
27
27
)
28
28
29
29
const (
30
- blockNumIdxKeyPrefix = 'n'
31
- blockHashIdxKeyPrefix = 'h'
32
- txIDIdxKeyPrefix = 't'
33
- indexCheckpointKeyStr = "indexCheckpointKey"
30
+ blockNumIdxKeyPrefix = 'n'
31
+ blockHashIdxKeyPrefix = 'h'
32
+ txIDIdxKeyPrefix = 't'
33
+ blockNumTranNumIdxKeyPrefix = 'a'
34
+ indexCheckpointKeyStr = "indexCheckpointKey"
34
35
)
35
36
36
37
var indexCheckpointKey = []byte (indexCheckpointKeyStr )
@@ -41,13 +42,14 @@ type index interface {
41
42
getBlockLocByHash (blockHash []byte ) (* fileLocPointer , error )
42
43
getBlockLocByBlockNum (blockNum uint64 ) (* fileLocPointer , error )
43
44
getTxLoc (txID string ) (* fileLocPointer , error )
45
+ getTXLocForBlockNumTranNum (blockNum uint64 , tranNum uint64 ) (* fileLocPointer , error )
44
46
}
45
47
46
48
type blockIdxInfo struct {
47
49
blockNum uint64
48
50
blockHash []byte
49
51
flp * fileLocPointer
50
- txOffsets map [ string ] * locPointer
52
+ txOffsets [] * txindexInfo
51
53
}
52
54
53
55
type blockIndex struct {
@@ -89,25 +91,42 @@ func (index *blockIndex) indexBlock(blockIdxInfo *blockIdxInfo) error {
89
91
return err
90
92
}
91
93
94
+ //Index1
92
95
if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockHash ]; ok {
93
96
batch .Put (constructBlockHashKey (blockIdxInfo .blockHash ), flpBytes )
94
97
}
95
98
99
+ //Index2
96
100
if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockNum ]; ok {
97
101
batch .Put (constructBlockNumKey (blockIdxInfo .blockNum ), flpBytes )
98
102
}
99
103
104
+ //Index3 Used to find a transactin by it's transaction id
100
105
if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrTxID ]; ok {
101
- for txid , txoffset := range txOffsets {
102
- txFlp := newFileLocationPointer (flp .fileSuffixNum , flp .offset , txoffset )
103
- logger .Debugf ("Adding txLoc [%s] for tx [%s] to index" , txFlp , txid )
106
+ for _ , txoffset := range txOffsets {
107
+ txFlp := newFileLocationPointer (flp .fileSuffixNum , flp .offset , txoffset . loc )
108
+ logger .Debugf ("Adding txLoc [%s] for tx ID: [%s] to index" , txFlp , txoffset . txID )
104
109
txFlpBytes , marshalErr := txFlp .marshal ()
105
110
if marshalErr != nil {
106
111
return marshalErr
107
112
}
108
- batch .Put (constructTxIDKey (txid ), txFlpBytes )
113
+ batch .Put (constructTxIDKey (txoffset . txID ), txFlpBytes )
109
114
}
110
115
}
116
+
117
+ //Index4 - Store BlockNumTranNum will be used to query history data
118
+ if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockNumTranNum ]; ok {
119
+ for txIterator , txoffset := range txOffsets {
120
+ txFlp := newFileLocationPointer (flp .fileSuffixNum , flp .offset , txoffset .loc )
121
+ logger .Debugf ("Adding txLoc [%s] for tx number:[%d] ID: [%s] to blockNumTranNum index" , txFlp , txIterator + 1 , txoffset .txID )
122
+ txFlpBytes , marshalErr := txFlp .marshal ()
123
+ if marshalErr != nil {
124
+ return marshalErr
125
+ }
126
+ batch .Put (constructBlockNumTranNumKey (blockIdxInfo .blockNum , uint64 (txIterator + 1 )), txFlpBytes )
127
+ }
128
+ }
129
+
111
130
batch .Put (indexCheckpointKey , encodeBlockNum (blockIdxInfo .blockNum ))
112
131
if err := index .db .WriteBatch (batch , false ); err != nil {
113
132
return err
@@ -163,6 +182,22 @@ func (index *blockIndex) getTxLoc(txID string) (*fileLocPointer, error) {
163
182
return txFLP , nil
164
183
}
165
184
185
+ func (index * blockIndex ) getTXLocForBlockNumTranNum (blockNum uint64 , tranNum uint64 ) (* fileLocPointer , error ) {
186
+ if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockNumTranNum ]; ! ok {
187
+ return nil , blkstorage .ErrAttrNotIndexed
188
+ }
189
+ b , err := index .db .Get (constructBlockNumTranNumKey (blockNum , tranNum ))
190
+ if err != nil {
191
+ return nil , err
192
+ }
193
+ if b == nil {
194
+ return nil , blkstorage .ErrNotFoundInIndex
195
+ }
196
+ txFLP := & fileLocPointer {}
197
+ txFLP .unmarshal (b )
198
+ return txFLP , nil
199
+ }
200
+
166
201
func constructBlockNumKey (blockNum uint64 ) []byte {
167
202
blkNumBytes := util .EncodeOrderPreservingVarUint64 (blockNum )
168
203
return append ([]byte {blockNumIdxKeyPrefix }, blkNumBytes ... )
@@ -176,6 +211,13 @@ func constructTxIDKey(txID string) []byte {
176
211
return append ([]byte {txIDIdxKeyPrefix }, []byte (txID )... )
177
212
}
178
213
214
+ func constructBlockNumTranNumKey (blockNum uint64 , txNum uint64 ) []byte {
215
+ blkNumBytes := util .EncodeOrderPreservingVarUint64 (blockNum )
216
+ tranNumBytes := util .EncodeOrderPreservingVarUint64 (txNum )
217
+ key := append (blkNumBytes , tranNumBytes ... )
218
+ return append ([]byte {blockNumTranNumIdxKeyPrefix }, key ... )
219
+ }
220
+
179
221
func constructTxID (blockNum uint64 , txNum int ) string {
180
222
return fmt .Sprintf ("%d:%d" , blockNum , txNum )
181
223
}
0 commit comments