Skip to content

Commit

Permalink
fix a boundary-guard bug
Browse files Browse the repository at this point in the history
  • Loading branch information
wangkui0508 committed Aug 10, 2023
1 parent a41bfcf commit 3b6e964
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
17 changes: 15 additions & 2 deletions datatree/load_recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ func compareNodes(t *testing.T, nodesA, nodesB map[NodePos]*[32]byte) {
}

func compareTwigs(t *testing.T, twigMapA, twigMapB map[int64]*Twig) {
//for k := range twigMapA {
// fmt.Printf("A %d\n", k)
//}
//for k := range twigMapB {
// fmt.Printf("B %d\n", k)
//}
assert.Equal(t, len(twigMapA), len(twigMapB))
for id, twigA := range twigMapA {
twigB := twigMapB[id]
Expand All @@ -29,12 +35,18 @@ func compareTwigs(t *testing.T, twigMapA, twigMapB map[int64]*Twig) {
}
}

func TestLoadTree(t *testing.T) {
func TestLoadRecover(t *testing.T) {
testLoadRecover(t, TwigMask, 6)
testLoadRecover(t, LeafCountInTwig*2-10, 10)
testLoadRecover(t, LeafCountInTwig*4-3, 3)
}

func testLoadRecover(t *testing.T, countBefore, countAfter int) {
dirName := "./DataTree"
_ = os.RemoveAll(dirName)
_ = os.Mkdir(dirName, 0700)
deactSNList := []int64{101, 999, 1002}
tree0, _, _ := buildTestTree(dirName, deactSNList, TwigMask, 6)
tree0, _, _ := buildTestTree(dirName, deactSNList, countBefore, countAfter)
tree0.EndBlock()
tree0.WaitForFlushing()
nodes0 := tree0.nodes
Expand All @@ -59,3 +71,4 @@ func TestLoadTree(t *testing.T) {

os.RemoveAll(dirName)
}

1 change: 1 addition & 0 deletions datatree/recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (tree *Tree) RecoverEntry(pos int64, entry *Entry, deactivedSNList []int64,
tree.youngestTwigID++
tree.activeTwigs[tree.youngestTwigID] = CopyNullTwig()
tree.mtree4YoungestTwig = NullMT4Twig
tree.touchedPosOf512b[(entry.SerialNum + 1)/512] = struct{}{}
}
}

Expand Down
11 changes: 11 additions & 0 deletions datatree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ func (tree *Tree) Close() {
tree.deactivedSNList = nil
}

func (tree *Tree) printNodes() {
nList := make([]NodePos, 0, len(tree.nodes))
for pos := range tree.nodes {
nList = append(nList, pos)
}
sort.Slice(nList, func(i, j int) bool { return nList[i] < nList[j] })
for i, pos := range nList {
fmt.Printf("NODE#%d %d-%d %#v\n", i, pos.Level(), pos.Nth(), tree.nodes[pos])
}
}

func calcMaxLevel(youngestTwigID int64) int {
return FirstLevelAboveTwig + 63 - bits.LeadingZeros64(uint64(youngestTwigID))
}
Expand Down

0 comments on commit 3b6e964

Please sign in to comment.