@@ -31,6 +31,7 @@ const (
31
31
blockHashIdxKeyPrefix = 'h'
32
32
txIDIdxKeyPrefix = 't'
33
33
blockNumTranNumIdxKeyPrefix = 'a'
34
+ blockTxIDIdxKeyPrefix = 'b'
34
35
indexCheckpointKeyStr = "indexCheckpointKey"
35
36
)
36
37
@@ -43,6 +44,7 @@ type index interface {
43
44
getBlockLocByBlockNum (blockNum uint64 ) (* fileLocPointer , error )
44
45
getTxLoc (txID string ) (* fileLocPointer , error )
45
46
getTXLocByBlockNumTranNum (blockNum uint64 , tranNum uint64 ) (* fileLocPointer , error )
47
+ getBlockLocByTxID (txID string ) (* fileLocPointer , error )
46
48
}
47
49
48
50
type blockIdxInfo struct {
@@ -127,6 +129,13 @@ func (index *blockIndex) indexBlock(blockIdxInfo *blockIdxInfo) error {
127
129
}
128
130
}
129
131
132
+ // Index5 - Store BlockNumber will be used to find block by transaction id
133
+ if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockTxID ]; ok {
134
+ for _ , txoffset := range txOffsets {
135
+ batch .Put (constructBlockTxIDKey (txoffset .txID ), flpBytes )
136
+ }
137
+ }
138
+
130
139
batch .Put (indexCheckpointKey , encodeBlockNum (blockIdxInfo .blockNum ))
131
140
if err := index .db .WriteBatch (batch , false ); err != nil {
132
141
return err
@@ -182,6 +191,22 @@ func (index *blockIndex) getTxLoc(txID string) (*fileLocPointer, error) {
182
191
return txFLP , nil
183
192
}
184
193
194
+ func (index * blockIndex ) getBlockLocByTxID (txID string ) (* fileLocPointer , error ) {
195
+ if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockTxID ]; ! ok {
196
+ return nil , blkstorage .ErrAttrNotIndexed
197
+ }
198
+ b , err := index .db .Get (constructBlockTxIDKey (txID ))
199
+ if err != nil {
200
+ return nil , err
201
+ }
202
+ if b == nil {
203
+ return nil , blkstorage .ErrNotFoundInIndex
204
+ }
205
+ txFLP := & fileLocPointer {}
206
+ txFLP .unmarshal (b )
207
+ return txFLP , nil
208
+ }
209
+
185
210
func (index * blockIndex ) getTXLocByBlockNumTranNum (blockNum uint64 , tranNum uint64 ) (* fileLocPointer , error ) {
186
211
if _ , ok := index .indexItemsMap [blkstorage .IndexableAttrBlockNumTranNum ]; ! ok {
187
212
return nil , blkstorage .ErrAttrNotIndexed
@@ -211,6 +236,10 @@ func constructTxIDKey(txID string) []byte {
211
236
return append ([]byte {txIDIdxKeyPrefix }, []byte (txID )... )
212
237
}
213
238
239
+ func constructBlockTxIDKey (txID string ) []byte {
240
+ return append ([]byte {blockTxIDIdxKeyPrefix }, []byte (txID )... )
241
+ }
242
+
214
243
func constructBlockNumTranNumKey (blockNum uint64 , txNum uint64 ) []byte {
215
244
blkNumBytes := util .EncodeOrderPreservingVarUint64 (blockNum )
216
245
tranNumBytes := util .EncodeOrderPreservingVarUint64 (txNum )
0 commit comments