-
Notifications
You must be signed in to change notification settings - Fork 29
/
graph.lisp
195 lines (192 loc) · 8.15 KB
/
graph.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
(in-package :graph-db)
(defun make-graph (name location &key master-p slave-p master-host
replication-port replication-key package
replay-txn-dir (buffer-pool-p t)
(buffer-pool-size 100000)
(vertex-buckets 8)
(edge-buckets 8))
(when (and replay-txn-dir (not slave-p))
(error ":REPLAY-TXN-DIR is only for slave graphs"))
(when (and (or slave-p master-p) (not replication-port))
(error ":REPLICATION-PORT is required for master and slave graphs"))
(when (and slave-p (not master-host))
(error ":MASTER-HOST required for slave graphs"))
(ensure-directories-exist location)
(let* ((path (pathname location))
(dirty-file (format nil "~A/.dirty" location)))
(unless (probe-file path)
(error "Unable to open graph location ~A" path))
(when buffer-pool-p
(ensure-buffer-pool buffer-pool-size))
(let* ((heap (create-memory
(format nil "~A/heap.dat" path)
(* 1024 1024 1000)))
(graph
(make-instance
(cond (slave-p 'slave-graph)
(master-p 'master-graph)
(t 'graph))
:graph-name name
:location path
:views
#+sbcl (make-hash-table :synchronized t)
#+ccl (make-hash-table :shared t)
#+lispworks (make-hash-table :single-thread nil)
:cache
(make-id-table :synchronized t :weakness :value)
:replication-key replication-key
:replication-port replication-port
:vertex-table (make-vertex-table
(format nil "~A/vertex/" path)
:base-buckets vertex-buckets)
:edge-table (make-edge-table
(format nil "~A/edge/" path)
:base-buckets edge-buckets)
:heap heap
:indexes (create-memory
(format nil "~A/indexes.dat" path)
(* 1024 1024 1000))
:ve-index-in (make-ve-index
(format nil "~A/ve-index-in/" path))
:ve-index-out (make-ve-index
(format nil "~A/ve-index-out/" path))
:vev-index (make-vev-index
(format nil "~A/vev-index/" path)))))
(setf (vertex-index graph)
(make-type-index
(format nil "~A/vertex-index.dat" path) heap))
(setf (edge-index graph)
(make-type-index
(format nil "~A/edge-index.dat" path) heap))
(let ((*graph* graph))
(init-schema graph)
(update-schema graph)
(with-open-file (out dirty-file :direction :output)
(format out "~S" (get-universal-time)))
(setf (gethash name *graphs*) graph))
(when slave-p
(setf (master-host graph) master-host)
(when replay-txn-dir
(let ((*graph* graph))
(replay graph replay-txn-dir package))))
(setf (transaction-manager graph)
(make-instance 'transaction-manager
:graph graph))
(ensure-directories-exist (persistent-transaction-directory graph))
(init-replication-log graph)
(start-replication graph :package package)
(setf (graph-open-p graph) t)
graph)))
(defun open-graph (name location &key master-p slave-p master-host replication-port
replication-key package (buffer-pool-p t) (gc-heap-p t))
(ensure-directories-exist location)
(let ((path (pathname location))
(dirty-file (format nil "~A/.dirty" location))
(schema-file (format nil "~A/schema.dat" location)))
(unless (probe-file path)
(error "Unable to open graph location ~A" path))
(when (probe-file dirty-file)
(error "~A exists; graph not closed properly. Run recovery." dirty-file))
(log:info "Opening graph.")
(when buffer-pool-p
(log:info "Initializing buffer pool.")
(ensure-buffer-pool))
(let* ((heap (open-memory (format nil "~A/heap.dat" path)))
(graph
(make-instance
(cond (slave-p 'slave-graph)
(master-p 'master-graph)
(t 'graph))
:graph-name name
:location path
:views
#+sbcl (make-hash-table :synchronized t)
#+ccl (make-hash-table :shared t)
#+lispworks (make-hash-table :single-thread nil)
:cache
(make-id-table :synchronized t :weakness :value)
:replication-key replication-key
:replication-port replication-port
:vertex-table (open-lhash
(format nil "~A/vertex/" path))
:edge-table (open-lhash
(format nil "~A/edge/" path))
:heap heap
:indexes (open-memory
(format nil "~A/indexes.dat" path))
:ve-index-in (open-ve-index
(format nil "~A/ve-index-in/" path))
:ve-index-out (open-ve-index
(format nil "~A/ve-index-out/" path))
:vev-index (open-vev-index
(format nil "~A/vev-index/" path)))))
(let ((*graph* graph))
(setf (vertex-index graph)
(open-type-index (format nil "~A/vertex-index.dat" path) heap))
(setf (edge-index graph)
(open-type-index (format nil "~A/edge-index.dat" path) heap))
(if (probe-file schema-file)
(setf (schema graph)
(cl-store:restore schema-file))
(init-schema graph))
(setf (schema-lock (schema graph)) (make-recursive-lock))
(update-schema graph)
(restore-views graph)
(with-open-file (out dirty-file :direction :output)
(format out "~S" (get-universal-time)))
(setf (gethash name *graphs*) graph)
(when gc-heap-p
(gc-heap graph))
(recover-transactions graph))
(when slave-p
(setf (master-host graph) master-host))
(setf (transaction-manager graph)
(make-instance 'transaction-manager
:graph graph))
(ensure-directories-exist (persistent-transaction-directory graph))
(init-replication-log graph)
(start-replication graph :package package)
(setf (graph-open-p graph) t)
graph)))
(defmethod close-graph ((graph graph) &key (snapshot-p t))
(when (graph-open-p graph)
(stop-replication graph)
(remhash (graph-name graph) *graphs*)
(when snapshot-p
(log:info "Snapshotting ~A" graph)
(snapshot graph))
(when (type-index-p (vertex-index graph))
(log:info "Closing ~A" (vertex-index graph))
(close-type-index (vertex-index graph)))
(when (type-index-p (edge-index graph))
(log:info "Closing ~A" (edge-index graph))
(close-type-index (edge-index graph)))
(when (vev-index-p (vev-index graph))
(log:info "Closing ~A" (vev-index graph))
(close-vev-index (vev-index graph)))
(when (ve-index-p (ve-index-in graph))
(log:info "Closing ~A" (ve-index-in graph))
(close-ve-index (ve-index-in graph)))
(when (ve-index-p (ve-index-out graph))
(log:info "Closing ~A" (ve-index-out graph))
(close-ve-index (ve-index-out graph)))
(when (lhash-p (vertex-table graph))
(log:info "Closing ~A" (vertex-table graph))
(close-lhash (vertex-table graph)))
(when (lhash-p (edge-table graph))
(log:info "Closing ~A" (edge-table graph))
(close-lhash (edge-table graph)))
(when (memory-p (indexes graph))
(log:info "Closing ~A" (indexes graph))
(close-memory (indexes graph)))
(when (memory-p (heap graph))
(log:info "Closing ~A" (heap graph))
(close-memory (heap graph)))
(setf (heap graph) nil
(vertex-table graph) nil
(edge-table graph) nil)
(let ((dirty-file (format nil "~A/.dirty" (location graph))))
(delete-file dirty-file))
(close-replication-log graph)
(setf (graph-open-p graph) nil))
graph)