Show More
@@ -1,1554 +1,1555 b'' | |||
|
1 | 1 | /* |
|
2 | 2 | parsers.c - efficient content parsing |
|
3 | 3 | |
|
4 | 4 | Copyright 2008 Matt Mackall <mpm@selenic.com> and others |
|
5 | 5 | |
|
6 | 6 | This software may be used and distributed according to the terms of |
|
7 | 7 | the GNU General Public License, incorporated herein by reference. |
|
8 | 8 | */ |
|
9 | 9 | |
|
10 | 10 | #include <Python.h> |
|
11 | 11 | #include <ctype.h> |
|
12 | #include <stddef.h> | |
|
12 | 13 | #include <string.h> |
|
13 | 14 | |
|
14 | 15 | #include "util.h" |
|
15 | 16 | |
|
16 | 17 | static inline int hexdigit(const char *p, Py_ssize_t off) |
|
17 | 18 | { |
|
18 | 19 | char c = p[off]; |
|
19 | 20 | |
|
20 | 21 | if (c >= '0' && c <= '9') |
|
21 | 22 | return c - '0'; |
|
22 | 23 | if (c >= 'a' && c <= 'f') |
|
23 | 24 | return c - 'a' + 10; |
|
24 | 25 | if (c >= 'A' && c <= 'F') |
|
25 | 26 | return c - 'A' + 10; |
|
26 | 27 | |
|
27 | 28 | PyErr_SetString(PyExc_ValueError, "input contains non-hex character"); |
|
28 | 29 | return 0; |
|
29 | 30 | } |
|
30 | 31 | |
|
31 | 32 | /* |
|
32 | 33 | * Turn a hex-encoded string into binary. |
|
33 | 34 | */ |
|
34 | 35 | static PyObject *unhexlify(const char *str, int len) |
|
35 | 36 | { |
|
36 | 37 | PyObject *ret; |
|
37 | 38 | char *d; |
|
38 | 39 | int i; |
|
39 | 40 | |
|
40 | 41 | ret = PyBytes_FromStringAndSize(NULL, len / 2); |
|
41 | 42 | |
|
42 | 43 | if (!ret) |
|
43 | 44 | return NULL; |
|
44 | 45 | |
|
45 | 46 | d = PyBytes_AsString(ret); |
|
46 | 47 | |
|
47 | 48 | for (i = 0; i < len;) { |
|
48 | 49 | int hi = hexdigit(str, i++); |
|
49 | 50 | int lo = hexdigit(str, i++); |
|
50 | 51 | *d++ = (hi << 4) | lo; |
|
51 | 52 | } |
|
52 | 53 | |
|
53 | 54 | return ret; |
|
54 | 55 | } |
|
55 | 56 | |
|
56 | 57 | /* |
|
57 | 58 | * This code assumes that a manifest is stitched together with newline |
|
58 | 59 | * ('\n') characters. |
|
59 | 60 | */ |
|
60 | 61 | static PyObject *parse_manifest(PyObject *self, PyObject *args) |
|
61 | 62 | { |
|
62 | 63 | PyObject *mfdict, *fdict; |
|
63 | 64 | char *str, *cur, *start, *zero; |
|
64 | 65 | int len; |
|
65 | 66 | |
|
66 | 67 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_manifest", |
|
67 | 68 | &PyDict_Type, &mfdict, |
|
68 | 69 | &PyDict_Type, &fdict, |
|
69 | 70 | &str, &len)) |
|
70 | 71 | goto quit; |
|
71 | 72 | |
|
72 | 73 | for (start = cur = str, zero = NULL; cur < str + len; cur++) { |
|
73 | 74 | PyObject *file = NULL, *node = NULL; |
|
74 | 75 | PyObject *flags = NULL; |
|
75 |
|
|
|
76 | ptrdiff_t nlen; | |
|
76 | 77 | |
|
77 | 78 | if (!*cur) { |
|
78 | 79 | zero = cur; |
|
79 | 80 | continue; |
|
80 | 81 | } |
|
81 | 82 | else if (*cur != '\n') |
|
82 | 83 | continue; |
|
83 | 84 | |
|
84 | 85 | if (!zero) { |
|
85 | 86 | PyErr_SetString(PyExc_ValueError, |
|
86 | 87 | "manifest entry has no separator"); |
|
87 | 88 | goto quit; |
|
88 | 89 | } |
|
89 | 90 | |
|
90 | 91 | file = PyBytes_FromStringAndSize(start, zero - start); |
|
91 | 92 | |
|
92 | 93 | if (!file) |
|
93 | 94 | goto bail; |
|
94 | 95 | |
|
95 | 96 | nlen = cur - zero - 1; |
|
96 | 97 | |
|
97 | node = unhexlify(zero + 1, nlen > 40 ? 40 : nlen); | |
|
98 | node = unhexlify(zero + 1, nlen > 40 ? 40 : (int)nlen); | |
|
98 | 99 | if (!node) |
|
99 | 100 | goto bail; |
|
100 | 101 | |
|
101 | 102 | if (nlen > 40) { |
|
102 | 103 | flags = PyBytes_FromStringAndSize(zero + 41, |
|
103 | 104 | nlen - 40); |
|
104 | 105 | if (!flags) |
|
105 | 106 | goto bail; |
|
106 | 107 | |
|
107 | 108 | if (PyDict_SetItem(fdict, file, flags) == -1) |
|
108 | 109 | goto bail; |
|
109 | 110 | } |
|
110 | 111 | |
|
111 | 112 | if (PyDict_SetItem(mfdict, file, node) == -1) |
|
112 | 113 | goto bail; |
|
113 | 114 | |
|
114 | 115 | start = cur + 1; |
|
115 | 116 | zero = NULL; |
|
116 | 117 | |
|
117 | 118 | Py_XDECREF(flags); |
|
118 | 119 | Py_XDECREF(node); |
|
119 | 120 | Py_XDECREF(file); |
|
120 | 121 | continue; |
|
121 | 122 | bail: |
|
122 | 123 | Py_XDECREF(flags); |
|
123 | 124 | Py_XDECREF(node); |
|
124 | 125 | Py_XDECREF(file); |
|
125 | 126 | goto quit; |
|
126 | 127 | } |
|
127 | 128 | |
|
128 | 129 | if (len > 0 && *(cur - 1) != '\n') { |
|
129 | 130 | PyErr_SetString(PyExc_ValueError, |
|
130 | 131 | "manifest contains trailing garbage"); |
|
131 | 132 | goto quit; |
|
132 | 133 | } |
|
133 | 134 | |
|
134 | 135 | Py_INCREF(Py_None); |
|
135 | 136 | return Py_None; |
|
136 | 137 | quit: |
|
137 | 138 | return NULL; |
|
138 | 139 | } |
|
139 | 140 | |
|
140 | 141 | static PyObject *parse_dirstate(PyObject *self, PyObject *args) |
|
141 | 142 | { |
|
142 | 143 | PyObject *dmap, *cmap, *parents = NULL, *ret = NULL; |
|
143 | 144 | PyObject *fname = NULL, *cname = NULL, *entry = NULL; |
|
144 | 145 | char *str, *cur, *end, *cpos; |
|
145 | 146 | int state, mode, size, mtime; |
|
146 | 147 | unsigned int flen; |
|
147 | 148 | int len; |
|
148 | 149 | |
|
149 | 150 | if (!PyArg_ParseTuple(args, "O!O!s#:parse_dirstate", |
|
150 | 151 | &PyDict_Type, &dmap, |
|
151 | 152 | &PyDict_Type, &cmap, |
|
152 | 153 | &str, &len)) |
|
153 | 154 | goto quit; |
|
154 | 155 | |
|
155 | 156 | /* read parents */ |
|
156 | 157 | if (len < 40) |
|
157 | 158 | goto quit; |
|
158 | 159 | |
|
159 | 160 | parents = Py_BuildValue("s#s#", str, 20, str + 20, 20); |
|
160 | 161 | if (!parents) |
|
161 | 162 | goto quit; |
|
162 | 163 | |
|
163 | 164 | /* read filenames */ |
|
164 | 165 | cur = str + 40; |
|
165 | 166 | end = str + len; |
|
166 | 167 | |
|
167 | 168 | while (cur < end - 17) { |
|
168 | 169 | /* unpack header */ |
|
169 | 170 | state = *cur; |
|
170 | 171 | mode = getbe32(cur + 1); |
|
171 | 172 | size = getbe32(cur + 5); |
|
172 | 173 | mtime = getbe32(cur + 9); |
|
173 | 174 | flen = getbe32(cur + 13); |
|
174 | 175 | cur += 17; |
|
175 | 176 | if (cur + flen > end || cur + flen < cur) { |
|
176 | 177 | PyErr_SetString(PyExc_ValueError, "overflow in dirstate"); |
|
177 | 178 | goto quit; |
|
178 | 179 | } |
|
179 | 180 | |
|
180 | 181 | entry = Py_BuildValue("ciii", state, mode, size, mtime); |
|
181 | 182 | if (!entry) |
|
182 | 183 | goto quit; |
|
183 | 184 | PyObject_GC_UnTrack(entry); /* don't waste time with this */ |
|
184 | 185 | |
|
185 | 186 | cpos = memchr(cur, 0, flen); |
|
186 | 187 | if (cpos) { |
|
187 | 188 | fname = PyBytes_FromStringAndSize(cur, cpos - cur); |
|
188 | 189 | cname = PyBytes_FromStringAndSize(cpos + 1, |
|
189 | 190 | flen - (cpos - cur) - 1); |
|
190 | 191 | if (!fname || !cname || |
|
191 | 192 | PyDict_SetItem(cmap, fname, cname) == -1 || |
|
192 | 193 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
193 | 194 | goto quit; |
|
194 | 195 | Py_DECREF(cname); |
|
195 | 196 | } else { |
|
196 | 197 | fname = PyBytes_FromStringAndSize(cur, flen); |
|
197 | 198 | if (!fname || |
|
198 | 199 | PyDict_SetItem(dmap, fname, entry) == -1) |
|
199 | 200 | goto quit; |
|
200 | 201 | } |
|
201 | 202 | cur += flen; |
|
202 | 203 | Py_DECREF(fname); |
|
203 | 204 | Py_DECREF(entry); |
|
204 | 205 | fname = cname = entry = NULL; |
|
205 | 206 | } |
|
206 | 207 | |
|
207 | 208 | ret = parents; |
|
208 | 209 | Py_INCREF(ret); |
|
209 | 210 | quit: |
|
210 | 211 | Py_XDECREF(fname); |
|
211 | 212 | Py_XDECREF(cname); |
|
212 | 213 | Py_XDECREF(entry); |
|
213 | 214 | Py_XDECREF(parents); |
|
214 | 215 | return ret; |
|
215 | 216 | } |
|
216 | 217 | |
|
217 | 218 | static inline int getintat(PyObject *tuple, int off, uint32_t *v) |
|
218 | 219 | { |
|
219 | 220 | PyObject *o = PyTuple_GET_ITEM(tuple, off); |
|
220 | 221 | long val; |
|
221 | 222 | |
|
222 | 223 | if (PyInt_Check(o)) |
|
223 | 224 | val = PyInt_AS_LONG(o); |
|
224 | 225 | else if (PyLong_Check(o)) { |
|
225 | 226 | val = PyLong_AsLong(o); |
|
226 | 227 | if (val == -1 && PyErr_Occurred()) |
|
227 | 228 | return -1; |
|
228 | 229 | } else { |
|
229 | 230 | PyErr_SetString(PyExc_TypeError, "expected an int or long"); |
|
230 | 231 | return -1; |
|
231 | 232 | } |
|
232 | 233 | if (LONG_MAX > INT_MAX && (val > INT_MAX || val < INT_MIN)) { |
|
233 | 234 | PyErr_SetString(PyExc_OverflowError, |
|
234 | 235 | "Python value to large to convert to uint32_t"); |
|
235 | 236 | return -1; |
|
236 | 237 | } |
|
237 | 238 | *v = (uint32_t)val; |
|
238 | 239 | return 0; |
|
239 | 240 | } |
|
240 | 241 | |
|
241 | 242 | static PyObject *dirstate_unset; |
|
242 | 243 | |
|
243 | 244 | /* |
|
244 | 245 | * Efficiently pack a dirstate object into its on-disk format. |
|
245 | 246 | */ |
|
246 | 247 | static PyObject *pack_dirstate(PyObject *self, PyObject *args) |
|
247 | 248 | { |
|
248 | 249 | PyObject *packobj = NULL; |
|
249 | 250 | PyObject *map, *copymap, *pl; |
|
250 | 251 | Py_ssize_t nbytes, pos, l; |
|
251 | 252 | PyObject *k, *v, *pn; |
|
252 | 253 | char *p, *s; |
|
253 | 254 | double now; |
|
254 | 255 | |
|
255 | 256 | if (!PyArg_ParseTuple(args, "O!O!Od:pack_dirstate", |
|
256 | 257 | &PyDict_Type, &map, &PyDict_Type, ©map, |
|
257 | 258 | &pl, &now)) |
|
258 | 259 | return NULL; |
|
259 | 260 | |
|
260 | 261 | if (!PySequence_Check(pl) || PySequence_Size(pl) != 2) { |
|
261 | 262 | PyErr_SetString(PyExc_TypeError, "expected 2-element sequence"); |
|
262 | 263 | return NULL; |
|
263 | 264 | } |
|
264 | 265 | |
|
265 | 266 | /* Figure out how much we need to allocate. */ |
|
266 | 267 | for (nbytes = 40, pos = 0; PyDict_Next(map, &pos, &k, &v);) { |
|
267 | 268 | PyObject *c; |
|
268 | 269 | if (!PyString_Check(k)) { |
|
269 | 270 | PyErr_SetString(PyExc_TypeError, "expected string key"); |
|
270 | 271 | goto bail; |
|
271 | 272 | } |
|
272 | 273 | nbytes += PyString_GET_SIZE(k) + 17; |
|
273 | 274 | c = PyDict_GetItem(copymap, k); |
|
274 | 275 | if (c) { |
|
275 | 276 | if (!PyString_Check(c)) { |
|
276 | 277 | PyErr_SetString(PyExc_TypeError, |
|
277 | 278 | "expected string key"); |
|
278 | 279 | goto bail; |
|
279 | 280 | } |
|
280 | 281 | nbytes += PyString_GET_SIZE(c) + 1; |
|
281 | 282 | } |
|
282 | 283 | } |
|
283 | 284 | |
|
284 | 285 | packobj = PyString_FromStringAndSize(NULL, nbytes); |
|
285 | 286 | if (packobj == NULL) |
|
286 | 287 | goto bail; |
|
287 | 288 | |
|
288 | 289 | p = PyString_AS_STRING(packobj); |
|
289 | 290 | |
|
290 | 291 | pn = PySequence_ITEM(pl, 0); |
|
291 | 292 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
292 | 293 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
293 | 294 | goto bail; |
|
294 | 295 | } |
|
295 | 296 | memcpy(p, s, l); |
|
296 | 297 | p += 20; |
|
297 | 298 | pn = PySequence_ITEM(pl, 1); |
|
298 | 299 | if (PyString_AsStringAndSize(pn, &s, &l) == -1 || l != 20) { |
|
299 | 300 | PyErr_SetString(PyExc_TypeError, "expected a 20-byte hash"); |
|
300 | 301 | goto bail; |
|
301 | 302 | } |
|
302 | 303 | memcpy(p, s, l); |
|
303 | 304 | p += 20; |
|
304 | 305 | |
|
305 | 306 | for (pos = 0; PyDict_Next(map, &pos, &k, &v); ) { |
|
306 | 307 | uint32_t mode, size, mtime; |
|
307 | 308 | Py_ssize_t len, l; |
|
308 | 309 | PyObject *o; |
|
309 | 310 | char *s, *t; |
|
310 | 311 | |
|
311 | 312 | if (!PyTuple_Check(v) || PyTuple_GET_SIZE(v) != 4) { |
|
312 | 313 | PyErr_SetString(PyExc_TypeError, "expected a 4-tuple"); |
|
313 | 314 | goto bail; |
|
314 | 315 | } |
|
315 | 316 | o = PyTuple_GET_ITEM(v, 0); |
|
316 | 317 | if (PyString_AsStringAndSize(o, &s, &l) == -1 || l != 1) { |
|
317 | 318 | PyErr_SetString(PyExc_TypeError, "expected one byte"); |
|
318 | 319 | goto bail; |
|
319 | 320 | } |
|
320 | 321 | *p++ = *s; |
|
321 | 322 | if (getintat(v, 1, &mode) == -1) |
|
322 | 323 | goto bail; |
|
323 | 324 | if (getintat(v, 2, &size) == -1) |
|
324 | 325 | goto bail; |
|
325 | 326 | if (getintat(v, 3, &mtime) == -1) |
|
326 | 327 | goto bail; |
|
327 | 328 | if (*s == 'n' && mtime == (uint32_t)now) { |
|
328 | 329 | /* See dirstate.py:write for why we do this. */ |
|
329 | 330 | if (PyDict_SetItem(map, k, dirstate_unset) == -1) |
|
330 | 331 | goto bail; |
|
331 | 332 | mode = 0, size = -1, mtime = -1; |
|
332 | 333 | } |
|
333 | 334 | putbe32(mode, p); |
|
334 | 335 | putbe32(size, p + 4); |
|
335 | 336 | putbe32(mtime, p + 8); |
|
336 | 337 | t = p + 12; |
|
337 | 338 | p += 16; |
|
338 | 339 | len = PyString_GET_SIZE(k); |
|
339 | 340 | memcpy(p, PyString_AS_STRING(k), len); |
|
340 | 341 | p += len; |
|
341 | 342 | o = PyDict_GetItem(copymap, k); |
|
342 | 343 | if (o) { |
|
343 | 344 | *p++ = '\0'; |
|
344 | 345 | l = PyString_GET_SIZE(o); |
|
345 | 346 | memcpy(p, PyString_AS_STRING(o), l); |
|
346 | 347 | p += l; |
|
347 | 348 | len += l + 1; |
|
348 | 349 | } |
|
349 | 350 | putbe32((uint32_t)len, t); |
|
350 | 351 | } |
|
351 | 352 | |
|
352 | 353 | pos = p - PyString_AS_STRING(packobj); |
|
353 | 354 | if (pos != nbytes) { |
|
354 | 355 | PyErr_Format(PyExc_SystemError, "bad dirstate size: %ld != %ld", |
|
355 | 356 | (long)pos, (long)nbytes); |
|
356 | 357 | goto bail; |
|
357 | 358 | } |
|
358 | 359 | |
|
359 | 360 | return packobj; |
|
360 | 361 | bail: |
|
361 | 362 | Py_XDECREF(packobj); |
|
362 | 363 | return NULL; |
|
363 | 364 | } |
|
364 | 365 | |
|
365 | 366 | /* |
|
366 | 367 | * A base-16 trie for fast node->rev mapping. |
|
367 | 368 | * |
|
368 | 369 | * Positive value is index of the next node in the trie |
|
369 | 370 | * Negative value is a leaf: -(rev + 1) |
|
370 | 371 | * Zero is empty |
|
371 | 372 | */ |
|
372 | 373 | typedef struct { |
|
373 | 374 | int children[16]; |
|
374 | 375 | } nodetree; |
|
375 | 376 | |
|
376 | 377 | /* |
|
377 | 378 | * This class has two behaviours. |
|
378 | 379 | * |
|
379 | 380 | * When used in a list-like way (with integer keys), we decode an |
|
380 | 381 | * entry in a RevlogNG index file on demand. Our last entry is a |
|
381 | 382 | * sentinel, always a nullid. We have limited support for |
|
382 | 383 | * integer-keyed insert and delete, only at elements right before the |
|
383 | 384 | * sentinel. |
|
384 | 385 | * |
|
385 | 386 | * With string keys, we lazily perform a reverse mapping from node to |
|
386 | 387 | * rev, using a base-16 trie. |
|
387 | 388 | */ |
|
388 | 389 | typedef struct { |
|
389 | 390 | PyObject_HEAD |
|
390 | 391 | /* Type-specific fields go here. */ |
|
391 | 392 | PyObject *data; /* raw bytes of index */ |
|
392 | 393 | PyObject **cache; /* cached tuples */ |
|
393 | 394 | const char **offsets; /* populated on demand */ |
|
394 | 395 | Py_ssize_t raw_length; /* original number of elements */ |
|
395 | 396 | Py_ssize_t length; /* current number of elements */ |
|
396 | 397 | PyObject *added; /* populated on demand */ |
|
397 | 398 | PyObject *headrevs; /* cache, invalidated on changes */ |
|
398 | 399 | nodetree *nt; /* base-16 trie */ |
|
399 | 400 | int ntlength; /* # nodes in use */ |
|
400 | 401 | int ntcapacity; /* # nodes allocated */ |
|
401 | 402 | int ntdepth; /* maximum depth of tree */ |
|
402 | 403 | int ntsplits; /* # splits performed */ |
|
403 | 404 | int ntrev; /* last rev scanned */ |
|
404 | 405 | int ntlookups; /* # lookups */ |
|
405 | 406 | int ntmisses; /* # lookups that miss the cache */ |
|
406 | 407 | int inlined; |
|
407 | 408 | } indexObject; |
|
408 | 409 | |
|
409 | 410 | static Py_ssize_t index_length(const indexObject *self) |
|
410 | 411 | { |
|
411 | 412 | if (self->added == NULL) |
|
412 | 413 | return self->length; |
|
413 | 414 | return self->length + PyList_GET_SIZE(self->added); |
|
414 | 415 | } |
|
415 | 416 | |
|
416 | 417 | static PyObject *nullentry; |
|
417 | 418 | static const char nullid[20]; |
|
418 | 419 | |
|
419 | 420 | static long inline_scan(indexObject *self, const char **offsets); |
|
420 | 421 | |
|
421 | 422 | #if LONG_MAX == 0x7fffffffL |
|
422 | 423 | static char *tuple_format = "Kiiiiiis#"; |
|
423 | 424 | #else |
|
424 | 425 | static char *tuple_format = "kiiiiiis#"; |
|
425 | 426 | #endif |
|
426 | 427 | |
|
427 | 428 | /* A RevlogNG v1 index entry is 64 bytes long. */ |
|
428 | 429 | static const long v1_hdrsize = 64; |
|
429 | 430 | |
|
430 | 431 | /* |
|
431 | 432 | * Return a pointer to the beginning of a RevlogNG record. |
|
432 | 433 | */ |
|
433 | 434 | static const char *index_deref(indexObject *self, Py_ssize_t pos) |
|
434 | 435 | { |
|
435 | 436 | if (self->inlined && pos > 0) { |
|
436 | 437 | if (self->offsets == NULL) { |
|
437 | 438 | self->offsets = malloc(self->raw_length * |
|
438 | 439 | sizeof(*self->offsets)); |
|
439 | 440 | if (self->offsets == NULL) |
|
440 | 441 | return (const char *)PyErr_NoMemory(); |
|
441 | 442 | inline_scan(self, self->offsets); |
|
442 | 443 | } |
|
443 | 444 | return self->offsets[pos]; |
|
444 | 445 | } |
|
445 | 446 | |
|
446 | 447 | return PyString_AS_STRING(self->data) + pos * v1_hdrsize; |
|
447 | 448 | } |
|
448 | 449 | |
|
449 | 450 | /* |
|
450 | 451 | * RevlogNG format (all in big endian, data may be inlined): |
|
451 | 452 | * 6 bytes: offset |
|
452 | 453 | * 2 bytes: flags |
|
453 | 454 | * 4 bytes: compressed length |
|
454 | 455 | * 4 bytes: uncompressed length |
|
455 | 456 | * 4 bytes: base revision |
|
456 | 457 | * 4 bytes: link revision |
|
457 | 458 | * 4 bytes: parent 1 revision |
|
458 | 459 | * 4 bytes: parent 2 revision |
|
459 | 460 | * 32 bytes: nodeid (only 20 bytes used) |
|
460 | 461 | */ |
|
461 | 462 | static PyObject *index_get(indexObject *self, Py_ssize_t pos) |
|
462 | 463 | { |
|
463 | 464 | uint64_t offset_flags; |
|
464 | 465 | int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2; |
|
465 | 466 | const char *c_node_id; |
|
466 | 467 | const char *data; |
|
467 | 468 | Py_ssize_t length = index_length(self); |
|
468 | 469 | PyObject *entry; |
|
469 | 470 | |
|
470 | 471 | if (pos < 0) |
|
471 | 472 | pos += length; |
|
472 | 473 | |
|
473 | 474 | if (pos < 0 || pos >= length) { |
|
474 | 475 | PyErr_SetString(PyExc_IndexError, "revlog index out of range"); |
|
475 | 476 | return NULL; |
|
476 | 477 | } |
|
477 | 478 | |
|
478 | 479 | if (pos == length - 1) { |
|
479 | 480 | Py_INCREF(nullentry); |
|
480 | 481 | return nullentry; |
|
481 | 482 | } |
|
482 | 483 | |
|
483 | 484 | if (pos >= self->length - 1) { |
|
484 | 485 | PyObject *obj; |
|
485 | 486 | obj = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
486 | 487 | Py_INCREF(obj); |
|
487 | 488 | return obj; |
|
488 | 489 | } |
|
489 | 490 | |
|
490 | 491 | if (self->cache) { |
|
491 | 492 | if (self->cache[pos]) { |
|
492 | 493 | Py_INCREF(self->cache[pos]); |
|
493 | 494 | return self->cache[pos]; |
|
494 | 495 | } |
|
495 | 496 | } else { |
|
496 | 497 | self->cache = calloc(self->raw_length, sizeof(PyObject *)); |
|
497 | 498 | if (self->cache == NULL) |
|
498 | 499 | return PyErr_NoMemory(); |
|
499 | 500 | } |
|
500 | 501 | |
|
501 | 502 | data = index_deref(self, pos); |
|
502 | 503 | if (data == NULL) |
|
503 | 504 | return NULL; |
|
504 | 505 | |
|
505 | 506 | offset_flags = getbe32(data + 4); |
|
506 | 507 | if (pos == 0) /* mask out version number for the first entry */ |
|
507 | 508 | offset_flags &= 0xFFFF; |
|
508 | 509 | else { |
|
509 | 510 | uint32_t offset_high = getbe32(data); |
|
510 | 511 | offset_flags |= ((uint64_t)offset_high) << 32; |
|
511 | 512 | } |
|
512 | 513 | |
|
513 | 514 | comp_len = getbe32(data + 8); |
|
514 | 515 | uncomp_len = getbe32(data + 12); |
|
515 | 516 | base_rev = getbe32(data + 16); |
|
516 | 517 | link_rev = getbe32(data + 20); |
|
517 | 518 | parent_1 = getbe32(data + 24); |
|
518 | 519 | parent_2 = getbe32(data + 28); |
|
519 | 520 | c_node_id = data + 32; |
|
520 | 521 | |
|
521 | 522 | entry = Py_BuildValue(tuple_format, offset_flags, comp_len, |
|
522 | 523 | uncomp_len, base_rev, link_rev, |
|
523 | 524 | parent_1, parent_2, c_node_id, 20); |
|
524 | 525 | |
|
525 | 526 | if (entry) |
|
526 | 527 | PyObject_GC_UnTrack(entry); |
|
527 | 528 | |
|
528 | 529 | self->cache[pos] = entry; |
|
529 | 530 | Py_INCREF(entry); |
|
530 | 531 | |
|
531 | 532 | return entry; |
|
532 | 533 | } |
|
533 | 534 | |
|
534 | 535 | /* |
|
535 | 536 | * Return the 20-byte SHA of the node corresponding to the given rev. |
|
536 | 537 | */ |
|
537 | 538 | static const char *index_node(indexObject *self, Py_ssize_t pos) |
|
538 | 539 | { |
|
539 | 540 | Py_ssize_t length = index_length(self); |
|
540 | 541 | const char *data; |
|
541 | 542 | |
|
542 | 543 | if (pos == length - 1 || pos == INT_MAX) |
|
543 | 544 | return nullid; |
|
544 | 545 | |
|
545 | 546 | if (pos >= length) |
|
546 | 547 | return NULL; |
|
547 | 548 | |
|
548 | 549 | if (pos >= self->length - 1) { |
|
549 | 550 | PyObject *tuple, *str; |
|
550 | 551 | tuple = PyList_GET_ITEM(self->added, pos - self->length + 1); |
|
551 | 552 | str = PyTuple_GetItem(tuple, 7); |
|
552 | 553 | return str ? PyString_AS_STRING(str) : NULL; |
|
553 | 554 | } |
|
554 | 555 | |
|
555 | 556 | data = index_deref(self, pos); |
|
556 | 557 | return data ? data + 32 : NULL; |
|
557 | 558 | } |
|
558 | 559 | |
|
559 | 560 | static int nt_insert(indexObject *self, const char *node, int rev); |
|
560 | 561 | |
|
561 | 562 | static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen) |
|
562 | 563 | { |
|
563 | 564 | if (PyString_AsStringAndSize(obj, node, nodelen) == -1) |
|
564 | 565 | return -1; |
|
565 | 566 | if (*nodelen == 20) |
|
566 | 567 | return 0; |
|
567 | 568 | PyErr_SetString(PyExc_ValueError, "20-byte hash required"); |
|
568 | 569 | return -1; |
|
569 | 570 | } |
|
570 | 571 | |
|
571 | 572 | static PyObject *index_insert(indexObject *self, PyObject *args) |
|
572 | 573 | { |
|
573 | 574 | PyObject *obj; |
|
574 | 575 | char *node; |
|
575 | 576 | long offset; |
|
576 | 577 | Py_ssize_t len, nodelen; |
|
577 | 578 | |
|
578 | 579 | if (!PyArg_ParseTuple(args, "lO", &offset, &obj)) |
|
579 | 580 | return NULL; |
|
580 | 581 | |
|
581 | 582 | if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) { |
|
582 | 583 | PyErr_SetString(PyExc_TypeError, "8-tuple required"); |
|
583 | 584 | return NULL; |
|
584 | 585 | } |
|
585 | 586 | |
|
586 | 587 | if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1) |
|
587 | 588 | return NULL; |
|
588 | 589 | |
|
589 | 590 | len = index_length(self); |
|
590 | 591 | |
|
591 | 592 | if (offset < 0) |
|
592 | 593 | offset += len; |
|
593 | 594 | |
|
594 | 595 | if (offset != len - 1) { |
|
595 | 596 | PyErr_SetString(PyExc_IndexError, |
|
596 | 597 | "insert only supported at index -1"); |
|
597 | 598 | return NULL; |
|
598 | 599 | } |
|
599 | 600 | |
|
600 | 601 | if (offset > INT_MAX) { |
|
601 | 602 | PyErr_SetString(PyExc_ValueError, |
|
602 | 603 | "currently only 2**31 revs supported"); |
|
603 | 604 | return NULL; |
|
604 | 605 | } |
|
605 | 606 | |
|
606 | 607 | if (self->added == NULL) { |
|
607 | 608 | self->added = PyList_New(0); |
|
608 | 609 | if (self->added == NULL) |
|
609 | 610 | return NULL; |
|
610 | 611 | } |
|
611 | 612 | |
|
612 | 613 | if (PyList_Append(self->added, obj) == -1) |
|
613 | 614 | return NULL; |
|
614 | 615 | |
|
615 | 616 | if (self->nt) |
|
616 | 617 | nt_insert(self, node, (int)offset); |
|
617 | 618 | |
|
618 | 619 | Py_CLEAR(self->headrevs); |
|
619 | 620 | Py_RETURN_NONE; |
|
620 | 621 | } |
|
621 | 622 | |
|
622 | 623 | static void _index_clearcaches(indexObject *self) |
|
623 | 624 | { |
|
624 | 625 | if (self->cache) { |
|
625 | 626 | Py_ssize_t i; |
|
626 | 627 | |
|
627 | 628 | for (i = 0; i < self->raw_length; i++) |
|
628 | 629 | Py_CLEAR(self->cache[i]); |
|
629 | 630 | free(self->cache); |
|
630 | 631 | self->cache = NULL; |
|
631 | 632 | } |
|
632 | 633 | if (self->offsets) { |
|
633 | 634 | free(self->offsets); |
|
634 | 635 | self->offsets = NULL; |
|
635 | 636 | } |
|
636 | 637 | if (self->nt) { |
|
637 | 638 | free(self->nt); |
|
638 | 639 | self->nt = NULL; |
|
639 | 640 | } |
|
640 | 641 | Py_CLEAR(self->headrevs); |
|
641 | 642 | } |
|
642 | 643 | |
|
643 | 644 | static PyObject *index_clearcaches(indexObject *self) |
|
644 | 645 | { |
|
645 | 646 | _index_clearcaches(self); |
|
646 | 647 | self->ntlength = self->ntcapacity = 0; |
|
647 | 648 | self->ntdepth = self->ntsplits = 0; |
|
648 | 649 | self->ntrev = -1; |
|
649 | 650 | self->ntlookups = self->ntmisses = 0; |
|
650 | 651 | Py_RETURN_NONE; |
|
651 | 652 | } |
|
652 | 653 | |
|
653 | 654 | static PyObject *index_stats(indexObject *self) |
|
654 | 655 | { |
|
655 | 656 | PyObject *obj = PyDict_New(); |
|
656 | 657 | |
|
657 | 658 | if (obj == NULL) |
|
658 | 659 | return NULL; |
|
659 | 660 | |
|
660 | 661 | #define istat(__n, __d) \ |
|
661 | 662 | if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \ |
|
662 | 663 | goto bail; |
|
663 | 664 | |
|
664 | 665 | if (self->added) { |
|
665 | 666 | Py_ssize_t len = PyList_GET_SIZE(self->added); |
|
666 | 667 | if (PyDict_SetItemString(obj, "index entries added", |
|
667 | 668 | PyInt_FromSsize_t(len)) == -1) |
|
668 | 669 | goto bail; |
|
669 | 670 | } |
|
670 | 671 | |
|
671 | 672 | if (self->raw_length != self->length - 1) |
|
672 | 673 | istat(raw_length, "revs on disk"); |
|
673 | 674 | istat(length, "revs in memory"); |
|
674 | 675 | istat(ntcapacity, "node trie capacity"); |
|
675 | 676 | istat(ntdepth, "node trie depth"); |
|
676 | 677 | istat(ntlength, "node trie count"); |
|
677 | 678 | istat(ntlookups, "node trie lookups"); |
|
678 | 679 | istat(ntmisses, "node trie misses"); |
|
679 | 680 | istat(ntrev, "node trie last rev scanned"); |
|
680 | 681 | istat(ntsplits, "node trie splits"); |
|
681 | 682 | |
|
682 | 683 | #undef istat |
|
683 | 684 | |
|
684 | 685 | return obj; |
|
685 | 686 | |
|
686 | 687 | bail: |
|
687 | 688 | Py_XDECREF(obj); |
|
688 | 689 | return NULL; |
|
689 | 690 | } |
|
690 | 691 | |
|
691 | 692 | /* |
|
692 | 693 | * When we cache a list, we want to be sure the caller can't mutate |
|
693 | 694 | * the cached copy. |
|
694 | 695 | */ |
|
695 | 696 | static PyObject *list_copy(PyObject *list) |
|
696 | 697 | { |
|
697 | 698 | Py_ssize_t len = PyList_GET_SIZE(list); |
|
698 | 699 | PyObject *newlist = PyList_New(len); |
|
699 | 700 | Py_ssize_t i; |
|
700 | 701 | |
|
701 | 702 | if (newlist == NULL) |
|
702 | 703 | return NULL; |
|
703 | 704 | |
|
704 | 705 | for (i = 0; i < len; i++) { |
|
705 | 706 | PyObject *obj = PyList_GET_ITEM(list, i); |
|
706 | 707 | Py_INCREF(obj); |
|
707 | 708 | PyList_SET_ITEM(newlist, i, obj); |
|
708 | 709 | } |
|
709 | 710 | |
|
710 | 711 | return newlist; |
|
711 | 712 | } |
|
712 | 713 | |
|
713 | 714 | static PyObject *index_headrevs(indexObject *self) |
|
714 | 715 | { |
|
715 | 716 | Py_ssize_t i, len, addlen; |
|
716 | 717 | char *nothead = NULL; |
|
717 | 718 | PyObject *heads; |
|
718 | 719 | |
|
719 | 720 | if (self->headrevs) |
|
720 | 721 | return list_copy(self->headrevs); |
|
721 | 722 | |
|
722 | 723 | len = index_length(self) - 1; |
|
723 | 724 | heads = PyList_New(0); |
|
724 | 725 | if (heads == NULL) |
|
725 | 726 | goto bail; |
|
726 | 727 | if (len == 0) { |
|
727 | 728 | PyObject *nullid = PyInt_FromLong(-1); |
|
728 | 729 | if (nullid == NULL || PyList_Append(heads, nullid) == -1) { |
|
729 | 730 | Py_XDECREF(nullid); |
|
730 | 731 | goto bail; |
|
731 | 732 | } |
|
732 | 733 | goto done; |
|
733 | 734 | } |
|
734 | 735 | |
|
735 | 736 | nothead = calloc(len, 1); |
|
736 | 737 | if (nothead == NULL) |
|
737 | 738 | goto bail; |
|
738 | 739 | |
|
739 | 740 | for (i = 0; i < self->raw_length; i++) { |
|
740 | 741 | const char *data = index_deref(self, i); |
|
741 | 742 | int parent_1 = getbe32(data + 24); |
|
742 | 743 | int parent_2 = getbe32(data + 28); |
|
743 | 744 | if (parent_1 >= 0) |
|
744 | 745 | nothead[parent_1] = 1; |
|
745 | 746 | if (parent_2 >= 0) |
|
746 | 747 | nothead[parent_2] = 1; |
|
747 | 748 | } |
|
748 | 749 | |
|
749 | 750 | addlen = self->added ? PyList_GET_SIZE(self->added) : 0; |
|
750 | 751 | |
|
751 | 752 | for (i = 0; i < addlen; i++) { |
|
752 | 753 | PyObject *rev = PyList_GET_ITEM(self->added, i); |
|
753 | 754 | PyObject *p1 = PyTuple_GET_ITEM(rev, 5); |
|
754 | 755 | PyObject *p2 = PyTuple_GET_ITEM(rev, 6); |
|
755 | 756 | long parent_1, parent_2; |
|
756 | 757 | |
|
757 | 758 | if (!PyInt_Check(p1) || !PyInt_Check(p2)) { |
|
758 | 759 | PyErr_SetString(PyExc_TypeError, |
|
759 | 760 | "revlog parents are invalid"); |
|
760 | 761 | goto bail; |
|
761 | 762 | } |
|
762 | 763 | parent_1 = PyInt_AS_LONG(p1); |
|
763 | 764 | parent_2 = PyInt_AS_LONG(p2); |
|
764 | 765 | if (parent_1 >= 0) |
|
765 | 766 | nothead[parent_1] = 1; |
|
766 | 767 | if (parent_2 >= 0) |
|
767 | 768 | nothead[parent_2] = 1; |
|
768 | 769 | } |
|
769 | 770 | |
|
770 | 771 | for (i = 0; i < len; i++) { |
|
771 | 772 | PyObject *head; |
|
772 | 773 | |
|
773 | 774 | if (nothead[i]) |
|
774 | 775 | continue; |
|
775 | 776 | head = PyInt_FromLong(i); |
|
776 | 777 | if (head == NULL || PyList_Append(heads, head) == -1) { |
|
777 | 778 | Py_XDECREF(head); |
|
778 | 779 | goto bail; |
|
779 | 780 | } |
|
780 | 781 | } |
|
781 | 782 | |
|
782 | 783 | done: |
|
783 | 784 | self->headrevs = heads; |
|
784 | 785 | free(nothead); |
|
785 | 786 | return list_copy(self->headrevs); |
|
786 | 787 | bail: |
|
787 | 788 | Py_XDECREF(heads); |
|
788 | 789 | free(nothead); |
|
789 | 790 | return NULL; |
|
790 | 791 | } |
|
791 | 792 | |
|
792 | 793 | static inline int nt_level(const char *node, Py_ssize_t level) |
|
793 | 794 | { |
|
794 | 795 | int v = node[level>>1]; |
|
795 | 796 | if (!(level & 1)) |
|
796 | 797 | v >>= 4; |
|
797 | 798 | return v & 0xf; |
|
798 | 799 | } |
|
799 | 800 | |
|
800 | 801 | /* |
|
801 | 802 | * Return values: |
|
802 | 803 | * |
|
803 | 804 | * -4: match is ambiguous (multiple candidates) |
|
804 | 805 | * -2: not found |
|
805 | 806 | * rest: valid rev |
|
806 | 807 | */ |
|
807 | 808 | static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen, |
|
808 | 809 | int hex) |
|
809 | 810 | { |
|
810 | 811 | int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level; |
|
811 | 812 | int level, maxlevel, off; |
|
812 | 813 | |
|
813 | 814 | if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0) |
|
814 | 815 | return -1; |
|
815 | 816 | |
|
816 | 817 | if (self->nt == NULL) |
|
817 | 818 | return -2; |
|
818 | 819 | |
|
819 | 820 | if (hex) |
|
820 | 821 | maxlevel = nodelen > 40 ? 40 : (int)nodelen; |
|
821 | 822 | else |
|
822 | 823 | maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2); |
|
823 | 824 | |
|
824 | 825 | for (level = off = 0; level < maxlevel; level++) { |
|
825 | 826 | int k = getnybble(node, level); |
|
826 | 827 | nodetree *n = &self->nt[off]; |
|
827 | 828 | int v = n->children[k]; |
|
828 | 829 | |
|
829 | 830 | if (v < 0) { |
|
830 | 831 | const char *n; |
|
831 | 832 | Py_ssize_t i; |
|
832 | 833 | |
|
833 | 834 | v = -v - 1; |
|
834 | 835 | n = index_node(self, v); |
|
835 | 836 | if (n == NULL) |
|
836 | 837 | return -2; |
|
837 | 838 | for (i = level; i < maxlevel; i++) |
|
838 | 839 | if (getnybble(node, i) != nt_level(n, i)) |
|
839 | 840 | return -2; |
|
840 | 841 | return v; |
|
841 | 842 | } |
|
842 | 843 | if (v == 0) |
|
843 | 844 | return -2; |
|
844 | 845 | off = v; |
|
845 | 846 | } |
|
846 | 847 | /* multiple matches against an ambiguous prefix */ |
|
847 | 848 | return -4; |
|
848 | 849 | } |
|
849 | 850 | |
|
850 | 851 | static int nt_new(indexObject *self) |
|
851 | 852 | { |
|
852 | 853 | if (self->ntlength == self->ntcapacity) { |
|
853 | 854 | self->ntcapacity *= 2; |
|
854 | 855 | self->nt = realloc(self->nt, |
|
855 | 856 | self->ntcapacity * sizeof(nodetree)); |
|
856 | 857 | if (self->nt == NULL) { |
|
857 | 858 | PyErr_SetString(PyExc_MemoryError, "out of memory"); |
|
858 | 859 | return -1; |
|
859 | 860 | } |
|
860 | 861 | memset(&self->nt[self->ntlength], 0, |
|
861 | 862 | sizeof(nodetree) * (self->ntcapacity - self->ntlength)); |
|
862 | 863 | } |
|
863 | 864 | return self->ntlength++; |
|
864 | 865 | } |
|
865 | 866 | |
|
866 | 867 | static int nt_insert(indexObject *self, const char *node, int rev) |
|
867 | 868 | { |
|
868 | 869 | int level = 0; |
|
869 | 870 | int off = 0; |
|
870 | 871 | |
|
871 | 872 | while (level < 40) { |
|
872 | 873 | int k = nt_level(node, level); |
|
873 | 874 | nodetree *n; |
|
874 | 875 | int v; |
|
875 | 876 | |
|
876 | 877 | n = &self->nt[off]; |
|
877 | 878 | v = n->children[k]; |
|
878 | 879 | |
|
879 | 880 | if (v == 0) { |
|
880 | 881 | n->children[k] = -rev - 1; |
|
881 | 882 | return 0; |
|
882 | 883 | } |
|
883 | 884 | if (v < 0) { |
|
884 | 885 | const char *oldnode = index_node(self, -v - 1); |
|
885 | 886 | int noff; |
|
886 | 887 | |
|
887 | 888 | if (!oldnode || !memcmp(oldnode, node, 20)) { |
|
888 | 889 | n->children[k] = -rev - 1; |
|
889 | 890 | return 0; |
|
890 | 891 | } |
|
891 | 892 | noff = nt_new(self); |
|
892 | 893 | if (noff == -1) |
|
893 | 894 | return -1; |
|
894 | 895 | /* self->nt may have been changed by realloc */ |
|
895 | 896 | self->nt[off].children[k] = noff; |
|
896 | 897 | off = noff; |
|
897 | 898 | n = &self->nt[off]; |
|
898 | 899 | n->children[nt_level(oldnode, ++level)] = v; |
|
899 | 900 | if (level > self->ntdepth) |
|
900 | 901 | self->ntdepth = level; |
|
901 | 902 | self->ntsplits += 1; |
|
902 | 903 | } else { |
|
903 | 904 | level += 1; |
|
904 | 905 | off = v; |
|
905 | 906 | } |
|
906 | 907 | } |
|
907 | 908 | |
|
908 | 909 | return -1; |
|
909 | 910 | } |
|
910 | 911 | |
|
911 | 912 | static int nt_init(indexObject *self) |
|
912 | 913 | { |
|
913 | 914 | if (self->nt == NULL) { |
|
914 | 915 | self->ntcapacity = self->raw_length < 4 |
|
915 | 916 | ? 4 : self->raw_length / 2; |
|
916 | 917 | self->nt = calloc(self->ntcapacity, sizeof(nodetree)); |
|
917 | 918 | if (self->nt == NULL) { |
|
918 | 919 | PyErr_NoMemory(); |
|
919 | 920 | return -1; |
|
920 | 921 | } |
|
921 | 922 | self->ntlength = 1; |
|
922 | 923 | self->ntrev = (int)index_length(self) - 1; |
|
923 | 924 | self->ntlookups = 1; |
|
924 | 925 | self->ntmisses = 0; |
|
925 | 926 | if (nt_insert(self, nullid, INT_MAX) == -1) |
|
926 | 927 | return -1; |
|
927 | 928 | } |
|
928 | 929 | return 0; |
|
929 | 930 | } |
|
930 | 931 | |
|
931 | 932 | /* |
|
932 | 933 | * Return values: |
|
933 | 934 | * |
|
934 | 935 | * -3: error (exception set) |
|
935 | 936 | * -2: not found (no exception set) |
|
936 | 937 | * rest: valid rev |
|
937 | 938 | */ |
|
938 | 939 | static int index_find_node(indexObject *self, |
|
939 | 940 | const char *node, Py_ssize_t nodelen) |
|
940 | 941 | { |
|
941 | 942 | int rev; |
|
942 | 943 | |
|
943 | 944 | self->ntlookups++; |
|
944 | 945 | rev = nt_find(self, node, nodelen, 0); |
|
945 | 946 | if (rev >= -1) |
|
946 | 947 | return rev; |
|
947 | 948 | |
|
948 | 949 | if (nt_init(self) == -1) |
|
949 | 950 | return -3; |
|
950 | 951 | |
|
951 | 952 | /* |
|
952 | 953 | * For the first handful of lookups, we scan the entire index, |
|
953 | 954 | * and cache only the matching nodes. This optimizes for cases |
|
954 | 955 | * like "hg tip", where only a few nodes are accessed. |
|
955 | 956 | * |
|
956 | 957 | * After that, we cache every node we visit, using a single |
|
957 | 958 | * scan amortized over multiple lookups. This gives the best |
|
958 | 959 | * bulk performance, e.g. for "hg log". |
|
959 | 960 | */ |
|
960 | 961 | if (self->ntmisses++ < 4) { |
|
961 | 962 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
962 | 963 | const char *n = index_node(self, rev); |
|
963 | 964 | if (n == NULL) |
|
964 | 965 | return -2; |
|
965 | 966 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
966 | 967 | if (nt_insert(self, n, rev) == -1) |
|
967 | 968 | return -3; |
|
968 | 969 | break; |
|
969 | 970 | } |
|
970 | 971 | } |
|
971 | 972 | } else { |
|
972 | 973 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
973 | 974 | const char *n = index_node(self, rev); |
|
974 | 975 | if (n == NULL) { |
|
975 | 976 | self->ntrev = rev + 1; |
|
976 | 977 | return -2; |
|
977 | 978 | } |
|
978 | 979 | if (nt_insert(self, n, rev) == -1) { |
|
979 | 980 | self->ntrev = rev + 1; |
|
980 | 981 | return -3; |
|
981 | 982 | } |
|
982 | 983 | if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) { |
|
983 | 984 | break; |
|
984 | 985 | } |
|
985 | 986 | } |
|
986 | 987 | self->ntrev = rev; |
|
987 | 988 | } |
|
988 | 989 | |
|
989 | 990 | if (rev >= 0) |
|
990 | 991 | return rev; |
|
991 | 992 | return -2; |
|
992 | 993 | } |
|
993 | 994 | |
|
994 | 995 | static PyObject *raise_revlog_error(void) |
|
995 | 996 | { |
|
996 | 997 | static PyObject *errclass; |
|
997 | 998 | PyObject *mod = NULL, *errobj; |
|
998 | 999 | |
|
999 | 1000 | if (errclass == NULL) { |
|
1000 | 1001 | PyObject *dict; |
|
1001 | 1002 | |
|
1002 | 1003 | mod = PyImport_ImportModule("mercurial.error"); |
|
1003 | 1004 | if (mod == NULL) |
|
1004 | 1005 | goto classfail; |
|
1005 | 1006 | |
|
1006 | 1007 | dict = PyModule_GetDict(mod); |
|
1007 | 1008 | if (dict == NULL) |
|
1008 | 1009 | goto classfail; |
|
1009 | 1010 | |
|
1010 | 1011 | errclass = PyDict_GetItemString(dict, "RevlogError"); |
|
1011 | 1012 | if (errclass == NULL) { |
|
1012 | 1013 | PyErr_SetString(PyExc_SystemError, |
|
1013 | 1014 | "could not find RevlogError"); |
|
1014 | 1015 | goto classfail; |
|
1015 | 1016 | } |
|
1016 | 1017 | Py_INCREF(errclass); |
|
1017 | 1018 | } |
|
1018 | 1019 | |
|
1019 | 1020 | errobj = PyObject_CallFunction(errclass, NULL); |
|
1020 | 1021 | if (errobj == NULL) |
|
1021 | 1022 | return NULL; |
|
1022 | 1023 | PyErr_SetObject(errclass, errobj); |
|
1023 | 1024 | return errobj; |
|
1024 | 1025 | |
|
1025 | 1026 | classfail: |
|
1026 | 1027 | Py_XDECREF(mod); |
|
1027 | 1028 | return NULL; |
|
1028 | 1029 | } |
|
1029 | 1030 | |
|
1030 | 1031 | static PyObject *index_getitem(indexObject *self, PyObject *value) |
|
1031 | 1032 | { |
|
1032 | 1033 | char *node; |
|
1033 | 1034 | Py_ssize_t nodelen; |
|
1034 | 1035 | int rev; |
|
1035 | 1036 | |
|
1036 | 1037 | if (PyInt_Check(value)) |
|
1037 | 1038 | return index_get(self, PyInt_AS_LONG(value)); |
|
1038 | 1039 | |
|
1039 | 1040 | if (node_check(value, &node, &nodelen) == -1) |
|
1040 | 1041 | return NULL; |
|
1041 | 1042 | rev = index_find_node(self, node, nodelen); |
|
1042 | 1043 | if (rev >= -1) |
|
1043 | 1044 | return PyInt_FromLong(rev); |
|
1044 | 1045 | if (rev == -2) |
|
1045 | 1046 | raise_revlog_error(); |
|
1046 | 1047 | return NULL; |
|
1047 | 1048 | } |
|
1048 | 1049 | |
|
1049 | 1050 | static int nt_partialmatch(indexObject *self, const char *node, |
|
1050 | 1051 | Py_ssize_t nodelen) |
|
1051 | 1052 | { |
|
1052 | 1053 | int rev; |
|
1053 | 1054 | |
|
1054 | 1055 | if (nt_init(self) == -1) |
|
1055 | 1056 | return -3; |
|
1056 | 1057 | |
|
1057 | 1058 | if (self->ntrev > 0) { |
|
1058 | 1059 | /* ensure that the radix tree is fully populated */ |
|
1059 | 1060 | for (rev = self->ntrev - 1; rev >= 0; rev--) { |
|
1060 | 1061 | const char *n = index_node(self, rev); |
|
1061 | 1062 | if (n == NULL) |
|
1062 | 1063 | return -2; |
|
1063 | 1064 | if (nt_insert(self, n, rev) == -1) |
|
1064 | 1065 | return -3; |
|
1065 | 1066 | } |
|
1066 | 1067 | self->ntrev = rev; |
|
1067 | 1068 | } |
|
1068 | 1069 | |
|
1069 | 1070 | return nt_find(self, node, nodelen, 1); |
|
1070 | 1071 | } |
|
1071 | 1072 | |
|
1072 | 1073 | static PyObject *index_partialmatch(indexObject *self, PyObject *args) |
|
1073 | 1074 | { |
|
1074 | 1075 | const char *fullnode; |
|
1075 | 1076 | int nodelen; |
|
1076 | 1077 | char *node; |
|
1077 | 1078 | int rev, i; |
|
1078 | 1079 | |
|
1079 | 1080 | if (!PyArg_ParseTuple(args, "s#", &node, &nodelen)) |
|
1080 | 1081 | return NULL; |
|
1081 | 1082 | |
|
1082 | 1083 | if (nodelen < 4) { |
|
1083 | 1084 | PyErr_SetString(PyExc_ValueError, "key too short"); |
|
1084 | 1085 | return NULL; |
|
1085 | 1086 | } |
|
1086 | 1087 | |
|
1087 | 1088 | if (nodelen > 40) { |
|
1088 | 1089 | PyErr_SetString(PyExc_ValueError, "key too long"); |
|
1089 | 1090 | return NULL; |
|
1090 | 1091 | } |
|
1091 | 1092 | |
|
1092 | 1093 | for (i = 0; i < nodelen; i++) |
|
1093 | 1094 | hexdigit(node, i); |
|
1094 | 1095 | if (PyErr_Occurred()) { |
|
1095 | 1096 | /* input contains non-hex characters */ |
|
1096 | 1097 | PyErr_Clear(); |
|
1097 | 1098 | Py_RETURN_NONE; |
|
1098 | 1099 | } |
|
1099 | 1100 | |
|
1100 | 1101 | rev = nt_partialmatch(self, node, nodelen); |
|
1101 | 1102 | |
|
1102 | 1103 | switch (rev) { |
|
1103 | 1104 | case -4: |
|
1104 | 1105 | raise_revlog_error(); |
|
1105 | 1106 | case -3: |
|
1106 | 1107 | return NULL; |
|
1107 | 1108 | case -2: |
|
1108 | 1109 | Py_RETURN_NONE; |
|
1109 | 1110 | case -1: |
|
1110 | 1111 | return PyString_FromStringAndSize(nullid, 20); |
|
1111 | 1112 | } |
|
1112 | 1113 | |
|
1113 | 1114 | fullnode = index_node(self, rev); |
|
1114 | 1115 | if (fullnode == NULL) { |
|
1115 | 1116 | PyErr_Format(PyExc_IndexError, |
|
1116 | 1117 | "could not access rev %d", rev); |
|
1117 | 1118 | return NULL; |
|
1118 | 1119 | } |
|
1119 | 1120 | return PyString_FromStringAndSize(fullnode, 20); |
|
1120 | 1121 | } |
|
1121 | 1122 | |
|
1122 | 1123 | static PyObject *index_m_get(indexObject *self, PyObject *args) |
|
1123 | 1124 | { |
|
1124 | 1125 | Py_ssize_t nodelen; |
|
1125 | 1126 | PyObject *val; |
|
1126 | 1127 | char *node; |
|
1127 | 1128 | int rev; |
|
1128 | 1129 | |
|
1129 | 1130 | if (!PyArg_ParseTuple(args, "O", &val)) |
|
1130 | 1131 | return NULL; |
|
1131 | 1132 | if (node_check(val, &node, &nodelen) == -1) |
|
1132 | 1133 | return NULL; |
|
1133 | 1134 | rev = index_find_node(self, node, nodelen); |
|
1134 | 1135 | if (rev == -3) |
|
1135 | 1136 | return NULL; |
|
1136 | 1137 | if (rev == -2) |
|
1137 | 1138 | Py_RETURN_NONE; |
|
1138 | 1139 | return PyInt_FromLong(rev); |
|
1139 | 1140 | } |
|
1140 | 1141 | |
|
1141 | 1142 | static int index_contains(indexObject *self, PyObject *value) |
|
1142 | 1143 | { |
|
1143 | 1144 | char *node; |
|
1144 | 1145 | Py_ssize_t nodelen; |
|
1145 | 1146 | |
|
1146 | 1147 | if (PyInt_Check(value)) { |
|
1147 | 1148 | long rev = PyInt_AS_LONG(value); |
|
1148 | 1149 | return rev >= -1 && rev < index_length(self); |
|
1149 | 1150 | } |
|
1150 | 1151 | |
|
1151 | 1152 | if (node_check(value, &node, &nodelen) == -1) |
|
1152 | 1153 | return -1; |
|
1153 | 1154 | |
|
1154 | 1155 | switch (index_find_node(self, node, nodelen)) { |
|
1155 | 1156 | case -3: |
|
1156 | 1157 | return -1; |
|
1157 | 1158 | case -2: |
|
1158 | 1159 | return 0; |
|
1159 | 1160 | default: |
|
1160 | 1161 | return 1; |
|
1161 | 1162 | } |
|
1162 | 1163 | } |
|
1163 | 1164 | |
|
1164 | 1165 | /* |
|
1165 | 1166 | * Invalidate any trie entries introduced by added revs. |
|
1166 | 1167 | */ |
|
1167 | 1168 | static void nt_invalidate_added(indexObject *self, Py_ssize_t start) |
|
1168 | 1169 | { |
|
1169 | 1170 | Py_ssize_t i, len = PyList_GET_SIZE(self->added); |
|
1170 | 1171 | |
|
1171 | 1172 | for (i = start; i < len; i++) { |
|
1172 | 1173 | PyObject *tuple = PyList_GET_ITEM(self->added, i); |
|
1173 | 1174 | PyObject *node = PyTuple_GET_ITEM(tuple, 7); |
|
1174 | 1175 | |
|
1175 | 1176 | nt_insert(self, PyString_AS_STRING(node), -1); |
|
1176 | 1177 | } |
|
1177 | 1178 | |
|
1178 | 1179 | if (start == 0) |
|
1179 | 1180 | Py_CLEAR(self->added); |
|
1180 | 1181 | } |
|
1181 | 1182 | |
|
1182 | 1183 | /* |
|
1183 | 1184 | * Delete a numeric range of revs, which must be at the end of the |
|
1184 | 1185 | * range, but exclude the sentinel nullid entry. |
|
1185 | 1186 | */ |
|
1186 | 1187 | static int index_slice_del(indexObject *self, PyObject *item) |
|
1187 | 1188 | { |
|
1188 | 1189 | Py_ssize_t start, stop, step, slicelength; |
|
1189 | 1190 | Py_ssize_t length = index_length(self); |
|
1190 | 1191 | int ret = 0; |
|
1191 | 1192 | |
|
1192 | 1193 | if (PySlice_GetIndicesEx((PySliceObject*)item, length, |
|
1193 | 1194 | &start, &stop, &step, &slicelength) < 0) |
|
1194 | 1195 | return -1; |
|
1195 | 1196 | |
|
1196 | 1197 | if (slicelength <= 0) |
|
1197 | 1198 | return 0; |
|
1198 | 1199 | |
|
1199 | 1200 | if ((step < 0 && start < stop) || (step > 0 && start > stop)) |
|
1200 | 1201 | stop = start; |
|
1201 | 1202 | |
|
1202 | 1203 | if (step < 0) { |
|
1203 | 1204 | stop = start + 1; |
|
1204 | 1205 | start = stop + step*(slicelength - 1) - 1; |
|
1205 | 1206 | step = -step; |
|
1206 | 1207 | } |
|
1207 | 1208 | |
|
1208 | 1209 | if (step != 1) { |
|
1209 | 1210 | PyErr_SetString(PyExc_ValueError, |
|
1210 | 1211 | "revlog index delete requires step size of 1"); |
|
1211 | 1212 | return -1; |
|
1212 | 1213 | } |
|
1213 | 1214 | |
|
1214 | 1215 | if (stop != length - 1) { |
|
1215 | 1216 | PyErr_SetString(PyExc_IndexError, |
|
1216 | 1217 | "revlog index deletion indices are invalid"); |
|
1217 | 1218 | return -1; |
|
1218 | 1219 | } |
|
1219 | 1220 | |
|
1220 | 1221 | if (start < self->length - 1) { |
|
1221 | 1222 | if (self->nt) { |
|
1222 | 1223 | Py_ssize_t i; |
|
1223 | 1224 | |
|
1224 | 1225 | for (i = start + 1; i < self->length - 1; i++) { |
|
1225 | 1226 | const char *node = index_node(self, i); |
|
1226 | 1227 | |
|
1227 | 1228 | if (node) |
|
1228 | 1229 | nt_insert(self, node, -1); |
|
1229 | 1230 | } |
|
1230 | 1231 | if (self->added) |
|
1231 | 1232 | nt_invalidate_added(self, 0); |
|
1232 | 1233 | if (self->ntrev > start) |
|
1233 | 1234 | self->ntrev = (int)start; |
|
1234 | 1235 | } |
|
1235 | 1236 | self->length = start + 1; |
|
1236 | 1237 | if (start < self->raw_length) |
|
1237 | 1238 | self->raw_length = start; |
|
1238 | 1239 | goto done; |
|
1239 | 1240 | } |
|
1240 | 1241 | |
|
1241 | 1242 | if (self->nt) { |
|
1242 | 1243 | nt_invalidate_added(self, start - self->length + 1); |
|
1243 | 1244 | if (self->ntrev > start) |
|
1244 | 1245 | self->ntrev = (int)start; |
|
1245 | 1246 | } |
|
1246 | 1247 | if (self->added) |
|
1247 | 1248 | ret = PyList_SetSlice(self->added, start - self->length + 1, |
|
1248 | 1249 | PyList_GET_SIZE(self->added), NULL); |
|
1249 | 1250 | done: |
|
1250 | 1251 | Py_CLEAR(self->headrevs); |
|
1251 | 1252 | return ret; |
|
1252 | 1253 | } |
|
1253 | 1254 | |
|
1254 | 1255 | /* |
|
1255 | 1256 | * Supported ops: |
|
1256 | 1257 | * |
|
1257 | 1258 | * slice deletion |
|
1258 | 1259 | * string assignment (extend node->rev mapping) |
|
1259 | 1260 | * string deletion (shrink node->rev mapping) |
|
1260 | 1261 | */ |
|
1261 | 1262 | static int index_assign_subscript(indexObject *self, PyObject *item, |
|
1262 | 1263 | PyObject *value) |
|
1263 | 1264 | { |
|
1264 | 1265 | char *node; |
|
1265 | 1266 | Py_ssize_t nodelen; |
|
1266 | 1267 | long rev; |
|
1267 | 1268 | |
|
1268 | 1269 | if (PySlice_Check(item) && value == NULL) |
|
1269 | 1270 | return index_slice_del(self, item); |
|
1270 | 1271 | |
|
1271 | 1272 | if (node_check(item, &node, &nodelen) == -1) |
|
1272 | 1273 | return -1; |
|
1273 | 1274 | |
|
1274 | 1275 | if (value == NULL) |
|
1275 | 1276 | return self->nt ? nt_insert(self, node, -1) : 0; |
|
1276 | 1277 | rev = PyInt_AsLong(value); |
|
1277 | 1278 | if (rev > INT_MAX || rev < 0) { |
|
1278 | 1279 | if (!PyErr_Occurred()) |
|
1279 | 1280 | PyErr_SetString(PyExc_ValueError, "rev out of range"); |
|
1280 | 1281 | return -1; |
|
1281 | 1282 | } |
|
1282 | 1283 | return nt_insert(self, node, (int)rev); |
|
1283 | 1284 | } |
|
1284 | 1285 | |
|
1285 | 1286 | /* |
|
1286 | 1287 | * Find all RevlogNG entries in an index that has inline data. Update |
|
1287 | 1288 | * the optional "offsets" table with those entries. |
|
1288 | 1289 | */ |
|
1289 | 1290 | static long inline_scan(indexObject *self, const char **offsets) |
|
1290 | 1291 | { |
|
1291 | 1292 | const char *data = PyString_AS_STRING(self->data); |
|
1292 | 1293 | const char *end = data + PyString_GET_SIZE(self->data); |
|
1293 | 1294 | long incr = v1_hdrsize; |
|
1294 | 1295 | Py_ssize_t len = 0; |
|
1295 | 1296 | |
|
1296 | 1297 | while (data + v1_hdrsize <= end) { |
|
1297 | 1298 | uint32_t comp_len; |
|
1298 | 1299 | const char *old_data; |
|
1299 | 1300 | /* 3rd element of header is length of compressed inline data */ |
|
1300 | 1301 | comp_len = getbe32(data + 8); |
|
1301 | 1302 | incr = v1_hdrsize + comp_len; |
|
1302 | 1303 | if (incr < v1_hdrsize) |
|
1303 | 1304 | break; |
|
1304 | 1305 | if (offsets) |
|
1305 | 1306 | offsets[len] = data; |
|
1306 | 1307 | len++; |
|
1307 | 1308 | old_data = data; |
|
1308 | 1309 | data += incr; |
|
1309 | 1310 | if (data <= old_data) |
|
1310 | 1311 | break; |
|
1311 | 1312 | } |
|
1312 | 1313 | |
|
1313 | 1314 | if (data != end && data + v1_hdrsize != end) { |
|
1314 | 1315 | if (!PyErr_Occurred()) |
|
1315 | 1316 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
1316 | 1317 | return -1; |
|
1317 | 1318 | } |
|
1318 | 1319 | |
|
1319 | 1320 | return len; |
|
1320 | 1321 | } |
|
1321 | 1322 | |
|
1322 | 1323 | static int index_init(indexObject *self, PyObject *args) |
|
1323 | 1324 | { |
|
1324 | 1325 | PyObject *data_obj, *inlined_obj; |
|
1325 | 1326 | Py_ssize_t size; |
|
1326 | 1327 | |
|
1327 | 1328 | if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj)) |
|
1328 | 1329 | return -1; |
|
1329 | 1330 | if (!PyString_Check(data_obj)) { |
|
1330 | 1331 | PyErr_SetString(PyExc_TypeError, "data is not a string"); |
|
1331 | 1332 | return -1; |
|
1332 | 1333 | } |
|
1333 | 1334 | size = PyString_GET_SIZE(data_obj); |
|
1334 | 1335 | |
|
1335 | 1336 | self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj); |
|
1336 | 1337 | self->data = data_obj; |
|
1337 | 1338 | self->cache = NULL; |
|
1338 | 1339 | |
|
1339 | 1340 | self->added = NULL; |
|
1340 | 1341 | self->headrevs = NULL; |
|
1341 | 1342 | self->offsets = NULL; |
|
1342 | 1343 | self->nt = NULL; |
|
1343 | 1344 | self->ntlength = self->ntcapacity = 0; |
|
1344 | 1345 | self->ntdepth = self->ntsplits = 0; |
|
1345 | 1346 | self->ntlookups = self->ntmisses = 0; |
|
1346 | 1347 | self->ntrev = -1; |
|
1347 | 1348 | Py_INCREF(self->data); |
|
1348 | 1349 | |
|
1349 | 1350 | if (self->inlined) { |
|
1350 | 1351 | long len = inline_scan(self, NULL); |
|
1351 | 1352 | if (len == -1) |
|
1352 | 1353 | goto bail; |
|
1353 | 1354 | self->raw_length = len; |
|
1354 | 1355 | self->length = len + 1; |
|
1355 | 1356 | } else { |
|
1356 | 1357 | if (size % v1_hdrsize) { |
|
1357 | 1358 | PyErr_SetString(PyExc_ValueError, "corrupt index file"); |
|
1358 | 1359 | goto bail; |
|
1359 | 1360 | } |
|
1360 | 1361 | self->raw_length = size / v1_hdrsize; |
|
1361 | 1362 | self->length = self->raw_length + 1; |
|
1362 | 1363 | } |
|
1363 | 1364 | |
|
1364 | 1365 | return 0; |
|
1365 | 1366 | bail: |
|
1366 | 1367 | return -1; |
|
1367 | 1368 | } |
|
1368 | 1369 | |
|
1369 | 1370 | static PyObject *index_nodemap(indexObject *self) |
|
1370 | 1371 | { |
|
1371 | 1372 | Py_INCREF(self); |
|
1372 | 1373 | return (PyObject *)self; |
|
1373 | 1374 | } |
|
1374 | 1375 | |
|
1375 | 1376 | static void index_dealloc(indexObject *self) |
|
1376 | 1377 | { |
|
1377 | 1378 | _index_clearcaches(self); |
|
1378 | 1379 | Py_DECREF(self->data); |
|
1379 | 1380 | Py_XDECREF(self->added); |
|
1380 | 1381 | PyObject_Del(self); |
|
1381 | 1382 | } |
|
1382 | 1383 | |
|
1383 | 1384 | static PySequenceMethods index_sequence_methods = { |
|
1384 | 1385 | (lenfunc)index_length, /* sq_length */ |
|
1385 | 1386 | 0, /* sq_concat */ |
|
1386 | 1387 | 0, /* sq_repeat */ |
|
1387 | 1388 | (ssizeargfunc)index_get, /* sq_item */ |
|
1388 | 1389 | 0, /* sq_slice */ |
|
1389 | 1390 | 0, /* sq_ass_item */ |
|
1390 | 1391 | 0, /* sq_ass_slice */ |
|
1391 | 1392 | (objobjproc)index_contains, /* sq_contains */ |
|
1392 | 1393 | }; |
|
1393 | 1394 | |
|
1394 | 1395 | static PyMappingMethods index_mapping_methods = { |
|
1395 | 1396 | (lenfunc)index_length, /* mp_length */ |
|
1396 | 1397 | (binaryfunc)index_getitem, /* mp_subscript */ |
|
1397 | 1398 | (objobjargproc)index_assign_subscript, /* mp_ass_subscript */ |
|
1398 | 1399 | }; |
|
1399 | 1400 | |
|
1400 | 1401 | static PyMethodDef index_methods[] = { |
|
1401 | 1402 | {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS, |
|
1402 | 1403 | "clear the index caches"}, |
|
1403 | 1404 | {"get", (PyCFunction)index_m_get, METH_VARARGS, |
|
1404 | 1405 | "get an index entry"}, |
|
1405 | 1406 | {"headrevs", (PyCFunction)index_headrevs, METH_NOARGS, |
|
1406 | 1407 | "get head revisions"}, |
|
1407 | 1408 | {"insert", (PyCFunction)index_insert, METH_VARARGS, |
|
1408 | 1409 | "insert an index entry"}, |
|
1409 | 1410 | {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS, |
|
1410 | 1411 | "match a potentially ambiguous node ID"}, |
|
1411 | 1412 | {"stats", (PyCFunction)index_stats, METH_NOARGS, |
|
1412 | 1413 | "stats for the index"}, |
|
1413 | 1414 | {NULL} /* Sentinel */ |
|
1414 | 1415 | }; |
|
1415 | 1416 | |
|
1416 | 1417 | static PyGetSetDef index_getset[] = { |
|
1417 | 1418 | {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL}, |
|
1418 | 1419 | {NULL} /* Sentinel */ |
|
1419 | 1420 | }; |
|
1420 | 1421 | |
|
1421 | 1422 | static PyTypeObject indexType = { |
|
1422 | 1423 | PyObject_HEAD_INIT(NULL) |
|
1423 | 1424 | 0, /* ob_size */ |
|
1424 | 1425 | "parsers.index", /* tp_name */ |
|
1425 | 1426 | sizeof(indexObject), /* tp_basicsize */ |
|
1426 | 1427 | 0, /* tp_itemsize */ |
|
1427 | 1428 | (destructor)index_dealloc, /* tp_dealloc */ |
|
1428 | 1429 | 0, /* tp_print */ |
|
1429 | 1430 | 0, /* tp_getattr */ |
|
1430 | 1431 | 0, /* tp_setattr */ |
|
1431 | 1432 | 0, /* tp_compare */ |
|
1432 | 1433 | 0, /* tp_repr */ |
|
1433 | 1434 | 0, /* tp_as_number */ |
|
1434 | 1435 | &index_sequence_methods, /* tp_as_sequence */ |
|
1435 | 1436 | &index_mapping_methods, /* tp_as_mapping */ |
|
1436 | 1437 | 0, /* tp_hash */ |
|
1437 | 1438 | 0, /* tp_call */ |
|
1438 | 1439 | 0, /* tp_str */ |
|
1439 | 1440 | 0, /* tp_getattro */ |
|
1440 | 1441 | 0, /* tp_setattro */ |
|
1441 | 1442 | 0, /* tp_as_buffer */ |
|
1442 | 1443 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
|
1443 | 1444 | "revlog index", /* tp_doc */ |
|
1444 | 1445 | 0, /* tp_traverse */ |
|
1445 | 1446 | 0, /* tp_clear */ |
|
1446 | 1447 | 0, /* tp_richcompare */ |
|
1447 | 1448 | 0, /* tp_weaklistoffset */ |
|
1448 | 1449 | 0, /* tp_iter */ |
|
1449 | 1450 | 0, /* tp_iternext */ |
|
1450 | 1451 | index_methods, /* tp_methods */ |
|
1451 | 1452 | 0, /* tp_members */ |
|
1452 | 1453 | index_getset, /* tp_getset */ |
|
1453 | 1454 | 0, /* tp_base */ |
|
1454 | 1455 | 0, /* tp_dict */ |
|
1455 | 1456 | 0, /* tp_descr_get */ |
|
1456 | 1457 | 0, /* tp_descr_set */ |
|
1457 | 1458 | 0, /* tp_dictoffset */ |
|
1458 | 1459 | (initproc)index_init, /* tp_init */ |
|
1459 | 1460 | 0, /* tp_alloc */ |
|
1460 | 1461 | }; |
|
1461 | 1462 | |
|
1462 | 1463 | /* |
|
1463 | 1464 | * returns a tuple of the form (index, index, cache) with elements as |
|
1464 | 1465 | * follows: |
|
1465 | 1466 | * |
|
1466 | 1467 | * index: an index object that lazily parses RevlogNG records |
|
1467 | 1468 | * cache: if data is inlined, a tuple (index_file_content, 0), else None |
|
1468 | 1469 | * |
|
1469 | 1470 | * added complications are for backwards compatibility |
|
1470 | 1471 | */ |
|
1471 | 1472 | static PyObject *parse_index2(PyObject *self, PyObject *args) |
|
1472 | 1473 | { |
|
1473 | 1474 | PyObject *tuple = NULL, *cache = NULL; |
|
1474 | 1475 | indexObject *idx; |
|
1475 | 1476 | int ret; |
|
1476 | 1477 | |
|
1477 | 1478 | idx = PyObject_New(indexObject, &indexType); |
|
1478 | 1479 | if (idx == NULL) |
|
1479 | 1480 | goto bail; |
|
1480 | 1481 | |
|
1481 | 1482 | ret = index_init(idx, args); |
|
1482 | 1483 | if (ret == -1) |
|
1483 | 1484 | goto bail; |
|
1484 | 1485 | |
|
1485 | 1486 | if (idx->inlined) { |
|
1486 | 1487 | cache = Py_BuildValue("iO", 0, idx->data); |
|
1487 | 1488 | if (cache == NULL) |
|
1488 | 1489 | goto bail; |
|
1489 | 1490 | } else { |
|
1490 | 1491 | cache = Py_None; |
|
1491 | 1492 | Py_INCREF(cache); |
|
1492 | 1493 | } |
|
1493 | 1494 | |
|
1494 | 1495 | tuple = Py_BuildValue("NN", idx, cache); |
|
1495 | 1496 | if (!tuple) |
|
1496 | 1497 | goto bail; |
|
1497 | 1498 | return tuple; |
|
1498 | 1499 | |
|
1499 | 1500 | bail: |
|
1500 | 1501 | Py_XDECREF(idx); |
|
1501 | 1502 | Py_XDECREF(cache); |
|
1502 | 1503 | Py_XDECREF(tuple); |
|
1503 | 1504 | return NULL; |
|
1504 | 1505 | } |
|
1505 | 1506 | |
|
1506 | 1507 | static char parsers_doc[] = "Efficient content parsing."; |
|
1507 | 1508 | |
|
1508 | 1509 | static PyMethodDef methods[] = { |
|
1509 | 1510 | {"pack_dirstate", pack_dirstate, METH_VARARGS, "pack a dirstate\n"}, |
|
1510 | 1511 | {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"}, |
|
1511 | 1512 | {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"}, |
|
1512 | 1513 | {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"}, |
|
1513 | 1514 | {NULL, NULL} |
|
1514 | 1515 | }; |
|
1515 | 1516 | |
|
1516 | 1517 | static void module_init(PyObject *mod) |
|
1517 | 1518 | { |
|
1518 | 1519 | indexType.tp_new = PyType_GenericNew; |
|
1519 | 1520 | if (PyType_Ready(&indexType) < 0) |
|
1520 | 1521 | return; |
|
1521 | 1522 | Py_INCREF(&indexType); |
|
1522 | 1523 | |
|
1523 | 1524 | PyModule_AddObject(mod, "index", (PyObject *)&indexType); |
|
1524 | 1525 | |
|
1525 | 1526 | nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0, |
|
1526 | 1527 | -1, -1, -1, -1, nullid, 20); |
|
1527 | 1528 | if (nullentry) |
|
1528 | 1529 | PyObject_GC_UnTrack(nullentry); |
|
1529 | 1530 | |
|
1530 | 1531 | dirstate_unset = Py_BuildValue("ciii", 'n', 0, -1, -1); |
|
1531 | 1532 | } |
|
1532 | 1533 | |
|
1533 | 1534 | #ifdef IS_PY3K |
|
1534 | 1535 | static struct PyModuleDef parsers_module = { |
|
1535 | 1536 | PyModuleDef_HEAD_INIT, |
|
1536 | 1537 | "parsers", |
|
1537 | 1538 | parsers_doc, |
|
1538 | 1539 | -1, |
|
1539 | 1540 | methods |
|
1540 | 1541 | }; |
|
1541 | 1542 | |
|
1542 | 1543 | PyMODINIT_FUNC PyInit_parsers(void) |
|
1543 | 1544 | { |
|
1544 | 1545 | PyObject *mod = PyModule_Create(&parsers_module); |
|
1545 | 1546 | module_init(mod); |
|
1546 | 1547 | return mod; |
|
1547 | 1548 | } |
|
1548 | 1549 | #else |
|
1549 | 1550 | PyMODINIT_FUNC initparsers(void) |
|
1550 | 1551 | { |
|
1551 | 1552 | PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc); |
|
1552 | 1553 | module_init(mod); |
|
1553 | 1554 | } |
|
1554 | 1555 | #endif |
General Comments 0
You need to be logged in to leave comments.
Login now