/
clobber.lisp
280 lines (241 loc) · 10.4 KB
/
clobber.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
(cl:in-package #:clobber)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Transaction log.
;;;
;;; Transactions can be any object. They are logged to the stream by
;;; using the serialization defined below. Typically, an application
;;; would either use a list (<function-name> <arg> ... <arg>) or an
;;; instance of STANDARD-OBJECT as a transaction. The application
;;; itself is responsible for what to do with a transaction when a
;;; transaction log is opened.
(defclass transaction-log ()
(;; A stream to which transactions are logged.
(%log-streami
:initarg :log-stream
:reader log-stream)
;; A table mapping objects to identities. Whenever we serialize an
;; object that has not been serialized before, it is entered into
;; the table, and the output is marked with the identity. Whenever
;; we serialize an object that has been serialized before, we just
;; print the identity.
(%object-id-table
:initarg :object-id-table
:reader object-id-table)
;; We allocate object identities sequentially.
(%next-object-id
:initarg :next-object-id
:accessor next-object-id)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Serialization.
;;;
;;; What we are doing is essentially a duplication of what
;;; print-object is doing, because unfortunately, there is no portable
;;; way of using print-object to accomplish what we want.
(defgeneric serialize (object transaction-log))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Serialization of objects other than instances of STANDARD-OBJECT.
;;; A number is serialized as it is normally printed.
(defmethod serialize ((object number) transaction-log)
(prin1 object (log-stream transaction-log)))
;;; A character is serialized as it is normally printed.
(defmethod serialize ((object character) transaction-log)
(prin1 object (log-stream transaction-log)))
;;; A symbol is serialized by printing it with its package prefix. We
;;; accomplish this by setting *package* to the keyword package while
;;; printing the symbol.
(defmethod serialize ((object symbol) transaction-log)
(let ((*package* (find-package '#:keyword)))
(prin1 object (log-stream transaction-log))))
(defmethod serialize ((object cons) transaction-log)
(with-accessors ((object-id-table object-id-table)
(next-object-id next-object-id)
(log-stream log-stream))
transaction-log
(let ((id (gethash object object-id-table)))
(if (null id)
(progn
(setf id (incf next-object-id))
(setf (gethash object object-id-table) id)
(format log-stream "#~d!(" id)
(serialize (car object) transaction-log)
(format log-stream " . ")
(serialize (cdr object) transaction-log)
(format log-stream ")"))
(format log-stream "#~d^" id)))))
;;; FIXME: add serialization of arbitrary arrays.
(defmethod serialize ((object vector) transaction-log)
(with-accessors ((object-id-table object-id-table)
(next-object-id next-object-id)
(log-stream log-stream))
transaction-log
(let ((id (gethash object object-id-table)))
(if (null id)
(progn (setf id (incf next-object-id))
(setf (gethash object object-id-table) id)
(format log-stream "#~d!#(" id)
(loop for element across object
do (serialize element transaction-log)
(format log-stream " "))
(format log-stream ")"))
(format log-stream "#~d^" id)))))
(defmethod serialize ((object string) transaction-log)
(with-accessors ((object-id-table object-id-table)
(next-object-id next-object-id)
(log-stream log-stream))
transaction-log
(let ((id (gethash object object-id-table)))
(if (null id)
(progn (setf id (incf next-object-id))
(setf (gethash object object-id-table) id)
(format log-stream "#~d!\"" id)
(loop for char across object
do (cond ((eql char #\")
(format log-stream "\\\""))
((eql char #\\)
(format log-stream "\\\\"))
(t
(princ char log-stream))))
(format log-stream "\""))
(format log-stream "#~d^" id)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Serialization of instances of STANDARD-OBJECT.
(defgeneric save-info (object)
(:method-combination append :most-specific-last))
(defmethod save-info append ((object standard-object))
'())
(defmacro define-save-info (type &body save-info)
`(defmethod save-info append ((obj ,type))
',save-info))
(defmethod serialize ((object standard-object) transaction-log)
(with-accessors ((object-id-table object-id-table)
(next-object-id next-object-id)
(log-stream log-stream))
transaction-log
(let ((id (gethash object object-id-table)))
(if (null id)
(progn (setf id (incf next-object-id))
(setf (gethash object object-id-table) id)
(format log-stream "#~d![" id)
(serialize (class-name (class-of object)) transaction-log)
(loop for info in (save-info object)
do (format log-stream " ")
(serialize (car info) transaction-log)
(format log-stream " ")
(serialize (funcall (cadr info) object)
transaction-log))
(format log-stream "]"))
(format log-stream "#~d^" id)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Open a transaction log.
;;;
;;; This is a compound function that first calls LOAD-TRANSACTION-LOG
;;; to load a transaction log from disk and execute the provided
;;; function on its contents, then utilizes the object-table modified
;;; in effect of the first function to create a transaction log object
;;; which can be used to log further transactions.
(defun open-transaction-log (filename function)
(let ((object-table (make-hash-table)))
(load-transaction-log filename function object-table)
(make-transaction-log filename object-table)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Load a transaction log.
;;;
;;; This function reads the transaction log from disk.
;;;
;;; After a transaction has been read, the function supplied by the
;;; application is called in order to execute the transaction.
(defun load-transaction-log (filename function object-table)
(with-open-file (stream filename
:direction :input
:if-does-not-exist :create)
(let ((*readtable* (copy-readtable)))
(set-syntax *readtable* object-table)
(loop for transaction = (read stream nil nil)
until (null transaction)
do (funcall function transaction)))))
;;; This function returns two values: the object identity table which
;;; is required for creating the transaction log object and the
;;; maximum object ID present in the table. Integers above this
;;; number are free for assignment in subsequent transactions.
(defun make-object-id-table (object-table)
(let* ((object-id-table (make-hash-table :test #'eq))
(max-object-id 0)
(fn (lambda (object-id object)
(setf (gethash object object-id-table) object-id
max-object-id (max max-object-id object-id)))))
(maphash fn object-table)
(values object-id-table max-object-id)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Make the transaction log object.
;;;
;;; This function takes an object table, creates an object identity
;;; table based on its context, and uses the ID table to create a
;;; transaction-log object that will be used to log new transactions.
(defun make-transaction-log (filename object-table)
(multiple-value-bind (object-id-table max-object-id)
(make-object-id-table object-table)
(let ((stream (open filename
:direction :output
:if-exists :append)))
(make-instance 'transaction-log
:log-stream stream
:object-id-table object-id-table
:next-object-id (1+ max-object-id)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Syntax-setting functions for the transaction log loader.
;;;
;;; The readtable used to read a transaction contains three additional
;;; reader macros, associated with #!, #^ and [.
(defun set-syntax (readtable object-table)
(let ((*readtable* readtable))
(%set-syntax-left-bracket )
(%set-syntax-right-bracket)
(%set-syntax-hash-bang object-table)
(%set-syntax-hash-caret object-table)))
(defun %set-syntax-left-bracket ()
(set-macro-character
#\[
(lambda (stream char)
(declare (ignore char))
(apply #'make-instance (read-delimited-list #\] stream t)))))
(defun %set-syntax-right-bracket ()
(set-syntax-from-char #\] #\)))
(defun %set-syntax-hash-bang (object-table)
(set-dispatch-macro-character
#\# #\!
(lambda (stream char param)
(declare (ignore char))
(setf (gethash param object-table)
(read stream nil nil t)))))
(defun %set-syntax-hash-caret (object-table)
(set-dispatch-macro-character
#\# #\^
(lambda (stream char param)
(declare (ignore stream char))
(gethash param object-table))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Close a transaction log.
(defun close-transaction-log (transaction-log)
(close (log-stream transaction-log)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;; Log a transaction.
;;;
;;; A transaction can be any object that the application sees fit.
(defun log-transaction (transaction transaction-log)
(serialize transaction transaction-log)
(terpri (log-stream transaction-log))
(finish-output (log-stream transaction-log)))
(defmacro with-transaction-log ((var file function) &body forms)
`(let ((,var (open-transaction-log ,file ,function)))
(unwind-protect (progn ,@forms)
(close-transaction-log ,var))))