[#447] pilorama: Use named constant for the key size

Signed-off-by: Evgenii Stratonikov <e.stratonikov@yadro.com>
This commit is contained in:
Evgenii Stratonikov 2023-06-20 13:39:18 +03:00 committed by Evgenii Stratonikov
parent b4ce0b0412
commit 4bf345225c
2 changed files with 13 additions and 9 deletions

View file

@ -64,7 +64,7 @@ func (b *batch) run() {
// See TestForest_ApplySameOperation for details.
// 3. Parent of each operation is already in tree.
var parents map[uint64]struct{}
var cKey [17]byte
var cKey [maxKeySize]byte
var slow bool
for i := range b.operations {
_, _, _, inTree := b.forest.getState(bTree, stateKey(cKey[:], b.operations[i].Child))
@ -75,7 +75,7 @@ func (b *batch) run() {
key := childrenKey(cKey[:], b.operations[i].Child, 0)
k, _ := bTree.Cursor().Seek(key)
if len(k) == 17 && binary.LittleEndian.Uint64(k[1:]) == b.operations[i].Child {
if len(k) == childrenKeySize && binary.LittleEndian.Uint64(k[1:]) == b.operations[i].Child {
slow = true
break
}
@ -104,9 +104,8 @@ func (b *batch) run() {
return b.forest.applyOperation(bLog, bTree, b.operations, &lm)
}
var key [17]byte
for i := range b.operations {
if err := b.forest.do(bLog, bTree, key[:], b.operations[i]); err != nil {
if err := b.forest.do(bLog, bTree, cKey[:], b.operations[i]); err != nil {
return err
}
}

View file

@ -37,6 +37,11 @@ type boltForest struct {
cfg
}
const (
childrenKeySize = 17
maxKeySize = childrenKeySize
)
var (
dataBucket = []byte{0}
logBucket = []byte{1}
@ -199,7 +204,7 @@ func (t *boltForest) TreeMove(ctx context.Context, d CIDDescriptor, treeID strin
if lm.Child == RootID {
lm.Child = t.findSpareID(bTree)
}
return t.do(bLog, bTree, make([]byte, 17), &lm)
return t.do(bLog, bTree, make([]byte, maxKeySize), &lm)
}))
}
@ -395,7 +400,7 @@ func (t *boltForest) addByPathInternal(d CIDDescriptor, attr string, treeID stri
}
var lm []Move
var key [17]byte
var key [maxKeySize]byte
fullID := bucketName(d.CID, treeID)
err := t.db.Batch(func(tx *bbolt.Tx) error {
@ -612,7 +617,7 @@ func (t *boltForest) getTreeBuckets(tx *bbolt.Tx, treeRoot []byte) (*bbolt.Bucke
// applyOperations applies log operations. Assumes lm are sorted by timestamp.
func (t *boltForest) applyOperation(logBucket, treeBucket *bbolt.Bucket, ms []*Move, lm *Move) error {
var tmp Move
var cKey [17]byte
var cKey [maxKeySize]byte
c := logBucket.Cursor()
@ -961,7 +966,7 @@ func (t *boltForest) TreeGetChildren(ctx context.Context, cid cidSDK.ID, treeID
b := treeRoot.Bucket(dataBucket)
c := b.Cursor()
for k, _ := c.Seek(key); len(k) == 17 && binary.LittleEndian.Uint64(k[1:]) == nodeID; k, _ = c.Next() {
for k, _ := c.Seek(key); len(k) == childrenKeySize && binary.LittleEndian.Uint64(k[1:]) == nodeID; k, _ = c.Next() {
children = append(children, binary.LittleEndian.Uint64(k[9:]))
}
return nil
@ -1216,7 +1221,7 @@ func childrenKey(key []byte, child, parent Node) []byte {
key[0] = 'c'
binary.LittleEndian.PutUint64(key[1:], parent)
binary.LittleEndian.PutUint64(key[9:], child)
return key[:17]
return key[:childrenKeySize]
}
// 'i' + attribute name (string) + attribute value (string) + parent (id) + node (id) -> 0/1.