diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-28 14:47:09 -0600 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2023-03-28 14:47:09 -0600 |
commit | 0b092a27122fcf19479d6cdeae5f7c9493d9741a (patch) | |
tree | d5e8802ad7b62f5222d3d88a0c592ff6cbb6b4ba /lib/btrfsutil/rebuilt_readitem.go | |
parent | bf5eed5af5c34b8cf9dc2985a7c4475602929bb1 (diff) | |
parent | f6f0a251ed962374f69e9fd7722dcd5c44aa58ad (diff) |
Merge branch 'lukeshu/node-cache'
Diffstat (limited to 'lib/btrfsutil/rebuilt_readitem.go')
-rw-r--r-- | lib/btrfsutil/rebuilt_readitem.go | 20 |
1 files changed, 7 insertions, 13 deletions
diff --git a/lib/btrfsutil/rebuilt_readitem.go b/lib/btrfsutil/rebuilt_readitem.go index 03a7cdc..d3a2253 100644 --- a/lib/btrfsutil/rebuilt_readitem.go +++ b/lib/btrfsutil/rebuilt_readitem.go @@ -26,18 +26,14 @@ func (ptr ItemPtr) String() string { return fmt.Sprintf("node@%v[%v]", ptr.Node, ptr.Slot) } -func (ts *RebuiltForrest) readNode(ctx context.Context, laddr btrfsvol.LogicalAddr) *btrfstree.Node { - if cached, ok := ts.nodes.Load(laddr); ok { - dlog.Tracef(ctx, "cache-hit node@%v", laddr) - return cached - } +func (ts *RebuiltForrest) readNode(ctx context.Context, laddr btrfsvol.LogicalAddr, out *btrfstree.Node) { + dlog.Debugf(ctx, "cache-miss node@%v, reading...", laddr) graphInfo, ok := ts.graph.Nodes[laddr] if !ok { panic(fmt.Errorf("should not happen: node@%v is not mentioned in the in-memory graph", laddr)) } - dlog.Debugf(ctx, "cache-miss node@%v, reading...", laddr) node, err := btrfstree.ReadNode[btrfsvol.LogicalAddr](ts.file, ts.sb, laddr, btrfstree.NodeExpectations{ LAddr: containers.OptionalValue(laddr), Level: containers.OptionalValue(graphInfo.Level), @@ -56,22 +52,20 @@ func (ts *RebuiltForrest) readNode(ctx context.Context, laddr btrfsvol.LogicalAd if err != nil { panic(fmt.Errorf("should not happen: i/o error: %w", err)) } - - ts.nodes.Store(laddr, node) - - return node + *out = *node } func (ts *RebuiltForrest) readItem(ctx context.Context, ptr ItemPtr) btrfsitem.Item { - ts.nodesMu.Lock() - defer ts.nodesMu.Unlock() if ts.graph.Nodes[ptr.Node].Level != 0 { panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for non-leaf node@%v", ptr.Node)) } if ptr.Slot < 0 { panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for negative item slot: %v", ptr.Slot)) } - items := ts.readNode(ctx, ptr.Node).BodyLeaf + + items := ts.nodes.Acquire(ctx, ptr.Node).BodyLeaf + defer ts.nodes.Release(ptr.Node) + if ptr.Slot >= len(items) { panic(fmt.Errorf("should not happen: btrfsutil.RebuiltForrest.readItem called for out-of-bounds item slot: slot=%v len=%v", ptr.Slot, len(items))) |