Debian Codegraph @

hgext/remotefilelog/metadatastore.py

raw
1 from __future__ import annotations
2
3 from mercurial.node import (
4 hex,
5 sha1nodeconstants,
6 )
7 from . import (
8 basestore,
9 shallowutil,
10 )
11
12
13 class unionmetadatastore(basestore.baseunionstore):
14 def __init__(self, *args, **kwargs):
15 super().__init__(*args, **kwargs)
16
17 self.writestore = kwargs.get('writestore')
18
19 # If allowincomplete==True then the union store can return partial
20 # ancestor lists, otherwise it will throw a KeyError if a full
21 # history can't be found.
22 self.allowincomplete = kwargs.get('allowincomplete', False)
23
24 def getancestors(self, name, node, known=None):
25 """Returns as many ancestors as we're aware of.
26
27 return value: {
28 node: (p1, p2, linknode, copyfrom),
29 ...
30 }
31 """
32 if known is None:
33 known = set()
34 if node in known:
35 return []
36
37 ancestors = {}
38
39 def traverse(curname, curnode):
40 # TODO: this algorithm has the potential to traverse parts of
41 # history twice. Ex: with A->B->C->F and A->B->D->F, both D and C
42 # may be queued as missing, then B and A are traversed for both.
43 queue = [(curname, curnode)]
44 missing = []
45 seen = set()
46 while queue:
47 name, node = queue.pop()
48 if (name, node) in seen:
49 continue
50 seen.add((name, node))
51 value = ancestors.get(node)
52 if not value:
53 missing.append((name, node))
54 continue
55 p1, p2, linknode, copyfrom = value
56 if p1 != sha1nodeconstants.nullid and p1 not in known:
57 queue.append((copyfrom or curname, p1))
58 if p2 != sha1nodeconstants.nullid and p2 not in known:
59 queue.append((curname, p2))
60 return missing
61
62 missing = [(name, node)]
63 while missing:
64 curname, curnode = missing.pop()
65 try:
66 ancestors.update(
67 self._getpartialancestors(curname, curnode, known=known)
68 )
69 newmissing = traverse(curname, curnode)
70 missing.extend(newmissing)
71 except KeyError:
72 # If we allow incomplete histories, don't throw.
73 if not self.allowincomplete:
74 raise
75 # If the requested name+node doesn't exist, always throw.
76 if (curname, curnode) == (name, node):
77 raise
78
79 # TODO: ancestors should probably be (name, node) -> (value)
80 return ancestors
81
82 @basestore.baseunionstore.retriable
83 def _getpartialancestors(self, name, node, known=None):
84 for store in self.stores:
85 try:
86 return store.getancestors(name, node, known=known)
87 except KeyError:
88 pass
89
90 raise KeyError((name, hex(node)))
91
92 @basestore.baseunionstore.retriable
93 def getnodeinfo(self, name, node):
94 for store in self.stores:
95 try:
96 return store.getnodeinfo(name, node)
97 except KeyError:
98 pass
99
100 raise KeyError((name, hex(node)))
101
102 def add(self, name, node, data):
103 raise RuntimeError(
104 b"cannot add content only to remotefilelog contentstore"
105 )
106
107 def getmissing(self, keys):
108 missing = keys
109 for store in self.stores:
110 if missing:
111 missing = store.getmissing(missing)
112 return missing
113
114 def markledger(self, ledger, options=None):
115 for store in self.stores:
116 store.markledger(ledger, options)
117
118 def getmetrics(self):
119 metrics = [s.getmetrics() for s in self.stores]
120 return shallowutil.sumdicts(*metrics)
121
122
123 class remotefilelogmetadatastore(basestore.basestore):
124 def getancestors(self, name, node, known=None):
125 """Returns as many ancestors as we're aware of.
126
127 return value: {
128 node: (p1, p2, linknode, copyfrom),
129 ...
130 }
131 """
132 data = self._getdata(name, node)
133 ancestors = shallowutil.ancestormap(data)
134 return ancestors
135
136 def getnodeinfo(self, name, node):
137 return self.getancestors(name, node)[node]
138
139 def add(self, name, node, parents, linknode):
140 raise RuntimeError(
141 b"cannot add metadata only to remotefilelog metadatastore"
142 )
143
144
145 class remotemetadatastore:
146 def __init__(self, ui, fileservice, shared):
147 self._fileservice = fileservice
148 self._shared = shared
149
150 def getancestors(self, name, node, known=None):
151 self._fileservice.prefetch(
152 [(name, hex(node))], force=True, fetchdata=False, fetchhistory=True
153 )
154 return self._shared.getancestors(name, node, known=known)
155
156 def getnodeinfo(self, name, node):
157 return self.getancestors(name, node)[node]
158
159 def add(self, name, node, data):
160 raise RuntimeError(b"cannot add to a remote store")
161
162 def getmissing(self, keys):
163 return keys
164
165 def markledger(self, ledger, options=None):
166 pass