py_graph.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. * String iterator for codec output.
  11. *
  12. * Yields one string (one or more lines) at a time.
  13. */
  14. typedef struct {
  15. PyObject_HEAD
  16. LSUP_CodecIterator *it;
  17. char *line;
  18. } StringIteratorObject;
  19. static void
  20. StringIterator_dealloc (StringIteratorObject *it_obj)
  21. { it_obj->it->codec->encode_graph_done (it_obj->it); }
  22. static PyObject *
  23. StringIterator_next (StringIteratorObject *it_obj)
  24. {
  25. unsigned char *rdf_str;
  26. LSUP_rc rc = it_obj->it->codec->encode_graph_iter (it_obj->it, &rdf_str);
  27. if (rc != LSUP_OK) {
  28. if (rc != LSUP_END)
  29. PyErr_SetString (PyExc_ValueError, "Error encoding graph.");
  30. // If not an error, this raises StopIteration.
  31. return NULL;
  32. }
  33. PyObject *rdf_obj = PyUnicode_FromString ((char*)rdf_str);
  34. if (UNLIKELY (!rdf_obj)) return NULL;
  35. Py_INCREF (rdf_obj);
  36. return (rdf_obj);
  37. }
  38. /*
  39. * String iterator type.
  40. *
  41. * Objects of this type are never generated from Python code, rather from
  42. * Graph_encode, hence the type has no special new or init function.
  43. */
  44. PyTypeObject StringIteratorType = {
  45. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  46. .tp_name = "graph.StringIterator",
  47. .tp_basicsize = sizeof (StringIteratorObject),
  48. .tp_itemsize = 0,
  49. .tp_flags = Py_TPFLAGS_DEFAULT,
  50. .tp_dealloc = (destructor) StringIterator_dealloc,
  51. .tp_iter = PyObject_SelfIter,
  52. .tp_iternext = (iternextfunc)StringIterator_next,
  53. };
  54. /*
  55. * Graph stuff.
  56. */
  57. typedef struct {
  58. PyObject_HEAD
  59. LSUP_Graph *ob_struct;
  60. } GraphObject;
  61. static int
  62. Graph_init (GraphObject *self, PyObject *args, PyObject *kwargs)
  63. {
  64. unsigned char store_type;
  65. if (!PyArg_ParseTuple (args, "b", &store_type))
  66. return -1;
  67. self->ob_struct = LSUP_graph_new ((LSUP_store_type)store_type);
  68. if (!self->ob_struct) {
  69. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  70. return -1;
  71. }
  72. return 0;
  73. }
  74. static void
  75. Graph_dealloc (GraphObject *self)
  76. {
  77. LSUP_graph_free (self->ob_struct);
  78. Py_TYPE (self)->tp_free ((PyObject *) self);
  79. }
  80. static PyObject *
  81. Graph_get_uri (GraphObject *self, void *closure)
  82. {
  83. PyObject *uri = PyUnicode_FromString (
  84. LSUP_graph_uri (self->ob_struct)->data);
  85. if ( UNLIKELY (!uri)) return NULL;
  86. Py_INCREF(uri);
  87. return uri;
  88. }
  89. static int
  90. Graph_set_uri (GraphObject *self, PyObject *value, void *closure)
  91. {
  92. if (!PyObject_TypeCheck (value, &TermType)) return -1;
  93. LSUP_rc rc = LSUP_graph_set_uri (
  94. self->ob_struct, ((TermObject*)value)->ob_struct->data);
  95. return rc == LSUP_OK ? 0 : -1;
  96. }
  97. static PyGetSetDef Graph_getsetters[] = {
  98. {
  99. "uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
  100. "Graph URI.", NULL
  101. },
  102. {NULL}
  103. };
  104. static PyObject *
  105. Graph_copy (PyTypeObject *cls, PyObject *src)
  106. {
  107. if (! PyObject_TypeCheck (src, cls)) return NULL;
  108. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  109. if (!res) return NULL;
  110. res->ob_struct = LSUP_graph_copy (((GraphObject *) src)->ob_struct);
  111. Py_INCREF(res);
  112. return (PyObject *) res;
  113. }
  114. static PyObject *
  115. Graph_new_from_rdf (PyTypeObject *cls, PyObject *args)
  116. {
  117. Py_buffer *buf;
  118. const char *type;
  119. if (! PyArg_ParseTuple (args, "s*s", &buf, &type)) return NULL;
  120. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  121. if (!res) goto fail;
  122. const LSUP_Codec *codec;
  123. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  124. // TODO other codecs here.
  125. else {
  126. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  127. goto fail;
  128. }
  129. size_t ct;
  130. char *err;
  131. codec->decode_graph (buf->buf, &res->ob_struct, &ct, &err);
  132. TRACE ("Decoded %lu triples.\n", ct);
  133. if (!UNLIKELY (err)) goto fail;
  134. PyBuffer_Release (buf);
  135. Py_INCREF(res);
  136. return (PyObject *) res;
  137. fail:
  138. PyBuffer_Release (buf);
  139. return NULL;
  140. }
  141. static PyObject *
  142. Graph_richcmp (PyObject *self, PyObject *other, int op)
  143. {
  144. // Only equality and non-equality are supported.
  145. if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
  146. LSUP_Graph *t1 = ((GraphObject *) self)->ob_struct;
  147. LSUP_Graph *t2 = ((GraphObject *) other)->ob_struct;
  148. if (LSUP_graph_equals (t1, t2) ^ (op == Py_NE)) Py_RETURN_TRUE;
  149. Py_RETURN_FALSE;
  150. }
  151. static inline PyObject *
  152. Graph_bool_op (
  153. PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
  154. {
  155. if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
  156. return NULL;
  157. GraphObject *res = (GraphObject *) cls->tp_alloc (cls, 0);
  158. if (!res) return NULL;
  159. res->ob_struct = LSUP_graph_bool_op (
  160. op, ((GraphObject *) gr1)->ob_struct,
  161. ((GraphObject *) gr2)->ob_struct);
  162. Py_INCREF(res);
  163. return (PyObject *) res;
  164. }
  165. static PyObject *
  166. Graph_add (PyObject *self, PyObject *triples)
  167. {
  168. // Triple may be any iterable.
  169. PyObject *iter = PyObject_GetIter (triples);
  170. if (! iter) {
  171. PyErr_SetString (
  172. PyExc_ValueError, "Triples object cannot be iterated.");
  173. return NULL;
  174. }
  175. PyObject *trp_obj = NULL;
  176. int rc = 0;
  177. size_t i;
  178. LSUP_SerTriple *sspo = LSUP_striple_new (BUF_DUMMY, BUF_DUMMY, BUF_DUMMY);
  179. LSUP_GraphIterator *it = LSUP_graph_add_init (
  180. ((GraphObject *)self)->ob_struct);
  181. for (i = 0; (trp_obj = PyIter_Next (iter)); i++) {
  182. if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
  183. PyErr_SetString (
  184. PyExc_ValueError, "Object is not a triple.");
  185. rc = -1;
  186. goto finalize;
  187. }
  188. TRACE ("Inserting triple #%lu\n", i);
  189. LSUP_triple_serialize (((TripleObject *)trp_obj)->ob_struct, sspo);
  190. LSUP_rc db_rc = LSUP_graph_add_iter (it, sspo);
  191. if (db_rc == LSUP_OK) rc = LSUP_OK;
  192. if (UNLIKELY (db_rc < 0)) {
  193. rc = -1;
  194. goto finalize;
  195. }
  196. }
  197. finalize:
  198. LSUP_graph_add_done (it);
  199. LSUP_striple_free (sspo);
  200. PyObject *ret = PyLong_FromSize_t (LSUP_graph_iter_cur (it));
  201. if (rc == 0) {
  202. Py_INCREF (ret);
  203. return ret;
  204. }
  205. return NULL;
  206. }
  207. // TODO
  208. static int Graph_remove (PyObject *self, PyObject *s, PyObject *p, PyObject *o)
  209. {
  210. return 0;
  211. }
  212. // TODO
  213. static PyObject *Graph_lookup (
  214. PyObject *self, PyObject *s, PyObject *p, PyObject *o)
  215. {
  216. return NULL;
  217. }
  218. static PyObject *
  219. Graph_encode (PyObject *self, PyObject *args)
  220. {
  221. char *type;
  222. if (! PyArg_ParseTuple (args, "s", &type)) return NULL;
  223. const LSUP_Codec *codec;
  224. if (strcmp(type, "nt") == 0) codec = &nt_codec;
  225. // TODO other codecs here.
  226. else {
  227. PyErr_SetString (PyExc_ValueError, "Unsupported codec.");
  228. return NULL;
  229. }
  230. LSUP_CodecIterator *it = codec->encode_graph_init (
  231. ((GraphObject *)self)->ob_struct);
  232. // Initialize the generator object.
  233. //StringIteratorType *it_type;
  234. StringIteratorObject *it_obj = PyObject_New (
  235. StringIteratorObject, &StringIteratorType);
  236. if (!it_obj) return NULL;
  237. it_obj->it = it;
  238. Py_INCREF (it_obj);
  239. return (PyObject *)it_obj;
  240. }
  241. static PyMethodDef Graph_methods[] = {
  242. {"copy", (PyCFunction) Graph_copy, METH_CLASS, "Copy a graph."},
  243. {
  244. "from_rdf", (PyCFunction) Graph_new_from_rdf, METH_CLASS,
  245. "Create a graph from a RDF file."
  246. },
  247. {"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
  248. {
  249. "remove", (PyCFunction) Graph_remove, METH_VARARGS,
  250. "Remove triples from a graph by matching a pattern."
  251. },
  252. {
  253. "lookup", (PyCFunction) Graph_lookup, METH_VARARGS,
  254. "Look triples in a graph by matching a pattern."
  255. },
  256. {
  257. "to_rdf", (PyCFunction) Graph_encode, METH_VARARGS,
  258. "Encode a graph into a RDF byte buffer."
  259. },
  260. {NULL},
  261. };
  262. static inline PyObject *Graph_bool_and (
  263. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  264. { return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
  265. static inline PyObject *Graph_bool_or (
  266. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  267. { return Graph_bool_op (cls, LSUP_BOOL_UNION, gr1, gr2); }
  268. static inline PyObject *Graph_bool_subtract (
  269. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  270. { return Graph_bool_op (cls, LSUP_BOOL_SUBTRACTION, gr1, gr2); }
  271. static inline PyObject *Graph_bool_xor (
  272. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  273. { return Graph_bool_op (cls, LSUP_BOOL_XOR, gr1, gr2); }
  274. static PyNumberMethods Graph_number_methods = {
  275. .nb_and = (binaryfunc) Graph_bool_and,
  276. .nb_or = (binaryfunc) Graph_bool_or,
  277. .nb_subtract = (binaryfunc) Graph_bool_subtract,
  278. .nb_xor = (binaryfunc) Graph_bool_xor,
  279. };
  280. static int
  281. Graph_contains (PyObject *self, PyObject *value)
  282. {
  283. if (!PyObject_TypeCheck (value, &TripleType)) {
  284. PyErr_SetString (PyExc_ValueError, "Error parsing input value.");
  285. return -1;
  286. }
  287. int rc = LSUP_graph_contains (
  288. ((GraphObject *) self)->ob_struct,
  289. ((TripleObject *) value)->ob_struct);
  290. return rc;
  291. }
  292. static Py_ssize_t
  293. Graph_get_size (PyObject *self)
  294. { return LSUP_graph_size (((GraphObject *) self)->ob_struct); }
  295. static PySequenceMethods Graph_seq_methods = {
  296. .sq_length = (lenfunc) Graph_get_size,
  297. .sq_contains = (objobjproc) Graph_contains,
  298. };
  299. PyTypeObject GraphType = {
  300. PyVarObject_HEAD_INIT(NULL, 0)
  301. .tp_name = "graph.Graph",
  302. .tp_doc = "RDF graph",
  303. .tp_basicsize = sizeof (GraphObject),
  304. .tp_itemsize = 0,
  305. .tp_flags = Py_TPFLAGS_DEFAULT,
  306. .tp_new = PyType_GenericNew,
  307. .tp_init = (initproc) Graph_init,
  308. .tp_dealloc = (destructor) Graph_dealloc,
  309. .tp_getset = Graph_getsetters,
  310. .tp_methods = Graph_methods,
  311. .tp_richcompare = (richcmpfunc) Graph_richcmp,
  312. .tp_as_number = &Graph_number_methods,
  313. .tp_as_sequence = &Graph_seq_methods,
  314. };
  315. #endif