-
Notifications
You must be signed in to change notification settings - Fork 8
/
ctrie.lisp
3056 lines (2520 loc) · 121 KB
/
ctrie.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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;;;; -*- mode: common-lisp; common-lisp-style: modern; coding: utf-8; -*-
;;;;;
(in-package :cl-ctrie)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Generational Descriptor Object (required for 'generational cas')
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun make-generational-descriptor ()
(create-unique-id-byte-vector))
(defun gen-eq (a b)
(equalp a b))
(defun new-fundamental-root ()
(with-active-layers (fundamental)
(make-inode (make-cnode) (make-generational-descriptor))))
(defun new-transient-root ()
(with-active-layers (transient)
(make-inode (make-cnode) (make-generational-descriptor))))
(defun new-persistent-root ()
(with-active-layers (persistent)
(make-inode (make-cnode) (make-generational-descriptor))))
(defun constantly-nil ()
nil)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dereferenced Instance Access
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass reference ()
((value
:accessor reference-value
:initarg :value)))
(defclass simple-reference (reference)
((value
:accessor contents-of
:initarg :contents)))
(defmethod pointer:deref ((thing simple-reference) &optional type &rest args)
(declare (ignore type args))
(contents-of thing))
(mm:defmmclass persistent-struct (mm:marray)
((contents :initform nil :accessor contents-of :initarg :contents :persistent nil)))
(defun make-persistent-struct (contents &optional (class 'persistent-struct))
(mm:make-marray (length contents) :marray-class class
:initial-contents (coerce contents 'list)))
(defmethod pointer:deref ((thing persistent-struct) &optional type &rest args)
(declare (ignore type args))
(or (contents-of thing)
(setf (contents-of thing) (cl:map 'vector 'maybe-unbox (mm:marray-to-list thing)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CTRIE Root Container
;;
;; the root container is now declared early to avoid performance penalties
;; incurred for structure slot accessors that are referenced prior to the
;; actual structure definition. The consequence is that this requires several
;; forward references to other functions defined later in this file.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defstruct (fundamental-ctrie (:copier nil))
(root (new-fundamental-root))
(index 0)
(name (byte-vector-to-hex-string (create-unique-id-byte-vector)))
(readonly-p nil)
(test 'equal)
(hash 'sxhash)
(stamp *timestamp-factory*)
(context '(fundamental))
(env nil))
(defmethod index-incf ((ctrie fundamental-ctrie) &optional (amount 1))
(sb-ext:atomic-update (fundamental-ctrie-index ctrie) #'+ amount))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CTRIE Root Container Classes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defclass transient-ctrie ()
((root
:initform (new-transient-root)
:accessor ctrie-root
:initarg :root)
(name
:initform (byte-vector-to-hex-string (create-unique-id-byte-vector))
:initarg :name
:reader ctrie-name
:type string)
(index
:initform 0
:accessor ctrie-index
:type integer
:initarg :index)
(readonly-p
:initform nil
:type boolean
:accessor ctrie-readonly-p
:initarg :readonly-p)
(test
:initform 'equal
:type symbol
:accessor ctrie-test
:initarg :test)
(hash
:initform 'sxhash
:type symbol
:accessor ctrie-hash
:initarg :hash)
(stamp
:initform *timestamp-factory*
:type symbol
:accessor ctrie-stamp
:initarg :stamp)
(context
:initarg :context
:accessor ctrie-context)
(env
:initarg :env
:initform nil))
(:default-initargs :context (list 'transient)))
(defmethod index-incf ((ctrie transient-ctrie) &optional (amount 1))
(sb-ext:atomic-update (slot-value ctrie 'index) #'+ amount))
(mm:defmmclass persistent-ctrie ()
((root
:initform (new-persistent-root)
:accessor ctrie-root
:initarg :root
:persistent t)
(name
:initform (byte-vector-to-hex-string (create-unique-id-byte-vector))
:accessor ctrie-name
:initarg :name
:type string
:persistent t)
(index
:initform 0
:accessor ctrie-index
:type integer
:initarg :index)
(readonly-p
:initform nil
:type boolean
:accessor ctrie-readonly-p
:initarg :readonly-p
:persistent t)
(test
:initform 'equal
:type symbol
:accessor ctrie-test
:initarg :test
:persistent t)
(hash
:initform 'sxhash
:type symbol
:accessor ctrie-hash
:initarg :hash
:persistent t)
(stamp
:initform *timestamp-factory*
:type symbol
:accessor ctrie-stamp
:initarg :stamp
:persistent t)
(context
:initarg :context
:accessor ctrie-context
:persistent t)
(env
:initarg :env
:initform nil
:persistent nil))
(:default-initargs :context '(persistent)))
(defmethod index-incf ((ctrie persistent-ctrie) &optional (amount 1))
(sb-ext:atomic-update (slot-value ctrie 'index) #'+ amount))
(defmethod ctrie-root ((ctrie fundamental-ctrie))
(fundamental-ctrie-root ctrie))
(defmethod ctrie-name ((ctrie fundamental-ctrie))
(fundamental-ctrie-name ctrie))
(defmethod ctrie-readonly-p ((ctrie fundamental-ctrie))
(fundamental-ctrie-readonly-p ctrie))
(defmethod ctrie-test ((ctrie fundamental-ctrie))
(fundamental-ctrie-test ctrie))
(defmethod ctrie-hash ((ctrie fundamental-ctrie))
(fundamental-ctrie-hash ctrie))
(defmethod ctrie-stamp ((ctrie fundamental-ctrie))
(fundamental-ctrie-stamp ctrie))
(defmethod ctrie-context ((ctrie fundamental-ctrie))
(fundamental-ctrie-context ctrie))
(defun configuration-context (layers &optional (default-context contextl::*active-context*))
(apply #'combined-layer-context default-context layers))
(defmethod ctrie-env ((thing fundamental-ctrie) &optional
(default-context contextl::*active-context*))
(or (fundamental-ctrie-env thing)
(setf (fundamental-ctrie-env thing)
(apply #'combined-layer-context default-context
(fundamental-ctrie-context thing)))))
(defmethod ctrie-env ((thing transient-ctrie) &optional
(default-context contextl::*active-context*))
(or (slot-value thing 'env)
(setf (slot-value thing 'env)
(apply #'combined-layer-context default-context
(ctrie-context thing)))))
(defmethod ctrie-env ((thing persistent-ctrie) &optional
(default-context contextl::*active-context*))
(or (slot-value thing 'env)
(setf (slot-value thing 'env)
(apply #'combined-layer-context default-context
(ctrie-context thing)))))
(defmethod print-object ((ctrie persistent-ctrie) stream)
(print-unreadable-object (ctrie stream :type nil)
(format stream "~A:~8,'-X ~34A [~8,'.D keys] (~A,~A,~A,~A)"
"CTRIE" (mm:lisp-object-to-mptr ctrie)
(concatenate 'string "\"" (string-upcase (ctrie-name ctrie)) "\"")
(ctrie-size ctrie) "mmap"
(if (ctrie-readonly-p ctrie) "ro" "rw")
(string-downcase (princ-to-string (ctrie-test ctrie)))
(string-downcase (princ-to-string (ctrie-hash ctrie))))))
(defmethod ctrie-p ((thing t))
nil)
(defmethod ctrie-p ((thing fundamental-ctrie))
t)
(defmethod ctrie-p ((thing transient-ctrie))
t)
(defmethod ctrie-p ((thing persistent-ctrie))
t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Node Equality
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod node-eq ((node1 t) (node2 t))
(eq node1 node2))
(defmethod node-eq ((node1 fixnum) (node2 fixnum))
(eql node1 node2))
(defmethod node-eq ((node1 mm:mm-object) (node2 mm:mm-object))
(mm:meq node1 node2))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Special CTRIES
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(mm:defmmclass special-persistent-ctrie (persistent-ctrie)
()
(:default-initargs :name (princ-to-string (gensym)) :hash 'sb-ext::psxhash :test 'equalp
:context (list 'persistent)))
(defvar *persistent-ctrie-registry* nil)
(defvar *persistent-ctrie-seqs* nil)
(defun persistent-ctrie-registry ()
(or *persistent-ctrie-registry*
(setf *persistent-ctrie-registry*
(find "registry" (mm:retrieve-all-instances 'special-persistent-ctrie)
:key #'ctrie-name))
(setf *persistent-ctrie-registry*
(make-instance 'special-persistent-ctrie :name "registry"))))
(defun persistent-ctrie-seqs ()
(or *persistent-ctrie-seqs*
(setf *persistent-ctrie-seqs*
(find "sequences" (mm:retrieve-all-instances 'special-persistent-ctrie)
:key #'ctrie-name))
(setf *persistent-ctrie-seqs*
(make-instance 'special-persistent-ctrie :name "sequences"))))
(defun find-persistent-ctrie (name)
(let1 namestring (typecase name
(string (string-upcase name))
(keyword (symbol-name name))
(symbol (string-upcase (symbol-name name)))
(package (string-upcase (package-name name)))
(t (error "persistent-ctries must be named by string or keyword")))
(ctrie-get (persistent-ctrie-registry) namestring)))
(defun (setf find-persistent-ctrie) (value name)
(let1 namestring (typecase name
(string (string-upcase name))
(keyword (symbol-name name))
(symbol (string-upcase (symbol-name name)))
(package (string-upcase (package-name name)))
(t (error "ctries must be named by string or keyword")))
(with-active-layers (persistent)
(prog1 value
(etypecase value
(null (ctrie-drop (persistent-ctrie-registry) namestring))
(persistent-ctrie (ctrie-put (persistent-ctrie-registry) namestring value))
((or transient-ctrie fundamental-ctrie)
;; (ctrie-put (ctrie-registry) namestring
;; (aprog1 (ctrie-from-hashtable
;; (ctrie-to-hashtable value :atomic t) :persistent t)
;; (setf (ctrie-name it) namestring)))))))))
(warn "persisting serialized transient instance as ~A" name)
(ctrie-put (persistent-ctrie-registry) name (maybe-box value))))))))
(defmethod initialize-instance :after ((self persistent-ctrie) &key)
(unless (typep self 'special-persistent-ctrie)
(setf (find-persistent-ctrie (ctrie-name self)) self)))
(defun all-persistent-ctries ()
(ctrie-values (persistent-ctrie-registry)))
(defun persistent-ctrie-names ()
(ctrie-keys (persistent-ctrie-registry)))
(defun persistent-ctrie-seq-names ()
(ctrie-keys (persistent-ctrie-seqs)))
(defmethod ctrie-next-index ((string string))
(or (ctrie-put-update-if (persistent-ctrie-seqs) string 1 '+ 'numberp)
(ctrie-put-ensure (persistent-ctrie-seqs) string 0)))
(defmethod ctrie-next-index ((symbol symbol))
(ctrie-next-index (fully-qualified-symbol-name symbol)))
(defmethod ctrie-next-index ((ctrie fundamental-ctrie))
(index-incf ctrie))
(defmethod ctrie-next-index ((ctrie transient-ctrie))
(index-incf ctrie))
(defmethod ctrie-next-index ((ctrie persistent-ctrie))
(index-incf ctrie))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; CTRIE Creation and Instance Handling
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod make-ctrie ((place null) &key (context '(fundamental)) (readonly-p nil)
(test 'equal) (hash 'sxhash) (stamp *timestamp-factory*))
(funcall-with-layer-context (configuration-context context)
(lambda ()
(make-fundamental-ctrie :name nil :context context :readonly-p readonly-p
:test test :hash hash :stamp (when *timestamps-enabled* stamp)))))
(defmethod make-ctrie ((place string) &key (context '(persistent)) (readonly-p nil)
(test 'equal) (hash 'sxhash) (stamp *timestamp-factory*))
(funcall-with-layer-context (configuration-context context)
(lambda ()
(make-instance 'persistent-ctrie :name place :context context :readonly-p readonly-p
:test test :hash hash :stamp (when *timestamps-enabled* stamp)))))
(defmethod pointer:deref ((ctrie fundamental-ctrie) &optional (type 'ctrie) &rest args)
(declare (ignore type args))
ctrie)
(defmethod pointer:deref ((ctrie transient-ctrie) &optional (type 'ctrie) &rest args)
(declare (ignore type args))
ctrie)
(defmethod pointer:deref ((ctrie persistent-ctrie) &optional (type 'ctrie) &rest args)
(declare (ignore type args))
ctrie)
(defmethod funcall-with-ctrie-context ((ctrie fundamental-ctrie) thunk)
(let ((*ctrie* ctrie))
(funcall-with-layer-context (ctrie-env ctrie) thunk)))
(defmethod funcall-with-ctrie-context ((ctrie transient-ctrie) thunk)
(let ((*ctrie* ctrie))
(funcall-with-layer-context (ctrie-env ctrie) thunk)))
(defmethod funcall-with-ctrie-context ((ctrie persistent-ctrie) thunk)
(let ((*ctrie* ctrie))
(funcall-with-layer-context (ctrie-env ctrie) thunk)))
(defmacro/once with-ctrie (&once ctrie &body body)
"Configure the dynamic environment with the appropriate condition
handlers, control fixtures, and instrumentation necessary to execute
the operations in BODY on the specified CTRIE. Unless specifically
documented, the particular configuration of this dynamic environment
should be considered an implementation detail and not relied upon. A
particular exception, however, is that within the dynamic extent of
a WITH-CTRIE form, the code implementing a CTRIE operation may
expect that the special variable `*CTRIE*` will be bound to the root
container of subject CTRIE. See also the documentation for
`*CTRIE*`"
`(funcall-with-ctrie-context ,ctrie (lambda () ,@body)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Flag Vector / Bitmap Operations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun cthash (key)
"Compute the hash value of KEY using the hash function defined by
the CTRIE designated by the innermost enclosing WITH-CTRIE form."
(funcall (ctrie-hash *ctrie*) key))
(defun ctequal (x y)
"Test the equality of X and Y using the equality predicate defined
by the CTRIE designated by the innermost enclosing WITH-CTRIE form."
(funcall (ctrie-test *ctrie*) x y))
(defun ctstamp (&optional last)
(when *timestamps-enabled*
(if *ctrie*
(apply (ctrie-stamp *ctrie*) (ensure-list last))
(apply *timestamp-factory* (ensure-list last)))))
(defun flag (key level &optional use-cached-p)
"For a given depth, LEVEL, within a CTRIE, extract the correspondant
sequence of bits from the computed hash of KEY that indicate the
logical index of the arc on the path to which that key may be found.
If USE-CACHED-P is non-NIL and the special-variable `*HASH-CODE*` is
non-NIL as well, the hash will not be recomputed, but instead the
value bound to `*HASH-CODE*` will be used as an optimization to
reduce unnecessary recomputation of hash function -- an expensive
operation. This value is NOT checked and assumed to be valid and
appropriate to the given situation -- care must be taken to use this
cached value correctly. When in doubt, recomputing hash may be a
performance penalty, but is guaranteed to always work in any
situation. Note that the logical index of the arc is most likely
not the same as the physical index where it is actually located --
for that see `FLAG-ARC-POSITION`"
(ash 1 (logand (ash (or (when use-cached-p *hash-code*) (cthash key))
(- level)) #x1f)))
(defun flag-present-p (flag bitmap)
"Tests the (fixnum) BITMAP representing the logical index of all
arcs present in a CNODE for the presence of a particular arc whose
logical index is represented by FLAG."
(plusp (logand flag bitmap)))
(defun flag-arc-position (flag bitmap)
"Given FLAG representing the logical index of an arc, and BITMAP
representing all arcs present, compute a physical index for FLAG in
such a manner as to always ensure all arcs map uniquely and
contiguously to the smallest vector that can contain the given
arcs."
(logcount (logand (- flag 1) bitmap)))
(defun flag-vector (&optional (content 0))
"FLAG-VECTOR is a bit-vector representation of the (fixnum)
BITMAP. It is currently not used for any calculation, however it is
included within each CNODE as a convenience because it makes it
immediately clear from visual inspection which logical arc indexes1
are represented in the node. For example, from the bit-vector
`#*10010000000000000000000000000000` one can easily see that the first
and fourth positions are occupied, and the rest empty."
(loop with new-vector = (make-array 32 :element-type 'bit)
for i from 0 to 31 when (logbitp i content)
do (setf (sbit new-vector i) 1)
finally (return new-vector)))
(defvar %empty-map% (vector)
"Defines the initial value of an empty CNODE arc-vector.")
(defvar %no-flags% (flag-vector)
"Defines the initial value of a flag-vector representing a
cnode containing an empty arc-vector.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; INODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defstruct (fundamental-ref
(:copier nil))
(prev nil)
(stamp (ctstamp))
(value t))
(defmethod ref-prev ((thing fundamental-ref))
(fundamental-ref-prev thing))
(defmethod ref-stamp ((thing fundamental-ref))
(fundamental-ref-stamp thing))
(defmethod ref-value ((thing fundamental-ref))
(fundamental-ref-value thing))
(defclass transient-ref ()
((prev
:initform nil
:accessor ref-prev
:initarg :prev)
(stamp
:initform (ctstamp)
:accessor ref-stamp
:initarg :stamp)
(value
:initform t
:accessor ref-value
:initarg :value)))
(mm:defmmclass persistent-ref ()
((prev
:initform nil
:accessor ref-prev
:initarg :prev
:persistent t)
(stamp
:initform (ctstamp)
:accessor ref-stamp
:initarg :stamp
:persistent t)
(value
:initform t
:accessor ref-value
:initarg :value
:persistent t)))
(defmethod ref-p ((thing t))
nil)
(defmethod ref-p ((thing fundamental-ref))
t)
(defmethod ref-p ((thing transient-ref))
t)
(defmethod ref-p ((thing persistent-ref))
t)
(define-layered-method make-ref :in t (&rest args &key stamp value prev) ;; persistent)
(declare (ignorable args))
(make-fundamental-ref :stamp stamp :value value :prev prev))
(define-layered-method make-ref :in transient (&rest args &key stamp value prev)
(declare (ignorable args))
(make-instance 'transient-ref :stamp stamp :value value :prev prev))
(define-layered-method make-ref :in persistent (&rest args &key stamp value prev)
(declare (ignorable args))
(funcall #'make-instance 'persistent-ref :stamp stamp :value value :prev prev))
(defstruct (fundamental-failed-ref
(:include fundamental-ref)
(:copier nil)))
(defclass transient-failed-ref (transient-ref)
())
(mm:defmmclass persistent-failed-ref (persistent-ref)
())
(defun failed-ref-prev (ref)
(ref-prev ref))
(defun failed-ref-value (ref)
(ref-value ref))
(defun failed-ref-stamp (ref)
(ref-stamp ref))
(defmethod failed-ref-p ((thing t))
nil)
(defmethod failed-ref-p ((thing fundamental-failed-ref))
t)
(defmethod failed-ref-p ((thing transient-failed-ref))
t)
(defmethod failed-ref-p ((thing persistent-failed-ref))
t)
(define-layered-method make-failed-ref :in t (&key stamp value prev) ; persistent)
(make-fundamental-failed-ref :stamp stamp :value value :prev prev))
(define-layered-method make-failed-ref :in transient (&key stamp value prev)
(make-instance 'transient-failed-ref :stamp stamp :value value :prev prev))
(define-layered-method make-failed-ref :in persistent (&key stamp value prev)
(make-instance 'persistent-failed-ref :stamp stamp :value value :prev prev))
(defstruct (fundamental-inode
(:copier nil))
(ref nil)
(gen nil))
(defclass transient-inode ()
((ref
:initform nil
:accessor inode-ref
:initarg :ref)
(gen
:initform nil
:accessor inode-gen
:initarg :gen)))
(mm:defmmclass persistent-inode ()
((ref
:initform nil
:accessor inode-ref
:initarg :ref
:persistent t)
(gen
:initform nil
:accessor inode-gen
:initarg :gen
:persistent t)))
(defmethod inode-ref ((thing fundamental-inode))
(fundamental-inode-ref thing))
(defmethod inode-gen ((thing fundamental-inode))
(fundamental-inode-gen thing))
(defmethod inode-p ((thing t))
nil)
(defmethod inode-p ((thing fundamental-inode))
t)
(defmethod inode-p ((thing transient-inode))
t)
(defmethod inode-p ((thing persistent-inode))
t)
(define-layered-method make-inode :in t (link-to &optional gen stamp prev)
(make-fundamental-inode
:gen (or gen (make-generational-descriptor))
:ref (make-ref :value link-to :stamp (or stamp (ctstamp)) :prev prev)))
(define-layered-method make-inode :in transient (link-to &optional gen stamp prev)
(make-instance 'transient-inode
:gen (or gen (make-generational-descriptor))
:ref (make-ref :value link-to :stamp (or stamp (ctstamp)) :prev prev)))
(define-layered-method make-inode :in persistent (link-to &optional gen stamp prev)
(make-instance 'persistent-inode
:gen (or gen (make-generational-descriptor))
:ref (make-ref :value link-to :stamp (or stamp (ctstamp)) :prev prev)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; GCAS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defmethod gcas-compare-and-set ((obj fundamental-inode)
expected new expected-stamp new-stamp prev)
(declare (ignore expected-stamp))
(let ((ref (inode-ref obj)))
(with-active-layers (fundamental)
(and
(eq (ref-value ref) expected)
(eq (cas (fundamental-inode-ref obj) ref (make-fundamental-ref
:value new
:stamp new-stamp
:prev prev))
ref)))))
(defmethod gcas-compare-and-set ((obj transient-inode)
expected new expected-stamp new-stamp prev)
(declare (ignore expected-stamp))
(let ((ref (inode-ref obj)))
(with-active-layers (transient)
(and
(eql (ref-value ref) expected)
(eq (cas (slot-value obj 'ref) ref (make-ref
:value new
:stamp new-stamp
:prev prev))
ref)))))
(defmethod gcas-compare-and-set ((obj persistent-inode)
expected new expected-stamp new-stamp prev)
(declare (ignore expected-stamp))
(let ((ref (inode-ref obj)))
(with-active-layers (persistent)
(let ((ref-ptr (mm:mptr ref)))
(and
(eql (mm:mptr (ref-value ref)) (mm:mptr expected))
(eql (cas-word-sap (mm::mm-object-pointer obj) ref-ptr
(mm:mptr (make-ref :value new :stamp new-stamp :prev prev)))
ref-ptr))))))
;; (defun/inline
(defun INODE-READ (inode)
"INODE-READ provides the top-level interface to the inode _GCAS ACCESS_
api, which is the mechanism which must be used to gain access to the
content of any NON-ROOT inode. For access to the root inode, refer
to the RDCSS inode api `ROOT-NODE-ACCESS`. Returns as four values,
the MAIN-NODE, the STAMP, the PREVIOUS STATE (if any), and the REF
structure encapsulated by the inode."
(check-type inode inode)
(let (ref)
(setf ref (inode-ref inode))
(multiple-value-bind (val stamp prev) (values
(ref-value ref)
(ref-stamp ref)
(ref-prev ref))
(if (null prev)
(return-from inode-read (values val stamp prev ref))
(let1 result (inode-commit inode ref)
(return-from inode-read (values
(ref-value result)
(ref-stamp result)
(ref-prev result)
result)))))))
(defun INODE-MUTATE (inode old-value new-value)
"INODE-MUTATE provides the top-level interface to the inode _GCAS
MODIFICATION_ api, which is the mechanism which must be used to
effect any change in a NON-ROOT inode. For modification of the
root-inode, refer to the `ROOT-NODE-REPLACE` _RDCSS ROOT NODE
PROTOCOL_ Returns a boolean value which indicates the success or
failure of the modification attempt."
(check-type inode inode)
(check-type old-value (or lnode snode tnode cnode))
(check-type new-value (or lnode snode tnode cnode))
(when (ctrie-readonly-p *ctrie*)
(ctrie-modification-failed "This CTRIE is READ-ONLY"
:op 'inode-mutate :place (describe-object inode nil)))
(multiple-value-bind (val stamp prev ref) (inode-read inode)
(declare (ignorable val stamp prev ref))
(if (gcas-compare-and-set inode old-value
new-value stamp (ctstamp stamp) old-value)
(return-from inode-mutate
(null (ref-prev (inode-commit inode (inode-ref inode)))))
(return-from inode-mutate nil))))
(defmethod cas-failed-ref ((inode fundamental-inode) ref)
(with-active-layers (fundamental)
(let* ((prev (fundamental-ref-prev ref))
(failed-ref-prev (fundamental-failed-ref-prev prev)))
(eq (cas (fundamental-inode-ref inode) ref failed-ref-prev) ref))))
(defmethod cas-failed-ref ((inode transient-inode) ref)
(with-active-layers (transient)
(let* ((prev (ref-prev ref))
(failed-ref-prev (failed-ref-prev prev)))
(eq (cas (slot-value inode 'ref) ref failed-ref-prev) ref))))
(defmethod cas-failed-ref ((inode persistent-inode) ref)
(with-active-layers (persistent)
(let* ((prev (ref-prev ref))
(failed-ref-prev (failed-ref-prev prev)))
(eql (cas-word-sap (mm::mm-object-pointer inode)
(mm:mptr ref) (mm:mptr failed-ref-prev))
(mm:mptr ref)))))
(defmethod cas-ref-prev ((ref fundamental-ref) prev new-generator)
(with-active-layers (fundamental)
(cas (fundamental-ref-prev ref) prev (funcall new-generator))))
(defmethod cas-ref-prev ((ref transient-ref) prev new-generator)
(with-active-layers (transient)
(cas (slot-value ref 'prev) prev (funcall new-generator))))
(defmethod cas-ref-prev ((ref persistent-ref) prev new-generator)
(with-active-layers (persistent)
(let* ((ref-prev-ptr (mm::mm-object-pointer ref))
(prev-mptr (mm:mptr prev))
(new (funcall new-generator))
(new-mptr (if new (mm:mptr new) 0)))
(unless (zerop (cas-word-sap ref-prev-ptr prev-mptr new-mptr))
prev))))
(defun INODE-COMMIT (inode ref)
"INODE-COMMIT implements the _GCAS COMMIT_ protocol which is invoked
as necessary by the `INODE-READ` and `INODE-MUTATE` entry-points. It is
not meant to be invoked directly, as this would most likely result
in corruption. Returns the `REF` structure representing the content of
whatever root inode wound up successfully committed -- either the
one requested, or one represented by a previous valid state. In order
to coexist with the _RDCSS ROOT NODE PROTOCOL_ this GCAS COMMIT
implementation is augmented with RDCSS ABORTABLE READ semantics
by a forward reference to a RDCSS-aware `ROOT-NODE-ACCESS` in order
to safely compare INODE's generational descriptor with the one found
in the root inode of the subject CTRIE."
(when (ctrie-readonly-p *ctrie*)
(ctrie-modification-failed "This CTRIE is READ-ONLY"
:op 'inode-commit :place (describe-object inode nil)))
(flet ((ABORTABLE-READ (ctrie)
(root-node-access ctrie t)))
(let1 prev (ref-prev ref)
(typecase prev
(null (return-from inode-commit ref))
(failed-ref (if (cas-failed-ref inode ref)
(return-from inode-commit (failed-ref-prev prev))
(return-from inode-commit (inode-commit inode (inode-ref inode)))))
(t (if (gen-eq (inode-gen (ABORTABLE-READ *ctrie*)) (inode-gen inode))
(let1 committed (cas-ref-prev ref prev (constantly nil))
(if (null committed)
(return-from inode-commit (inode-commit inode ref))
(return-from inode-commit ref)))
(progn
(cas-ref-prev ref prev (lambda ()
(make-failed-ref
:value (ref-value prev)
:stamp (ref-stamp prev)
:prev (ref-prev prev))))
(return-from inode-commit
(inode-commit inode (inode-ref inode))))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Serial Boxes
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(mm:defmmclass serial-box ()
((contents
:initarg :contents
:accessor serial-box-contents
:persistent t
:initform nil)))
(define-layered-method maybe-box :in t (thing)
thing)
(define-layered-method maybe-box :in persistent ((thing t))
(let ((byte-vector (hu.dwim.serializer:serialize thing)))
(make-instance 'serial-box :contents (mm:lisp-object-to-mptr byte-vector))))
(define-layered-method maybe-box :in persistent ((thing mm:mm-object))
thing)
(define-layered-method maybe-box :in persistent ((thing number))
thing)
(define-layered-method maybe-box :in persistent ((thing symbol))
thing)
(define-layered-method maybe-box :in persistent ((thing null))
thing)
(define-layered-method maybe-box :in persistent ((thing string))
thing)
(define-layered-method maybe-box :in persistent ((thing list))
(mapcar #'maybe-box thing))
(define-layered-method maybe-unbox :in t (thing)
thing)
(define-layered-method maybe-unbox :in t ((thing list))
(mapcar #'maybe-unbox thing))
(define-layered-method maybe-unbox :in t ((box serial-box))
(hu.dwim.serializer:deserialize (first (mm:mptr-to-lisp-object
(serial-box-contents box)))))
(define-layered-method maybe-unbox :in persistent ((box serial-box))
(hu.dwim.serializer:deserialize (first (mm:mptr-to-lisp-object
(first (serial-box-contents box))))))
(define-layered-method maybe-unbox :in persistent ((thing t))
thing)
(define-layered-method maybe-unbox :in t ((thing mm::mm-fixed-string))
(mm:mm-fixed-string-value thing))
(define-layered-method maybe-unbox :in persistent ((thing mm::mm-fixed-string))
(mm:mm-fixed-string-value thing))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; SNODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defstruct (fundamental-snode
(:copier nil))
(key nil)
(value nil))
(defclass transient-snode ()
((key :initform nil :initarg :key :accessor snode-key)
(value :initform nil :initarg :value :accessor snode-value)))
(mm:defmmclass persistent-snode ()
((key :initform nil :initarg :key :accessor %snode-key :persistent t)
(value :initform nil :initarg :value :accessor %snode-value :persistent t)))
(defmethod snode-p ((thing t))
nil)
(defmethod snode-p ((thing fundamental-snode))
t)
(defmethod snode-p ((thing transient-snode))
t)
(defmethod snode-p ((thing persistent-snode))
t)
(define-layered-method snode :in t (key value &key)
(make-fundamental-snode :key key :value value))
(define-layered-method snode :in transient (key value &key)
(make-instance 'transient-snode :key key :value value))
(define-layered-method snode :in persistent (key value &key)
(make-instance 'persistent-snode :key (maybe-box key) :value (maybe-box value)))
(defmethod snode-key ((snode fundamental-snode))
(fundamental-snode-key snode))
(defmethod snode-value ((snode fundamental-snode))
(fundamental-snode-value snode))
(defmethod snode-key ((snode persistent-snode))
(maybe-unbox (%snode-key snode)))
(defmethod snode-value ((snode persistent-snode))
(maybe-unbox (%snode-value snode)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; LNODE
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defstruct (fundamental-lnode
(:copier nil))
(elt nil)
(next nil))
(defclass transient-lnode ()
((elt
:initform nil
:accessor lnode-elt
:initarg :elt)
(next
:initform nil
:accessor lnode-next
:initarg :next)))
(mm:defmmclass persistent-lnode ()
((elt
:initform nil
:accessor lnode-elt