aboutsummaryrefslogtreecommitdiff
path: root/leveldb.scm
blob: 090cf1bab0e4826d44d28c77465bad708460cc48 (plain)
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
(define-module (leveldb)
  #:use-module (rnrs bytevectors)
  #:use-module (system foreign)
  #:use-module (ice-9 exceptions)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-167)
  #:export (leveldb-open
            leveldb-db?
            leveldb-close

            leveldb-put
            leveldb-delete
            leveldb-get

            leveldb-options-create
            leveldb-options-set-error-if-exists
            leveldb-options-set-create-if-missing

            leveldb-version

            make-leveldb-okvs))

(define libleveldb (dynamic-link "libleveldb"))

;; Version

(define leveldb-major-version
  (pointer->procedure
   int
   (dynamic-func "leveldb_major_version" libleveldb)
   (list)))

(define leveldb-minor-version
 (pointer->procedure
  int
  (dynamic-func "leveldb_minor_version" libleveldb)
  (list)))

(define (leveldb-version)
  "Return the version of LevelDB as a dotted pair of major-version and minor-version."
  (cons (leveldb-major-version) (leveldb-minor-version)))


;; Options

;; TODO expose options

(define-wrapped-pointer-type <leveldb-options>
  leveldb-options?
  wrap-leveldb-options unwrap-leveldb-options
  (lambda (o p)
    (format p "#<leveldb-options ~x>"
            (pointer-address (unwrap-leveldb-options o)))))

(define leveldb-options-create
  (let ((proc (pointer->procedure
               '*
               (dynamic-pointer "leveldb_options_create" libleveldb)
               (list))))
    (lambda ()
      (wrap-leveldb-options (proc)))))

