Debian Codegraph @

internal/graph/memory.go

raw
1 /*
2 Copyright The ORAS Authors.
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 */
15
16 package graph
17
18 import (
19 "context"
20 "errors"
21 "sync"
22
23 "github.com/opencontainers/go-digest"
24 ocispec "github.com/opencontainers/image-spec/specs-go/v1"
25 "oras.land/oras-go/v2/content"
26 "oras.land/oras-go/v2/errdef"
27 "oras.land/oras-go/v2/internal/container/set"
28 "oras.land/oras-go/v2/internal/status"
29 "oras.land/oras-go/v2/internal/syncutil"
30 )
31
32 // Memory is a memory based PredecessorFinder.
33 type Memory struct {
34 // nodes has the following properties and behaviors:
35 // 1. a node exists in Memory.nodes if and only if it exists in the memory
36 // 2. Memory.nodes saves the ocispec.Descriptor indexed by digest, which are used by
37 // the other fields.
38 nodes map[digest.Digest]ocispec.Descriptor
39
40 // predecessors has the following properties and behaviors:
41 // 1. a node exists in Memory.predecessors if it has at least one predecessor
42 // in the memory, regardless of whether or not the node itself exists in
43 // the memory.
44 // 2. a node does not exist in Memory.predecessors, if it doesn't have any predecessors
45 // in the memory.
46 predecessors map[digest.Digest]set.Set[digest.Digest]
47
48 // successors has the following properties and behaviors:
49 // 1. a node exists in Memory.successors if and only if it exists in the memory.
50 // 2. a node's entry in Memory.successors is always consistent with the actual
51 // content of the node, regardless of whether or not each successor exists
52 // in the memory.
53 successors map[digest.Digest]set.Set[digest.Digest]
54
55 lock sync.RWMutex
56 }
57
58 // NewMemory creates a new memory PredecessorFinder.
59 func NewMemory() *Memory {
60 return &Memory{
61 nodes: make(map[digest.Digest]ocispec.Descriptor),
62 predecessors: make(map[digest.Digest]set.Set[digest.Digest]),
63 successors: make(map[digest.Digest]set.Set[digest.Digest]),
64 }
65 }
66
67 // Index indexes predecessors for each direct successor of the given node.
68 func (m *Memory) Index(ctx context.Context, fetcher content.Fetcher, node ocispec.Descriptor) error {
69 _, err := m.index(ctx, fetcher, node)
70 return err
71 }
72
73 // Index indexes predecessors for all the successors of the given node.
74 func (m *Memory) IndexAll(ctx context.Context, fetcher content.Fetcher, node ocispec.Descriptor) error {
75 // track content status
76 tracker := status.NewTracker()
77 var fn syncutil.GoFunc[ocispec.Descriptor]
78 fn = func(ctx context.Context, region *syncutil.LimitedRegion, desc ocispec.Descriptor) error {
79 // skip the node if other go routine is working on it
80 _, committed := tracker.TryCommit(desc)
81 if !committed {
82 return nil
83 }
84 successors, err := m.index(ctx, fetcher, desc)
85 if err != nil {
86 if errors.Is(err, errdef.ErrNotFound) {
87 // skip the node if it does not exist
88 return nil
89 }
90 return err
91 }
92 if len(successors) > 0 {
93 // traverse and index successors
94 return syncutil.Go(ctx, nil, fn, successors...)
95 }
96 return nil
97 }
98 return syncutil.Go(ctx, nil, fn, node)
99 }
100
101 // Predecessors returns the nodes directly pointing to the current node.
102 // Predecessors returns nil without error if the node does not exists in the
103 // store. Like other operations, calling Predecessors() is go-routine safe.
104 // However, it does not necessarily correspond to any consistent snapshot of
105 // the stored contents.
106 func (m *Memory) Predecessors(_ context.Context, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
107 m.lock.RLock()
108 defer m.lock.RUnlock()
109
110 set, exists := m.predecessors[node.Digest]
111 if !exists {
112 return nil, nil
113 }
114 var res []ocispec.Descriptor
115 for digest := range set {
116 res = append(res, m.nodes[digest])
117 }
118 return res, nil
119 }
120
121 // Remove removes the node from its predecessors and successors, and returns the
122 // dangling root nodes caused by the deletion.
123 func (m *Memory) Remove(node ocispec.Descriptor) []ocispec.Descriptor {
124 m.lock.Lock()
125 defer m.lock.Unlock()
126
127 var danglings []ocispec.Descriptor
128 // remove the node from its successors' predecessor list
129 for successorDigest := range m.successors[node.Digest] {
130 predecessorEntry := m.predecessors[successorDigest]
131 predecessorEntry.Delete(node.Digest)
132
133 // if none of the predecessors of the node still exists, we remove the
134 // predecessors entry and return it as a dangling node. Otherwise, we do
135 // not remove the entry.
136 if len(predecessorEntry) == 0 {
137 delete(m.predecessors, successorDigest)
138 if _, exists := m.nodes[successorDigest]; exists {
139 danglings = append(danglings, m.nodes[successorDigest])
140 }
141 }
142 }
143 delete(m.successors, node.Digest)
144 delete(m.nodes, node.Digest)
145 return danglings
146 }
147
148 // DigestSet returns the set of node digest in memory.
149 func (m *Memory) DigestSet() set.Set[digest.Digest] {
150 m.lock.RLock()
151 defer m.lock.RUnlock()
152
153 s := set.New[digest.Digest]()
154 for digest := range m.nodes {
155 s.Add(digest)
156 }
157 return s
158 }
159
160 // index indexes predecessors for each direct successor of the given node.
161 func (m *Memory) index(ctx context.Context, fetcher content.Fetcher, node ocispec.Descriptor) ([]ocispec.Descriptor, error) {
162 successors, err := content.Successors(ctx, fetcher, node)
163 if err != nil {
164 return nil, err
165 }
166 m.lock.Lock()
167 defer m.lock.Unlock()
168
169 // index the node
170 m.nodes[node.Digest] = node
171
172 // for each successor, put it into the node's successors list, and
173 // put node into the succeesor's predecessors list
174 successorSet := set.New[digest.Digest]()
175 m.successors[node.Digest] = successorSet
176 for _, successor := range successors {
177 successorSet.Add(successor.Digest)
178 predecessorSet, exists := m.predecessors[successor.Digest]
179 if !exists {
180 predecessorSet = set.New[digest.Digest]()
181 m.predecessors[successor.Digest] = predecessorSet
182 }
183 predecessorSet.Add(node.Digest)
184 }
185 return successors, nil
186 }
187
188 // Exists checks if the node exists in the graph
189 func (m *Memory) Exists(node ocispec.Descriptor) bool {
190 m.lock.RLock()
191 defer m.lock.RUnlock()
192
193 _, exists := m.nodes[node.Digest]
194 return exists
195 }