blockindex.go 4.74 KB
Newer Older
manish's avatar
manish committed
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
/*
Copyright IBM Corp. 2016 All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

		 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package fsblkstorage

import (
	"fmt"

	"github.com/golang/protobuf/proto"
	"github.com/hyperledger/fabric/core/ledgernext/util"
	"github.com/hyperledger/fabric/core/ledgernext/util/db"
	"github.com/tecbot/gorocksdb"
)

type blockIndex struct {
	db           *db.DB
	blockIndexCF *gorocksdb.ColumnFamilyHandle
}

func newBlockIndex(db *db.DB) *blockIndex {
	//TODO during init make sure that the index is in sync with block strorage
	return &blockIndex{db, db.GetCFHandle(blockIndexCF)}
}

func (index *blockIndex) indexBlock(blockNum uint64, blockHash []byte, flp *fileLocPointer, blockLen int, skip int, txOffsets []int) error {
	logger.Debugf("Adding blockLoc [%s] to index", flp)
	batch := gorocksdb.NewWriteBatch()
	defer batch.Destroy()
	flpBytes, err := flp.marshal()
	if err != nil {
		return err
	}
	batch.PutCF(index.blockIndexCF, index.constructBlockHashKey(blockHash), flpBytes)
	batch.PutCF(index.blockIndexCF, index.constructBlockNumKey(blockNum), flpBytes)
	for i := 0; i < len(txOffsets)-1; i++ {
		txID := constructTxID(blockNum, i)
		txBytesLength := txOffsets[i+1] - txOffsets[i]
		txFLP := newFileLocationPointer(flp.fileSuffixNum, flp.offset+skip, &locPointer{txOffsets[i], txBytesLength})
		logger.Debugf("Adding txLoc [%s] for tx [%s] to index", txFLP, txID)
		txFLPBytes, marshalErr := txFLP.marshal()
		if marshalErr != nil {
			return marshalErr
		}
		batch.PutCF(index.blockIndexCF, index.constructTxIDKey(txID), txFLPBytes)
	}
manish's avatar
manish committed
59
	if err := index.db.WriteBatch(batch); err != nil {
manish's avatar
manish committed
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
		return err
	}
	return nil
}

func (index *blockIndex) getBlockLocByHash(blockHash []byte) (*fileLocPointer, error) {
	b, err := index.db.Get(index.blockIndexCF, index.constructBlockHashKey(blockHash))
	if err != nil {
		return nil, err
	}
	blkLoc := &fileLocPointer{}
	blkLoc.unmarshal(b)
	return blkLoc, nil
}

func (index *blockIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, error) {
	b, err := index.db.Get(index.blockIndexCF, index.constructBlockNumKey(blockNum))
	if err != nil {
		return nil, err
	}
	blkLoc := &fileLocPointer{}
	blkLoc.unmarshal(b)
	return blkLoc, nil
}

func (index *blockIndex) getTxLoc(txID string) (*fileLocPointer, error) {
	b, err := index.db.Get(index.blockIndexCF, index.constructTxIDKey(txID))
	if err != nil {
		return nil, err
	}
	txFLP := &fileLocPointer{}
	txFLP.unmarshal(b)
	return txFLP, nil
}

func (index *blockIndex) constructBlockNumKey(blockNum uint64) []byte {
	blkNumBytes := util.EncodeOrderPreservingVarUint64(blockNum)
	return append([]byte{'n'}, blkNumBytes...)
}

func (index *blockIndex) constructBlockHashKey(blockHash []byte) []byte {
	return append([]byte{'b'}, blockHash...)
}

func (index *blockIndex) constructTxIDKey(txID string) []byte {
	return append([]byte{'t'}, []byte(txID)...)
}

func constructTxID(blockNum uint64, txNum int) string {
	return fmt.Sprintf("%d:%d", blockNum, txNum)
}

type locPointer struct {
	offset      int
	bytesLength int
}

func (lp *locPointer) String() string {
	return fmt.Sprintf("offset=%d, bytesLength=%d",
		lp.offset, lp.bytesLength)
}

manish's avatar
manish committed
122
// fileLocPointer
manish's avatar
manish committed
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
type fileLocPointer struct {
	fileSuffixNum int
	locPointer
}

func newFileLocationPointer(fileSuffixNum int, begginingOffset int, relativeLP *locPointer) *fileLocPointer {
	flp := &fileLocPointer{fileSuffixNum: fileSuffixNum}
	flp.offset = begginingOffset + relativeLP.offset
	flp.bytesLength = relativeLP.bytesLength
	return flp
}

func (flp *fileLocPointer) marshal() ([]byte, error) {
	buffer := proto.NewBuffer([]byte{})
	e := buffer.EncodeVarint(uint64(flp.fileSuffixNum))
	if e != nil {
		return nil, e
	}
	e = buffer.EncodeVarint(uint64(flp.offset))
	if e != nil {
		return nil, e
	}
	e = buffer.EncodeVarint(uint64(flp.bytesLength))
	if e != nil {
		return nil, e
	}
	return buffer.Bytes(), nil
}

func (flp *fileLocPointer) unmarshal(b []byte) error {
	buffer := proto.NewBuffer(b)
	i, e := buffer.DecodeVarint()
	if e != nil {
		return e
	}
	flp.fileSuffixNum = int(i)

	i, e = buffer.DecodeVarint()
	if e != nil {
		return e
	}
	flp.offset = int(i)
	i, e = buffer.DecodeVarint()
	if e != nil {
		return e
	}
	flp.bytesLength = int(i)
	return nil
}

func (flp *fileLocPointer) String() string {
	return fmt.Sprintf("fileSuffixNum=%d, %s", flp.fileSuffixNum, flp.locPointer.String())
}