From 4bf345225c8dc98004cc79b6d9dd88008c936dd3 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Tue, 20 Jun 2023 13:39:18 +0300 Subject: [PATCH] [#447] pilorama: Use named constant for the key size Signed-off-by: Evgenii Stratonikov --- pkg/local_object_storage/pilorama/batch.go | 7 +++---- pkg/local_object_storage/pilorama/boltdb.go | 15 ++++++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/local_object_storage/pilorama/batch.go b/pkg/local_object_storage/pilorama/batch.go index bccd640f1..c65488b74 100644 --- a/pkg/local_object_storage/pilorama/batch.go +++ b/pkg/local_object_storage/pilorama/batch.go @@ -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 } } diff --git a/pkg/local_object_storage/pilorama/boltdb.go b/pkg/local_object_storage/pilorama/boltdb.go index a19561139..53a52433d 100644 --- a/pkg/local_object_storage/pilorama/boltdb.go +++ b/pkg/local_object_storage/pilorama/boltdb.go @@ -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.