-
Notifications
You must be signed in to change notification settings - Fork 30
/
index-vector.lisp
197 lines (182 loc) · 7.63 KB
/
index-vector.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
(in-package :graph-db)
(defstruct
(index-vector
(:print-function
(lambda (i s d)
(declare (ignore d))
(let ((*print-base* 10))
(format s "#<INDEX-VECTOR~% :ADRESS ~S~% :SIZE ~S~% :VECTOR ~S>"
(index-vector-address i)
(index-vector-size i)
(map 'list
#'string-id
(index-vector-vector i))))))
(:constructor %make-index-vector))
(address 0 :type (unsigned-byte 64))
(size 0 :type integer)
(vector #() :type (array (simple-array (unsigned-byte 8) (16))))
heap)
(defun make-index-vector (heap key-vector)
(let* ((total-size ( 8 (* 16 (length key-vector))))
(address (allocate heap total-size))
(index-vector (%make-index-vector :address address
:heap heap
:size (length key-vector)
:vector key-vector)))
(serialize-uint64 (memory-mmap heap) (index-vector-size index-vector) address)
(incf address 7)
(dotimes (i (index-vector-size index-vector))
(dotimes (j 16)
(set-byte (memory-mmap heap) (incf address) (aref (aref key-vector i) j))))
(if *graph*
(setf (gethash (index-vector-address index-vector)
(index-vector-cache *graph*)) index-vector)
index-vector)))
(defun get-index-vector (heap address)
(or (and *graph* *cache-enabled* (gethash address (index-vector-cache *graph*)))
(let* ((size (deserialize-uint64 (memory-mmap heap) address))
(index-vector
(%make-index-vector
:address address
:heap heap
:size size
:vector (make-array size
:element-type
'(simple-array (unsigned-byte 8) (16))))))
(incf address 7)
(dotimes (i size)
(let ((key (get-buffer 16)))
(dotimes (j 16)
(setf (aref key j) (get-byte (memory-mmap heap) (incf address))))
(setf (aref (index-vector-vector index-vector) i) key)))
(if *graph*
(setf (gethash (index-vector-address index-vector)
(index-vector-cache *graph*)) index-vector)
index-vector))))
(defun index-vector-push-extend (index-vector key &key free-old-p)
(let* ((heap (index-vector-heap index-vector))
(new-size (1 (index-vector-size index-vector)))
(total-size ( 8 (* 16 new-size)))
(old-address (index-vector-address index-vector))
(new-address (allocate heap total-size))
(current-address new-address)
(key-vector (make-array new-size
:initial-element null-key
:element-type
'(simple-array (unsigned-byte 8) (16)))))
(serialize-uint64 (memory-mmap heap) new-size current-address)
(incf current-address 7)
(dotimes (i (index-vector-size index-vector))
(setf (aref key-vector i) (aref (index-vector-vector index-vector) i))
(dotimes (j 16)
(set-byte (memory-mmap heap)
(incf current-address)
(aref (aref key-vector i) j))))
;; Add new key
(setf (aref key-vector (1- new-size)) key)
(dotimes (j 16)
(set-byte (memory-mmap heap)
(incf current-address)
(aref key j)))
(setf (index-vector-address index-vector)
new-address
(index-vector-vector index-vector)
key-vector
(index-vector-size index-vector)
new-size)
(when free-old-p
(free heap old-address))
;;(when *graph*
;; (setf (gethash (index-vector-address index-vector) (index-vector-cache *graph*))
;; index-vector))
(values index-vector old-address)))
(defun index-vector-remove (index-vector key &key free-old-p)
(let* ((heap (index-vector-heap index-vector))
(key-vector (remove key (index-vector-vector index-vector) :test 'equalp))
(new-size (length key-vector))
(total-size ( 8 (* 16 new-size)))
(old-address (index-vector-address index-vector))
(new-address (allocate heap total-size))
(current-address new-address))
(serialize-uint64 (memory-mmap heap) new-size current-address)
(incf current-address 7)
(dotimes (i new-size)
(dotimes (j 16)
(set-byte (memory-mmap heap)
(incf current-address)
(aref (aref key-vector i) j))))
(setf (index-vector-address index-vector)
new-address
(index-vector-vector index-vector)
key-vector
(index-vector-size index-vector)
new-size)
(when free-old-p
(free heap old-address))
;;(when *graph*
;; (setf (gethash (index-vector-address index-vector) (index-vector-cache *graph*))
;; index-vector))
(values index-vector old-address)))
(defclass index-vector-cursor (cursor)
((node :initarg :node :accessor index-vector-cursor-node)
(index :initarg :index :accessor index :initform 0)
(index-vector :initarg :index-vector :accessor index-vector)
(key-fn :initarg :key-fn :accessor key-fn)
(value-fn :initarg :value-fn :accessor value-fn)
(lookup-fn :initarg :lookup-fn :accessor lookup-fn)))
(defmethod make-cursor ((index-vector index-vector) &key
(cursor-class 'index-vector-cursor)
key-fn value-fn lookup-fn)
(make-instance cursor-class
:index-vector index-vector
:node (aref (index-vector-vector index-vector) 0)
:index 0
:lookup-fn lookup-fn
:key-fn key-fn
:value-fn value-fn))
(defmethod cursor-next ((cursor index-vector-cursor) &optional eoc)
(with-slots (node index index-vector) cursor
(if node
(let ((result node))
(incf index)
(setq node (funcall (lookup-fn cursor)
(aref (index-vector-vector index-vector) index)))
result)
eoc)))
(defclass index-vector-value-cursor (index-vector-cursor)
())
(defmethod make-values-cursor ((index-vector index-vector) &key
key-fn value-fn lookup-fn)
(make-cursor index-vector
:cursor-class 'index-vector-value-cursor
:lookup-fn lookup-fn
:key-fn key-fn
:value-fn value-fn))
(defmethod cursor-next :around ((cursor index-vector-value-cursor) &optional eoc)
(let ((result (call-next-method)))
(if (eql result eoc)
eoc
(funcall (value-fn cursor) result))))
(defclass index-vector-key-cursor (index-vector-cursor)
())
(defmethod make-keys-cursor ((index-vector index-vector) &key
key-fn value-fn lookup-fn)
(make-cursor index-vector
:cursor-class 'index-vector-key-cursor
:lookup-fn lookup-fn
:key-fn key-fn
:value-fn value-fn))
(defmethod cursor-next :around ((cursor index-vector-key-cursor) &optional eoc)
(let ((result (call-next-method)))
(if (eql result eoc)
eoc
(funcall (key-fn cursor) result))))
(defmethod map-index-vector (fun (index-vector index-vector) &key lookup-fn key-fn value-fn)
(let ((cursor (make-cursor index-vector
:lookup-fn lookup-fn
:key-fn key-fn
:value-fn value-fn)))
(do ((val (cursor-next cursor)
(cursor-next cursor)))
((null val))
(apply fun val))))