~amirouche/ruse-babeltoy

ref: cfe490a7c8237964d34b16df7316fcc5dd60d61d ruse-babeltoy/src/foundationdb/sync.scm -rw-r--r-- 6.8 KiB
cfe490a7Amirouche wip 1 year, 3 months ago
                                                                                
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
(library (foundationdb sync)

  (export fdb-init!
          make-fdb
          fdb-close
          fdb-in-transaction
          fdb-ref
          fdb-set!
          ;; fdb-delete!
          fdb-range
          strinc)

  (import (chezscheme) (foundationdb))

  (begin

    (define-syntax define-record-type*
      (lambda (stx)
        (syntax-case stx ()
          ((_ type (constructor constructor-tag ...)
              predicate
              (field-tag accessor setter ...) ...)
           (and (for-all identifier?
                         #'(type constructor constructor-tag ... predicate
                                 field-tag ... accessor ... setter ... ...))
                (for-all (lambda (s) (<= 0 (length s) 1))
                         #'((setter ...) ...))
                (for-all (lambda (ct)
                           (memp (lambda (ft) (bound-identifier=? ct ft))
                                 #'(field-tag ...)))
                         #'(constructor-tag ...)))
           (with-syntax (((field-clause ...)
                          (map (lambda (clause)
                                 (if (= 2 (length clause))
                                     #`(immutable . #,clause)
                                     #`(mutable . #,clause)))
                               #'((field-tag accessor setter ...) ...)))
                         ((unspec-tag ...)
                          (remp (lambda (ft)
                                  (memp (lambda (ct) (bound-identifier=? ft ct))
                                        #'(constructor-tag ...)))
                                #'(field-tag ...))))
                        #'(define-record-type (type constructor predicate)
                            (protocol (lambda (ctor)
                                        (lambda (constructor-tag ...)
                                          (define unspec-tag) ...
                                          (ctor field-tag ...))))
                            (fields field-clause ...)))))))

    (define-record-type* <fdb>
      (%make-fdb database)
      fdb?
      (database fdb-database))

    (define-record-type* <transaction>
      (make-transaction pointer)
      okvs-transaction?
      (pointer transaction-pointer))

    (define %network-thread #f)

    (define (fdb-init!)
      (fdb-select-api-version 630)
      ;; setup network thread
      (fdb-setup-network)
      (set! %network-thread (fork-thread fdb-run-network)))

    (define (make-fdb)
      (%make-fdb (fdb-create-database #f)))

    (define (fdb-close fdb)
      (fdb-database-destroy (fdb-database fdb)))

    (define (fdb-transaction-begin fdb)
      (make-transaction (fdb-database-create-transaction (fdb-database fdb))))

    (define (pk . args)
      (write args)(newline)
      (car (reverse args)))
    
    (define (fdb-transaction-commit* transaction)
      (let ((future (fdb-transaction-commit (transaction-pointer transaction))))
        (fdb-future-block-until-ready future)
        (let ((error (fdb-future-get-error future)))
          (fdb-future-destroy future)
          (fdb-transaction-destroy (transaction-pointer transaction))
          error)))

    (define (fdb-transaction-rollback transaction)
      (fdb-transaction-cancel (transaction-pointer transaction))
      (fdb-transaction-destroy (transaction-pointer transaction)))

    (define (fdb-in-transaction fdb proc)
      (let ((tx (fdb-transaction-begin fdb)))
        (let loop ()
          (call-with-values (lambda () (proc tx))
            (lambda out
              (let ((error (fdb-transaction-commit* tx)))
                (if (fxzero? error)
                    (apply values out)
                    (let ((future (fdb-transaction-on-error (transaction-pointer tx) error)))
                      (fdb-future-block-until-ready future)
                      (let ((error (fdb-future-get-error future)))
                        (fdb-future-destroy future)
                        (if (fxzero? error)
                            (loop)
                            (begin
                              (fdb-transaction-rollback (transaction-pointer tx))
                              (raise (cons 'foundationdb error))))))))))))))

    (define (fdb-ref tx key)
      (let ((future (fdb-transaction-get (transaction-pointer tx)
                                         key
                                         #f)))
        (fdb-future-block-until-ready future)
        (let ((error (fdb-future-get-error future)))
          (if (fxzero? error)
              (let ((value (fdb-future-get-value future)))
                (fdb-future-destroy future)
                value)
               (begin
                 (fdb-future-destroy future)
                 (raise (cons 'foundationdb error)))))))

    (define (fdb-set! tx key value)
      (fdb-transaction-set (transaction-pointer tx) key value))

    (define (fdb-delete! transaction key)
      (fdb-transaction-clear (transaction-pointer transaction) key))

    (define (fdb-range transaction
                       start-key
                       start-include?
                       end-key
                       end-include?
                       reverse?)
      (let ((future (fdb-transaction-get-range (transaction-pointer transaction)
                                               start-key
                                               start-include?
                                               0
                                               end-key
                                               end-include?
                                               1
                                               0
                                               0
                                               -2 ;; WANT_ALL
                                               0
                                               #f
                                               reverse?)))
        (fdb-future-block-until-ready future)
        (let ((error (fdb-future-get-error future)))
          (if (fxzero? error)
              (let ((range (fdb-future-get-range future)))
                (fdb-future-destroy future)
                range)
              (raise (cons 'foundationdb error))))))

    (define (strinc bytevector)
      "Return the first bytevector that is not prefix of BYTEVECTOR"
      ;; See https://git.io/fj34F, TODO: OPTIMIZE
      (let ((bytes (reverse (bytevector->u8-list bytevector))))
        ;; strip #xFF
        (let loop ((out bytes))
          (when (null? out)
            (error 'foundationdb
                   "BYTEVECTOR must contain at least one byte not equal to #xFF."
                   bytevector))
          (if (= (car out) #xFF)
              (loop (cdr out))
              (set! bytes out)))
        ;; increment first byte, reverse and return the bytevector
        (u8-list->bytevector (reverse (cons (+ 1 (car bytes)) (cdr bytes)))))))