~amirouche/ruse-babeltoy

ref: bf724338095cfa1a4080e60a563345dcb7f0aa22 ruse-babeltoy/src/foundationdb/sync.scm -rw-r--r-- 7.7 KiB
bf724338Amirouche 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(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 (lambda () (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-future-block-until-ready* future)

      (define on-ready 
        (lambda (condition self)
          (condition-signal condition)
          (unlock-object self)))
      (pk 'ready?)
      (unless (pk 'ready (fdb-future-ready? future))
        (let ((mutex (make-mutex))
              (condition (make-condition)))
          (letrec* ((code (foreign-callable (lambda (a b) (on-ready condition code)) (void* void*) void)))
            (lock-object code)
            (mutex-acquire mutex)
            (condition-wait condition mutex)
            (mutex-release mutex)
            (fdb-future-set-callback (foreign-callable-entry-point code))))))
          
    (define (fdb-transaction-commit* transaction)
      (let ((future (fdb-transaction-commit (transaction-pointer transaction))))
        (pk 'bloking-commit)
        (fdb-future-block-until-ready* future)
        (pk 'unblock)
        (let ((error (fdb-future-get-error future)))
          (pk 'error error)
          (fdb-future-destroy future)
          (fdb-transaction-destroy (transaction-pointer transaction))
          (pk 'error2 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 (pk (fdb-transaction-begin fdb))))
        (let loop ()
          (pk 'loop)
          (call-with-values (lambda () (proc tx))
            (lambda out
              (pk 'commiting)
              (let ((error (fdb-transaction-commit* tx)))
                (pk 'error 'again error)
                (if (pk (fxzero? error))
                    (apply values (pk out))
                    (let ((future (fdb-transaction-on-error (transaction-pointer tx) error)))
                      (pk 'block)
                      (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)))))))