-
Notifications
You must be signed in to change notification settings - Fork 8
/
collection-class.lisp
209 lines (184 loc) · 7.14 KB
/
collection-class.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
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(in-package :cl-ctrie)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Transactional Collection Object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass collection-object ()
()
(:documentation "collection-object protocol class"))
(define-transactional-class transactional-collection-object (collection-object)
((root-node
:initform nil
:initarg :root-node
:accessor root-node-of)
(context
:initarg :context
:accessor context-of)
(env
:compute-as (apply #'combined-layer-context contextl::*root-context* (context-of -self-))
:cache t
:reader env-of)
(type-name
:initform nil
:initarg :type-name
:accessor type-name-of
:index-type index:hash-index
:index-reader collections-of-type
:index-values all-collection-types)
(category
:initform nil
:initarg :category
:accessor category-of
:index-type index:category-index
:index-var *categorized-collection-index*
:index-reader collections-with-category
:index-values all-collection-categories)
(flags
:initform nil
:initarg :flags
:accessor :flags-of
:index-type index:hash-list-index
:index-reader collections-with-flag
:index-values all-collection-flags)
(creation-timestamp
:initform (get-universal-time)
:accessor creation-timestamp-of
:index-type index:hash-index
:index-reader collections-with-creation-timestamp
:index-values all-collection-creation-timestamps)
(package-name
:initform nil
:initarg :package-name
:accessor package-name-of
:index-type index:hash-index
:index-initargs (:test #'equal)
:index-reader collections-in-package-name)
(symbol-name
:initform (byte-vector-to-hex-string (create-unique-id-byte-vector))
:initarg :symbol-name
:accessor symbol-name-of
:index-type index:hash-index
:index-initargs (:test #'equal)
:index-reader collections-with-symbol-name)
(symbol-for-binding
:compute-as (let* ((package-name (package-name-of -self-))
(package (when package-name
(or (find-package package-name)
(make-package package-name)))))
(if package
(intern (symbol-name-of -self-) package)
(make-symbol (symbol-name-of -self-))))
:reader symbol-for-binding-of
:cache t)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Persistent Collection Object
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define-persistent-class persistent-collection-object (transactional-collection-object)
((root-node
:initform nil
:initarg :root-node
:accessor root-node-of)
(context
:initarg :context
:accessor context-of)
(type-name
:initform nil
:initarg :type-name
:accessor type-name-of
:index-type index:hash-index
:index-reader persistent-collections-of-type
:index-values all-persistent--collection-types)
(category
:initform nil
:initarg :category
:accessor category-of
:index-type index:category-index
:index-var *categorized-persistent-collection-index*
:index-reader persistent-collections-with-category
:index-values all-persistent-collection-categories)
(flags
:initform nil
:initarg :flags
:accessor :flags-of
:index-type index:hash-list-index
:index-reader persistent-collections-with-flag
:index-values all-persistent-collection-flags)
(creation-timestamp
:initform (get-universal-time)
:reader creation-timestamp-of
:index-type index:hash-index
:index-reader persistent-collections-with-creation-timestamp
:index-values all-persistent-collection-creation-timestamps)
(package-name
:initform nil
:initarg :package-name
:accessor package-name-of
:index-type index:hash-index
:index-initargs (:test #'equal)
:index-reader persistent-collections-in-package-name)
(symbol-name
:initform (byte-vector-to-hex-string (create-unique-id-byte-vector))
:initarg :symbol-name
:accessor symbol-name-of
:index-type index:hash-index
:index-initargs (:test #'equal)
:index-reader persistent-collections-with-symbol-name)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; The Class "Collection"
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(import '(order:order-layer order:default-order))
(defclass collection ()
((object
:initarg :object
:accessor collection-object-of)
(context
:initarg :context
:initform '()
:accessor collection-context-of)))
(defclass set (collection)
())
(defclass bag (collection)
())
(defclass map (collection)
())
(defclass seq (collection)
())
(defun make-collection (type &key object name package (persistent nil) (flags nil) (category '(t))
(order 'order:default-order) (balance 'weight-balanced) (transactions 'dstm))
;; (if-exists :error) (if-does-not-exist :create)
(when object (check-type object collection-object)
(unless (eq type (type-name-of object))
(warn "TYPE arg (~S) disagrees with type of OBJECT arg (~S)" type object))
(return-from make-collection
(make-instance (type-name-of object) :object object :context (context-of object))))
(let* ((allocation (if persistent 'persistent 'transient))
(object-class (if persistent 'persistent-collection-object 'transactional-collection-object))
(context (list allocation type transactions order balance))
(object (apply #'make-instance object-class
(append `(:context ,context :type-name ,type)
(when name `(:symbol-name ,name))
(when package `(:package-name ,package))
(when flags `(:flags ,flags))
(when category `(:category ,category))))))
(make-instance type :object object :context context)))
(defmacro with-collection ((collection) &body body)
`(funcall-with-layer-context (env-of (collection-object-of ,collection))
(lambda () ,@body)))
(defmethod root-node-of ((c null))
nil)
(defmethod root-node-of ((c collection))
(with-collection (c)
(root-node-of (collection-object-of c))))
(defmethod (setf root-node-of) (value (c collection))
(prog1 value
(with-collection (c)
(setf (root-node-of (collection-object-of c)) value))))
(defmacro/once with-update-to-collection ((root-node &once collection) &body body)
`(prog1 ,collection
(when ,collection
(with-collection (,collection)
(let1 ,root-node (root-node-of ,collection)
(declare (ignorable ,root-node))
(setf (root-node-of ,collection)
,@body))))))