py_graph.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. #ifndef _PY_GRAPH_MOD_H
  2. #define _PY_GRAPH_MOD_H
  3. #define PY_SSIZE_T_CLEAN
  4. #include <Python.h>
  5. #include <structmember.h>
  6. #include "graph.h"
  7. #include "codec_nt.h"
  8. #include "py_triple.h"
  9. /*
  10. * Iterator helpers.
  11. */
  12. /*
  13. * String iterator for encoder output.
  14. *
  15. * Yields one string (one or more lines) at a time.
  16. */
  17. typedef struct {
  18. PyObject_HEAD
  19. LSUP_CodecIterator *it;
  20. unsigned char *line;
  21. } StringIteratorObject;
  22. static void
  23. StringIterator_dealloc (StringIteratorObject *it_obj)
  24. { it_obj->it->codec->encode_graph_done (it_obj->it); }
  25. static PyObject *
  26. StringIterator_next (StringIteratorObject *it_obj)
  27. {
  28. LSUP_rc rc = it_obj->it->codec->encode_graph_iter (
  29. it_obj->it, &it_obj->line);
  30. if (rc != LSUP_OK) {
  31. if (rc != LSUP_END)
  32. PyErr_SetString (PyExc_ValueError, "Error encoding graph.");
  33. // If not an error, this raises StopIteration.
  34. return NULL;
  35. }
  36. PyObject *rdf_obj = PyUnicode_FromString ((char*)it_obj->line);
  37. if (UNLIKELY (!rdf_obj)) return NULL;
  38. Py_INCREF (rdf_obj);
  39. return (rdf_obj);
  40. }
  41. /*
  42. * String iterator type.
  43. *
  44. * Objects of this type are never generated from Python code, rather from
  45. * Graph_encode, hence the type has no special new or init function.
  46. */
  47. PyTypeObject StringIteratorType = {
  48. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  49. .tp_name = "graph.StringIterator",
  50. .tp_basicsize = sizeof (StringIteratorObject),
  51. .tp_itemsize = 0,
  52. .tp_flags = Py_TPFLAGS_DEFAULT,
  53. .tp_dealloc = (destructor) StringIterator_dealloc,
  54. .tp_iter = PyObject_SelfIter,
  55. .tp_iternext = (iternextfunc)StringIterator_next,
  56. };
  57. /*
  58. * Graph iterator.
  59. *
  60. * Yields one triple at a time.
  61. */
  62. typedef struct {
  63. PyObject_HEAD
  64. LSUP_GraphIterator *it;
  65. LSUP_Triple *spo;
  66. } GraphIteratorObject;
  67. static void
  68. GraphIterator_dealloc (GraphIteratorObject *it_obj)
  69. {
  70. LSUP_graph_iter_free (it_obj->it);
  71. free (it_obj->spo);
  72. }
  73. static PyObject *
  74. GraphIterator_next (GraphIteratorObject *it_obj)
  75. {
  76. LSUP_rc rc = LSUP_graph_iter_next (it_obj->it, it_obj->spo);
  77. if (rc != LSUP_OK) {
  78. if (rc != LSUP_END)
  79. PyErr_SetString (PyExc_ValueError, "Error encoding graph.");
  80. // If not an error, this raises StopIteration.
  81. return NULL;
  82. }
  83. return build_triple (it_obj->spo);
  84. }
  85. /*
  86. * Graph iterator type.
  87. */
  88. PyTypeObject GraphIteratorType = {
  89. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  90. .tp_name = "graph.GraphIterator",
  91. .tp_basicsize = sizeof (GraphIteratorObject),
  92. .tp_itemsize = 0,
  93. .tp_flags = Py_TPFLAGS_DEFAULT,
  94. .tp_dealloc = (destructor) GraphIterator_dealloc,
  95. .tp_iter = PyObject_SelfIter,
  96. .tp_iternext = (iternextfunc) GraphIterator_next,
  97. };
  98. /*
  99. * Graph stuff.
  100. */
  101. typedef struct {
  102. PyObject_HEAD
  103. LSUP_Graph *ob_struct;
  104. } GraphObject;
  105. static int
  106. Graph_init (GraphObject *self, PyObject *args, PyObject *kwargs)
  107. {
  108. unsigned char store_type;
  109. PyObject *uri_obj;
  110. LSUP_Term *uri;
  111. static char *kwlist[] = {"", "uri", NULL};
  112. if (!PyArg_ParseTupleAndKeywords (
  113. args, kwargs, "b|O", kwlist, &store_type, &uri_obj))
  114. return -1;
  115. if (uri_obj)
  116. if (!PyObject_TypeCheck (uri, &TermType)) {
  117. PyErr_SetString (PyExc_TypeError, "uri is not a Term type.");
  118. return -1;
  119. }
  120. } else uri = LSUP_iriref_new (NULL, NULL);
  121. self->ob_struct = LSUP_graph_new (uri, (LSUP_store_type) store_type);
  122. if (!self->ob_struct) {
  123. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  124. return -1;
  125. }
  126. return 0;
  127. }
  128. static void
  129. Graph_dealloc (GraphObject *self)
  130. {
  131. LSUP_graph_free (self->ob_struct);
  132. Py_TYPE (self)->tp_free ((PyObject *) self);
  133. }
  134. static PyObject *
  135. Graph_get_uri (GraphObject *self, void *closure)
  136. {
  137. PyObject *uri = PyUnicode_FromString (
  138. LSUP_graph_uri (self->ob_struct)->data);
  139. if ( UNLIKELY (!uri)) return NULL;
  140. Py_INCREF(uri);
  141. return uri;
  142. }
  143. static int
  144. Graph_set_uri (GraphObject *self, PyObject *value, void *closure)
  145. {
  146. if (!PyObject_TypeCheck (value, &TermType)) {
  147. PyErr_SetString (PyExc_TypeError, "URI is not a Term type.");
  148. return -1;
  149. }
  150. LSUP_rc rc = LSUP_graph_set_uri (
  151. self->ob_struct, ((TermObject*)value)->ob_struct->data);
  152. return rc == LSUP_OK ? 0 : -1;
  153. }
  154. static PyGetSetDef Graph_getsetters[] = {
  155. {
  156. "uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
  157. "Graph URI.", NULL
  158. },
  159. {NULL}
  160. };
  161. static PyObject *
  162. Graph_copy (PyTypeObject *cls, PyObject *src)
  163. {
  164. if (! PyObject_TypeCheck (src, cls)) return NULL;
  165. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  166. if (!res) return NULL;
  167. res->ob_struct = LSUP_graph_copy (((GraphObject *) src)->ob_struct);
  168. Py_INCREF(res);
  169. return (PyObject *) res;
  170. };
  171. static PyObject *
  172. Graph_store (PyObject *self)
  173. {
  174. GraphObject *dest_obj = (GraphObject *) Py_TYPE (self)->tp_alloc(
  175. Py_TYPE (self), 0);
  176. if (!dest_obj) return PyErr_NoMemory();
  177. LSUP_rc rc = LSUP_graph_store (
  178. ((GraphObject *) self)->ob_struct, &((dest_obj)->ob_struct), NULL);
  179. if (rc != LSUP_OK) {
  180. log_error (LSUP_strerror (rc));
  181. PyErr_SetString (PyExc_SystemError, "Error storing graph.");
  182. return NULL;
  183. };
  184. Py_INCREF (dest_obj);
  185. return (PyObject *)dest_obj;
  186. }
  187. static PyObject *
  188. Graph_new_from_rdf (PyTypeObject *cls, PyObject *args)
  189. {
  190. PyObject *buf, *fileno_fn, *fileno_obj;
  191. const char *type;
  192. if (! PyArg_ParseTuple (args, "Os", &buf, &type)) return NULL;
  193. // Get the file descriptor from the Python BufferedIO object.
  194. // FIXME This is not sure to be reliable. See
  195. // https://docs.python.org/3/library/io.html?highlight=io%20bufferedreader#io.IOBase.fileno
  196. if (! (fileno_fn = PyObject_GetAttrString (buf, "fileno"))) {
  197. PyErr_SetString (PyExc_TypeError, "Object has no fileno function.");
  198. return NULL;
  199. }
  200. PyObject* fileno_args = PyTuple_New(0);
  201. if (! (fileno_obj = PyObject_CallObject (fileno_fn, fileno_args))) {
  202. PyErr_SetString (PyExc_SystemError, "Error calling fileno function.");
  203. return NULL;
  204. }
  205. int fd = PyLong_AsSize_t (fileno_obj);
  206. /*
  207. * From the Linux man page:
  208. *
  209. * > The file descriptor is not dup'ed, and will be closed when the stream
  210. * > created by fdopen() is closed. The result of applying fdopen() to a
  211. * > shared memory object is undefined.
  212. *
  213. * This handle must not be closed. Leave open for the Python caller to
  214. * handle it.
  215. */
  216. FILE *fh = fdopen (fd, "r");
  217. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  218. if (!res) return PyErr_NoMemory();
  219. const LSUP_Codec *codec;
  220. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  221. // TODO other codecs here.
  222. else {
  223. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  224. return NULL;
  225. }
  226. size_t ct;
  227. char *err;
  228. codec->decode_graph (fh, &res->ob_struct, &ct, &err);
  229. log_debug ("Decoded %lu triples.", ct);
  230. if (UNLIKELY (err)) {
  231. PyErr_SetString (PyExc_IOError, err);
  232. return NULL;
  233. }
  234. Py_INCREF(res);
  235. return (PyObject *) res;
  236. }
  237. /** @brief Build a triple pattern for lookup purposes.
  238. */
  239. inline static int build_trp_pattern (PyObject *args, LSUP_Term *spo[])
  240. {
  241. PyObject *s_obj, *p_obj, *o_obj;
  242. if (! (PyArg_ParseTuple (args, "OOO", &s_obj, &p_obj, &o_obj)))
  243. return -1;
  244. if (s_obj != Py_None && !PyObject_TypeCheck (s_obj, &TermType)) {
  245. PyErr_SetString (PyExc_TypeError, "Subject must be a term or None.");
  246. return -1;
  247. }
  248. if (p_obj != Py_None && !PyObject_TypeCheck (p_obj, &TermType)) {
  249. PyErr_SetString (PyExc_TypeError, "Predicate must be a term or None.");
  250. return -1;
  251. }
  252. if (o_obj != Py_None && !PyObject_TypeCheck (o_obj, &TermType)) {
  253. PyErr_SetString (PyExc_TypeError, "Object must be a term or None.");
  254. return -1;
  255. }
  256. spo[0] = s_obj != Py_None ? ((TermObject *)s_obj)->ob_struct : NULL;
  257. spo[1] = p_obj != Py_None ? ((TermObject *)p_obj)->ob_struct : NULL;
  258. spo[2] = o_obj != Py_None ? ((TermObject *)o_obj)->ob_struct : NULL;
  259. return 0;
  260. }
  261. static PyObject *
  262. Graph_new_set_from_store_lookup (PyTypeObject *cls, PyObject *args)
  263. {
  264. LSUP_Term *spo[3];
  265. if (UNLIKELY ((build_trp_pattern (args, spo)) < 0)) return NULL;
  266. LSUP_Graph **gr_a = LSUP_graph_new_lookup (spo[0], spo[1], spo[2]);
  267. if (UNLIKELY (!gr_a)) {
  268. // TODO implement LSUP_strerror for more details.
  269. PyErr_SetString (PyExc_SystemError, "Error looking up triples.");
  270. return NULL;
  271. }
  272. PyObject *ret = PySet_New (NULL);
  273. size_t i;
  274. for (i = 0; gr_a[i] != NULL; i++) {
  275. PyObject *gr_obj = cls->tp_alloc(cls, 0);
  276. if (!gr_obj) return NULL;
  277. ((GraphObject *) gr_obj)->ob_struct = gr_a[i];
  278. Py_INCREF (gr_obj);
  279. PySet_Add (ret, gr_obj);
  280. }
  281. log_debug ("Found %lu graphs for pattern.", i + 1);
  282. Py_INCREF (ret);
  283. return ret;
  284. }
  285. static PyObject *
  286. Graph_richcmp (PyObject *self, PyObject *other, int op)
  287. {
  288. // Only equality and non-equality are supported.
  289. if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
  290. LSUP_Graph *t1 = ((GraphObject *) self)->ob_struct;
  291. LSUP_Graph *t2 = ((GraphObject *) other)->ob_struct;
  292. if (LSUP_graph_equals (t1, t2) ^ (op == Py_NE)) Py_RETURN_TRUE;
  293. Py_RETURN_FALSE;
  294. }
  295. static inline PyObject *
  296. Graph_bool_op (
  297. PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
  298. {
  299. if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
  300. return NULL;
  301. GraphObject *res = (GraphObject *) cls->tp_alloc (cls, 0);
  302. if (!res) return NULL;
  303. res->ob_struct = LSUP_graph_bool_op (
  304. op, ((GraphObject *) gr1)->ob_struct,
  305. ((GraphObject *) gr2)->ob_struct);
  306. Py_INCREF(res);
  307. return (PyObject *) res;
  308. }
  309. static PyObject *
  310. Graph_add (PyObject *self, PyObject *triples)
  311. {
  312. // Triple may be any iterable.
  313. PyObject *iter = PyObject_GetIter (triples);
  314. if (! iter) {
  315. PyErr_SetString (
  316. PyExc_ValueError, "Triples object cannot be iterated.");
  317. return NULL;
  318. }
  319. PyObject *trp_obj;
  320. int rc = 0;
  321. size_t i;
  322. LSUP_GraphIterator *it = LSUP_graph_add_init (
  323. ((GraphObject *)self)->ob_struct);
  324. for (i = 0; (trp_obj = PyIter_Next (iter)); i++) {
  325. if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
  326. PyErr_SetString (
  327. PyExc_ValueError, "Object is not a triple.");
  328. rc = -1;
  329. goto finally;
  330. }
  331. log_trace ("Inserting triple #%lu", i);
  332. LSUP_rc db_rc = LSUP_graph_add_iter (
  333. it, ((TripleObject *) trp_obj)->ob_struct);
  334. if (db_rc == LSUP_OK) rc = LSUP_OK;
  335. if (UNLIKELY (db_rc < 0)) {
  336. rc = -1;
  337. goto finally;
  338. }
  339. }
  340. finally:
  341. LSUP_graph_add_done (it);
  342. PyObject *ret = PyLong_FromSize_t (LSUP_graph_iter_cur (it));
  343. if (rc == LSUP_OK) {
  344. Py_INCREF (ret);
  345. return ret;
  346. }
  347. return NULL;
  348. }
  349. static PyObject *Graph_remove (PyObject *self, PyObject *args)
  350. {
  351. LSUP_rc rc;
  352. LSUP_Term *spo[3];
  353. rc = build_trp_pattern (args, spo);
  354. if (rc < 0) goto finally;
  355. size_t ct;
  356. rc = LSUP_graph_remove (
  357. ((GraphObject *)self)->ob_struct, spo[0], spo[1], spo[2], &ct);
  358. if (rc < 0) {
  359. // TODO implement strerror for more details.
  360. PyErr_SetString (PyExc_SystemError, "Error removing triples.");
  361. goto finally;
  362. }
  363. log_debug ("Removed %lu triples.", ct);
  364. finally:
  365. if (rc < 0) return NULL;
  366. Py_RETURN_NONE;
  367. }
  368. static PyObject *Graph_lookup (PyObject *self, PyObject *args)
  369. {
  370. LSUP_rc rc;
  371. GraphIteratorObject *it_obj = NULL;
  372. LSUP_Term *spo[3];
  373. rc = build_trp_pattern (args, spo);
  374. if (UNLIKELY (rc < 0)) goto finally;
  375. size_t ct;
  376. LSUP_GraphIterator *it = LSUP_graph_lookup (
  377. ((GraphObject *)self)->ob_struct, spo[0], spo[1], spo[2], &ct);
  378. if (UNLIKELY (!it)) {
  379. // TODO implement LSUP_strerror for more details.
  380. PyErr_SetString (PyExc_SystemError, "Error looking up triples.");
  381. rc = -1;
  382. goto finally;
  383. }
  384. log_debug ("Found %lu triples.", ct);
  385. // Initialize the generator object.
  386. it_obj = PyObject_New (
  387. GraphIteratorObject, &GraphIteratorType);
  388. if (UNLIKELY (!it_obj)) goto finally;
  389. it_obj->it = it;
  390. it_obj->spo = TRP_DUMMY;
  391. Py_INCREF (it_obj);
  392. finally:
  393. return (PyObject *)it_obj;
  394. }
  395. static PyObject *
  396. Graph_encode (PyObject *self, PyObject *args)
  397. {
  398. const char *type;
  399. if (! PyArg_ParseTuple (args, "s", &type)) return NULL;
  400. const LSUP_Codec *codec;
  401. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  402. // TODO other codecs here.
  403. else {
  404. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  405. return NULL;
  406. }
  407. LSUP_CodecIterator *it = codec->encode_graph_init (
  408. ((GraphObject *)self)->ob_struct);
  409. // Initialize the generator object.
  410. StringIteratorObject *it_obj = PyObject_New (
  411. StringIteratorObject, &StringIteratorType);
  412. if (!it_obj) return NULL;
  413. it_obj->it = it;
  414. it_obj->line = NULL;
  415. Py_INCREF (it_obj);
  416. return (PyObject *)it_obj;
  417. }
  418. static PyMethodDef Graph_methods[] = {
  419. {
  420. "copy", (PyCFunction) Graph_copy, METH_CLASS | METH_VARARGS,
  421. "Copy a graph."
  422. },
  423. {
  424. "from_rdf", (PyCFunction) Graph_new_from_rdf,
  425. METH_CLASS | METH_VARARGS,
  426. "Create a graph from a RDF file."
  427. },
  428. {
  429. "from_lookup", (PyCFunction) Graph_new_set_from_store_lookup,
  430. METH_CLASS | METH_VARARGS,
  431. "Create a set of graphs from a store SPO lookup."
  432. },
  433. {
  434. "store", (PyCFunction) Graph_store, METH_NOARGS,
  435. "Store a graph into the permanent back end."
  436. },
  437. {"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
  438. {
  439. "remove", (PyCFunction) Graph_remove, METH_VARARGS,
  440. "Remove triples from a graph by matching a pattern."
  441. },
  442. {
  443. "lookup", (PyCFunction) Graph_lookup, METH_VARARGS,
  444. "Look triples in a graph by matching a pattern."
  445. },
  446. {
  447. "to_rdf", (PyCFunction) Graph_encode, METH_VARARGS,
  448. "Encode a graph into a RDF byte buffer."
  449. },
  450. {NULL},
  451. };
  452. static inline PyObject *Graph_bool_and (
  453. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  454. { return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
  455. static inline PyObject *Graph_bool_or (
  456. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  457. { return Graph_bool_op (cls, LSUP_BOOL_UNION, gr1, gr2); }
  458. static inline PyObject *Graph_bool_subtract (
  459. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  460. { return Graph_bool_op (cls, LSUP_BOOL_SUBTRACTION, gr1, gr2); }
  461. static inline PyObject *Graph_bool_xor (
  462. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  463. { return Graph_bool_op (cls, LSUP_BOOL_XOR, gr1, gr2); }
  464. static PyNumberMethods Graph_number_methods = {
  465. .nb_and = (binaryfunc) Graph_bool_and,
  466. .nb_or = (binaryfunc) Graph_bool_or,
  467. .nb_subtract = (binaryfunc) Graph_bool_subtract,
  468. .nb_xor = (binaryfunc) Graph_bool_xor,
  469. };
  470. static int
  471. Graph_contains (PyObject *self, PyObject *value)
  472. {
  473. if (!PyObject_TypeCheck (value, &TripleType)) {
  474. PyErr_SetString (PyExc_ValueError, "Error parsing input value.");
  475. return -1;
  476. }
  477. int rc = LSUP_graph_contains (
  478. ((GraphObject *) self)->ob_struct,
  479. ((TripleObject *) value)->ob_struct);
  480. return rc;
  481. }
  482. static Py_ssize_t
  483. Graph_get_size (PyObject *self)
  484. { return LSUP_graph_size (((GraphObject *) self)->ob_struct); }
  485. static PySequenceMethods Graph_seq_methods = {
  486. .sq_length = (lenfunc) Graph_get_size,
  487. .sq_contains = (objobjproc) Graph_contains,
  488. };
  489. PyTypeObject GraphType = {
  490. PyVarObject_HEAD_INIT(NULL, 0)
  491. .tp_name = "graph.Graph",
  492. .tp_doc = "RDF graph",
  493. .tp_basicsize = sizeof (GraphObject),
  494. .tp_itemsize = 0,
  495. .tp_flags = Py_TPFLAGS_DEFAULT,
  496. .tp_new = PyType_GenericNew,
  497. .tp_init = (initproc) Graph_init,
  498. .tp_dealloc = (destructor) Graph_dealloc,
  499. .tp_getset = Graph_getsetters,
  500. .tp_methods = Graph_methods,
  501. .tp_richcompare = (richcmpfunc) Graph_richcmp,
  502. .tp_as_number = &Graph_number_methods,
  503. .tp_as_sequence = &Graph_seq_methods,
  504. };
  505. #endif