[#447] pilorama: Use named constant for the key size
All checks were successful
ci/woodpecker/pr/pre-commit Pipeline was successful

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

View file

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

View file

@ -37,6 +37,11 @@ type boltForest struct {
cfg cfg
} }
const (
childrenKeySize = 17
maxKeySize = childrenKeySize
)
var ( var (
dataBucket = []byte{0} dataBucket = []byte{0}
logBucket = []byte{1} logBucket = []byte{1}
@ -185,7 +190,7 @@ func (t *boltForest) TreeMove(ctx context.Context, d CIDDescriptor, treeID strin
if lm.Child == RootID { if lm.Child == RootID {
lm.Child = t.findSpareID(bTree) lm.Child = t.findSpareID(bTree)
} }
return t.do(bLog, bTree, make([]byte, 17), &lm) return t.do(bLog, bTree, make([]byte, maxKeySize), &lm)
})) }))
} }
@ -340,7 +345,7 @@ func (t *boltForest) TreeAddByPath(ctx context.Context, d CIDDescriptor, treeID
} }
var lm []Move var lm []Move
var key [17]byte var key [maxKeySize]byte
fullID := bucketName(d.CID, treeID) fullID := bucketName(d.CID, treeID)
err := t.db.Batch(func(tx *bbolt.Tx) error { err := t.db.Batch(func(tx *bbolt.Tx) error {
@ -542,7 +547,7 @@ func (t *boltForest) getTreeBuckets(tx *bbolt.Tx, treeRoot []byte) (*bbolt.Bucke
// applyOperations applies log operations. Assumes lm are sorted by timestamp. // applyOperations applies log operations. Assumes lm are sorted by timestamp.
func (t *boltForest) applyOperation(logBucket, treeBucket *bbolt.Bucket, ms []*Move, lm *Move) error { func (t *boltForest) applyOperation(logBucket, treeBucket *bbolt.Bucket, ms []*Move, lm *Move) error {
var tmp Move var tmp Move
var cKey [17]byte var cKey [maxKeySize]byte
c := logBucket.Cursor() c := logBucket.Cursor()
@ -864,7 +869,7 @@ func (t *boltForest) TreeGetChildren(ctx context.Context, cid cidSDK.ID, treeID
b := treeRoot.Bucket(dataBucket) b := treeRoot.Bucket(dataBucket)
c := b.Cursor() 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:])) children = append(children, binary.LittleEndian.Uint64(k[9:]))
} }
return nil return nil
@ -1093,7 +1098,7 @@ func childrenKey(key []byte, child, parent Node) []byte {
key[0] = 'c' key[0] = 'c'
binary.LittleEndian.PutUint64(key[1:], parent) binary.LittleEndian.PutUint64(key[1:], parent)
binary.LittleEndian.PutUint64(key[9:], child) 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. // 'i' + attribute name (string) + attribute value (string) + parent (id) + node (id) -> 0/1.