summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpukkamustard <pukkamustard@posteo.net>2021-01-05 21:07:25 +0100
committerpukkamustard <pukkamustard@posteo.net>2021-01-05 21:07:25 +0100
commit9209f548c73fca274c64a168a1244d70466c5fdf (patch)
tree9f9285e17feeffa7f05aa7646792cd487bd3a41f
parenta511b34a91b24509fb61c1d44a50a6b6ea485171 (diff)
(lmdb): Initial higher-level API
-rw-r--r--lmdb.scm65
-rw-r--r--lmdb/low-level.scm44
2 files changed, 108 insertions, 1 deletions
diff --git a/lmdb.scm b/lmdb.scm
index e933e20..8c4982d 100644
--- a/lmdb.scm
+++ b/lmdb.scm
@@ -3,4 +3,69 @@
; SPDX-License-Identifier: GPL-3.0-or-later
(define-module (lmdb)
+ #:use-module (srfi srfi-9)
#:use-module (lmdb low-level))
+
+(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 and open environment
+ (define env (mdb-env-create))
+ (mdb-env-open env path)
+
+ ;; create a transaction and open a database
+ (define txn (mdb-txn-begin env))
+ (define dbi (mdb-dbi-open txn))
+
+ ;; commit transaction so that database handle can now be used from other
+ ;; transactions
+ (mdb-txn-commit txn)
+
+ (&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 (lmdb-put! db key value)
+ (let ((txn (mdb-txn-begin (lmdb-db-env db))))
+ (mdb-put txn (lmdb-db-dbi db)
+ (make-mdb-val key)
+ (make-mdb-val value))
+ (mdb-txn-commit txn)))
+
+(define (lmdb-get db key)
+ (let* ((txn (mdb-txn-begin (lmdb-db-env db)))
+ (val (mdb-get txn (lmdb-db-dbi db)
+ (make-mdb-val key))))
+ (mdb-txn-abort txn)
+ (mdb-val-data val)))
+
+(define (lmdb-delete! db key)
+ (let ((txn (mdb-txn-begin (lmdb-db-env db))))
+ (mdb-del txn (lmdb-db-dbi db)
+ (make-mdb-val key))
+ (mdb-txn-commit txn)))
+
+(define db (lmdb-open "./testdb"))
+
+(lmdb-db? db)
+
+(use-modules (rnrs bytevectors))
+
+(lmdb-put! db
+ (string->utf8 "hi")
+ (string->utf8 "blupsblups"))
+
+(lmdb-get db (string->utf8 "hi"))
+
+(lmdb-delete! db (string->utf8 "hi"))
diff --git a/lmdb/low-level.scm b/lmdb/low-level.scm
index 56bdf49..30ddba6 100644
--- a/lmdb/low-level.scm
+++ b/lmdb/low-level.scm
@@ -12,7 +12,32 @@
#:use-module (rnrs bytevectors)
- #:use-module (lmdb internal))
+ #:use-module (lmdb internal)
+
+ #:export (<mdb-env>
+ mdb-env?
+ mdb-env-create
+ mdb-env-open
+ mdb-env-close
+
+ <mdb-txn>
+ mdb-txn?
+ mdb-txn-begin
+ mdb-txn-abort
+ mdb-txn-commit
+
+ <mdb-dbi>
+ mdb-dbi?
+ mdb-dbi-open
+
+ <mdb-val>
+ mdb-val?
+ make-mdb-val
+ mdb-val-data
+
+ mdb-put
+ mdb-get
+ mdb-del))
;; MDB_env
@@ -193,3 +218,20 @@
data-ptr)
;; wrap data as <mdb-val>
(wrap-mdb-val data-ptr)))
+
+(define (mdb-del txn dbi key)
+ "Delete items from a database"
+ (let ((proc (liblmdb-func "mdb_del"
+ (list
+ ;; MDB_txn* txn
+ '*
+ ;; MDB_dbi* dbi
+ '*
+ ;; MDB_val* key
+ '*
+ ;; MDB_val* data
+ '*))))
+ (proc (unwrap-mdb-txn txn)
+ (unwrap-mdb-dbi dbi)
+ (unwrap-mdb-val key)
+ (make-pointer 0))))