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