diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-02-28 13:10:16 -0700 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-15 08:03:53 -0600 |
commit | e46cbc7ee7f3a2b93bd1bdf02e1eb95b98100f57 (patch) | |
tree | f179efcbcdc856f18ee91a67cf7f1b197f97d841 | |
parent | 9ff81649f425370bf3c1aee32542a784119947f8 (diff) |
misc: btrfsutil: Shrink the size of GraphNode
-rw-r--r-- | lib/btrfsutil/graph.go | 30 | ||||
-rw-r--r-- | lib/btrfsutil/graph_loops.go | 21 | ||||
-rw-r--r-- | lib/btrfsutil/rebuilt_readitem.go | 4 |
3 files changed, 30 insertions, 25 deletions
diff --git a/lib/btrfsutil/graph.go b/lib/btrfsutil/graph.go index 8debe9d..9e453eb 100644 --- a/lib/btrfsutil/graph.go +++ b/lib/btrfsutil/graph.go @@ -27,20 +27,31 @@ type GraphNode struct { Level uint8 Generation btrfsprim.Generation Owner btrfsprim.ObjID - NumItems uint32 - MinItem btrfsprim.Key - MaxItem btrfsprim.Key Items []btrfsprim.Key } +func (n GraphNode) MinItem() btrfsprim.Key { + if len(n.Items) == 0 { + return btrfsprim.Key{} + } + return n.Items[0] +} + +func (n GraphNode) MaxItem() btrfsprim.Key { + if len(n.Items) == 0 { + return btrfsprim.Key{} + } + return n.Items[len(n.Items)-1] +} + func (n GraphNode) String() string { if reflect.ValueOf(n).IsZero() { return "{}" } return fmt.Sprintf(`{lvl:%v, gen:%v, tree:%v, cnt:%v, min:(%v,%v,%v), max:(%v,%v,%v)}`, - n.Level, n.Generation, n.Owner, n.NumItems, - n.MinItem.ObjectID, n.MinItem.ItemType, n.MinItem.Offset, - n.MaxItem.ObjectID, n.MaxItem.ItemType, n.MaxItem.Offset) + n.Level, n.Generation, n.Owner, len(n.Items), + n.MinItem().ObjectID, n.MinItem().ItemType, n.MinItem().Offset, + n.MaxItem().ObjectID, n.MaxItem().ItemType, n.MaxItem().Offset) } type GraphEdge struct { @@ -143,9 +154,6 @@ func (g Graph) InsertNode(nodeRef *diskio.Ref[btrfsvol.LogicalAddr, btrfstree.No Level: nodeRef.Data.Head.Level, Generation: nodeRef.Data.Head.Generation, Owner: nodeRef.Data.Head.Owner, - NumItems: nodeRef.Data.Head.NumItems, - MinItem: discardOK(nodeRef.Data.MinItem()), - MaxItem: discardOK(nodeRef.Data.MaxItem()), } if nodeRef.Data.Head.Level == 0 { @@ -256,7 +264,3 @@ func (g Graph) FinalCheck(ctx context.Context, fs diskio.File[btrfsvol.LogicalAd return nil } - -func discardOK[T any](val T, _ bool) T { - return val -} diff --git a/lib/btrfsutil/graph_loops.go b/lib/btrfsutil/graph_loops.go index 3382705..b5690af 100644 --- a/lib/btrfsutil/graph_loops.go +++ b/lib/btrfsutil/graph_loops.go @@ -22,15 +22,15 @@ func (g Graph) renderNode(node btrfsvol.LogicalAddr) []string { fmt.Sprintf("{addr: %v,", node), fmt.Sprintf(" level: %v,", nodeData.Level), fmt.Sprintf(" gen: %v,", nodeData.Generation), - fmt.Sprintf(" num_items: %v,", nodeData.NumItems), + fmt.Sprintf(" num_items: %v,", len(nodeData.Items)), fmt.Sprintf(" min_item: {%d,%v,%d},", - nodeData.MinItem.ObjectID, - nodeData.MinItem.ItemType, - nodeData.MinItem.Offset), + nodeData.MinItem().ObjectID, + nodeData.MinItem().ItemType, + nodeData.MinItem().Offset), fmt.Sprintf(" max_item: {%d,%v,%d}}", - nodeData.MaxItem.ObjectID, - nodeData.MaxItem.ItemType, - nodeData.MaxItem.Offset), + nodeData.MaxItem().ObjectID, + nodeData.MaxItem().ItemType, + nodeData.MaxItem().Offset), } } else if nodeErr, ok := g.BadNodes[node]; ok { return []string{ @@ -120,11 +120,12 @@ func checkNodeExpectations(kp GraphEdge, toNode GraphNode) error { errs = append(errs, fmt.Errorf("kp.generation=%v != node.generation=%v", kp.ToGeneration, toNode.Generation)) } - if toNode.NumItems == 0 { + switch { + case len(toNode.Items) == 0: errs = append(errs, fmt.Errorf("node.num_items=0")) - } else if kp.ToKey != (btrfsprim.Key{}) && toNode.MinItem != kp.ToKey { + case kp.ToKey != (btrfsprim.Key{}) && toNode.MinItem() != kp.ToKey: errs = append(errs, fmt.Errorf("kp.key=%v != node.items[0].key=%v", - kp.ToKey, toNode.MinItem)) + kp.ToKey, toNode.MinItem())) } if len(errs) > 0 { return errs diff --git a/lib/btrfsutil/rebuilt_readitem.go b/lib/btrfsutil/rebuilt_readitem.go index 57440cf..a508d99 100644 --- a/lib/btrfsutil/rebuilt_readitem.go +++ b/lib/btrfsutil/rebuilt_readitem.go @@ -141,8 +141,8 @@ func (o *KeyIO) readNode(ctx context.Context, laddr btrfsvol.LogicalAddr) *diski } return nil }, - MinItem: containers.Optional[btrfsprim.Key]{OK: true, Val: graphInfo.MinItem}, - MaxItem: containers.Optional[btrfsprim.Key]{OK: true, Val: graphInfo.MaxItem}, + MinItem: containers.Optional[btrfsprim.Key]{OK: true, Val: graphInfo.MinItem()}, + MaxItem: containers.Optional[btrfsprim.Key]{OK: true, Val: graphInfo.MaxItem()}, }) if err != nil { panic(fmt.Errorf("should not happen: i/o error: %w", err)) |