summaryrefslogtreecommitdiff
path: root/lmdb.scm
blob: 8495c8aa7eb978b97972cfdf1d18861ba8fcdf25 (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
; SPDX-FileCopyrightText: 2021 pukkamustard <pukkamustard@posteo.net>
;
; SPDX-License-Identifier: GPL-3.0-or-later

(define-module (lmdb)
  #:use-module (srfi srfi-9)
  #:use-module (srfi srfi-34)
  #:use-module (srfi srfi-35)
  #:use-module (srfi srfi-158)

  #:use-module (rnrs io ports)
 
  #:use-module (lmdb low-level)

  #:export (&lmdb-error
            lmdb-error?
            lmdb-error-function-name
            lmdb-error-code
            lmdb-error-description

            <lmdb-db>
            lmdb-db?
            lmdb-open
            lmdb-close
            lmdb-get
            lmdb-put!
            lmdb-delete!
            lmdb-range))

;; Error handling

(define-condition-type &lmdb-error
  &error
  lmdb-error?
  (code lmdb-error-code))

(define (raise-lmdb-error code)
  (raise
   (make-compound-condition
    (make-condition &lmdb-error 'code code)
    (make-condition &message 'message (mdb-strerror code)))))

;; DB handle

(define-record-type <lmdb-db>
  (&make-lmdb-db env dbi closed?)
  lmdb-db?
  (env lmdb-db-env)
  (dbi lmdb-db-dbi)
  (closed? lmdb-db-closed? set-lmdb-db-closed?))

(define* (lmdb-open path #:key db-name)

  ;; create environment
  (define env (mdb-env-create))
  (unless (mdb-env? env)
    (raise-lmdb-error env))

  ;; open environment
  (define mdb-env-open-return-code (mdb-env-open env path))
  (unless (eqv? MDB_SUCCESS mdb-env-open-return-code)
    (raise-lmdb-error mdb-env-open-return-code))

  ;; create a transaction
  (define txn (mdb-txn-begin env))
  (unless (mdb-txn? txn)
    (raise-lmdb-error txn))

  ;; open a database
  ;; TODO error handling
  (define dbi (mdb-dbi-open txn))

  ;; commit transaction so that database handle can now be used from other
  ;; transactions
  (define mdb-txn-commit-return-code (mdb-txn-commit txn))
  (unless (eqv? MDB_SUCCESS mdb-txn-commit-return-code)
    (raise-lmdb-error mdb-txn-commit-return-code))

  (&make-lmdb-db env dbi #f))

(define (lmdb-close db)
  (mdb-env-close (lmdb-db-env db))
  (set-lmdb-db-closed? db #t))

(define lmdb-transaction?
  mdb-txn?)

(define (call-with-txn db-or-txn proc)
  (if (mdb-txn? db-or-txn)
      (proc db-or-txn)
      (let* ((txn (mdb-txn-begin (lmdb-db-env db-or-txn)))
             (return-value (proc txn)))
        (mdb-txn-commit txn)
        return-value)))


(define (lmdb-put! db key value)
  (case (call-with-txn db
                       (lambda (txn) (mdb-put txn (lmdb-db-dbi db)
                                              (make-mdb-val key)
                                              (make-mdb-val value))))
    ((0) #t)
    (else => raise-lmdb-error)))

(define (lmdb-get db key)
  (let ((return-value
         (call-with-txn db (lambda (txn)
                             (mdb-get txn (lmdb-db-dbi db)
                                      (make-mdb-val key))))))
    (cond
     ((mdb-val? return-value) (mdb-val-data return-value))
     ((eqv? return-value MDB_NOTFOUND) #f)
     (else (raise-lmdb-error return-value)))))

(define (lmdb-delete! db key)
  (let ((return-value
         (call-with-txn db (lambda (txn)
                             (mdb-del txn (lmdb-db-dbi db)
                                      (make-mdb-val key))))))
    (cond
     ((eqv? return-value MDB_SUCCESS) #t)
     ((eqv? return-value MDB_NOTFOUND) #f)
     (else (raise-lmdb-error return-value)))))

(define (lmdb-range db start-key end-key)
  (let* ((txn (mdb-txn-begin (lmdb-db-env db)))
         (cursor (mdb-cursor-open txn (lmdb-db-dbi db)))
         ;; wrap start-key and end-key as mdb-val
         (start-key-val (make-mdb-val start-key))
         (end-key-val (make-mdb-val end-key))
         ;; get an initial value with mdb-cursor-set
         (init-value (mdb-cursor-set cursor start-key-val MDB_SET_RANGE)))
    (make-coroutine-generator
     (lambda (yield)
       (let next ((return-value init-value))
         (cond

          ;; returned value is a key-value pair
          ((pair? return-value)
           ;; compare value with end-key and only yield if within range
           (if (>= 0 (mdb-cmp txn (lmdb-db-dbi db) (car return-value) end-key-val))
               (begin
                 ;; yield key-value pair
                 (yield (cons (mdb-val-data (car return-value))
                              (mdb-val-data (cdr return-value))))
                 ;; loop with next value
                 (next (mdb-cursor-get cursor MDB_NEXT)))
               (mdb-txn-abort txn)))

          ;; no value found, end generator
          ((equal? MDB_NOTFOUND return-value)
           (mdb-txn-abort txn))

          ;; unexpected return code, raise an error
          ((integer? return-value)
           (mdb-txn-abort txn)
           (raise-lmdb-error return-value))))))))