-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathschema.lisp
411 lines (388 loc) · 20.5 KB
/
schema.lisp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
(in-package :graph-db)
(defstruct schema
(lock (make-recursive-lock))
(type-table
#+sbcl (make-hash-table :test 'eql :synchronized t)
#+ccl (make-hash-table :test 'eql :shared t)
#+lispworks (make-hash-table :test 'eql :single-thread nil))
(class-locks
#+sbcl (make-hash-table :test 'eql :synchronized t)
#+ccl (make-hash-table :test 'eql :shared t)
#+lispworks (make-hash-table :test 'eql :single-thread nil))
(next-edge-id 1 :type (unsigned-byte 16))
(next-vertex-id 1 :type (unsigned-byte 16)))
(defstruct node-type
name
parent-type
id
graph-name
slots
package
constructor)
(defgeneric instantiate-node-type (node-type-def graph))
(defmacro with-write-locked-class ((name graph) &body body)
`(let ((rw-lock (gethash ,name (schema-class-locks (schema ,graph)))))
(with-write-lock (rw-lock)
,@body)))
(defmacro with-read-locked-class ((name graph) &body body)
`(let ((rw-lock (gethash ,name (schema-class-locks (schema ,graph)))))
(with-read-lock (rw-lock)
,@body)))
(defun list-edge-types (&optional (graph *graph*))
(nconc (list 0)
(loop
for key being the hash-keys
in (gethash :edge (schema-type-table (schema graph)))
if (numberp key)
collecting key)))
(defun list-vertex-types (&optional (graph *graph*))
(nconc (list 0)
(loop
for key being the hash-keys
in (gethash :vertex (schema-type-table (schema graph)))
if (numberp key)
collecting key)))
(defmethod init-schema ((graph graph))
(let ((schema (make-schema)))
(setf (schema graph) schema)
(setf (gethash :edge (schema-type-table (schema graph)))
#+sbcl (make-hash-table :test 'eql :synchronized t)
#+ccl (make-hash-table :test 'eql :shared t)
#+lispworks (make-hash-table :test 'eql :single-thread nil))
(setf (gethash :vertex (schema-type-table (schema graph)))
#+sbcl (make-hash-table :test 'eql :synchronized t)
#+ccl (make-hash-table :test 'eql :shared t)
#+lispworks (make-hash-table :test 'eql :single-thread nil))
(setf (gethash 'edge (schema-class-locks schema))
(make-rw-lock))
(setf (gethash 'vertex (schema-class-locks schema))
(make-rw-lock))
(slot-value graph 'schema)))
(defmethod save-schema ((schema schema) (graph graph))
(with-recursive-lock-held ((schema-lock schema))
(let ((schema-file (format nil "~A/schema.dat" (location graph))))
(let ((locks (schema-class-locks schema))
(schema-lock (schema-lock schema)))
(setf (schema-class-locks schema) nil)
(setf (schema-lock schema) nil)
(cl-store:store (schema graph) schema-file)
(setf (schema-lock schema) schema-lock)
(setf (schema-class-locks schema) locks)
schema))))
(defmethod get-next-type-id ((schema schema) parent)
(with-recursive-lock-held ((schema-lock schema))
(cond ((or (eql parent :edge) (eql parent 'edge))
(prog1
(schema-next-edge-id schema)
(incf (schema-next-edge-id schema))))
((or (eql parent :vertex) (eql parent 'vertex))
(prog1
(schema-next-vertex-id schema)
(incf (schema-next-vertex-id schema))))
(t (error "Unknown parent type ~S" parent)))))
(defmethod schema-string-representation ((schema schema))
"Return a string representation of SCHEMA. Two schemas with the same
node structure will have EQUALP string representations. This is meant
for a quick test of replication compatibility, not guaranteed equality
testing."
(with-output-to-string (stream)
(loop with parent-alist = (alexandria:hash-table-alist
(schema-type-table schema))
for (parent . table) in (sort parent-alist #'string<
:key 'car)
do
(format stream "~A~%" parent)
(loop with node-types = (remove-if-not #'node-type-p
(alexandria:hash-table-values table))
for node-type in (sort node-types #'string<
:key 'node-type-name)
do
(format stream " ~A~%" (node-type-name node-type))
(loop with slots = (mapcar 'first (node-type-slots node-type))
for slot in (sort slots #'string<)
do (format stream " ~A~%" slot))))))
(defmethod schema-digest ((schema schema))
"Return a digest of the string representation of SCHEMA. Used in
replication for a quick schema compatibility check."
(with-output-to-string (stream)
(map nil
(lambda (octet)
(format stream "~(~2,'0X~)" octet))
(md5:md5sum-string (schema-string-representation schema)
:external-format :utf8))))
(defmethod all-node-types ((graph graph))
(let ((types nil))
(maphash (lambda (parent table)
(push (intern (symbol-name parent)) types)
(maphash (lambda (child ctable)
(declare (ignore ctable))
(when (and (not (keywordp child))
(not (numberp child)))
(push child types)))
table))
(schema-type-table (schema graph)))
types))
(defun lookup-node-type-by-id (id parent &key (graph *graph*))
(assert (and (integerp id) (>= id 0) (< id +max-node-types+)))
(let ((meta (gethash id (gethash parent (schema-type-table (schema graph))))))
meta))
(defun lookup-node-type-by-name (name parent &key (graph *graph*))
(let ((id (gethash name (gethash parent (schema-type-table (schema graph))))))
(when id
(lookup-node-type-by-id id parent :graph graph))))
(defmethod update-node-type ((meta node-type) (graph graph))
(setf (gethash (node-type-id meta)
(gethash (node-type-parent-type meta)
(schema-type-table (schema graph))))
meta)
(setf (gethash (node-type-name meta)
(gethash (node-type-parent-type meta)
(schema-type-table (schema graph))))
(node-type-id meta))
(setf (gethash (intern (symbol-name (node-type-name meta)) :keyword)
(gethash (node-type-parent-type meta)
(schema-type-table (schema graph))))
(node-type-id meta))
(finalize-inheritance (find-class (node-type-name meta)))
(save-schema (schema graph) graph))
(defmacro def-node-type (name parent-types slot-specs graph-name)
(with-gensyms (meta graph)
(let* ((constructor (intern (format nil "MAKE-~A" name)))
(predicate (intern (format nil "~A-P" name)))
(lookup-fn (intern (format nil "LOOKUP-~A" name))))
(setq slot-specs
(mapcar (lambda (spec)
(let ((s1
(if (listp spec)
(if (find :accessor spec)
spec
(append spec (list :accessor (first spec))))
(list spec :accessor spec))))
(if (find :initarg s1)
s1
(append s1 (list :initarg (intern (symbol-name (first s1)) :keyword))))))
slot-specs))
`(progn
(defclass ,name (,@parent-types)
(,@slot-specs)
(:metaclass node-class))
(let* ((,meta
(make-node-type
:name ',name
:parent-type
',(intern (symbol-name (last1 parent-types)) :keyword)
:graph-name ',graph-name
:slots ',slot-specs
:package (package-name *package*)
:constructor ',constructor)))
;; FIXME: why is this necessary when inheriting from another node subclass?
;;(unless (class-finalized-p (find-class ',name))
(finalize-inheritance (find-class ',name))
;;)
(defun ,predicate (thing)
(typep thing ',name))
(defun ,lookup-fn (id &key include-deleted-p)
(let ((thing ,(if (eql (last1 parent-types) 'edge)
`(lookup-edge id)
`(lookup-vertex id))))
(when (and (typep thing ',name)
(or include-deleted-p
(not (deleted-p thing))))
thing)))
,(let ((args (if (eql (last1 parent-types) 'edge)
'(&rest make-args
&key (graph *graph*) id deleted-p revision from to weight &allow-other-keys)
'(&rest make-args
&key (graph *graph*) id deleted-p revision &allow-other-keys))))
`(defun ,constructor ,args
(let ((slots (remove-if
'null
(mapcar
(lambda (slot-name)
(let ((key (intern (symbol-name slot-name) :keyword)))
(let ((pos (position key make-args)))
(when pos
(cons key (nth (1+ pos) make-args))))))
(data-slots (find-class ',name))))))
,(if (eql (last1 parent-types) 'edge)
`(make-edge (node-type-id
(lookup-node-type-by-name ',name :edge))
from to weight
slots ;(list ,@slots)
:id id :revision revision :deleted-p deleted-p
:graph graph)
`(make-vertex (node-type-id
(lookup-node-type-by-name ',name :vertex))
slots ;(list ,@slots)
:id id :revision revision :deleted-p deleted-p
:graph graph)))))
,(when (eql (last1 parent-types) 'edge)
(let ((functor-name (intern (format nil "~A/2" name))))
`(def-global-prolog-functor ,functor-name (from to cont)
(setq from (var-deref from)
to (var-deref to))
(when *prolog-trace*
(format t "TRACE: ~A(~S ~S)~%" ',functor-name from to))
(cond ((and (not (graph-db::var-p from)) (not (graph-db::var-p to)))
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v1 (lookup-vertex (from edge))))
(when (unify from v1)
(let ((v2 (lookup-vertex (to edge))))
(when (unify to v2)
(funcall cont)))))
(undo-bindings old-trail)))
*graph*
:from-vertex from
:to-vertex to
:edge-type ',name))
((not (graph-db::var-p from))
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v2 (lookup-vertex (to edge))))
(when (unify to v2)
(funcall cont)))
(undo-bindings old-trail)))
*graph*
:vertex from
:direction :out
:edge-type ',name))
((not (graph-db::var-p to))
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v2 (lookup-vertex (from edge))))
(when (unify from v2)
(funcall cont)))
(undo-bindings old-trail)))
*graph*
:vertex to
:direction :in
:edge-type ',name))
(t
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v1 (lookup-vertex (from edge))))
(when (unify from v1)
(let ((v2 (lookup-vertex (to edge))))
(when (unify to v2)
(funcall cont)))))
(undo-bindings old-trail)))
*graph*
:edge-type ',name))))))
,(when (eql (last1 parent-types) 'edge)
(let ((functor-name (intern (format nil "~A/3" name))))
`(def-global-prolog-functor ,functor-name (from to weight cont)
(setq from (var-deref from)
to (var-deref to)
weight (var-deref weight))
(when *prolog-trace*
(format t "TRACE: ~A(~S ~S ~S)~%" ',functor-name from to weight))
(cond ((and (not (graph-db::var-p from)) (not (graph-db::var-p to)))
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v1 (lookup-vertex (from edge))))
(when (unify from v1)
(let ((v2 (lookup-vertex (to edge))))
(when (unify to v2)
(when (unify weight (weight edge))
(funcall cont))))))
(undo-bindings old-trail)))
*graph*
:from-vertex from
:to-vertex to
:edge-type ',name))
((not (graph-db::var-p from))
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v2 (lookup-vertex (to edge))))
(when (unify to v2)
(when (unify weight (weight edge))
(funcall cont))))
(undo-bindings old-trail)))
*graph*
:vertex from
:direction :out
:edge-type ',name))
((not (graph-db::var-p to))
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v2 (lookup-vertex (from edge))))
(when (unify from v2)
(when (unify weight (weight edge))
(funcall cont))))
(undo-bindings old-trail)))
*graph*
:vertex to
:direction :in
:edge-type ',name))
(t
(map-edges (lambda (edge)
(let ((old-trail (fill-pointer *trail*)))
(let ((v1 (lookup-vertex (from edge))))
(when (unify from v1)
(let ((v2 (lookup-vertex (to edge))))
(when (unify to v2)
(when (unify weight (weight edge))
(funcall cont))))))
(undo-bindings old-trail)))
*graph*
:edge-type ',name)))))
)
(push ,meta (gethash ',graph-name *schema-node-metadata*))
(let ((,graph (lookup-graph ',graph-name)))
(when ,graph
(instantiate-node-type ,meta ,graph)))
)))))
(defmacro def-vertex (name parent-types slot-specs graph-name)
`(def-node-type ,name (,@parent-types vertex) ,slot-specs ,graph-name))
(defmacro def-edge (name parent-types slot-specs graph-name)
`(def-node-type ,name (,@parent-types edge) ,slot-specs ,graph-name))
(defmethod node-type-diff ((meta1 node-type) (meta2 node-type))
(let ((new-slots (set-difference (node-type-slots meta2)
(node-type-slots meta1)
:test 'equalp))
(removed-slots (set-difference (node-type-slots meta1)
(node-type-slots meta2)
:test 'equalp)))
(values (or new-slots removed-slots) new-slots removed-slots)))
(defmethod instantiate-node-type ((meta node-type) (graph graph))
(with-recursive-lock-held ((schema-lock (schema graph)))
;; Check if this type exists and if it differs from old spec
(log:debug "Looking up ~A: ~A ~A" meta (node-type-name meta) (node-type-parent-type meta))
(let ((old-meta (lookup-node-type-by-name (node-type-name meta)
(node-type-parent-type meta))))
(if (node-type-p old-meta)
(multiple-value-bind (changes-p new-slots removed-slots)
(node-type-diff old-meta meta)
(setf (node-type-id meta) (node-type-id old-meta))
(if changes-p
(progn
;; FIXME: what to do with slot changes-p
(log:debug "REMOVED SLOTS FOR ~S:~% ~S"
(node-type-name meta) removed-slots)
(log:debug "NEW SLOTS FOR ~S:~% ~S"
(node-type-name meta) new-slots)
(update-node-type meta graph))
old-meta))
;; Else if new, assign node-type-id
(progn
(setf (gethash (node-type-name meta)
(schema-class-locks (schema graph)))
(make-rw-lock))
(setf (node-type-id meta)
(get-next-type-id (schema graph)
(node-type-parent-type meta)))
(update-node-type meta graph))))))
(defmethod update-schema ((graph-name symbol))
(let ((graph (lookup-graph graph-name)))
(if graph
(update-schema graph)
(error "Cannot update schema for graph ~A: graph not open!" graph-name))))
(defmethod update-schema ((graph graph))
(with-recursive-lock-held ((schema-lock (schema graph)))
(let ((node-metadata (gethash (graph-name graph) *schema-node-metadata*)))
;; New metadata is pushed on the front of its list; apply
;; metadata oldest to newest by reversing
(dolist (meta (reverse node-metadata))
(instantiate-node-type meta graph)))
(save-schema (schema graph) graph)))