summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-01-12 11:19:29 +0100
committerpukkamustard <pukkamustard@posteo.net>2021-01-12 11:19:29 +0100
commit6b808fcf5c22f75ad0b4ad649749c6ae29b44bed (patch)
tree7eb7502cce96d876a432833eb417c1b06fbc71a9
parent617a31003623bce844cb077cdec45cfb7b604f7f (diff)
(lmdb): Fix error-handlingHEADmain
-rw-r--r--lmdb.scm80
1 files changed, 46 insertions, 34 deletions
diff --git a/lmdb.scm b/lmdb.scm
index 39d3846..e1f2b8e 100644
--- a/lmdb.scm
+++ b/lmdb.scm
@@ -68,8 +68,9 @@
(raise-lmdb-error txn))
;; open a database
- ;; TODO error handling
- (define dbi (mdb-dbi-open txn))
+ (define dbi (mdb-dbi-open txn #:name db-name))
+ (unless (mdb-dbi? dbi)
+ (raise-lmdb-error dbi))
;; commit transaction so that database handle can now be used from other
;; transactions
@@ -126,35 +127,46 @@
(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))))))))
+
+ ;; create a new read-only transaction
+ (define txn (mdb-txn-begin (lmdb-db-env db) #:flags MDB_RDONLY))
+ (unless (mdb-txn? txn)
+ (raise-lmdb-error txn))
+
+ ;; create a new cursor
+ (define cursor (mdb-cursor-open txn (lmdb-db-dbi db)))
+ (unless (mdb-cursor? cursor)
+ (raise-lmdb-error cursor))
+
+ ;; wrap start-key and end-key as mdb-val
+ (define start-key-val (make-mdb-val start-key))
+ (define end-key-val (make-mdb-val end-key))
+
+ ;; get an initial value with mdb-cursor-set
+ (define 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)))))))