(define leveldb-options-set-error-if-exists
  (let ((proc (pointer->procedure
               void
               (dynamic-pointer "leveldb_options_set_error_if_exists" libleveldb)
               (list '* uint8))))
    (lambda (options flag)
      (proc (unwrap-leveldb-options options) flag))))

(define leveldb-options-set-create-if-missing
  (let ((proc (pointer->procedure
               void
               (dynamic-pointer "leveldb_options_set_create_if_missing" libleveldb)
               (list '* uint8))))
    (lambda (options flag)
      (proc (unwrap-leveldb-options options) flag))))

;; Write options

(define-wrapped-pointer-type <write-options>
  write-options?
  wrap-write-options unwrap-write-options
  (lambda (o p)
    (format p "#<leveldb:write-options ~x>"
            (pointer-address (unwrap-write-options o)))))

(define write-options-create
  (let ((proc (pointer->procedure
               '*
               (dynamic-pointer "leveldb_writeoptions_create" libleveldb)
               (list))))
    (lambda () (wrap-write-options (proc)))))

;; Read options

(define-wrapped-pointer-type <read-options>
  read-options?
  wrap-read-options unwrap-read-options
  (lambda (o p)
    (format p "#<leveldb:read-options ~x>"
            (pointer-address (unwrap-read-options o)))))

(define read-options-create
  (let ((proc (pointer->procedure
               '*
               (dynamic-pointer "leveldb_readoptions_create" libleveldb)
               (list))))
    (lambda () (wrap-read-options (proc)))))

;; Error handling

(define free
  (pointer->procedure void (dynamic-func "free" libleveldb) (list '*)))

;; LevelDB takes a char** errptr as last argument to procedures. If error occurs *errptr points to malloc()ed error messgae.
(define pointer->procedure-with-error-handling
  (lambda (return-type proc-ptr arg-types)
    (let ((proc (pointer->procedure return-type proc-ptr
                                   (append arg-types (list '*)))))

      (lambda* (#:rest args)
        (let*
            ( ;; create a pointer to hold potential error messages
             (err-ptr (bytevector->pointer
                       (make-bytevector (sizeof '*) 0)))

             ;; execute procedure with args and err-ptr
             (ret (apply proc
                   (append args (list err-ptr)))))

          (if (null-pointer? (dereference-pointer err-ptr))
              ret
              (let
                  ;; copy error message
                  ((msg (string-copy
                         (pointer->string (dereference-pointer err-ptr)))))
                ;; free the malloc()ed error message
                (free (dereference-pointer err-ptr))
                (raise-exception
                 (make-exception
                  (make-external-error)
                  (make-exception-with-message msg))
                 #:continuable? #t))))))))

;; DB type for convenience

(define-record-type <leveldb-db>
  (make-db pointer options read-options write-options open?)
  db?
  (pointer db-pointer)
  (options db-options)
  (read-options db-read-options)
  (write-options db-write-options)
  (open? db-open? set-db-open?!))

;; DB operations

(define leveldb-open
  (let ((proc (pointer->procedure-with-error-handling
               '*
               (dynamic-pointer "leveldb_open" libleveldb)
               (list
                ;; leveldb_options_t*
                '*
                ;; char* name
                '*))))
    (lambda* (name #:optional
                   (options (leveldb-options-create))
                   (read-options (read-options-create))
                   (write-options (write-options-create)))
      (make-db
       (proc (unwrap-leveldb-options options)
             (string->pointer name))
       options
       read-options
       write-options
       #t))))

(define-syntax leveldb-db?
  (identifier-syntax db?))

(define leveldb-close
  (let ((proc (pointer->procedure
               void
               (dynamic-pointer "leveldb_close" libleveldb)
               (list '*))))
    (lambda (db)
      (when (db-open? db)
        (proc (db-pointer db))
        (set-db-open?! db #f)))))

(define leveldb-put
  (let ((proc (pointer->procedure-with-error-handling
               void
               (dynamic-pointer "leveldb_put" libleveldb)
               (list
                ;; leveldb_t*
                '*
                ;; leveldb_writeoptions_t*
                '*
                ;; char* key and size_t keylen
                '* size_t
                ;; char* val and size_t vallen
                '* size_t))))

    (lambda (db key val)
      (when (db-open? db)
        (proc (db-pointer db)
              (unwrap-write-options (db-write-options db))
              (bytevector->pointer key) (bytevector-length key)
              (bytevector->pointer val) (bytevector-length val))))))

(define leveldb-delete
  (let ((proc (pointer->procedure-with-error-handling
               void
               (dynamic-func "leveldb_delete" libleveldb)
               (list
                ;; leveldb_t*
                '*
                ;; leveldb_writeoptions_t*
                '*
                ;; char* key and size_t keylen
                '* size_t))))

    (lambda (db key)
      (when (db-open? db)
        (proc (db-pointer db)
              (unwrap-write-options (db-write-options db))
              (bytevector->pointer key) (bytevector-length key))))))

(define leveldb-get
  (let ((proc (pointer->procedure-with-error-handling
               '*
               (dynamic-func "leveldb_get" libleveldb)
               (list
                ;; leveldb_t*
                '*
                ;; leveldb_readoptions_t*
                '*
                ;; char* key and size_t keylen
                '* size_t
                ;; size_t* vallen
                '*))))
    (lambda (db key)
      (when (db-open? db)
        (let* ( ;; pointer that will hold lenght of value
               (vallen (bytevector->pointer (make-bytevector (sizeof '*) 0)))
               ;; execute procedure
               (ret (proc (db-pointer db)
                          (unwrap-read-options (db-read-options db))
                          (bytevector->pointer key) (bytevector-length key)
                          vallen)))
          (unless (null-pointer? ret)
            (pointer->bytevector ret
                                 ;; buckle your belts, this is pointer magic
                                 (pointer-address (dereference-pointer vallen)))))))))


;; SRFI-167 binding

(define (make-leveldb-okvs)
  (wrap-okvs
   leveldb-open
   leveldb-close
   leveldb-get
   leveldb-put
   leveldb-delete))