Coverage report: /development/source/library/com/dydra/gitlab/dydra-cgi/ffi/lisp/rdfcache/prelude.lisp
| Kind | Covered | All | % |
| expression | 49 | 196 | 25.0 |
| branch | 1 | 6 | 16.7 |
Key
Not instrumented
Conditionalized out
Executed
Not executed
Both branches taken
One branch taken
Neither branch taken
3
(defmacro string->keyword (string)
4
`(values (intern (string-upcase ,string) "KEYWORD")))
6
(defmacro keyword->string (keyword)
7
"Returns the lowercase string corresponding to the given KEYWORD."
8
`(write-to-string ,keyword :case :downcase :escape nil))
10
(defmacro all-integers? (&body terms)
11
`(and ,@(mapcar (lambda (term) `(integerp ,term)) terms)))
13
(defmacro any-integers? (&body terms)
14
`(or ,@(mapcar (lambda (term) `(integerp ,term)) terms)))
16
(defmacro all-nulls? (&body terms)
17
`(and ,@(mapcar (lambda (term) `(null ,term)) terms)))
19
(defmacro any-nulls? (&body terms)
20
`(or ,@(mapcar (lambda (term) `(null ,term)) terms)))
22
(defmacro any-ephemerals? (&body terms)
23
`(or ,@(mapcar (lambda (term) `(< ,term -2)) terms)))
25
(defmacro with-checked-pointer ((ptr) &rest body)
26
(let* ((ptr-var (gensym))
27
(body (or body (list ptr-var))))
28
`(let ((,ptr-var ,ptr))
29
(declare (type foreign-pointer ,ptr-var))
30
(if (null-pointer-p ,ptr-var)
31
(cl:error "~A is a null pointer." ,ptr-var)
34
(defmacro with-checked-pointers ((&rest ptrs) &rest body)
35
(let ((result `(progn ,@body)))
36
(dolist (ptr (reverse ptrs))
37
(setf result `(with-checked-pointer (,ptr) ,result)))
40
(defmacro with-checked-errno-result (cfun-name &rest body)
41
(let ((errno-var (gensym)))
42
`(let ((,errno-var (abs (progn ,@body))))
43
(declare (type integer ,errno-var))
44
(if (zerop ,errno-var)
45
(values) ;; no meaningful return value
46
(foreign-function-error ,errno-var ,cfun-name)))))
48
(defmacro with-checked-ssize-result (cfun-name &rest body)
49
(let ((ssize-var (gensym))
51
`(let ((,ssize-var (progn ,@body)))
52
(declare (type integer ,ssize-var))
55
(let ((,errno-var (abs ,ssize-var)))
56
(declare (type integer ,errno-var))
57
(foreign-function-error ,errno-var ,cfun-name))))))
59
(defmacro with-checked-int-result (cfun-name &rest body)
60
`(with-checked-ssize-result ,cfun-name ,@body))
62
(defmacro with-checked-long-result (cfun-name &rest body)
63
`(with-checked-ssize-result ,cfun-name ,@body))
65
(defmacro with-checked-bool-result (cfun-name &rest body)
66
`(with-checked-ssize-result ,cfun-name ,@body))
68
(defmacro with-checked-pointer-result (cfun-name &rest body)
69
`(progn ,@body)) ;; TODO
71
(defun count-wildcards (&rest term-numbers)
72
(count-if #'zerop term-numbers))
73
(in-package :rdfcache)
75
(defcenum (struct-type :int)
76
(:none 0) :id :term :triple :quad :graph :index :revision :unused1 :unused2 :cursor :transaction :term-cursor)
80
(defctype size_t :uint)
81
(defctype ssize_t :int)
82
(defctype off_t :int) ; FIXME: this varies based on a number of considerations
83
(defctype wchar_t :int)
85
(defcvar ("errno" %errno :read-only t) :int)
87
(declaim (inline %%memcpy))
88
(defcfun ("memcpy" %%memcpy) :pointer (dst :pointer) (src :pointer) (len size_t))
90
(declaim (inline %%strerror))
91
(defcfun ("strerror" %%strerror) :pointer (errnum :int))
93
(defun %strerror (errnum)
94
(declare (type integer errnum))
95
(foreign-string-to-lisp (%%strerror errnum)))
100
(data :uint8 :count 16))
102
(defcstruct uuid-string
103
(data :char :count 37))
107
(defcenum (term-type :int)
108
(:none 0) (:uri 1) (:node 2) (:literal 5) (:default-context 255)) ;; defined in libdgrdf
133
(defctype term-number :int64)
135
(defcstruct term-numbers
136
(subject-number :int32)
137
(predicate-number :int32)
138
(object-number :int32)
139
(context-number :int32))
148
(term-numbers :pointer)
152
;; second cache line:
158
;; fourth cache line:
161
(defcenum (term-position :int)
162
(:any -1) ;; RDF_TERM_ANY
163
(:subject 0) ;; RDF_TERM_SUBJECT
164
(:predicate 1) ;; RDF_TERM_PREDICATE
165
(:object 2) ;; RDF_TERM_OBJECT
166
(:context 3)) ;; RDF_TERM_CONTEXT
168
(defcenum (term-cursor-position :int)
177
(defcstruct term-cursor
178
(unused-was-flags :int)
179
(term-position term-cursor-position)
180
(term-number term-number)
181
(unused-was-term-count :uint64)
182
(opaque-ptr :pointer)
183
(padding :uint8 :count 48))
187
(defcenum (transaction-status :int)
195
(defcstruct transaction
196
(status transaction-status)
198
(repository-name :string)
199
(parent-revision-uuid uuid)
200
(delete-count :uint32)
201
(insert-count :uint32)
202
(context-map :pointer)
205
(term-data :pointer))
209
(defcallback string-callback :boolean ((argument :string))
210
(funcall (symbol-value '*current-callback*) argument))
211
(in-package :rdfcache)
213
;; src/rdfcache/cursor.c
215
(defcfunwi ("rdfcache_ffi_fetch_term" %%cursor-fetch-term) :int (term-number term-number) (term-pointer :pointer))
217
(defcfun ("spocq_thread_init" %%thread-init) :void)
218
(defcfun ("spocq_thread_exit" %%thread-exit) :void)
220
;; src/rdfcache/features.c
222
(defcfun ("spocq_feature_exists" %%has-feature) :boolean (feature-name :string))
223
(defcfun ("spocq_module_exists" %%has-module) :boolean (module-name :string))
225
(defcfun ("spocq_feature_enumerate" %%each-feature) :void (callback :pointer))
226
(defcfun ("spocq_module_enumerate" %%each-module) :void (callback :pointer))
228
;; src/rdfcache/ffi.c
230
(defcfun ("spocq_thread_boost" boost) :void (priority :int))
231
(defcfun ("spocq_thread_yield" yield) :void)
233
(defcfun ("rdfcache_ffi_lookup_term_number" %%lookup-term-number) :long (term-pointer :pointer))
235
(defcfun ("rdfcache_ffi_count" %%count) :long (revision uuid) (c term-number) (s term-number) (p term-number) (o term-number))
236
(defcfun ("rdfcache_ffi_match" %%match) :long (revision uuid) (c term-number) (s term-number) (p term-number) (o term-number) (cursor cursor))
237
(defcfun ("rdfcache_ffi_match_with_filter" %%match-with-filter) :long (revision uuid) (c term-number) (s term-number) (p term-number) (o term-number) (min term-number) (max term-number) (cursor cursor))
239
(defcfunwi ("rdfcache_ffi_intern_term" %%intern-term) :int64 (type term-type) (value :string) (language :string) (datatype :string))
241
(defcfunwi ("rdfcache_ffi_generate_node" generate-node) :long)
243
;; src/rdfcache/repository.c
245
(defcfun ("spocq_repository_revision" %%repository-revision) :string (repository-name :string) (revision-designator :string) (flags :uint))
247
;; src/rdfcache/shard.c
249
(defcfun ("dydra_storage_attach" %%attach) :void)
250
(defcfun ("dydra_storage_detach" %%detach) :void)
252
;; src/rdfcache/sparql_query.c
254
(declaim (inline %%sparql-same-term))
255
(defcfun ("spocq_sparql_same_term" %%sparql-same-term) :boolean (term1-number term-number) (term2-number term-number))
257
(declaim (inline %%sparql-equal))
258
(defcfun ("spocq_sparql_equal" %%sparql-equal) :int (term1-number term-number) (term2-number term-number))
260
(declaim (inline %%sparql-is-uri))
261
(defcfun ("spocq_sparql_is_uri" %%sparql-is-uri) :boolean (term-number term-number))
263
(declaim (inline %%sparql-is-blank))
264
(defcfun ("spocq_sparql_is_blank" %%sparql-is-blank) :boolean (term-number term-number))
266
(declaim (inline %%sparql-is-literal))
267
(defcfun ("spocq_sparql_is_literal" %%sparql-is-literal) :boolean (term-number term-number))
269
(declaim (inline %%sparql-is-numeric))
270
(defcfun ("spocq_sparql_is_numeric" %%sparql-is-numeric) :boolean (term-number term-number))
272
(declaim (inline %%sparql-strlen))
273
(defcfunwi ("spocq_sparql_strlen" %%sparql-strlen) :long (term-number term-number))
275
;; src/rdfcache/sparql_update.c
277
(defcfun ("spocq_sparql_add" %%sparql-add) :int (transaction :pointer) (graph1-uri :string) (graph2-uri :string) (silent :boolean))
278
(defcfun ("spocq_sparql_clear" %%sparql-clear) :int (transaction :pointer) (graph-uri :string) (silent :boolean))
279
(defcfun ("spocq_sparql_copy" %%sparql-copy) :int (transaction :pointer) (graph1-uri :string) (graph2-uri :string) (silent :boolean))
280
(defcfun ("spocq_sparql_create" %%sparql-create) :int (transaction :pointer) (graph-uri :string) (silent :boolean))
281
(defcfun ("spocq_sparql_drop" %%sparql-drop) :int (transaction :pointer) (graph-uri :string) (silent :boolean))
282
(defcfun ("spocq_sparql_load" %%sparql-load) :int (transaction :pointer) (graph-uri :string) (from-url :string) (silent :boolean))
283
(defcfun ("spocq_sparql_move" %%sparql-move) :int (transaction :pointer) (graph1-uri :string) (graph2-uri :string) (silent :boolean))
285
;; src/rdfcache/term_cursor.c
287
(defcfun ("rdfcache_term_cursor_alloc" %%term-cursor-alloc) :pointer)
288
(defcfun ("rdfcache_term_cursor_free" %%term-cursor-free) :void (cursor-pointer :pointer))
289
(defcfun ("rdfcache_term_cursor_init" %%term-cursor-init) :int (cursor-pointer :pointer) (flags :int) (term-position term-cursor-position))
290
(defcfun ("rdfcache_term_cursor_dispose" %%term-cursor-dispose) :int (cursor-pointer :pointer))
291
(defcfun ("rdfcache_term_cursor_open" %%term-cursor-open) :int (cursor-pointer :pointer) (transaction-uuid-pointer uuid) (context-no term-number))
292
(defcfun ("rdfcache_term_cursor_count" %%term-cursor-count) :long (cursor-pointer :pointer))
293
(defcfun ("rdfcache_term_cursor_next" %%term-cursor-next) term-number (cursor-pointer :pointer))
294
(defcfun ("rdfcache_term_cursor_skip" %%term-cursor-skip) :long (cursor-pointer :pointer) (count :ulong))
295
(defcfun ("rdfcache_term_cursor_close" %%term-cursor-close) :int (cursor-pointer :pointer))
297
;; src/rdfcache/transaction.c
299
(defcvar ("rdfcache_transaction" *current-transaction*) :pointer)
301
(defcfunwi ("rdfcache_transaction_clone" %%transaction-clone) :pointer (transaction-pointer :pointer) (uuid-pointer :pointer))
303
(defcfunwi ("rdfcache_transaction_init2" %%transaction-init) :int (transaction-pointer :pointer) (uuid-pointer :pointer) (repository-name :string) (revision-id :string) (flags :uint))
304
(defcfunwi ("rdfcache_transaction_dispose" %%transaction-dispose) :int (transaction-pointer :pointer))
306
(defcfunwi ("rdfcache_transaction_begin" %%transaction-begin) :int (transaction-pointer :pointer))
307
(defcfunwi ("rdfcache_transaction_abort" %%transaction-abort) :int (transaction-pointer :pointer))
308
(defcfunwi ("rdfcache_transaction_commit" %%transaction-commit) :int (transaction-pointer :pointer))
310
(defcfunwi ("rdfcache_transaction_insert_fast" %%transaction-insert) :int (transaction-pointer :pointer) (c term-number) (s term-number) (p term-number) (o term-number))
311
(defcfunwi ("rdfcache_transaction_delete_fast" %%transaction-delete) :int (transaction-pointer :pointer) (c term-number) (s term-number) (p term-number) (o term-number))
313
(defcfunwi ("rdfcache_transaction_fetch_term" %%transaction-fetch-term) :int (transaction-pointer :pointer) (term-number :int64) (term-pointer :pointer))
314
(defcfunwi ("rdfcache_transaction_lookup_term" %%transaction-lookup-term) :int64 (transaction-pointer :pointer) (type term-type) (value :string) (language :string) (datatype :string))
315
(defcfunwi ("rdfcache_transaction_intern_term" %%transaction-intern-term) :int64 (transaction-pointer :pointer) (type term-type) (value :string) (language :string) (datatype :string))
317
(defcfun ("rdfcache_transaction_debug" %%transaction-debug) :void (transaction-pointer :pointer))
319
;; src/rdfcache/uuid.c
321
(cffi:defcfun ("spocq_uuid_init" %%uuid-init) :int (uuid-pointer :pointer))
322
(cffi:defcfun ("spocq_uuid_is_null" %%uuid-empty) :int (uuid-pointer :pointer))
323
(cffi:defcfun ("spocq_uuid_generate" %%uuid-generate) :int (uuid-pointer :pointer))
324
(cffi:defcfun ("spocq_uuid_serialize" %%uuid-serialize) :int (uuid-pointer :pointer) (buffer-pointer :pointer) (buffer-size size_t))
325
(cffi:defcfun ("spocq_uuid_parse" %%uuid-parse) :int (uuid-pointer :pointer) (uuid-string :string))
327
;; src/rdfcache/utility.c
329
(cffi:defcfun ("spocq_thread_time" time-in-thread) :uint64)
331
(defcvar ("rdfcache_default_context" %default-context-pointer :read-only t) term)
333
;(defcvar ("rdfcache_default_context_id" %default-context-id :read-only t) id)
335
;;; UUID API: Accessors
337
(defun uuid-to-string (uuid-pointer)
338
(declare (type foreign-pointer uuid-pointer))
339
(with-foreign-object (uuid-string '(:struct uuid-string))
340
(with-checked-ssize-result "spocq_uuid_serialize"
341
(%%uuid-serialize uuid-pointer uuid-string 37))
342
(foreign-string-to-lisp uuid-string)))
346
(defmacro with-uuid ((uuid-var) &body body)
347
`(with-foreign-object (,uuid-var '(:struct uuid))
350
;;; UUID API: Debugging
352
(defun print-uuid (uuid-pointer &optional (stream *standard-output*))
353
(print-unreadable-object (uuid-pointer stream :identity nil :type nil)
354
(format stream "rdfcache:uuid ~8,'0x: ~s"
355
(pointer-address uuid-pointer)
356
(uuid-to-string uuid-pointer))))
360
(defun resolve-repository (repository-name &key revision)
361
(declare (type string repository-name)
362
(type (or string null) revision))
363
;; !! do not change this logic until and unless _all_ store installations do not require it.
364
(when (stringp revision)
365
(if (string-equal "head" revision)
366
;; head must be upcase, but for the revision id
367
(setf revision (string-upcase revision))
368
;; 2017-10-05 an uppercase string crashed the store
369
(setf revision (string-downcase revision))))
370
(%%repository-revision repository-name (or revision "") 0))
374
(defun make-triple (subject predicate object &key id)
375
"Returns a foreign pointer to a newly-allocated triple."
376
(let ((triple (%make-triple (or subject (null-pointer))
377
(or predicate (null-pointer))
378
(or object (null-pointer)))))
379
;; TODO: set the triple identifier, if given.