graph_obj.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "triple_obj.h"
  8. typedef struct {
  9. PyObject_HEAD
  10. LSUP_Graph *ob_struct;
  11. } GraphObject;
  12. static int
  13. Graph_init (GraphObject *self, PyObject *args, PyObject *kwargs)
  14. {
  15. unsigned char store_type;
  16. if (!PyArg_ParseTuple (args, "b", &store_type))
  17. return -1;
  18. self->ob_struct = LSUP_graph_new ((LSUP_store_type)store_type);
  19. if (!self->ob_struct) {
  20. PyErr_SetString (PyExc_ValueError, "Could not create graph.");
  21. return -1;
  22. }
  23. return 0;
  24. }
  25. static void
  26. Graph_dealloc (GraphObject *self)
  27. {
  28. LSUP_graph_free (self->ob_struct);
  29. Py_TYPE (self)->tp_free ((PyObject *) self);
  30. }
  31. static PyObject *
  32. Graph_get_size (GraphObject *self, void *closure)
  33. {
  34. size_t gr_size = LSUP_graph_size (self->ob_struct);
  35. PyObject *size = PyLong_FromSize_t (gr_size);
  36. Py_INCREF (size);
  37. return size;
  38. }
  39. /*
  40. * FIXME This should be deleted and the setter set to NULL or omitted, but
  41. * in doing so Python 3.8 raises a SystemError: bad call flags. See
  42. * https://bugs.python.org/issue39884 No resolution is clear yet.
  43. */
  44. static int
  45. Graph_set_size (GraphObject *self, PyObject value, void *closure)
  46. { return -1; }
  47. static PyObject *
  48. Graph_get_uri (GraphObject *self, void *closure)
  49. {
  50. PyObject *uri = PyUnicode_FromString (
  51. LSUP_graph_uri (self->ob_struct)->data);
  52. Py_INCREF (uri);
  53. return uri;
  54. }
  55. static int
  56. Graph_set_uri (GraphObject *self, PyObject *uri, void *closure)
  57. {
  58. if (PyUnicode_READY (uri) != 0) return -1; // TODO redundant?
  59. if (PyUnicode_KIND (uri) != PyUnicode_1BYTE_KIND) return -1;
  60. LSUP_rc rc = LSUP_graph_set_uri (self->ob_struct, PyUnicode_DATA (uri));
  61. return rc == LSUP_OK ? 0 : -1;
  62. }
  63. static PyGetSetDef Graph_getsetters[] = {
  64. {
  65. .name = "__len__",
  66. .get = (getter) Graph_get_size,
  67. .set = (setter) Graph_set_size,
  68. .doc = "Number of triples in the graph.",
  69. //.closure = NULL,
  70. },
  71. {
  72. "uri", (getter) Graph_get_uri, (setter) Graph_set_uri,
  73. "Graph URI.", NULL
  74. },
  75. {NULL}
  76. };
  77. static PyObject *
  78. Graph_copy (PyTypeObject *cls, PyObject *src)
  79. {
  80. if (! PyObject_TypeCheck (src, cls)) return NULL;
  81. GraphObject *res = (GraphObject *) cls->tp_alloc(cls, 0);
  82. if (!res) return NULL;
  83. res->ob_struct = LSUP_graph_copy (((GraphObject *) src)->ob_struct);
  84. Py_INCREF(res);
  85. return (PyObject *) res;
  86. }
  87. /* TODO implement LSUP_graph_equals
  88. static PyObject *
  89. Graph_richcmp (PyObject *gr1, PyObject *gr2, int op)
  90. {
  91. if (op != Py_EQ && op != Py_NE) Py_RETURN_NOTIMPLEMENTED;
  92. PyObject *res = NULL;
  93. LSUP_Graph *t1 = ((GraphObject *) gr1)->ob_struct;
  94. LSUP_Graph *t2 = ((GraphObject *) gr2)->ob_struct;
  95. if (LSUP_graph_equals (t1, t2) && op == Py_EQ) Py_RETURN_TRUE;
  96. Py_RETURN_FALSE;
  97. }
  98. */
  99. /* TODO Implement mdbstore bool ops
  100. static inline PyObject *
  101. Graph_bool_op (
  102. PyTypeObject *cls, LSUP_bool_op op, PyObject *gr1, PyObject *gr2)
  103. {
  104. if (! PyObject_TypeCheck (gr1, cls) || ! PyObject_TypeCheck (gr2, cls))
  105. return NULL;
  106. GraphObject *res = (GraphObject *) cls->tp_alloc (cls, 0);
  107. if (!res) return NULL;
  108. res->ob_struct = LSUP_graph_bool_op (
  109. op, ((GraphObject *) gr1)->ob_struct,
  110. ((GraphObject *) gr2)->ob_struct);
  111. Py_INCREF(res);
  112. return (PyObject *) res;
  113. }
  114. */
  115. static int Graph_add (PyObject *self, PyObject *triples)
  116. {
  117. // Triple may be any iterable.
  118. if (! PyIter_Check (triples)) return -1;
  119. PyObject *trp_obj = NULL;
  120. int rc = 0;
  121. size_t i;
  122. LSUP_SerTriple *sspo = STRP_DUMMY;
  123. LSUP_GraphIterator *it = LSUP_graph_add_stream_init (
  124. ((GraphObject *)self)->ob_struct);
  125. for (i = 0; (trp_obj = PyIter_Next (triples)); i++) {
  126. if (!PyObject_TypeCheck (trp_obj, &TripleType)) {
  127. rc = -1;
  128. goto finalize;
  129. }
  130. TRACE ("Inserting triple #%lu\n", i);
  131. LSUP_triple_serialize (((TripleObject *)trp_obj)->ob_struct, sspo);
  132. LSUP_rc db_rc = LSUP_graph_add_stream_iter (it, sspo);
  133. if (db_rc == LSUP_OK) rc = LSUP_OK;
  134. if (UNLIKELY (db_rc < 0)) {
  135. rc = -1;
  136. goto finalize;
  137. }
  138. }
  139. LSUP_striple_free (sspo);
  140. finalize:
  141. LSUP_graph_add_stream_done (it);
  142. LSUP_striple_free (sspo);
  143. return rc;
  144. }
  145. static int Graph_remove (PyObject *self, PyObject *s, PyObject *p, PyObject *o)
  146. {
  147. return 0;
  148. }
  149. static PyObject *Graph_lookup (
  150. PyObject *self, PyObject *s, PyObject *p, PyObject *o)
  151. {
  152. return NULL;
  153. }
  154. static PyMethodDef Graph_methods[] = {
  155. {"copy", (PyCFunction) Graph_copy, METH_CLASS, "Copy a graph."},
  156. {"add", (PyCFunction) Graph_add, METH_O, "Add triples to a graph."},
  157. {
  158. "remove", (PyCFunction) Graph_remove, METH_VARARGS,
  159. "Remove triples from a graph by matching a pattern."
  160. },
  161. {
  162. "lookup", (PyCFunction) Graph_lookup, METH_VARARGS,
  163. "Look triples in a graph by matching a pattern."
  164. },
  165. };
  166. /* TODO Implement mdbstore bool ops
  167. static inline PyObject *Graph_bool_and (
  168. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  169. { return Graph_bool_op (cls, LSUP_BOOL_INTERSECTION, gr1, gr2); }
  170. static inline PyObject *Graph_bool_or (
  171. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  172. { return Graph_bool_op (cls, LSUP_BOOL_UNION, gr1, gr2); }
  173. static inline PyObject *Graph_bool_subtract (
  174. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  175. { return Graph_bool_op (cls, LSUP_BOOL_SUBTRACTION, gr1, gr2); }
  176. static inline PyObject *Graph_bool_xor (
  177. PyTypeObject *cls, PyObject *gr1, PyObject *gr2)
  178. { return Graph_bool_op (cls, LSUP_BOOL_XOR, gr1, gr2); }
  179. static PyNumberMethods Graph_number_methods = {
  180. .nb_and = (binaryfunc) Graph_bool_and,
  181. .nb_or = (binaryfunc) Graph_bool_or,
  182. .nb_subtract = (binaryfunc) Graph_bool_subtract,
  183. .nb_xor = (binaryfunc) Graph_bool_xor,
  184. };
  185. */
  186. PyTypeObject GraphType = {
  187. PyVarObject_HEAD_INIT(NULL, 0)
  188. .tp_name = "graph.Graph",
  189. .tp_doc = "RDF graph",
  190. .tp_basicsize = sizeof(GraphObject),
  191. .tp_itemsize = 0,
  192. .tp_flags = Py_TPFLAGS_DEFAULT,
  193. .tp_new = PyType_GenericNew,
  194. .tp_init = (initproc) Graph_init,
  195. .tp_dealloc = (destructor) Graph_dealloc,
  196. .tp_methods = Graph_methods,
  197. .tp_getset = Graph_getsetters,
  198. //.tp_richcompare = Graph_richcmp, TODO implement LSUP_graph_equals
  199. //.tp_as_number = &Graph_number_methods,
  200. };
  201. #endif