-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathndb-quads.lisp
More file actions
149 lines (126 loc) · 5.27 KB
/
ndb-quads.lisp
File metadata and controls
149 lines (126 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
;;; Copyright (c) 2022 Max-Gerd Retzlaff <mgr@matroid.org>, Datagraph GmbH.
;;; Distributed under the terms of the GNU General Public License, Version 2.0,
;;; see file LICENSE in the top level directory of this repository.
(in-package :ndb.quads)
#|
create table as:
create table test
(g int unsigned not null, s int unsigned not null,
p int unsigned not null, o int unsigned not null,
index gspo (g,s,p,o), index gpos (g,p,o,s), index gosp (g,o,s,p),
index spog (s,p,o,g), index posg (p,o,s,g), index ospg (o,s,p,g),
unique (g,s,p,o));
load data with:
load data infile '/path/to/data.tsv' into table test;
|#
(cffi:defcstruct quad-vis
(:g :unsigned-int)
(:s :unsigned-int)
(:p :unsigned-int)
(:o :unsigned-int)
(:vis-size :uint16) ;; two bytes contain size of varbinary
(:visibility :uint8 :count #.(/ 28000 (cffi:foreign-type-size :uint8)))
;;(:visibility :uint32 :count #.(/ 28000 (cffi:foreign-type-size :uint32)))
)
(cffi:defcstruct quad
(:g :unsigned-int)
(:s :unsigned-int)
(:p :unsigned-int)
(:o :unsigned-int))
(cffi:defcstruct triple
(:s :unsigned-int)
(:p :unsigned-int)
(:o :unsigned-int))
(cffi:defcstruct tuple
(:s :unsigned-int)
(:p :unsigned-int))
(cffi:defcstruct single
(:s :unsigned-int))
(defconstant +quad-size+ (cffi:foreign-type-size '(:struct ndb.quads:quad)))
(defconstant +triple-size+ (cffi:foreign-type-size '(:struct ndb.quads:triple)))
(defconstant +tuple-size+ (cffi:foreign-type-size '(:struct ndb.quads:tuple)))
(defconstant +single-size+ (cffi:foreign-type-size '(:struct ndb.quads:single)))
(defconstant +quad-count+ (/ +quad-size+ +single-size+))
(defconstant +triple-count+ (/ +triple-size+ +single-size+))
(defconstant +tuple-count+ (/ +tuple-size+ +single-size+))
(defconstant +single-count+ (/ +single-size+ +single-size+))
#|
(defparameter *test* (cffi:convert-to-foreign (list :s 32 :p 42) '(:struct ndb.quads:tuple)))
*TEST*
(cffi:foreign-slot-value *test* '(:struct ndb.quads:tuple) :s)
32
(cffi:foreign-slot-value *test* '(:struct ndb.quads:tuple) :p)
42
(cffi:mem-aref *test* :uint32 0)
32
(cffi:mem-aref *test* :uint32 1)
42
(cffi:convert-from-foreign *test* '(:struct ndb.quads:tuple))
(:P 42 :S 32)
(cffi:free-converted-object *test* '(:struct ndb.quads:tuple) t)
|#
#|
(ndbapi:with-foreign-struct (high (list :s 1109 :p 1105 :o 1106 :g 1108)
'(:struct ndb.quads:quad))
(cffi:convert-from-foreign high '(:struct ndb.quads:quad)))
(:G 1108 :O 1106 :P 1105 :S 1109)
|#
(defmacro with-foreign-quad ((var initform &optional alloc-params) &body body)
`(ndbapi:with-foreign-struct (,var
,initform
'(:struct ndb.quads:quad)
,alloc-params)
,@body))
(defun list-to-quad (list)
"take spog list and return as quad (given as property list)"
(destructuring-bind (&optional (g 0) (s 0) (p 0) (o 0) (visibility nil) (vis-size 0)) list
(if visibility
(list :g g :s s :p p :o o :visibility visibility :vis-size vis-size)
(list :g g :s s :p p :o o))))
(defun list-to-quad* (&rest list)
(list-to-quad list))
(defun decode-visibility (visibility vis-size &key word-size)
(let* ((word-size (if (and word-size
(not (eq word-size t)))
word-size :uint8))
(count (floor vis-size (cffi:foreign-type-size word-size)))
(vector (make-array count)))
(dotimes (index count)
(setf (aref vector index) (cffi:mem-aref visibility word-size index)))
vector))
(defun decode-visibility-size (vis-size &key word-size)
(let* ((word-size (if (and word-size
(not (eq word-size t)))
word-size :uint8))
(count (floor vis-size (cffi:foreign-type-size word-size))))
count))
(defun quad-to-list (quad &key (decode-visibility :uint8))
"deconstruct quad (given as property list) and return as spog list"
(destructuring-bind (&key (g 0) (s 0) (p 0) (o 0) (visibility nil) (vis-size 0)) quad
(if visibility
(list g s p o
(if decode-visibility
(decode-visibility-size vis-size :word-size decode-visibility)
vis-size)
(if decode-visibility
(decode-visibility visibility vis-size :word-size decode-visibility)
visibility))
(list g s p o))))
(defun quad-to-list* (&rest quad)
(quad-to-list quad))
(defun convert-foreign-quad (sap)
(cffi:convert-from-foreign sap '(:struct ndb.quads:quad-vis)))
#+(or)
(defun %quad-to-list (%quad &key decode-visibility)
(declare (ignore decode-visibility))
(list (cffi:mem-aref %quad :unsigned-int 0)
(cffi:mem-aref %quad :unsigned-int 1)
(cffi:mem-aref %quad :unsigned-int 2)
(cffi:mem-aref %quad :unsigned-int 3)))
(defmacro %quad-g (q) `(cffi:foreign-slot-value ,q '(:struct quad) :g))
(defmacro %quad-s (q) `(cffi:foreign-slot-value ,q '(:struct quad) :s))
(defmacro %quad-p (q) `(cffi:foreign-slot-value ,q '(:struct quad) :p))
(defmacro %quad-o (q) `(cffi:foreign-slot-value ,q '(:struct quad) :o))
(defun %quad-to-list (%quad &key decode-visibility)
(declare (ignore decode-visibility))
(list (%quad-g %quad) (%quad-s %quad) (%quad-p %quad) (%quad-o %quad)))