##// END OF EJS Templates
parsers: use Py_CLEAR where appropriate
Bryan O'Sullivan -
r16732:277e2acb default
parent child Browse files
Show More
@@ -1,1293 +1,1287 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 /*
217 /*
218 * A base-16 trie for fast node->rev mapping.
218 * A base-16 trie for fast node->rev mapping.
219 *
219 *
220 * Positive value is index of the next node in the trie
220 * Positive value is index of the next node in the trie
221 * Negative value is a leaf: -(rev + 1)
221 * Negative value is a leaf: -(rev + 1)
222 * Zero is empty
222 * Zero is empty
223 */
223 */
224 typedef struct {
224 typedef struct {
225 int children[16];
225 int children[16];
226 } nodetree;
226 } nodetree;
227
227
228 /*
228 /*
229 * This class has two behaviours.
229 * This class has two behaviours.
230 *
230 *
231 * When used in a list-like way (with integer keys), we decode an
231 * When used in a list-like way (with integer keys), we decode an
232 * entry in a RevlogNG index file on demand. Our last entry is a
232 * entry in a RevlogNG index file on demand. Our last entry is a
233 * sentinel, always a nullid. We have limited support for
233 * sentinel, always a nullid. We have limited support for
234 * integer-keyed insert and delete, only at elements right before the
234 * integer-keyed insert and delete, only at elements right before the
235 * sentinel.
235 * sentinel.
236 *
236 *
237 * With string keys, we lazily perform a reverse mapping from node to
237 * With string keys, we lazily perform a reverse mapping from node to
238 * rev, using a base-16 trie.
238 * rev, using a base-16 trie.
239 */
239 */
240 typedef struct {
240 typedef struct {
241 PyObject_HEAD
241 PyObject_HEAD
242 /* Type-specific fields go here. */
242 /* Type-specific fields go here. */
243 PyObject *data; /* raw bytes of index */
243 PyObject *data; /* raw bytes of index */
244 PyObject **cache; /* cached tuples */
244 PyObject **cache; /* cached tuples */
245 const char **offsets; /* populated on demand */
245 const char **offsets; /* populated on demand */
246 Py_ssize_t raw_length; /* original number of elements */
246 Py_ssize_t raw_length; /* original number of elements */
247 Py_ssize_t length; /* current number of elements */
247 Py_ssize_t length; /* current number of elements */
248 PyObject *added; /* populated on demand */
248 PyObject *added; /* populated on demand */
249 nodetree *nt; /* base-16 trie */
249 nodetree *nt; /* base-16 trie */
250 int ntlength; /* # nodes in use */
250 int ntlength; /* # nodes in use */
251 int ntcapacity; /* # nodes allocated */
251 int ntcapacity; /* # nodes allocated */
252 int ntdepth; /* maximum depth of tree */
252 int ntdepth; /* maximum depth of tree */
253 int ntsplits; /* # splits performed */
253 int ntsplits; /* # splits performed */
254 int ntrev; /* last rev scanned */
254 int ntrev; /* last rev scanned */
255 int ntlookups; /* # lookups */
255 int ntlookups; /* # lookups */
256 int ntmisses; /* # lookups that miss the cache */
256 int ntmisses; /* # lookups that miss the cache */
257 int inlined;
257 int inlined;
258 } indexObject;
258 } indexObject;
259
259
260 static Py_ssize_t index_length(const indexObject *self)
260 static Py_ssize_t index_length(const indexObject *self)
261 {
261 {
262 if (self->added == NULL)
262 if (self->added == NULL)
263 return self->length;
263 return self->length;
264 return self->length + PyList_GET_SIZE(self->added);
264 return self->length + PyList_GET_SIZE(self->added);
265 }
265 }
266
266
267 static PyObject *nullentry;
267 static PyObject *nullentry;
268 static const char nullid[20];
268 static const char nullid[20];
269
269
270 static long inline_scan(indexObject *self, const char **offsets);
270 static long inline_scan(indexObject *self, const char **offsets);
271
271
272 #if LONG_MAX == 0x7fffffffL
272 #if LONG_MAX == 0x7fffffffL
273 static char *tuple_format = "Kiiiiiis#";
273 static char *tuple_format = "Kiiiiiis#";
274 #else
274 #else
275 static char *tuple_format = "kiiiiiis#";
275 static char *tuple_format = "kiiiiiis#";
276 #endif
276 #endif
277
277
278 /*
278 /*
279 * Return a pointer to the beginning of a RevlogNG record.
279 * Return a pointer to the beginning of a RevlogNG record.
280 */
280 */
281 static const char *index_deref(indexObject *self, Py_ssize_t pos)
281 static const char *index_deref(indexObject *self, Py_ssize_t pos)
282 {
282 {
283 if (self->inlined && pos > 0) {
283 if (self->inlined && pos > 0) {
284 if (self->offsets == NULL) {
284 if (self->offsets == NULL) {
285 self->offsets = malloc(self->raw_length *
285 self->offsets = malloc(self->raw_length *
286 sizeof(*self->offsets));
286 sizeof(*self->offsets));
287 if (self->offsets == NULL)
287 if (self->offsets == NULL)
288 return (const char *)PyErr_NoMemory();
288 return (const char *)PyErr_NoMemory();
289 inline_scan(self, self->offsets);
289 inline_scan(self, self->offsets);
290 }
290 }
291 return self->offsets[pos];
291 return self->offsets[pos];
292 }
292 }
293
293
294 return PyString_AS_STRING(self->data) + pos * 64;
294 return PyString_AS_STRING(self->data) + pos * 64;
295 }
295 }
296
296
297 /*
297 /*
298 * RevlogNG format (all in big endian, data may be inlined):
298 * RevlogNG format (all in big endian, data may be inlined):
299 * 6 bytes: offset
299 * 6 bytes: offset
300 * 2 bytes: flags
300 * 2 bytes: flags
301 * 4 bytes: compressed length
301 * 4 bytes: compressed length
302 * 4 bytes: uncompressed length
302 * 4 bytes: uncompressed length
303 * 4 bytes: base revision
303 * 4 bytes: base revision
304 * 4 bytes: link revision
304 * 4 bytes: link revision
305 * 4 bytes: parent 1 revision
305 * 4 bytes: parent 1 revision
306 * 4 bytes: parent 2 revision
306 * 4 bytes: parent 2 revision
307 * 32 bytes: nodeid (only 20 bytes used)
307 * 32 bytes: nodeid (only 20 bytes used)
308 */
308 */
309 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
309 static PyObject *index_get(indexObject *self, Py_ssize_t pos)
310 {
310 {
311 uint64_t offset_flags;
311 uint64_t offset_flags;
312 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
312 int comp_len, uncomp_len, base_rev, link_rev, parent_1, parent_2;
313 const char *c_node_id;
313 const char *c_node_id;
314 const char *data;
314 const char *data;
315 Py_ssize_t length = index_length(self);
315 Py_ssize_t length = index_length(self);
316 PyObject *entry;
316 PyObject *entry;
317
317
318 if (pos < 0)
318 if (pos < 0)
319 pos += length;
319 pos += length;
320
320
321 if (pos < 0 || pos >= length) {
321 if (pos < 0 || pos >= length) {
322 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
322 PyErr_SetString(PyExc_IndexError, "revlog index out of range");
323 return NULL;
323 return NULL;
324 }
324 }
325
325
326 if (pos == length - 1) {
326 if (pos == length - 1) {
327 Py_INCREF(nullentry);
327 Py_INCREF(nullentry);
328 return nullentry;
328 return nullentry;
329 }
329 }
330
330
331 if (pos >= self->length - 1) {
331 if (pos >= self->length - 1) {
332 PyObject *obj;
332 PyObject *obj;
333 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
333 obj = PyList_GET_ITEM(self->added, pos - self->length + 1);
334 Py_INCREF(obj);
334 Py_INCREF(obj);
335 return obj;
335 return obj;
336 }
336 }
337
337
338 if (self->cache) {
338 if (self->cache) {
339 if (self->cache[pos]) {
339 if (self->cache[pos]) {
340 Py_INCREF(self->cache[pos]);
340 Py_INCREF(self->cache[pos]);
341 return self->cache[pos];
341 return self->cache[pos];
342 }
342 }
343 } else {
343 } else {
344 self->cache = calloc(self->raw_length, sizeof(PyObject *));
344 self->cache = calloc(self->raw_length, sizeof(PyObject *));
345 if (self->cache == NULL)
345 if (self->cache == NULL)
346 return PyErr_NoMemory();
346 return PyErr_NoMemory();
347 }
347 }
348
348
349 data = index_deref(self, pos);
349 data = index_deref(self, pos);
350 if (data == NULL)
350 if (data == NULL)
351 return NULL;
351 return NULL;
352
352
353 offset_flags = getbe32(data + 4);
353 offset_flags = getbe32(data + 4);
354 if (pos == 0) /* mask out version number for the first entry */
354 if (pos == 0) /* mask out version number for the first entry */
355 offset_flags &= 0xFFFF;
355 offset_flags &= 0xFFFF;
356 else {
356 else {
357 uint32_t offset_high = getbe32(data);
357 uint32_t offset_high = getbe32(data);
358 offset_flags |= ((uint64_t)offset_high) << 32;
358 offset_flags |= ((uint64_t)offset_high) << 32;
359 }
359 }
360
360
361 comp_len = getbe32(data + 8);
361 comp_len = getbe32(data + 8);
362 uncomp_len = getbe32(data + 12);
362 uncomp_len = getbe32(data + 12);
363 base_rev = getbe32(data + 16);
363 base_rev = getbe32(data + 16);
364 link_rev = getbe32(data + 20);
364 link_rev = getbe32(data + 20);
365 parent_1 = getbe32(data + 24);
365 parent_1 = getbe32(data + 24);
366 parent_2 = getbe32(data + 28);
366 parent_2 = getbe32(data + 28);
367 c_node_id = data + 32;
367 c_node_id = data + 32;
368
368
369 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
369 entry = Py_BuildValue(tuple_format, offset_flags, comp_len,
370 uncomp_len, base_rev, link_rev,
370 uncomp_len, base_rev, link_rev,
371 parent_1, parent_2, c_node_id, 20);
371 parent_1, parent_2, c_node_id, 20);
372
372
373 if (entry)
373 if (entry)
374 PyObject_GC_UnTrack(entry);
374 PyObject_GC_UnTrack(entry);
375
375
376 self->cache[pos] = entry;
376 self->cache[pos] = entry;
377 Py_INCREF(entry);
377 Py_INCREF(entry);
378
378
379 return entry;
379 return entry;
380 }
380 }
381
381
382 /*
382 /*
383 * Return the 20-byte SHA of the node corresponding to the given rev.
383 * Return the 20-byte SHA of the node corresponding to the given rev.
384 */
384 */
385 static const char *index_node(indexObject *self, Py_ssize_t pos)
385 static const char *index_node(indexObject *self, Py_ssize_t pos)
386 {
386 {
387 Py_ssize_t length = index_length(self);
387 Py_ssize_t length = index_length(self);
388 const char *data;
388 const char *data;
389
389
390 if (pos == length - 1 || pos == INT_MAX)
390 if (pos == length - 1 || pos == INT_MAX)
391 return nullid;
391 return nullid;
392
392
393 if (pos >= length)
393 if (pos >= length)
394 return NULL;
394 return NULL;
395
395
396 if (pos >= self->length - 1) {
396 if (pos >= self->length - 1) {
397 PyObject *tuple, *str;
397 PyObject *tuple, *str;
398 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
398 tuple = PyList_GET_ITEM(self->added, pos - self->length + 1);
399 str = PyTuple_GetItem(tuple, 7);
399 str = PyTuple_GetItem(tuple, 7);
400 return str ? PyString_AS_STRING(str) : NULL;
400 return str ? PyString_AS_STRING(str) : NULL;
401 }
401 }
402
402
403 data = index_deref(self, pos);
403 data = index_deref(self, pos);
404 return data ? data + 32 : NULL;
404 return data ? data + 32 : NULL;
405 }
405 }
406
406
407 static int nt_insert(indexObject *self, const char *node, int rev);
407 static int nt_insert(indexObject *self, const char *node, int rev);
408
408
409 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
409 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
410 {
410 {
411 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
411 if (PyString_AsStringAndSize(obj, node, nodelen) == -1)
412 return -1;
412 return -1;
413 if (*nodelen == 20)
413 if (*nodelen == 20)
414 return 0;
414 return 0;
415 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
415 PyErr_SetString(PyExc_ValueError, "20-byte hash required");
416 return -1;
416 return -1;
417 }
417 }
418
418
419 static PyObject *index_insert(indexObject *self, PyObject *args)
419 static PyObject *index_insert(indexObject *self, PyObject *args)
420 {
420 {
421 PyObject *obj;
421 PyObject *obj;
422 char *node;
422 char *node;
423 long offset;
423 long offset;
424 Py_ssize_t len, nodelen;
424 Py_ssize_t len, nodelen;
425
425
426 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
426 if (!PyArg_ParseTuple(args, "lO", &offset, &obj))
427 return NULL;
427 return NULL;
428
428
429 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
429 if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
430 PyErr_SetString(PyExc_TypeError, "8-tuple required");
430 PyErr_SetString(PyExc_TypeError, "8-tuple required");
431 return NULL;
431 return NULL;
432 }
432 }
433
433
434 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
434 if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
435 return NULL;
435 return NULL;
436
436
437 len = index_length(self);
437 len = index_length(self);
438
438
439 if (offset < 0)
439 if (offset < 0)
440 offset += len;
440 offset += len;
441
441
442 if (offset != len - 1) {
442 if (offset != len - 1) {
443 PyErr_SetString(PyExc_IndexError,
443 PyErr_SetString(PyExc_IndexError,
444 "insert only supported at index -1");
444 "insert only supported at index -1");
445 return NULL;
445 return NULL;
446 }
446 }
447
447
448 if (offset > INT_MAX) {
448 if (offset > INT_MAX) {
449 PyErr_SetString(PyExc_ValueError,
449 PyErr_SetString(PyExc_ValueError,
450 "currently only 2**31 revs supported");
450 "currently only 2**31 revs supported");
451 return NULL;
451 return NULL;
452 }
452 }
453
453
454 if (self->added == NULL) {
454 if (self->added == NULL) {
455 self->added = PyList_New(0);
455 self->added = PyList_New(0);
456 if (self->added == NULL)
456 if (self->added == NULL)
457 return NULL;
457 return NULL;
458 }
458 }
459
459
460 if (PyList_Append(self->added, obj) == -1)
460 if (PyList_Append(self->added, obj) == -1)
461 return NULL;
461 return NULL;
462
462
463 if (self->nt)
463 if (self->nt)
464 nt_insert(self, node, (int)offset);
464 nt_insert(self, node, (int)offset);
465
465
466 Py_RETURN_NONE;
466 Py_RETURN_NONE;
467 }
467 }
468
468
469 static void _index_clearcaches(indexObject *self)
469 static void _index_clearcaches(indexObject *self)
470 {
470 {
471 if (self->cache) {
471 if (self->cache) {
472 Py_ssize_t i;
472 Py_ssize_t i;
473
473
474 for (i = 0; i < self->raw_length; i++) {
474 for (i = 0; i < self->raw_length; i++)
475 if (self->cache[i]) {
475 Py_CLEAR(self->cache[i]);
476 Py_DECREF(self->cache[i]);
477 self->cache[i] = NULL;
478 }
479 }
480 free(self->cache);
476 free(self->cache);
481 self->cache = NULL;
477 self->cache = NULL;
482 }
478 }
483 if (self->offsets) {
479 if (self->offsets) {
484 free(self->offsets);
480 free(self->offsets);
485 self->offsets = NULL;
481 self->offsets = NULL;
486 }
482 }
487 if (self->nt) {
483 if (self->nt) {
488 free(self->nt);
484 free(self->nt);
489 self->nt = NULL;
485 self->nt = NULL;
490 }
486 }
491 }
487 }
492
488
493 static PyObject *index_clearcaches(indexObject *self)
489 static PyObject *index_clearcaches(indexObject *self)
494 {
490 {
495 _index_clearcaches(self);
491 _index_clearcaches(self);
496 self->ntlength = self->ntcapacity = 0;
492 self->ntlength = self->ntcapacity = 0;
497 self->ntdepth = self->ntsplits = 0;
493 self->ntdepth = self->ntsplits = 0;
498 self->ntrev = -1;
494 self->ntrev = -1;
499 self->ntlookups = self->ntmisses = 0;
495 self->ntlookups = self->ntmisses = 0;
500 Py_RETURN_NONE;
496 Py_RETURN_NONE;
501 }
497 }
502
498
503 static PyObject *index_stats(indexObject *self)
499 static PyObject *index_stats(indexObject *self)
504 {
500 {
505 PyObject *obj = PyDict_New();
501 PyObject *obj = PyDict_New();
506
502
507 if (obj == NULL)
503 if (obj == NULL)
508 return NULL;
504 return NULL;
509
505
510 #define istat(__n, __d) \
506 #define istat(__n, __d) \
511 if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \
507 if (PyDict_SetItemString(obj, __d, PyInt_FromSsize_t(self->__n)) == -1) \
512 goto bail;
508 goto bail;
513
509
514 if (self->added) {
510 if (self->added) {
515 Py_ssize_t len = PyList_GET_SIZE(self->added);
511 Py_ssize_t len = PyList_GET_SIZE(self->added);
516 if (PyDict_SetItemString(obj, "index entries added",
512 if (PyDict_SetItemString(obj, "index entries added",
517 PyInt_FromSsize_t(len)) == -1)
513 PyInt_FromSsize_t(len)) == -1)
518 goto bail;
514 goto bail;
519 }
515 }
520
516
521 if (self->raw_length != self->length - 1)
517 if (self->raw_length != self->length - 1)
522 istat(raw_length, "revs on disk");
518 istat(raw_length, "revs on disk");
523 istat(length, "revs in memory");
519 istat(length, "revs in memory");
524 istat(ntcapacity, "node trie capacity");
520 istat(ntcapacity, "node trie capacity");
525 istat(ntdepth, "node trie depth");
521 istat(ntdepth, "node trie depth");
526 istat(ntlength, "node trie count");
522 istat(ntlength, "node trie count");
527 istat(ntlookups, "node trie lookups");
523 istat(ntlookups, "node trie lookups");
528 istat(ntmisses, "node trie misses");
524 istat(ntmisses, "node trie misses");
529 istat(ntrev, "node trie last rev scanned");
525 istat(ntrev, "node trie last rev scanned");
530 istat(ntsplits, "node trie splits");
526 istat(ntsplits, "node trie splits");
531
527
532 #undef istat
528 #undef istat
533
529
534 return obj;
530 return obj;
535
531
536 bail:
532 bail:
537 Py_XDECREF(obj);
533 Py_XDECREF(obj);
538 return NULL;
534 return NULL;
539 }
535 }
540
536
541 static inline int nt_level(const char *node, Py_ssize_t level)
537 static inline int nt_level(const char *node, Py_ssize_t level)
542 {
538 {
543 int v = node[level>>1];
539 int v = node[level>>1];
544 if (!(level & 1))
540 if (!(level & 1))
545 v >>= 4;
541 v >>= 4;
546 return v & 0xf;
542 return v & 0xf;
547 }
543 }
548
544
549 /*
545 /*
550 * Return values:
546 * Return values:
551 *
547 *
552 * -4: match is ambiguous (multiple candidates)
548 * -4: match is ambiguous (multiple candidates)
553 * -2: not found
549 * -2: not found
554 * rest: valid rev
550 * rest: valid rev
555 */
551 */
556 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
552 static int nt_find(indexObject *self, const char *node, Py_ssize_t nodelen,
557 int hex)
553 int hex)
558 {
554 {
559 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
555 int (*getnybble)(const char *, Py_ssize_t) = hex ? hexdigit : nt_level;
560 int level, maxlevel, off;
556 int level, maxlevel, off;
561
557
562 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
558 if (nodelen == 20 && node[0] == '\0' && memcmp(node, nullid, 20) == 0)
563 return -1;
559 return -1;
564
560
565 if (self->nt == NULL)
561 if (self->nt == NULL)
566 return -2;
562 return -2;
567
563
568 if (hex)
564 if (hex)
569 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
565 maxlevel = nodelen > 40 ? 40 : (int)nodelen;
570 else
566 else
571 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
567 maxlevel = nodelen > 20 ? 40 : ((int)nodelen * 2);
572
568
573 for (level = off = 0; level < maxlevel; level++) {
569 for (level = off = 0; level < maxlevel; level++) {
574 int k = getnybble(node, level);
570 int k = getnybble(node, level);
575 nodetree *n = &self->nt[off];
571 nodetree *n = &self->nt[off];
576 int v = n->children[k];
572 int v = n->children[k];
577
573
578 if (v < 0) {
574 if (v < 0) {
579 const char *n;
575 const char *n;
580 Py_ssize_t i;
576 Py_ssize_t i;
581
577
582 v = -v - 1;
578 v = -v - 1;
583 n = index_node(self, v);
579 n = index_node(self, v);
584 if (n == NULL)
580 if (n == NULL)
585 return -2;
581 return -2;
586 for (i = level; i < maxlevel; i++)
582 for (i = level; i < maxlevel; i++)
587 if (getnybble(node, i) != nt_level(n, i))
583 if (getnybble(node, i) != nt_level(n, i))
588 return -2;
584 return -2;
589 return v;
585 return v;
590 }
586 }
591 if (v == 0)
587 if (v == 0)
592 return -2;
588 return -2;
593 off = v;
589 off = v;
594 }
590 }
595 /* multiple matches against an ambiguous prefix */
591 /* multiple matches against an ambiguous prefix */
596 return -4;
592 return -4;
597 }
593 }
598
594
599 static int nt_new(indexObject *self)
595 static int nt_new(indexObject *self)
600 {
596 {
601 if (self->ntlength == self->ntcapacity) {
597 if (self->ntlength == self->ntcapacity) {
602 self->ntcapacity *= 2;
598 self->ntcapacity *= 2;
603 self->nt = realloc(self->nt,
599 self->nt = realloc(self->nt,
604 self->ntcapacity * sizeof(nodetree));
600 self->ntcapacity * sizeof(nodetree));
605 if (self->nt == NULL) {
601 if (self->nt == NULL) {
606 PyErr_SetString(PyExc_MemoryError, "out of memory");
602 PyErr_SetString(PyExc_MemoryError, "out of memory");
607 return -1;
603 return -1;
608 }
604 }
609 memset(&self->nt[self->ntlength], 0,
605 memset(&self->nt[self->ntlength], 0,
610 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
606 sizeof(nodetree) * (self->ntcapacity - self->ntlength));
611 }
607 }
612 return self->ntlength++;
608 return self->ntlength++;
613 }
609 }
614
610
615 static int nt_insert(indexObject *self, const char *node, int rev)
611 static int nt_insert(indexObject *self, const char *node, int rev)
616 {
612 {
617 int level = 0;
613 int level = 0;
618 int off = 0;
614 int off = 0;
619
615
620 while (level < 40) {
616 while (level < 40) {
621 int k = nt_level(node, level);
617 int k = nt_level(node, level);
622 nodetree *n;
618 nodetree *n;
623 int v;
619 int v;
624
620
625 n = &self->nt[off];
621 n = &self->nt[off];
626 v = n->children[k];
622 v = n->children[k];
627
623
628 if (v == 0) {
624 if (v == 0) {
629 n->children[k] = -rev - 1;
625 n->children[k] = -rev - 1;
630 return 0;
626 return 0;
631 }
627 }
632 if (v < 0) {
628 if (v < 0) {
633 const char *oldnode = index_node(self, -v - 1);
629 const char *oldnode = index_node(self, -v - 1);
634 int noff;
630 int noff;
635
631
636 if (!oldnode || !memcmp(oldnode, node, 20)) {
632 if (!oldnode || !memcmp(oldnode, node, 20)) {
637 n->children[k] = -rev - 1;
633 n->children[k] = -rev - 1;
638 return 0;
634 return 0;
639 }
635 }
640 noff = nt_new(self);
636 noff = nt_new(self);
641 if (noff == -1)
637 if (noff == -1)
642 return -1;
638 return -1;
643 /* self->nt may have been changed by realloc */
639 /* self->nt may have been changed by realloc */
644 self->nt[off].children[k] = noff;
640 self->nt[off].children[k] = noff;
645 off = noff;
641 off = noff;
646 n = &self->nt[off];
642 n = &self->nt[off];
647 n->children[nt_level(oldnode, ++level)] = v;
643 n->children[nt_level(oldnode, ++level)] = v;
648 if (level > self->ntdepth)
644 if (level > self->ntdepth)
649 self->ntdepth = level;
645 self->ntdepth = level;
650 self->ntsplits += 1;
646 self->ntsplits += 1;
651 } else {
647 } else {
652 level += 1;
648 level += 1;
653 off = v;
649 off = v;
654 }
650 }
655 }
651 }
656
652
657 return -1;
653 return -1;
658 }
654 }
659
655
660 static int nt_init(indexObject *self)
656 static int nt_init(indexObject *self)
661 {
657 {
662 if (self->nt == NULL) {
658 if (self->nt == NULL) {
663 self->ntcapacity = self->raw_length < 4
659 self->ntcapacity = self->raw_length < 4
664 ? 4 : self->raw_length / 2;
660 ? 4 : self->raw_length / 2;
665 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
661 self->nt = calloc(self->ntcapacity, sizeof(nodetree));
666 if (self->nt == NULL) {
662 if (self->nt == NULL) {
667 PyErr_NoMemory();
663 PyErr_NoMemory();
668 return -1;
664 return -1;
669 }
665 }
670 self->ntlength = 1;
666 self->ntlength = 1;
671 self->ntrev = (int)index_length(self) - 1;
667 self->ntrev = (int)index_length(self) - 1;
672 self->ntlookups = 1;
668 self->ntlookups = 1;
673 self->ntmisses = 0;
669 self->ntmisses = 0;
674 if (nt_insert(self, nullid, INT_MAX) == -1)
670 if (nt_insert(self, nullid, INT_MAX) == -1)
675 return -1;
671 return -1;
676 }
672 }
677 return 0;
673 return 0;
678 }
674 }
679
675
680 /*
676 /*
681 * Return values:
677 * Return values:
682 *
678 *
683 * -3: error (exception set)
679 * -3: error (exception set)
684 * -2: not found (no exception set)
680 * -2: not found (no exception set)
685 * rest: valid rev
681 * rest: valid rev
686 */
682 */
687 static int index_find_node(indexObject *self,
683 static int index_find_node(indexObject *self,
688 const char *node, Py_ssize_t nodelen)
684 const char *node, Py_ssize_t nodelen)
689 {
685 {
690 int rev;
686 int rev;
691
687
692 self->ntlookups++;
688 self->ntlookups++;
693 rev = nt_find(self, node, nodelen, 0);
689 rev = nt_find(self, node, nodelen, 0);
694 if (rev >= -1)
690 if (rev >= -1)
695 return rev;
691 return rev;
696
692
697 if (nt_init(self) == -1)
693 if (nt_init(self) == -1)
698 return -3;
694 return -3;
699
695
700 /*
696 /*
701 * For the first handful of lookups, we scan the entire index,
697 * For the first handful of lookups, we scan the entire index,
702 * and cache only the matching nodes. This optimizes for cases
698 * and cache only the matching nodes. This optimizes for cases
703 * like "hg tip", where only a few nodes are accessed.
699 * like "hg tip", where only a few nodes are accessed.
704 *
700 *
705 * After that, we cache every node we visit, using a single
701 * After that, we cache every node we visit, using a single
706 * scan amortized over multiple lookups. This gives the best
702 * scan amortized over multiple lookups. This gives the best
707 * bulk performance, e.g. for "hg log".
703 * bulk performance, e.g. for "hg log".
708 */
704 */
709 if (self->ntmisses++ < 4) {
705 if (self->ntmisses++ < 4) {
710 for (rev = self->ntrev - 1; rev >= 0; rev--) {
706 for (rev = self->ntrev - 1; rev >= 0; rev--) {
711 const char *n = index_node(self, rev);
707 const char *n = index_node(self, rev);
712 if (n == NULL)
708 if (n == NULL)
713 return -2;
709 return -2;
714 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
710 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
715 if (nt_insert(self, n, rev) == -1)
711 if (nt_insert(self, n, rev) == -1)
716 return -3;
712 return -3;
717 break;
713 break;
718 }
714 }
719 }
715 }
720 } else {
716 } else {
721 for (rev = self->ntrev - 1; rev >= 0; rev--) {
717 for (rev = self->ntrev - 1; rev >= 0; rev--) {
722 const char *n = index_node(self, rev);
718 const char *n = index_node(self, rev);
723 if (n == NULL) {
719 if (n == NULL) {
724 self->ntrev = rev + 1;
720 self->ntrev = rev + 1;
725 return -2;
721 return -2;
726 }
722 }
727 if (nt_insert(self, n, rev) == -1) {
723 if (nt_insert(self, n, rev) == -1) {
728 self->ntrev = rev + 1;
724 self->ntrev = rev + 1;
729 return -3;
725 return -3;
730 }
726 }
731 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
727 if (memcmp(node, n, nodelen > 20 ? 20 : nodelen) == 0) {
732 break;
728 break;
733 }
729 }
734 }
730 }
735 self->ntrev = rev;
731 self->ntrev = rev;
736 }
732 }
737
733
738 if (rev >= 0)
734 if (rev >= 0)
739 return rev;
735 return rev;
740 return -2;
736 return -2;
741 }
737 }
742
738
743 static PyObject *raise_revlog_error(void)
739 static PyObject *raise_revlog_error(void)
744 {
740 {
745 static PyObject *errclass;
741 static PyObject *errclass;
746 PyObject *mod = NULL, *errobj;
742 PyObject *mod = NULL, *errobj;
747
743
748 if (errclass == NULL) {
744 if (errclass == NULL) {
749 PyObject *dict;
745 PyObject *dict;
750
746
751 mod = PyImport_ImportModule("mercurial.error");
747 mod = PyImport_ImportModule("mercurial.error");
752 if (mod == NULL)
748 if (mod == NULL)
753 goto classfail;
749 goto classfail;
754
750
755 dict = PyModule_GetDict(mod);
751 dict = PyModule_GetDict(mod);
756 if (dict == NULL)
752 if (dict == NULL)
757 goto classfail;
753 goto classfail;
758
754
759 errclass = PyDict_GetItemString(dict, "RevlogError");
755 errclass = PyDict_GetItemString(dict, "RevlogError");
760 if (errclass == NULL) {
756 if (errclass == NULL) {
761 PyErr_SetString(PyExc_SystemError,
757 PyErr_SetString(PyExc_SystemError,
762 "could not find RevlogError");
758 "could not find RevlogError");
763 goto classfail;
759 goto classfail;
764 }
760 }
765 Py_INCREF(errclass);
761 Py_INCREF(errclass);
766 }
762 }
767
763
768 errobj = PyObject_CallFunction(errclass, NULL);
764 errobj = PyObject_CallFunction(errclass, NULL);
769 if (errobj == NULL)
765 if (errobj == NULL)
770 return NULL;
766 return NULL;
771 PyErr_SetObject(errclass, errobj);
767 PyErr_SetObject(errclass, errobj);
772 return errobj;
768 return errobj;
773
769
774 classfail:
770 classfail:
775 Py_XDECREF(mod);
771 Py_XDECREF(mod);
776 return NULL;
772 return NULL;
777 }
773 }
778
774
779 static PyObject *index_getitem(indexObject *self, PyObject *value)
775 static PyObject *index_getitem(indexObject *self, PyObject *value)
780 {
776 {
781 char *node;
777 char *node;
782 Py_ssize_t nodelen;
778 Py_ssize_t nodelen;
783 int rev;
779 int rev;
784
780
785 if (PyInt_Check(value))
781 if (PyInt_Check(value))
786 return index_get(self, PyInt_AS_LONG(value));
782 return index_get(self, PyInt_AS_LONG(value));
787
783
788 if (node_check(value, &node, &nodelen) == -1)
784 if (node_check(value, &node, &nodelen) == -1)
789 return NULL;
785 return NULL;
790 rev = index_find_node(self, node, nodelen);
786 rev = index_find_node(self, node, nodelen);
791 if (rev >= -1)
787 if (rev >= -1)
792 return PyInt_FromLong(rev);
788 return PyInt_FromLong(rev);
793 if (rev == -2)
789 if (rev == -2)
794 raise_revlog_error();
790 raise_revlog_error();
795 return NULL;
791 return NULL;
796 }
792 }
797
793
798 static int nt_partialmatch(indexObject *self, const char *node,
794 static int nt_partialmatch(indexObject *self, const char *node,
799 Py_ssize_t nodelen)
795 Py_ssize_t nodelen)
800 {
796 {
801 int rev;
797 int rev;
802
798
803 if (nt_init(self) == -1)
799 if (nt_init(self) == -1)
804 return -3;
800 return -3;
805
801
806 if (self->ntrev > 0) {
802 if (self->ntrev > 0) {
807 /* ensure that the radix tree is fully populated */
803 /* ensure that the radix tree is fully populated */
808 for (rev = self->ntrev - 1; rev >= 0; rev--) {
804 for (rev = self->ntrev - 1; rev >= 0; rev--) {
809 const char *n = index_node(self, rev);
805 const char *n = index_node(self, rev);
810 if (n == NULL)
806 if (n == NULL)
811 return -2;
807 return -2;
812 if (nt_insert(self, n, rev) == -1)
808 if (nt_insert(self, n, rev) == -1)
813 return -3;
809 return -3;
814 }
810 }
815 self->ntrev = rev;
811 self->ntrev = rev;
816 }
812 }
817
813
818 return nt_find(self, node, nodelen, 1);
814 return nt_find(self, node, nodelen, 1);
819 }
815 }
820
816
821 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
817 static PyObject *index_partialmatch(indexObject *self, PyObject *args)
822 {
818 {
823 const char *fullnode;
819 const char *fullnode;
824 int nodelen;
820 int nodelen;
825 char *node;
821 char *node;
826 int rev, i;
822 int rev, i;
827
823
828 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
824 if (!PyArg_ParseTuple(args, "s#", &node, &nodelen))
829 return NULL;
825 return NULL;
830
826
831 if (nodelen < 4) {
827 if (nodelen < 4) {
832 PyErr_SetString(PyExc_ValueError, "key too short");
828 PyErr_SetString(PyExc_ValueError, "key too short");
833 return NULL;
829 return NULL;
834 }
830 }
835
831
836 if (nodelen > 40)
832 if (nodelen > 40)
837 nodelen = 40;
833 nodelen = 40;
838
834
839 for (i = 0; i < nodelen; i++)
835 for (i = 0; i < nodelen; i++)
840 hexdigit(node, i);
836 hexdigit(node, i);
841 if (PyErr_Occurred()) {
837 if (PyErr_Occurred()) {
842 /* input contains non-hex characters */
838 /* input contains non-hex characters */
843 PyErr_Clear();
839 PyErr_Clear();
844 Py_RETURN_NONE;
840 Py_RETURN_NONE;
845 }
841 }
846
842
847 rev = nt_partialmatch(self, node, nodelen);
843 rev = nt_partialmatch(self, node, nodelen);
848
844
849 switch (rev) {
845 switch (rev) {
850 case -4:
846 case -4:
851 raise_revlog_error();
847 raise_revlog_error();
852 case -3:
848 case -3:
853 return NULL;
849 return NULL;
854 case -2:
850 case -2:
855 Py_RETURN_NONE;
851 Py_RETURN_NONE;
856 case -1:
852 case -1:
857 return PyString_FromStringAndSize(nullid, 20);
853 return PyString_FromStringAndSize(nullid, 20);
858 }
854 }
859
855
860 fullnode = index_node(self, rev);
856 fullnode = index_node(self, rev);
861 if (fullnode == NULL) {
857 if (fullnode == NULL) {
862 PyErr_Format(PyExc_IndexError,
858 PyErr_Format(PyExc_IndexError,
863 "could not access rev %d", rev);
859 "could not access rev %d", rev);
864 return NULL;
860 return NULL;
865 }
861 }
866 return PyString_FromStringAndSize(fullnode, 20);
862 return PyString_FromStringAndSize(fullnode, 20);
867 }
863 }
868
864
869 static PyObject *index_m_get(indexObject *self, PyObject *args)
865 static PyObject *index_m_get(indexObject *self, PyObject *args)
870 {
866 {
871 Py_ssize_t nodelen;
867 Py_ssize_t nodelen;
872 PyObject *val;
868 PyObject *val;
873 char *node;
869 char *node;
874 int rev;
870 int rev;
875
871
876 if (!PyArg_ParseTuple(args, "O", &val))
872 if (!PyArg_ParseTuple(args, "O", &val))
877 return NULL;
873 return NULL;
878 if (node_check(val, &node, &nodelen) == -1)
874 if (node_check(val, &node, &nodelen) == -1)
879 return NULL;
875 return NULL;
880 rev = index_find_node(self, node, nodelen);
876 rev = index_find_node(self, node, nodelen);
881 if (rev == -3)
877 if (rev == -3)
882 return NULL;
878 return NULL;
883 if (rev == -2)
879 if (rev == -2)
884 Py_RETURN_NONE;
880 Py_RETURN_NONE;
885 return PyInt_FromLong(rev);
881 return PyInt_FromLong(rev);
886 }
882 }
887
883
888 static int index_contains(indexObject *self, PyObject *value)
884 static int index_contains(indexObject *self, PyObject *value)
889 {
885 {
890 char *node;
886 char *node;
891 Py_ssize_t nodelen;
887 Py_ssize_t nodelen;
892
888
893 if (PyInt_Check(value)) {
889 if (PyInt_Check(value)) {
894 long rev = PyInt_AS_LONG(value);
890 long rev = PyInt_AS_LONG(value);
895 return rev >= -1 && rev < index_length(self);
891 return rev >= -1 && rev < index_length(self);
896 }
892 }
897
893
898 if (node_check(value, &node, &nodelen) == -1)
894 if (node_check(value, &node, &nodelen) == -1)
899 return -1;
895 return -1;
900
896
901 switch (index_find_node(self, node, nodelen)) {
897 switch (index_find_node(self, node, nodelen)) {
902 case -3:
898 case -3:
903 return -1;
899 return -1;
904 case -2:
900 case -2:
905 return 0;
901 return 0;
906 default:
902 default:
907 return 1;
903 return 1;
908 }
904 }
909 }
905 }
910
906
911 /*
907 /*
912 * Invalidate any trie entries introduced by added revs.
908 * Invalidate any trie entries introduced by added revs.
913 */
909 */
914 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
910 static void nt_invalidate_added(indexObject *self, Py_ssize_t start)
915 {
911 {
916 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
912 Py_ssize_t i, len = PyList_GET_SIZE(self->added);
917
913
918 for (i = start; i < len; i++) {
914 for (i = start; i < len; i++) {
919 PyObject *tuple = PyList_GET_ITEM(self->added, i);
915 PyObject *tuple = PyList_GET_ITEM(self->added, i);
920 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
916 PyObject *node = PyTuple_GET_ITEM(tuple, 7);
921
917
922 nt_insert(self, PyString_AS_STRING(node), -1);
918 nt_insert(self, PyString_AS_STRING(node), -1);
923 }
919 }
924
920
925 if (start == 0) {
921 if (start == 0)
926 Py_DECREF(self->added);
922 Py_CLEAR(self->added);
927 self->added = NULL;
928 }
929 }
923 }
930
924
931 /*
925 /*
932 * Delete a numeric range of revs, which must be at the end of the
926 * Delete a numeric range of revs, which must be at the end of the
933 * range, but exclude the sentinel nullid entry.
927 * range, but exclude the sentinel nullid entry.
934 */
928 */
935 static int index_slice_del(indexObject *self, PyObject *item)
929 static int index_slice_del(indexObject *self, PyObject *item)
936 {
930 {
937 Py_ssize_t start, stop, step, slicelength;
931 Py_ssize_t start, stop, step, slicelength;
938 Py_ssize_t length = index_length(self);
932 Py_ssize_t length = index_length(self);
939
933
940 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
934 if (PySlice_GetIndicesEx((PySliceObject*)item, length,
941 &start, &stop, &step, &slicelength) < 0)
935 &start, &stop, &step, &slicelength) < 0)
942 return -1;
936 return -1;
943
937
944 if (slicelength <= 0)
938 if (slicelength <= 0)
945 return 0;
939 return 0;
946
940
947 if ((step < 0 && start < stop) || (step > 0 && start > stop))
941 if ((step < 0 && start < stop) || (step > 0 && start > stop))
948 stop = start;
942 stop = start;
949
943
950 if (step < 0) {
944 if (step < 0) {
951 stop = start + 1;
945 stop = start + 1;
952 start = stop + step*(slicelength - 1) - 1;
946 start = stop + step*(slicelength - 1) - 1;
953 step = -step;
947 step = -step;
954 }
948 }
955
949
956 if (step != 1) {
950 if (step != 1) {
957 PyErr_SetString(PyExc_ValueError,
951 PyErr_SetString(PyExc_ValueError,
958 "revlog index delete requires step size of 1");
952 "revlog index delete requires step size of 1");
959 return -1;
953 return -1;
960 }
954 }
961
955
962 if (stop != length - 1) {
956 if (stop != length - 1) {
963 PyErr_SetString(PyExc_IndexError,
957 PyErr_SetString(PyExc_IndexError,
964 "revlog index deletion indices are invalid");
958 "revlog index deletion indices are invalid");
965 return -1;
959 return -1;
966 }
960 }
967
961
968 if (start < self->length - 1) {
962 if (start < self->length - 1) {
969 if (self->nt) {
963 if (self->nt) {
970 Py_ssize_t i;
964 Py_ssize_t i;
971
965
972 for (i = start + 1; i < self->length - 1; i++) {
966 for (i = start + 1; i < self->length - 1; i++) {
973 const char *node = index_node(self, i);
967 const char *node = index_node(self, i);
974
968
975 if (node)
969 if (node)
976 nt_insert(self, node, -1);
970 nt_insert(self, node, -1);
977 }
971 }
978 if (self->added)
972 if (self->added)
979 nt_invalidate_added(self, 0);
973 nt_invalidate_added(self, 0);
980 if (self->ntrev > start)
974 if (self->ntrev > start)
981 self->ntrev = (int)start;
975 self->ntrev = (int)start;
982 }
976 }
983 self->length = start + 1;
977 self->length = start + 1;
984 return 0;
978 return 0;
985 }
979 }
986
980
987 if (self->nt) {
981 if (self->nt) {
988 nt_invalidate_added(self, start - self->length + 1);
982 nt_invalidate_added(self, start - self->length + 1);
989 if (self->ntrev > start)
983 if (self->ntrev > start)
990 self->ntrev = (int)start;
984 self->ntrev = (int)start;
991 }
985 }
992 return self->added
986 return self->added
993 ? PyList_SetSlice(self->added, start - self->length + 1,
987 ? PyList_SetSlice(self->added, start - self->length + 1,
994 PyList_GET_SIZE(self->added), NULL)
988 PyList_GET_SIZE(self->added), NULL)
995 : 0;
989 : 0;
996 }
990 }
997
991
998 /*
992 /*
999 * Supported ops:
993 * Supported ops:
1000 *
994 *
1001 * slice deletion
995 * slice deletion
1002 * string assignment (extend node->rev mapping)
996 * string assignment (extend node->rev mapping)
1003 * string deletion (shrink node->rev mapping)
997 * string deletion (shrink node->rev mapping)
1004 */
998 */
1005 static int index_assign_subscript(indexObject *self, PyObject *item,
999 static int index_assign_subscript(indexObject *self, PyObject *item,
1006 PyObject *value)
1000 PyObject *value)
1007 {
1001 {
1008 char *node;
1002 char *node;
1009 Py_ssize_t nodelen;
1003 Py_ssize_t nodelen;
1010 long rev;
1004 long rev;
1011
1005
1012 if (PySlice_Check(item) && value == NULL)
1006 if (PySlice_Check(item) && value == NULL)
1013 return index_slice_del(self, item);
1007 return index_slice_del(self, item);
1014
1008
1015 if (node_check(item, &node, &nodelen) == -1)
1009 if (node_check(item, &node, &nodelen) == -1)
1016 return -1;
1010 return -1;
1017
1011
1018 if (value == NULL)
1012 if (value == NULL)
1019 return self->nt ? nt_insert(self, node, -1) : 0;
1013 return self->nt ? nt_insert(self, node, -1) : 0;
1020 rev = PyInt_AsLong(value);
1014 rev = PyInt_AsLong(value);
1021 if (rev > INT_MAX || rev < 0) {
1015 if (rev > INT_MAX || rev < 0) {
1022 if (!PyErr_Occurred())
1016 if (!PyErr_Occurred())
1023 PyErr_SetString(PyExc_ValueError, "rev out of range");
1017 PyErr_SetString(PyExc_ValueError, "rev out of range");
1024 return -1;
1018 return -1;
1025 }
1019 }
1026 return nt_insert(self, node, (int)rev);
1020 return nt_insert(self, node, (int)rev);
1027 }
1021 }
1028
1022
1029 /*
1023 /*
1030 * Find all RevlogNG entries in an index that has inline data. Update
1024 * Find all RevlogNG entries in an index that has inline data. Update
1031 * the optional "offsets" table with those entries.
1025 * the optional "offsets" table with those entries.
1032 */
1026 */
1033 static long inline_scan(indexObject *self, const char **offsets)
1027 static long inline_scan(indexObject *self, const char **offsets)
1034 {
1028 {
1035 const char *data = PyString_AS_STRING(self->data);
1029 const char *data = PyString_AS_STRING(self->data);
1036 const char *end = data + PyString_GET_SIZE(self->data);
1030 const char *end = data + PyString_GET_SIZE(self->data);
1037 const long hdrsize = 64;
1031 const long hdrsize = 64;
1038 long incr = hdrsize;
1032 long incr = hdrsize;
1039 Py_ssize_t len = 0;
1033 Py_ssize_t len = 0;
1040
1034
1041 while (data + hdrsize <= end) {
1035 while (data + hdrsize <= end) {
1042 uint32_t comp_len;
1036 uint32_t comp_len;
1043 const char *old_data;
1037 const char *old_data;
1044 /* 3rd element of header is length of compressed inline data */
1038 /* 3rd element of header is length of compressed inline data */
1045 comp_len = getbe32(data + 8);
1039 comp_len = getbe32(data + 8);
1046 incr = hdrsize + comp_len;
1040 incr = hdrsize + comp_len;
1047 if (incr < hdrsize)
1041 if (incr < hdrsize)
1048 break;
1042 break;
1049 if (offsets)
1043 if (offsets)
1050 offsets[len] = data;
1044 offsets[len] = data;
1051 len++;
1045 len++;
1052 old_data = data;
1046 old_data = data;
1053 data += incr;
1047 data += incr;
1054 if (data <= old_data)
1048 if (data <= old_data)
1055 break;
1049 break;
1056 }
1050 }
1057
1051
1058 if (data != end && data + hdrsize != end) {
1052 if (data != end && data + hdrsize != end) {
1059 if (!PyErr_Occurred())
1053 if (!PyErr_Occurred())
1060 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1054 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1061 return -1;
1055 return -1;
1062 }
1056 }
1063
1057
1064 return len;
1058 return len;
1065 }
1059 }
1066
1060
1067 static int index_init(indexObject *self, PyObject *args)
1061 static int index_init(indexObject *self, PyObject *args)
1068 {
1062 {
1069 PyObject *data_obj, *inlined_obj;
1063 PyObject *data_obj, *inlined_obj;
1070 Py_ssize_t size;
1064 Py_ssize_t size;
1071
1065
1072 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1066 if (!PyArg_ParseTuple(args, "OO", &data_obj, &inlined_obj))
1073 return -1;
1067 return -1;
1074 if (!PyString_Check(data_obj)) {
1068 if (!PyString_Check(data_obj)) {
1075 PyErr_SetString(PyExc_TypeError, "data is not a string");
1069 PyErr_SetString(PyExc_TypeError, "data is not a string");
1076 return -1;
1070 return -1;
1077 }
1071 }
1078 size = PyString_GET_SIZE(data_obj);
1072 size = PyString_GET_SIZE(data_obj);
1079
1073
1080 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1074 self->inlined = inlined_obj && PyObject_IsTrue(inlined_obj);
1081 self->data = data_obj;
1075 self->data = data_obj;
1082 self->cache = NULL;
1076 self->cache = NULL;
1083
1077
1084 self->added = NULL;
1078 self->added = NULL;
1085 self->offsets = NULL;
1079 self->offsets = NULL;
1086 self->nt = NULL;
1080 self->nt = NULL;
1087 self->ntlength = self->ntcapacity = 0;
1081 self->ntlength = self->ntcapacity = 0;
1088 self->ntdepth = self->ntsplits = 0;
1082 self->ntdepth = self->ntsplits = 0;
1089 self->ntlookups = self->ntmisses = 0;
1083 self->ntlookups = self->ntmisses = 0;
1090 self->ntrev = -1;
1084 self->ntrev = -1;
1091 Py_INCREF(self->data);
1085 Py_INCREF(self->data);
1092
1086
1093 if (self->inlined) {
1087 if (self->inlined) {
1094 long len = inline_scan(self, NULL);
1088 long len = inline_scan(self, NULL);
1095 if (len == -1)
1089 if (len == -1)
1096 goto bail;
1090 goto bail;
1097 self->raw_length = len;
1091 self->raw_length = len;
1098 self->length = len + 1;
1092 self->length = len + 1;
1099 } else {
1093 } else {
1100 if (size % 64) {
1094 if (size % 64) {
1101 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1095 PyErr_SetString(PyExc_ValueError, "corrupt index file");
1102 goto bail;
1096 goto bail;
1103 }
1097 }
1104 self->raw_length = size / 64;
1098 self->raw_length = size / 64;
1105 self->length = self->raw_length + 1;
1099 self->length = self->raw_length + 1;
1106 }
1100 }
1107
1101
1108 return 0;
1102 return 0;
1109 bail:
1103 bail:
1110 return -1;
1104 return -1;
1111 }
1105 }
1112
1106
1113 static PyObject *index_nodemap(indexObject *self)
1107 static PyObject *index_nodemap(indexObject *self)
1114 {
1108 {
1115 Py_INCREF(self);
1109 Py_INCREF(self);
1116 return (PyObject *)self;
1110 return (PyObject *)self;
1117 }
1111 }
1118
1112
1119 static void index_dealloc(indexObject *self)
1113 static void index_dealloc(indexObject *self)
1120 {
1114 {
1121 _index_clearcaches(self);
1115 _index_clearcaches(self);
1122 Py_DECREF(self->data);
1116 Py_DECREF(self->data);
1123 Py_XDECREF(self->added);
1117 Py_XDECREF(self->added);
1124 PyObject_Del(self);
1118 PyObject_Del(self);
1125 }
1119 }
1126
1120
1127 static PySequenceMethods index_sequence_methods = {
1121 static PySequenceMethods index_sequence_methods = {
1128 (lenfunc)index_length, /* sq_length */
1122 (lenfunc)index_length, /* sq_length */
1129 0, /* sq_concat */
1123 0, /* sq_concat */
1130 0, /* sq_repeat */
1124 0, /* sq_repeat */
1131 (ssizeargfunc)index_get, /* sq_item */
1125 (ssizeargfunc)index_get, /* sq_item */
1132 0, /* sq_slice */
1126 0, /* sq_slice */
1133 0, /* sq_ass_item */
1127 0, /* sq_ass_item */
1134 0, /* sq_ass_slice */
1128 0, /* sq_ass_slice */
1135 (objobjproc)index_contains, /* sq_contains */
1129 (objobjproc)index_contains, /* sq_contains */
1136 };
1130 };
1137
1131
1138 static PyMappingMethods index_mapping_methods = {
1132 static PyMappingMethods index_mapping_methods = {
1139 (lenfunc)index_length, /* mp_length */
1133 (lenfunc)index_length, /* mp_length */
1140 (binaryfunc)index_getitem, /* mp_subscript */
1134 (binaryfunc)index_getitem, /* mp_subscript */
1141 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
1135 (objobjargproc)index_assign_subscript, /* mp_ass_subscript */
1142 };
1136 };
1143
1137
1144 static PyMethodDef index_methods[] = {
1138 static PyMethodDef index_methods[] = {
1145 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
1139 {"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
1146 "clear the index caches"},
1140 "clear the index caches"},
1147 {"get", (PyCFunction)index_m_get, METH_VARARGS,
1141 {"get", (PyCFunction)index_m_get, METH_VARARGS,
1148 "get an index entry"},
1142 "get an index entry"},
1149 {"insert", (PyCFunction)index_insert, METH_VARARGS,
1143 {"insert", (PyCFunction)index_insert, METH_VARARGS,
1150 "insert an index entry"},
1144 "insert an index entry"},
1151 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
1145 {"partialmatch", (PyCFunction)index_partialmatch, METH_VARARGS,
1152 "match a potentially ambiguous node ID"},
1146 "match a potentially ambiguous node ID"},
1153 {"stats", (PyCFunction)index_stats, METH_NOARGS,
1147 {"stats", (PyCFunction)index_stats, METH_NOARGS,
1154 "stats for the index"},
1148 "stats for the index"},
1155 {NULL} /* Sentinel */
1149 {NULL} /* Sentinel */
1156 };
1150 };
1157
1151
1158 static PyGetSetDef index_getset[] = {
1152 static PyGetSetDef index_getset[] = {
1159 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
1153 {"nodemap", (getter)index_nodemap, NULL, "nodemap", NULL},
1160 {NULL} /* Sentinel */
1154 {NULL} /* Sentinel */
1161 };
1155 };
1162
1156
1163 static PyTypeObject indexType = {
1157 static PyTypeObject indexType = {
1164 PyObject_HEAD_INIT(NULL)
1158 PyObject_HEAD_INIT(NULL)
1165 0, /* ob_size */
1159 0, /* ob_size */
1166 "parsers.index", /* tp_name */
1160 "parsers.index", /* tp_name */
1167 sizeof(indexObject), /* tp_basicsize */
1161 sizeof(indexObject), /* tp_basicsize */
1168 0, /* tp_itemsize */
1162 0, /* tp_itemsize */
1169 (destructor)index_dealloc, /* tp_dealloc */
1163 (destructor)index_dealloc, /* tp_dealloc */
1170 0, /* tp_print */
1164 0, /* tp_print */
1171 0, /* tp_getattr */
1165 0, /* tp_getattr */
1172 0, /* tp_setattr */
1166 0, /* tp_setattr */
1173 0, /* tp_compare */
1167 0, /* tp_compare */
1174 0, /* tp_repr */
1168 0, /* tp_repr */
1175 0, /* tp_as_number */
1169 0, /* tp_as_number */
1176 &index_sequence_methods, /* tp_as_sequence */
1170 &index_sequence_methods, /* tp_as_sequence */
1177 &index_mapping_methods, /* tp_as_mapping */
1171 &index_mapping_methods, /* tp_as_mapping */
1178 0, /* tp_hash */
1172 0, /* tp_hash */
1179 0, /* tp_call */
1173 0, /* tp_call */
1180 0, /* tp_str */
1174 0, /* tp_str */
1181 0, /* tp_getattro */
1175 0, /* tp_getattro */
1182 0, /* tp_setattro */
1176 0, /* tp_setattro */
1183 0, /* tp_as_buffer */
1177 0, /* tp_as_buffer */
1184 Py_TPFLAGS_DEFAULT, /* tp_flags */
1178 Py_TPFLAGS_DEFAULT, /* tp_flags */
1185 "revlog index", /* tp_doc */
1179 "revlog index", /* tp_doc */
1186 0, /* tp_traverse */
1180 0, /* tp_traverse */
1187 0, /* tp_clear */
1181 0, /* tp_clear */
1188 0, /* tp_richcompare */
1182 0, /* tp_richcompare */
1189 0, /* tp_weaklistoffset */
1183 0, /* tp_weaklistoffset */
1190 0, /* tp_iter */
1184 0, /* tp_iter */
1191 0, /* tp_iternext */
1185 0, /* tp_iternext */
1192 index_methods, /* tp_methods */
1186 index_methods, /* tp_methods */
1193 0, /* tp_members */
1187 0, /* tp_members */
1194 index_getset, /* tp_getset */
1188 index_getset, /* tp_getset */
1195 0, /* tp_base */
1189 0, /* tp_base */
1196 0, /* tp_dict */
1190 0, /* tp_dict */
1197 0, /* tp_descr_get */
1191 0, /* tp_descr_get */
1198 0, /* tp_descr_set */
1192 0, /* tp_descr_set */
1199 0, /* tp_dictoffset */
1193 0, /* tp_dictoffset */
1200 (initproc)index_init, /* tp_init */
1194 (initproc)index_init, /* tp_init */
1201 0, /* tp_alloc */
1195 0, /* tp_alloc */
1202 };
1196 };
1203
1197
1204 /*
1198 /*
1205 * returns a tuple of the form (index, index, cache) with elements as
1199 * returns a tuple of the form (index, index, cache) with elements as
1206 * follows:
1200 * follows:
1207 *
1201 *
1208 * index: an index object that lazily parses RevlogNG records
1202 * index: an index object that lazily parses RevlogNG records
1209 * cache: if data is inlined, a tuple (index_file_content, 0), else None
1203 * cache: if data is inlined, a tuple (index_file_content, 0), else None
1210 *
1204 *
1211 * added complications are for backwards compatibility
1205 * added complications are for backwards compatibility
1212 */
1206 */
1213 static PyObject *parse_index2(PyObject *self, PyObject *args)
1207 static PyObject *parse_index2(PyObject *self, PyObject *args)
1214 {
1208 {
1215 PyObject *tuple = NULL, *cache = NULL;
1209 PyObject *tuple = NULL, *cache = NULL;
1216 indexObject *idx;
1210 indexObject *idx;
1217 int ret;
1211 int ret;
1218
1212
1219 idx = PyObject_New(indexObject, &indexType);
1213 idx = PyObject_New(indexObject, &indexType);
1220 if (idx == NULL)
1214 if (idx == NULL)
1221 goto bail;
1215 goto bail;
1222
1216
1223 ret = index_init(idx, args);
1217 ret = index_init(idx, args);
1224 if (ret == -1)
1218 if (ret == -1)
1225 goto bail;
1219 goto bail;
1226
1220
1227 if (idx->inlined) {
1221 if (idx->inlined) {
1228 cache = Py_BuildValue("iO", 0, idx->data);
1222 cache = Py_BuildValue("iO", 0, idx->data);
1229 if (cache == NULL)
1223 if (cache == NULL)
1230 goto bail;
1224 goto bail;
1231 } else {
1225 } else {
1232 cache = Py_None;
1226 cache = Py_None;
1233 Py_INCREF(cache);
1227 Py_INCREF(cache);
1234 }
1228 }
1235
1229
1236 tuple = Py_BuildValue("NN", idx, cache);
1230 tuple = Py_BuildValue("NN", idx, cache);
1237 if (!tuple)
1231 if (!tuple)
1238 goto bail;
1232 goto bail;
1239 return tuple;
1233 return tuple;
1240
1234
1241 bail:
1235 bail:
1242 Py_XDECREF(idx);
1236 Py_XDECREF(idx);
1243 Py_XDECREF(cache);
1237 Py_XDECREF(cache);
1244 Py_XDECREF(tuple);
1238 Py_XDECREF(tuple);
1245 return NULL;
1239 return NULL;
1246 }
1240 }
1247
1241
1248 static char parsers_doc[] = "Efficient content parsing.";
1242 static char parsers_doc[] = "Efficient content parsing.";
1249
1243
1250 static PyMethodDef methods[] = {
1244 static PyMethodDef methods[] = {
1251 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
1245 {"parse_manifest", parse_manifest, METH_VARARGS, "parse a manifest\n"},
1252 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1246 {"parse_dirstate", parse_dirstate, METH_VARARGS, "parse a dirstate\n"},
1253 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
1247 {"parse_index2", parse_index2, METH_VARARGS, "parse a revlog index\n"},
1254 {NULL, NULL}
1248 {NULL, NULL}
1255 };
1249 };
1256
1250
1257 static void module_init(PyObject *mod)
1251 static void module_init(PyObject *mod)
1258 {
1252 {
1259 indexType.tp_new = PyType_GenericNew;
1253 indexType.tp_new = PyType_GenericNew;
1260 if (PyType_Ready(&indexType) < 0)
1254 if (PyType_Ready(&indexType) < 0)
1261 return;
1255 return;
1262 Py_INCREF(&indexType);
1256 Py_INCREF(&indexType);
1263
1257
1264 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
1258 PyModule_AddObject(mod, "index", (PyObject *)&indexType);
1265
1259
1266 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
1260 nullentry = Py_BuildValue("iiiiiiis#", 0, 0, 0,
1267 -1, -1, -1, -1, nullid, 20);
1261 -1, -1, -1, -1, nullid, 20);
1268 if (nullentry)
1262 if (nullentry)
1269 PyObject_GC_UnTrack(nullentry);
1263 PyObject_GC_UnTrack(nullentry);
1270 }
1264 }
1271
1265
1272 #ifdef IS_PY3K
1266 #ifdef IS_PY3K
1273 static struct PyModuleDef parsers_module = {
1267 static struct PyModuleDef parsers_module = {
1274 PyModuleDef_HEAD_INIT,
1268 PyModuleDef_HEAD_INIT,
1275 "parsers",
1269 "parsers",
1276 parsers_doc,
1270 parsers_doc,
1277 -1,
1271 -1,
1278 methods
1272 methods
1279 };
1273 };
1280
1274
1281 PyMODINIT_FUNC PyInit_parsers(void)
1275 PyMODINIT_FUNC PyInit_parsers(void)
1282 {
1276 {
1283 PyObject *mod = PyModule_Create(&parsers_module);
1277 PyObject *mod = PyModule_Create(&parsers_module);
1284 module_init(mod);
1278 module_init(mod);
1285 return mod;
1279 return mod;
1286 }
1280 }
1287 #else
1281 #else
1288 PyMODINIT_FUNC initparsers(void)
1282 PyMODINIT_FUNC initparsers(void)
1289 {
1283 {
1290 PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc);
1284 PyObject *mod = Py_InitModule3("parsers", methods, parsers_doc);
1291 module_init(mod);
1285 module_init(mod);
1292 }
1286 }
1293 #endif
1287 #endif
General Comments 0
You need to be logged in to leave comments. Login now