mercurial/cext/revlog.c
changeset 38859 49628742d264
parent 38314 1445b556e9d2
child 38860 44bbc89ec5e0
equal deleted inserted replaced
38858:e411774a2e0f 38859:49628742d264
   262 	return node;
   262 	return node;
   263 }
   263 }
   264 
   264 
   265 static int nt_insert(indexObject *self, const char *node, int rev);
   265 static int nt_insert(indexObject *self, const char *node, int rev);
   266 
   266 
   267 static int node_check(PyObject *obj, char **node, Py_ssize_t *nodelen)
   267 static int node_check(PyObject *obj, char **node)
   268 {
   268 {
   269 	if (PyBytes_AsStringAndSize(obj, node, nodelen) == -1)
   269 	Py_ssize_t nodelen;
       
   270 	if (PyBytes_AsStringAndSize(obj, node, &nodelen) == -1)
   270 		return -1;
   271 		return -1;
   271 	if (*nodelen == 20)
   272 	if (nodelen == 20)
   272 		return 0;
   273 		return 0;
   273 	PyErr_SetString(PyExc_ValueError, "20-byte hash required");
   274 	PyErr_SetString(PyExc_ValueError, "20-byte hash required");
   274 	return -1;
   275 	return -1;
   275 }
   276 }
   276 
   277 
   277 static PyObject *index_insert(indexObject *self, PyObject *args)
   278 static PyObject *index_insert(indexObject *self, PyObject *args)
   278 {
   279 {
   279 	PyObject *obj;
   280 	PyObject *obj;
   280 	char *node;
   281 	char *node;
   281 	int index;
   282 	int index;
   282 	Py_ssize_t len, nodelen;
   283 	Py_ssize_t len;
   283 
   284 
   284 	if (!PyArg_ParseTuple(args, "iO", &index, &obj))
   285 	if (!PyArg_ParseTuple(args, "iO", &index, &obj))
   285 		return NULL;
   286 		return NULL;
   286 
   287 
   287 	if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
   288 	if (!PyTuple_Check(obj) || PyTuple_GET_SIZE(obj) != 8) {
   288 		PyErr_SetString(PyExc_TypeError, "8-tuple required");
   289 		PyErr_SetString(PyExc_TypeError, "8-tuple required");
   289 		return NULL;
   290 		return NULL;
   290 	}
   291 	}
   291 
   292 
   292 	if (node_check(PyTuple_GET_ITEM(obj, 7), &node, &nodelen) == -1)
   293 	if (node_check(PyTuple_GET_ITEM(obj, 7), &node) == -1)
   293 		return NULL;
   294 		return NULL;
   294 
   295 
   295 	len = index_length(self);
   296 	len = index_length(self);
   296 
   297 
   297 	if (index < 0)
   298 	if (index < 0)
  1212 }
  1213 }
  1213 
  1214 
  1214 static PyObject *index_getitem(indexObject *self, PyObject *value)
  1215 static PyObject *index_getitem(indexObject *self, PyObject *value)
  1215 {
  1216 {
  1216 	char *node;
  1217 	char *node;
  1217 	Py_ssize_t nodelen;
       
  1218 	int rev;
  1218 	int rev;
  1219 
  1219 
  1220 	if (PyInt_Check(value))
  1220 	if (PyInt_Check(value))
  1221 		return index_get(self, PyInt_AS_LONG(value));
  1221 		return index_get(self, PyInt_AS_LONG(value));
  1222 
  1222 
  1223 	if (node_check(value, &node, &nodelen) == -1)
  1223 	if (node_check(value, &node) == -1)
  1224 		return NULL;
  1224 		return NULL;
  1225 	rev = index_find_node(self, node, nodelen);
  1225 	rev = index_find_node(self, node, 20);
  1226 	if (rev >= -1)
  1226 	if (rev >= -1)
  1227 		return PyInt_FromLong(rev);
  1227 		return PyInt_FromLong(rev);
  1228 	if (rev == -2)
  1228 	if (rev == -2)
  1229 		raise_revlog_error();
  1229 		raise_revlog_error();
  1230 	return NULL;
  1230 	return NULL;
  1358 	return PyBytes_FromStringAndSize(fullnode, 20);
  1358 	return PyBytes_FromStringAndSize(fullnode, 20);
  1359 }
  1359 }
  1360 
  1360 
  1361 static PyObject *index_shortest(indexObject *self, PyObject *args)
  1361 static PyObject *index_shortest(indexObject *self, PyObject *args)
  1362 {
  1362 {
  1363 	Py_ssize_t nodelen;
       
  1364 	PyObject *val;
  1363 	PyObject *val;
  1365 	char *node;
  1364 	char *node;
  1366 	int length;
  1365 	int length;
  1367 
  1366 
  1368 	if (!PyArg_ParseTuple(args, "O", &val))
  1367 	if (!PyArg_ParseTuple(args, "O", &val))
  1369 		return NULL;
  1368 		return NULL;
  1370 	if (node_check(val, &node, &nodelen) == -1)
  1369 	if (node_check(val, &node) == -1)
  1371 		return NULL;
  1370 		return NULL;
  1372 
  1371 
  1373 	self->ntlookups++;
  1372 	self->ntlookups++;
  1374 	length = nt_shortest(self, node);
  1373 	length = nt_shortest(self, node);
  1375 	if (length == -3)
  1374 	if (length == -3)
  1381 	return PyInt_FromLong(length);
  1380 	return PyInt_FromLong(length);
  1382 }
  1381 }
  1383 
  1382 
  1384 static PyObject *index_m_get(indexObject *self, PyObject *args)
  1383 static PyObject *index_m_get(indexObject *self, PyObject *args)
  1385 {
  1384 {
  1386 	Py_ssize_t nodelen;
       
  1387 	PyObject *val;
  1385 	PyObject *val;
  1388 	char *node;
  1386 	char *node;
  1389 	int rev;
  1387 	int rev;
  1390 
  1388 
  1391 	if (!PyArg_ParseTuple(args, "O", &val))
  1389 	if (!PyArg_ParseTuple(args, "O", &val))
  1392 		return NULL;
  1390 		return NULL;
  1393 	if (node_check(val, &node, &nodelen) == -1)
  1391 	if (node_check(val, &node) == -1)
  1394 		return NULL;
  1392 		return NULL;
  1395 	rev = index_find_node(self, node, nodelen);
  1393 	rev = index_find_node(self, node, 20);
  1396 	if (rev == -3)
  1394 	if (rev == -3)
  1397 		return NULL;
  1395 		return NULL;
  1398 	if (rev == -2)
  1396 	if (rev == -2)
  1399 		Py_RETURN_NONE;
  1397 		Py_RETURN_NONE;
  1400 	return PyInt_FromLong(rev);
  1398 	return PyInt_FromLong(rev);
  1401 }
  1399 }
  1402 
  1400 
  1403 static int index_contains(indexObject *self, PyObject *value)
  1401 static int index_contains(indexObject *self, PyObject *value)
  1404 {
  1402 {
  1405 	char *node;
  1403 	char *node;
  1406 	Py_ssize_t nodelen;
       
  1407 
  1404 
  1408 	if (PyInt_Check(value)) {
  1405 	if (PyInt_Check(value)) {
  1409 		long rev = PyInt_AS_LONG(value);
  1406 		long rev = PyInt_AS_LONG(value);
  1410 		return rev >= -1 && rev < index_length(self);
  1407 		return rev >= -1 && rev < index_length(self);
  1411 	}
  1408 	}
  1412 
  1409 
  1413 	if (node_check(value, &node, &nodelen) == -1)
  1410 	if (node_check(value, &node) == -1)
  1414 		return -1;
  1411 		return -1;
  1415 
  1412 
  1416 	switch (index_find_node(self, node, nodelen)) {
  1413 	switch (index_find_node(self, node, 20)) {
  1417 	case -3:
  1414 	case -3:
  1418 		return -1;
  1415 		return -1;
  1419 	case -2:
  1416 	case -2:
  1420 		return 0;
  1417 		return 0;
  1421 	default:
  1418 	default:
  1895  */
  1892  */
  1896 static int index_assign_subscript(indexObject *self, PyObject *item,
  1893 static int index_assign_subscript(indexObject *self, PyObject *item,
  1897 				  PyObject *value)
  1894 				  PyObject *value)
  1898 {
  1895 {
  1899 	char *node;
  1896 	char *node;
  1900 	Py_ssize_t nodelen;
       
  1901 	long rev;
  1897 	long rev;
  1902 
  1898 
  1903 	if (PySlice_Check(item) && value == NULL)
  1899 	if (PySlice_Check(item) && value == NULL)
  1904 		return index_slice_del(self, item);
  1900 		return index_slice_del(self, item);
  1905 
  1901 
  1906 	if (node_check(item, &node, &nodelen) == -1)
  1902 	if (node_check(item, &node) == -1)
  1907 		return -1;
  1903 		return -1;
  1908 
  1904 
  1909 	if (value == NULL)
  1905 	if (value == NULL)
  1910 		return self->nt ? nt_insert(self, node, -1) : 0;
  1906 		return self->nt ? nt_insert(self, node, -1) : 0;
  1911 	rev = PyInt_AsLong(value);
  1907 	rev = PyInt_AsLong(value);