changelog shortlog graph tags branches files raw help

Mercurial > core / changeset: ffi

changeset 2: ca8af026ee3d
parent 1: 4f38a5cb8b09
child 3: 4dbfe05407bd
author: ellis <ellis@rwest.io>
date: Sun, 15 Oct 2023 01:50:27 -0400
files: .hgignore .hgsubstate lisp/btrfs.asd lisp/btrfs/btrfs.lisp lisp/btrfs/tests.lisp lisp/ffi/btrfs.asd lisp/ffi/btrfs/btrfs.lisp lisp/ffi/btrfs/tests.lisp lisp/ffi/quiche.asd lisp/ffi/quiche.lisp lisp/ffi/rocksdb.asd lisp/ffi/rocksdb/rocksdb.lisp lisp/ffi/rocksdb/tests.lisp lisp/ffi/uring.asd lisp/ffi/uring.lisp lisp/rdb/rdb.lisp lisp/rocksdb.asd lisp/rocksdb/rocksdb.lisp lisp/rocksdb/tests.lisp readme.org
description: ffi
     1.1--- a/.hgignore	Thu Oct 12 22:51:00 2023 -0400
     1.2+++ b/.hgignore	Sun Oct 15 01:50:27 2023 -0400
     1.3@@ -0,0 +1,7 @@
     1.4+.*Cargo.lock$
     1.5+.*target/.*
     1.6+.*[.]fasl$
     1.7+.*[.]o$
     1.8+.*[.]so$
     1.9+.*[.]dylib$
    1.10+.*[.]a$
    1.11\ No newline at end of file
     2.1--- a/.hgsubstate	Thu Oct 12 22:51:00 2023 -0400
     2.2+++ b/.hgsubstate	Sun Oct 15 01:50:27 2023 -0400
     2.3@@ -1,3 +1,3 @@
     2.4 0000000000000000000000000000000000000000 lisp/macs
     2.5-0000000000000000000000000000000000000000 lisp/organ
     2.6-0000000000000000000000000000000000000000 lisp/skel
     2.7+080137fb0579395424bb96622252d1ddb3dade5d lisp/organ
     2.8+6b7881cc28419cd135ce64216267a42a0960eb10 lisp/skel
     3.1--- a/lisp/btrfs.asd	Thu Oct 12 22:51:00 2023 -0400
     3.2+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3@@ -1,34 +0,0 @@
     3.4-;;; btrfs.asd --- BTRFS SYSTEMS
     3.5-
     3.6-;; BTRFS for lisp.
     3.7-
     3.8-;;; Code:
     3.9-(eval-when (:compile-toplevel :load-toplevel :execute)
    3.10-  (require :sb-grovel))
    3.11-
    3.12-(defpackage :btrfs.sys
    3.13-  (:use :cl :asdf :sb-grovel :sb-alien))
    3.14-
    3.15-(in-package :btrfs.sys)
    3.16-
    3.17-(defsystem "btrfs"
    3.18-  :version "0.1.0"
    3.19-  :license (:file "LICENSE")
    3.20-  :maintainer "ellis <ellis@rwest.io>"
    3.21-  :homepage "https://nas-t.net"
    3.22-  :bug-tracker "https://lab.rwest.io/comp/startup/nas-t/issues"
    3.23-;;  :depends-on (:macs :sxp)
    3.24-  :in-order-to ((test-op (test-op "btrfs/tests")))
    3.25-  :components ((:module "btrfs"
    3.26-                :components
    3.27-                ((:file "btrfs")))))
    3.28-
    3.29-(defsystem "btrfs/tests"
    3.30-  :version "0.1.0"
    3.31-  :license (:file "LICENSE")
    3.32-  :maintainer "ellis <ellis@rwest.io>"
    3.33-  :homepage "https://nas-t.net"
    3.34-  :bug-tracker "https://lab.rwest.io/comp/startup/nas-t/issues"
    3.35-  :depends-on (:btrfs :sb-rt :rt)
    3.36-  :components ((:file "btrfs/tests"))
    3.37-  :perform (test-op (op c) (uiop:symbol-call '#:btrfs.tests '#:run-all-tests)))
     4.1--- a/lisp/btrfs/btrfs.lisp	Thu Oct 12 22:51:00 2023 -0400
     4.2+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3@@ -1,55 +0,0 @@
     4.4-;;; src/fs/btrfs/btrfs.lisp --- BTRFS common-lisp API
     4.5-
     4.6-;; This package contains FFI bindings to the BTRFS C libraries libbtrfs and
     4.7-;; libbtrfsutil as well as some additional core routines from Rust.
     4.8-
     4.9-;;; Commentary:
    4.10-
    4.11-;; BTRFS is a core component of the NAS-T stack. We might even consider NAS-T as a
    4.12-;; wrapper around BTRFS APIs in the same we we could say that TrueNAS is a wrapper
    4.13-;; around ZFS.
    4.14-
    4.15-;; NOTE 2023-09-03: currently the app has no concrete use-cases for accessing BTRFS APIs
    4.16-;; directly from lisp. This will inevitably change, and we want the bindings for
    4.17-;; debugging and experimentation.
    4.18-
    4.19-;;; Code:
    4.20-(defpackage btrfs
    4.21-  (:use :cl :sb-alien)
    4.22-  (:export
    4.23-   :btrfs-shared-objects
    4.24-   :btrfs-lib-path
    4.25-   :load-btrfs :unload-btrfs
    4.26-   :load-btrfsutil :unload-btrfsutil
    4.27-   :define-btrfs-ioctl))
    4.28-
    4.29-(in-package :btrfs)
    4.30-
    4.31-(eval-when (:compile-toplevel :load-toplevel :execute)
    4.32-  (defvar btrfs-shared-objects
    4.33-    (list '(:btrfs "/usr/lib/libbtrfs.so")
    4.34-          '(:btrfsutil "/usr/lib/libbtrfsutil.so")))
    4.35-  
    4.36-  (defun btrfs-lib-path (lib) (cadr (assoc lib btrfs-shared-objects))))
    4.37-                
    4.38-(defmacro when-lib-exists-p ((sym lib) &body body)
    4.39-  `(let ((,sym ,(btrfs-lib-path lib)))
    4.40-    (when (uiop:file-exists-p ,sym) ,@body)))
    4.41-
    4.42-(defun load-btrfs (&optional save)
    4.43-  "Open 'libbtrfs' using `dlopen'. exposing the C API to the current Lisp image."
    4.44-  (when-lib-exists-p (l :btrfs) (load-shared-object l :dont-save (not save))))
    4.45-
    4.46-(defun unload-btrfs ()
    4.47-  "Close 'libbtrfs' using `dlclose'."
    4.48-  (unload-shared-object (btrfs-lib-path :btrfs)))
    4.49-
    4.50-(defun load-btrfsutil ()
    4.51-  "Open 'libbtrfsutil' using `dlopen'. exposing the C API to the current Lisp image."
    4.52-  (when-lib-exists-p (l :btrfsutil) (load-shared-object l)))
    4.53-
    4.54-(defun unload-btrfsutil ()
    4.55-  "Close 'libbtrfsutil' using `dlclose'."
    4.56-  (unload-shared-object (btrfs-lib-path :btrfsutil)))
    4.57-
    4.58-(defmacro define-btrfs-ioctl () "Define a wrapper for IOCTLs exposed by BTRFS.")
     5.1--- a/lisp/btrfs/tests.lisp	Thu Oct 12 22:51:00 2023 -0400
     5.2+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3@@ -1,10 +0,0 @@
     5.4-;;; src/fs/btrfs/tests.lisp --- BTRFS common-lisp tests
     5.5-
     5.6-;;; Code:
     5.7-(defpackage btrfs.tests
     5.8-  (:use :cl :rt :btrfs))
     5.9-(in-package :btrfs.tests)
    5.10-
    5.11-(defsuite :btrfs)
    5.12-
    5.13-(in-suite :btrfs)
     6.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2+++ b/lisp/ffi/btrfs.asd	Sun Oct 15 01:50:27 2023 -0400
     6.3@@ -0,0 +1,26 @@
     6.4+;;; btrfs.asd --- BTRFS SYSTEMS
     6.5+
     6.6+;; BTRFS for lisp.
     6.7+
     6.8+;;; Code:
     6.9+(defsystem "btrfs"
    6.10+  :version "0.1.0"
    6.11+  :license (:file "LICENSE")
    6.12+  :maintainer "ellis <ellis@rwest.io>"
    6.13+  :homepage "https://nas-t.net"
    6.14+  :bug-tracker "https://lab.rwest.io/comp/core/issues"
    6.15+  :depends-on (:macs :sxp)
    6.16+  :in-order-to ((test-op (test-op "btrfs/tests")))
    6.17+  :components ((:module "btrfs"
    6.18+                :components
    6.19+                ((:file "btrfs")))))
    6.20+
    6.21+(defsystem "btrfs/tests"
    6.22+  :version "0.1.0"
    6.23+  :license (:file "LICENSE")
    6.24+  :maintainer "ellis <ellis@rwest.io>"
    6.25+  :homepage "https://nas-t.net"
    6.26+  :bug-tracker "https://lab.rwest.io/comp/core/issues"
    6.27+  :depends-on (:btrfs :rt)
    6.28+  :components ((:file "btrfs/tests"))
    6.29+  :perform (test-op (op c) (uiop:symbol-call '#:rt '#:do-tests :btrfs)))
     7.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2+++ b/lisp/ffi/btrfs/btrfs.lisp	Sun Oct 15 01:50:27 2023 -0400
     7.3@@ -0,0 +1,55 @@
     7.4+;;; src/fs/btrfs/btrfs.lisp --- BTRFS common-lisp API
     7.5+
     7.6+;; This package contains FFI bindings to the BTRFS C libraries libbtrfs and
     7.7+;; libbtrfsutil as well as some additional core routines from Rust.
     7.8+
     7.9+;;; Commentary:
    7.10+
    7.11+;; BTRFS is a core component of the NAS-T stack. We might even consider NAS-T as a
    7.12+;; wrapper around BTRFS APIs in the same we we could say that TrueNAS is a wrapper
    7.13+;; around ZFS.
    7.14+
    7.15+;; NOTE 2023-09-03: currently the app has no concrete use-cases for accessing BTRFS APIs
    7.16+;; directly from lisp. This will inevitably change, and we want the bindings for
    7.17+;; debugging and experimentation.
    7.18+
    7.19+;;; Code:
    7.20+(defpackage btrfs
    7.21+  (:use :cl :sb-alien)
    7.22+  (:export
    7.23+   :btrfs-shared-objects
    7.24+   :btrfs-lib-path
    7.25+   :load-btrfs :unload-btrfs
    7.26+   :load-btrfsutil :unload-btrfsutil
    7.27+   :define-btrfs-ioctl))
    7.28+
    7.29+(in-package :btrfs)
    7.30+
    7.31+(eval-when (:compile-toplevel :load-toplevel :execute)
    7.32+  (defvar btrfs-shared-objects
    7.33+    (list '(:btrfs "/usr/lib/libbtrfs.so")
    7.34+          '(:btrfsutil "/usr/lib/libbtrfsutil.so")))
    7.35+  
    7.36+  (defun btrfs-lib-path (lib) (cadr (assoc lib btrfs-shared-objects))))
    7.37+                
    7.38+(defmacro when-lib-exists-p ((sym lib) &body body)
    7.39+  `(let ((,sym ,(btrfs-lib-path lib)))
    7.40+    (when (uiop:file-exists-p ,sym) ,@body)))
    7.41+
    7.42+(defun load-btrfs (&optional save)
    7.43+  "Open 'libbtrfs' using `dlopen'. exposing the C API to the current Lisp image."
    7.44+  (when-lib-exists-p (l :btrfs) (load-shared-object l :dont-save (not save))))
    7.45+
    7.46+(defun unload-btrfs ()
    7.47+  "Close 'libbtrfs' using `dlclose'."
    7.48+  (unload-shared-object (btrfs-lib-path :btrfs)))
    7.49+
    7.50+(defun load-btrfsutil ()
    7.51+  "Open 'libbtrfsutil' using `dlopen'. exposing the C API to the current Lisp image."
    7.52+  (when-lib-exists-p (l :btrfsutil) (load-shared-object l)))
    7.53+
    7.54+(defun unload-btrfsutil ()
    7.55+  "Close 'libbtrfsutil' using `dlclose'."
    7.56+  (unload-shared-object (btrfs-lib-path :btrfsutil)))
    7.57+
    7.58+(defmacro define-btrfs-ioctl () "Define a wrapper for IOCTLs exposed by BTRFS.")
     8.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2+++ b/lisp/ffi/btrfs/tests.lisp	Sun Oct 15 01:50:27 2023 -0400
     8.3@@ -0,0 +1,10 @@
     8.4+;;; src/fs/btrfs/tests.lisp --- BTRFS common-lisp tests
     8.5+
     8.6+;;; Code:
     8.7+(defpackage btrfs.tests
     8.8+  (:use :cl :rt :btrfs))
     8.9+(in-package :btrfs.tests)
    8.10+
    8.11+(defsuite :btrfs)
    8.12+
    8.13+(in-suite :btrfs)
     9.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2+++ b/lisp/ffi/quiche.asd	Sun Oct 15 01:50:27 2023 -0400
     9.3@@ -0,0 +1,7 @@
     9.4+(defsystem "quiche"
     9.5+  :version "0.1.0"
     9.6+  :maintainer "ellis <ellis@rwest.io>"
     9.7+  :homepage "https://nas-t.net"
     9.8+  :bug-tracker "https://lab.rwest.io/comp/startup/nas-t/issues"
     9.9+  :depends-on (:macs)
    9.10+  :components ((:file "quiche")))
    10.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    10.2+++ b/lisp/ffi/quiche.lisp	Sun Oct 15 01:50:27 2023 -0400
    10.3@@ -0,0 +1,10 @@
    10.4+(pkg:defpkg :quiche
    10.5+  (:use :cl :pkg :alien)
    10.6+  (:export :quiche-version))
    10.7+(in-package :quiche)
    10.8+(load-shared-object #P"/usr/local/lib/libquiche.so")
    10.9+
   10.10+(define-alien-type quiche-config (struct quiche-config))
   10.11+(define-alien-routine quiche-config-new quiche-config (version unsigned-int))
   10.12+(define-alien-routine quiche-version c-string)
   10.13+
    11.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    11.2+++ b/lisp/ffi/rocksdb.asd	Sun Oct 15 01:50:27 2023 -0400
    11.3@@ -0,0 +1,28 @@
    11.4+;;; rocksdb.asd --- ROCKSDB SYSTEMS
    11.5+
    11.6+;; rocksdb for lisp.
    11.7+
    11.8+;;; Commentary:
    11.9+
   11.10+;; based on Vee's cl-rocksdb: https://github.com/veer66/cl-rocksdb/tree/main
   11.11+
   11.12+;;; Code:
   11.13+(defsystem "rocksdb"
   11.14+  :version "0.1.0"
   11.15+  :license (:file "LICENSE")
   11.16+  :maintainer "ellis <ellis@rwest.io>"
   11.17+  :homepage "https://nas-t.net"
   11.18+  :bug-tracker "https://lab.rwest.io/comp/startup/nas-t/issues"
   11.19+  :depends-on (:macs)
   11.20+  :in-order-to ((test-op (test-op "rocksdb/tests")))
   11.21+  :components ((:file "rocksdb/rocksdb")))
   11.22+
   11.23+(defsystem "rocksdb/tests"
   11.24+  :version "0.1.0"
   11.25+  :license (:file "LICENSE")
   11.26+  :maintainer "ellis <ellis@rwest.io>"
   11.27+  :homepage "https://nas-t.net"
   11.28+  :bug-tracker "https://lab.rwest.io/comp/nas-t/issues"
   11.29+  :depends-on (:rocksdb :rt)
   11.30+  :components ((:file "rocksdb/tests"))
   11.31+  :perform (test-op (op c) (uiop:symbol-call '#:rt '#:do-tests :rocksdb)))
    12.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.2+++ b/lisp/ffi/rocksdb/rocksdb.lisp	Sun Oct 15 01:50:27 2023 -0400
    12.3@@ -0,0 +1,235 @@
    12.4+;;; rocksdb.lisp --- low-level bindings to the RocksDB C API
    12.5+
    12.6+;; for the high-level interface, see rdb.lisp.
    12.7+
    12.8+;;; Commentary:
    12.9+
   12.10+;; if ur on archlinux and installed rocksdb via AUR you may receive an error from
   12.11+;; jemalloc: cannot allocate memory in static TLS block:
   12.12+
   12.13+;; https://github.com/veer66/cl-rocksdb/issues/1
   12.14+
   12.15+;; for best results, you should compile rocksdb from source - use j0ni's snippet as a
   12.16+;; starting point.
   12.17+
   12.18+;; make shared_lib DISABLE_JEMALLOC=1 && 
   12.19+;; sudo cp librocksdb.so.* /usr/local/lib/ && 
   12.20+;; sudo cp -rf include/* /usr/local/include/
   12.21+
   12.22+;; https://github.com/facebook/rocksdb/blob/main/Makefile
   12.23+
   12.24+;; check /usr/local/include/rocksdb/c.h for the C API header, the source is under
   12.25+;; db/c.cc
   12.26+
   12.27+;; here are some important notes to keepin mind (from the API header):
   12.28+#|
   12.29+  C bindings for rocksdb.  May be useful as a stable ABI that can be
   12.30+  used by programs that keep rocksdb in a shared library, or for
   12.31+  a JNI api.
   12.32+
   12.33+  Does not support:
   12.34+  . getters for the option types
   12.35+  . custom comparators that implement key shortening
   12.36+  . capturing post-write-snapshot
   12.37+  . custom iter, db, env, cache implementations using just the C bindings
   12.38+
   12.39+  Some conventions:
   12.40+
   12.41+  (1) We expose just opaque struct pointers and functions to clients.
   12.42+  This allows us to change internal representations without having to
   12.43+  recompile clients.
   12.44+
   12.45+  (2) For simplicity, there is no equivalent to the Slice type.  Instead,
   12.46+  the caller has to pass the pointer and length as separate
   12.47+  arguments.
   12.48+
   12.49+  (3) Errors are represented by a null-terminated c string.  NULL
   12.50+  means no error.  All operations that can raise an error are passed
   12.51+  a "char** errptr" as the last argument.  One of the following must
   12.52+  be true on entry:
   12.53+     *errptr == NULL
   12.54+     *errptr points to a malloc()ed null-terminated error message
   12.55+  On success, a leveldb routine leaves *errptr unchanged.
   12.56+  On failure, leveldb frees the old value of *errptr and
   12.57+  set *errptr to a malloc()ed error message.
   12.58+
   12.59+  (4) Bools have the type unsigned char (0 == false; rest == true)
   12.60+
   12.61+  (5) All of the pointer arguments must be non-NULL.|#
   12.62+
   12.63+;;; Code:
   12.64+(defpackage :rocksdb
   12.65+  (:use :cl :sb-alien :macs.alien)
   12.66+  (:export
   12.67+   :load-rocksdb
   12.68+   ;; ERR
   12.69+   :rocksdb-errptr
   12.70+   ;; DB
   12.71+   :rocksdb
   12.72+   :rocksdb-open
   12.73+   :rocksdb-close
   12.74+   :rocksdb-destroy-db
   12.75+   :rocksdb-put
   12.76+   :rocksdb-get
   12.77+   :rocksdb-delete
   12.78+   :rocksdb-cancel-all-background-work
   12.79+   ;; CACHE
   12.80+   :rocksdb-cache
   12.81+   :rocksdb-cache-create-lru
   12.82+   ;; BLOCK-BASED OPTIONS
   12.83+   :rocksdb-block-based-table-options
   12.84+   :rocksdb-block-based-options-create
   12.85+   :rocksdb-block-based-options-destroy
   12.86+   :rocksdb-block-based-options-set-block-cache
   12.87+   :set-block-based-options-cache-index-and-filter-blocks
   12.88+   ;; OPTIONS
   12.89+   :rocksdb-options
   12.90+   :rocksdb-options-create
   12.91+   :rocksdb-options-destroy
   12.92+   :rocksdb-options-increase-parallelism
   12.93+   :rocksdb-options-optimize-level-style-compaction
   12.94+   :rocksdb-options-set-create-if-missing
   12.95+   :rocksdb-options-set-block-based-table-factory
   12.96+   ;; read
   12.97+   :rocksdb-readoptions
   12.98+   :rocksdb-readoptions-create
   12.99+   :rocksdb-readoptions-destroy
  12.100+   ;; write
  12.101+   :rocksdb-writeoptions
  12.102+   :rocksdb-writeoptions-create
  12.103+   :rocksdb-writeoptions-destroy
  12.104+   ;; compact
  12.105+   :rocksdb-compactoptions
  12.106+   ;; ITERATOR
  12.107+   :rocksdb-iterator
  12.108+   :rocksdb-iter-seek-to-first
  12.109+   :rocksdb-iter-next
  12.110+   :rocksdb-iter-prev
  12.111+   :rocksdb-iter-valid
  12.112+   :rocksdb-create-iterator
  12.113+   :rocksdb-iter-key
  12.114+   :rocksdb-iter-value
  12.115+   :rocksdb-iter-destroy))
  12.116+
  12.117+(in-package :rocksdb)
  12.118+
  12.119+(defun load-rocksdb () 
  12.120+  (unless (member :rocksdb *features*)
  12.121+    (sb-alien:load-shared-object "librocksdb.so" :dont-save t)
  12.122+    (push :rocksdb *features*)))
  12.123+
  12.124+(load-rocksdb)  
  12.125+
  12.126+;;; Alien Types
  12.127+(define-alien-type rocksdb (struct rocksdb-t))
  12.128+(define-alien-type rocksdb-options (struct rocksdb-options-t))
  12.129+(define-alien-type rocksdb-readoptions (struct rocksdb-readoptions-t))
  12.130+(define-alien-type rocksdb-writeoptions (struct rocksdb-writeoptions-t))
  12.131+(define-alien-type rocksdb-compactoptions (struct rocksdb-compactoptions-t))
  12.132+(define-alien-type rocksdb-block-based-table-options (struct rocksdb-block-based-table-options-t))
  12.133+(define-alien-type rocksdb-iterator (struct rocksdb-iterator-t))
  12.134+(define-alien-type rocksdb-cache (struct rocksdb-cache-t))
  12.135+(define-alien-type rocksdb-column-family-handle (struct rocksdb-column-family-handler-t))
  12.136+(define-alien-type rocksdb-sstfilewriter (struct rocksdb-sstfilewriter-t))
  12.137+
  12.138+;; either (* void) or c-string (* (* char))
  12.139+(define-alien-type rocksdb-errptr (* (* t)))
  12.140+
  12.141+;;; Cache
  12.142+(define-alien-routine rocksdb-cache-create-lru (* rocksdb) (capacity unsigned-int))
  12.143+
  12.144+;;; Options
  12.145+
  12.146+;;;; block-based
  12.147+(define-alien-routine rocksdb-block-based-options-create (* rocksdb-block-based-table-options))
  12.148+(define-alien-routine rocksdb-block-based-options-destroy void 
  12.149+  (options (* rocksdb-block-based-table-options)))
  12.150+(define-alien-routine rocksdb-block-based-options-set-block-cache void 
  12.151+  (options (* rocksdb-block-based-table-options)) 
  12.152+  (block-cache (* rocksdb-cache)))
  12.153+(define-alien-routine rocksdb-block-based-options-set-cache-index-and-filter-blocks void
  12.154+  (options (* rocksdb-block-based-table-options))
  12.155+  (val c-string))
  12.156+
  12.157+;;;; db
  12.158+(define-alien-routine rocksdb-options-create rocksdb-options)
  12.159+(define-alien-routine rocksdb-options-destroy void 
  12.160+  (options rocksdb-options))
  12.161+(define-alien-routine rocksdb-options-increase-parallelism void 
  12.162+  (opt rocksdb-options) (total-threads int))
  12.163+(define-alien-routine rocksdb-options-optimize-level-style-compaction void 
  12.164+  (opt rocksdb-options) 
  12.165+  (memtable_memory_budget (unsigned 4)))
  12.166+(define-alien-routine rocksdb-options-set-create-if-missing void 
  12.167+  (opt rocksdb-options) 
  12.168+  (val boolean))
  12.169+(define-alien-routine rocksdb-options-set-block-based-table-factory void
  12.170+  (opt rocksdb-options)
  12.171+  (table-options rocksdb-block-based-table-options))
  12.172+;;;; write
  12.173+(define-alien-routine rocksdb-writeoptions-create rocksdb-writeoptions)
  12.174+(define-alien-routine rocksdb-writeoptions-destroy void
  12.175+  (opt rocksdb-writeoptions))
  12.176+;;;; read
  12.177+(define-alien-routine rocksdb-readoptions-create rocksdb-readoptions)
  12.178+(define-alien-routine rocksdb-readoptions-destroy void
  12.179+  (opt rocksdb-readoptions))
  12.180+
  12.181+;;; DB
  12.182+(define-alien-routine rocksdb-open rocksdb 
  12.183+  (opt rocksdb-options) 
  12.184+  (name c-string) 
  12.185+  (errptr rocksdb-errptr))
  12.186+(define-alien-routine rocksdb-close void 
  12.187+  (db rocksdb))
  12.188+(define-alien-routine rocksdb-cancel-all-background-work void 
  12.189+  (db rocksdb) 
  12.190+  (wait boolean))
  12.191+
  12.192+(define-alien-routine rocksdb-put void 
  12.193+  (db rocksdb) 
  12.194+  (options rocksdb-writeoptions) 
  12.195+  (key (* char))
  12.196+  (keylen size-t) 
  12.197+  (val (* char))
  12.198+  (vallen size-t) 
  12.199+  (errptr rocksdb-errptr))
  12.200+
  12.201+(define-alien-routine rocksdb-get (* char)
  12.202+  (db rocksdb) 
  12.203+  (options rocksdb-readoptions) 
  12.204+  (key (* char))
  12.205+  (keylen size-t) 
  12.206+  (vallen (* size-t))
  12.207+  (errptr rocksdb-errptr))
  12.208+
  12.209+(define-alien-routine rocksdb-delete void
  12.210+  (db rocksdb)
  12.211+  (options rocksdb-writeoptions)
  12.212+  (key (* char))
  12.213+  (keylen size-t)
  12.214+  (errptr rocksdb-errptr))
  12.215+
  12.216+;;; Iterators
  12.217+(define-alien-routine rocksdb-create-iterator rocksdb-iterator 
  12.218+  (db rocksdb) 
  12.219+  (opt rocksdb-readoptions))
  12.220+(define-alien-routine rocksdb-iter-destroy void 
  12.221+  (iter rocksdb-iterator))
  12.222+(define-alien-routine rocksdb-iter-seek-to-first void 
  12.223+  (iter rocksdb-iterator))
  12.224+(define-alien-routine rocksdb-iter-valid boolean 
  12.225+  (iter rocksdb-iterator))
  12.226+(define-alien-routine rocksdb-iter-next void 
  12.227+  (iter rocksdb-iterator))
  12.228+(define-alien-routine rocksdb-iter-prev void 
  12.229+  (iter rocksdb-iterator))
  12.230+(define-alien-routine rocksdb-iter-key (* char)
  12.231+  (iter rocksdb-iterator) 
  12.232+  (klen-ptr (* size-t)))
  12.233+(define-alien-routine rocksdb-iter-value (* char) 
  12.234+  (iter rocksdb-iterator) (vlen-ptr (* size-t)))
  12.235+(define-alien-routine rocksdb-destroy-db void
  12.236+  (options rocksdb-options)
  12.237+  (name c-string) 
  12.238+  (errptr rocksdb-errptr))
    13.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    13.2+++ b/lisp/ffi/rocksdb/tests.lisp	Sun Oct 15 01:50:27 2023 -0400
    13.3@@ -0,0 +1,71 @@
    13.4+;;; rocksdb/tests.lisp --- RocksDB tests
    13.5+
    13.6+;;; Code:
    13.7+(defpackage :rocksdb.tests
    13.8+  (:use :cl :rt :rocksdb :rdb :sb-alien :alien :sb-ext))
    13.9+
   13.10+(in-package :rocksdb.tests)
   13.11+
   13.12+(defun rocksdb-test-dir ()
   13.13+  (format nil "/tmp/~A/" (gensym "rocksdb-tests-")))
   13.14+
   13.15+(defun test-opts () (rdb::default-rocksdb-options%))
   13.16+
   13.17+;; not thread safe (gensym-counter)
   13.18+(defun genkey (&optional prefix) (string-to-octets (symbol-name (gensym (or prefix "key")))))
   13.19+(defun genval (&optional prefix) (string-to-octets (symbol-name (gensym (or prefix "val")))))
   13.20+
   13.21+(defsuite :rocksdb)
   13.22+
   13.23+(in-suite :rocksdb)
   13.24+
   13.25+(deftest set-opts ()
   13.26+  (let ((opts (rocksdb-options-create))
   13.27+        (wopts (rocksdb-writeoptions-create))
   13.28+        (ropts (rocksdb-readoptions-create))
   13.29+        (bopts (rocksdb-block-based-options-create)))
   13.30+    (rocksdb-options-set-create-if-missing opts t)
   13.31+    (rocksdb-options-destroy opts)
   13.32+    (rocksdb-writeoptions-destroy wopts)
   13.33+    (rocksdb-readoptions-destroy ropts)
   13.34+    (rocksdb-block-based-options-destroy bopts)))
   13.35+
   13.36+(deftest db ()
   13.37+  (let* ((opts (test-opts))
   13.38+         (path (rocksdb-test-dir))
   13.39+         (db (rocksdb-open opts path nil)))
   13.40+    (let* ((key (genkey))
   13.41+           (val (genval))
   13.42+	   (klen (length key))
   13.43+	   (vlen (length val))
   13.44+           (wopts (rocksdb-writeoptions-create))
   13.45+           (ropts (rocksdb-readoptions-create)))
   13.46+      (with-alien ((k (* char) (make-alien char klen))
   13.47+                   (v (* char) (make-alien char vlen))
   13.48+                   (errptr rocksdb-errptr nil))
   13.49+	(loop for x across key
   13.50+	      for i from 0 below klen
   13.51+	      do (setf (deref k i) x))
   13.52+	(loop for y across val
   13.53+	      for i from 0 below vlen
   13.54+	      do (setf (deref v i) y))
   13.55+        (rocksdb-put db 
   13.56+                     wopts
   13.57+                     k
   13.58+                     klen
   13.59+                     v
   13.60+                     vlen
   13.61+                     errptr)
   13.62+	(is (null-alien errptr))
   13.63+        (rocksdb-get db ropts k klen (make-alien size-t vlen) errptr)
   13.64+	(is (null-alien errptr))
   13.65+	(let ((rval (make-array vlen :element-type 'unsigned-byte)))
   13.66+	  (loop for i from 0 below vlen do (let ((x (deref v i))) (setf (aref rval i) x)))
   13.67+	  (is (string= (octets-to-string val) (concatenate 'string (map 'vector #'code-char rval)))))
   13.68+        (rocksdb-delete db wopts k klen errptr)
   13.69+	(is (null-alien errptr))
   13.70+        (rocksdb-writeoptions-destroy wopts)
   13.71+        (rocksdb-readoptions-destroy ropts)))
   13.72+    (rocksdb-close db)
   13.73+    (rocksdb-options-destroy opts)
   13.74+    (sb-ext:delete-directory path :recursive t)))
    14.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    14.2+++ b/lisp/ffi/uring.asd	Sun Oct 15 01:50:27 2023 -0400
    14.3@@ -0,0 +1,7 @@
    14.4+;;; uring.asd-*- mode: lisp; -*-
    14.5+(in-package :sys.uring)
    14.6+(defsystem :uring
    14.7+  :depends-on (sb-grovel)
    14.8+  :components ((grovel-constants-file "uring/cs" :package :uring)
    14.9+	       (grovel-constants-file "uring/cs.unix" :package :uring)
   14.10+	       (:file "uring/uring")))
    15.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
    15.2+++ b/lisp/ffi/uring.lisp	Sun Oct 15 01:50:27 2023 -0400
    15.3@@ -0,0 +1,1 @@
    15.4+(in-package :uring)
    16.1--- a/lisp/rdb/rdb.lisp	Thu Oct 12 22:51:00 2023 -0400
    16.2+++ b/lisp/rdb/rdb.lisp	Sun Oct 15 01:50:27 2023 -0400
    16.3@@ -7,14 +7,51 @@
    16.4 ;; Commentary:
    16.5 
    16.6 ;; Code:
    16.7-(defpackage :rdb
    16.8-  (:use :cl :rocksdb :sb-alien :alien)
    16.9+(pkg:defpkg :rdb
   16.10+  (:use :cl :alien :fu :rocksdb)
   16.11   (:import-from :sb-ext :string-to-octets :octets-to-string)
   16.12-  (:export :with-open-db :with-iter
   16.13-           :create-iter
   16.14-           :iter-key :iter-key-str
   16.15-           :iter-val :iter-val-str
   16.16-   :unable-to-open-db :unable-to-put-key-value-to-db :unable-to-get-value-to-db))
   16.17+  (:reexport :rocksdb)
   16.18+  (:export 
   16.19+   ;; opts
   16.20+   :make-rdb-opts
   16.21+   :rdb-opts
   16.22+   :default-rdb-opts
   16.23+   ;; db
   16.24+   :open-db
   16.25+   :with-open-db 
   16.26+   ;; iter
   16.27+   :create-iter :with-iter
   16.28+   :iter-key :iter-key-str
   16.29+   :iter-val :iter-val-str
   16.30+   ;; err
   16.31+   :unable-to-open-db 
   16.32+   :unable-to-put-key-value-to-db 
   16.33+   :unable-to-get-value-to-db))
   16.34+
   16.35+(in-package :rdb)
   16.36+
   16.37+(defstruct rdb-opts
   16.38+  (create-if-missing nil :type boolean)
   16.39+  (total-threads 1 :type integer) ;; numcpus is default
   16.40+  (max-open-files 10000 :type integer)
   16.41+  (use-fsync nil :type boolean)
   16.42+  (disable-auto-compations nil :type boolean))
   16.43+
   16.44+;; unsafe
   16.45+(defun bind-rocksdb-opts% (opts)
   16.46+  (let ((o (rocksdb-options-create)))
   16.47+    (with-slots (create-if-missing total-threads) opts
   16.48+      (rocksdb-options-set-create-if-missing o create-if-missing)
   16.49+      (rocksdb-options-increase-parallelism o total-threads))
   16.50+    o))
   16.51+
   16.52+(defun default-rdb-opts () 
   16.53+  (make-rdb-opts
   16.54+   :create-if-missing t 
   16.55+   :total-threads 4))
   16.56+
   16.57+(defun default-rocksdb-options% ()
   16.58+  (bind-rocksdb-opts% (default-rdb-opts)))
   16.59 
   16.60 (defmacro with-open-db ((db-var db-path &optional opt) &body body)
   16.61   `(let ((,db-var (open-db ,db-path ,opt)))
   16.62@@ -26,7 +63,6 @@
   16.63      (unwind-protect (progn ,@body)
   16.64        (rocksdb-iter-destroy ,iter-var))))
   16.65 
   16.66-
   16.67 ;;; Conditions
   16.68 (define-condition unable-to-open-db (error)
   16.69   ((db-path :initarg :db-path
   16.70@@ -57,121 +93,112 @@
   16.71                   :reader error-message)))
   16.72 
   16.73 ;;; API
   16.74-(defun open-db (db-path &optional opt)
   16.75-  (unless opt
   16.76-    (setq opt (rocksdb-options-create)))
   16.77-  (with-alien ((e rocksdb-errptr nil))
   16.78-    (let* ((db-path (if (pathnamep db-path)
   16.79-                        (namestring db-path)
   16.80-                        db-path))
   16.81-           (db (rocksdb-open opt db-path e)))
   16.82-      (if (null-alien e)
   16.83-          db
   16.84-          (error 'unable-to-open-db
   16.85-                 :db-path db-path
   16.86-                 :error-message e)))))
   16.87+(defun open-db (db-path &optional opts)
   16.88+  (let ((opts (if opts (bind-rocksdb-opts% opts) (default-rocksdb-options%))))
   16.89+    (with-alien ((e rocksdb-errptr))
   16.90+      (let* ((db-path (if (pathnamep db-path)
   16.91+                          (namestring db-path)
   16.92+                          db-path))
   16.93+             (db (rocksdb-open opts db-path e)))
   16.94+	(if (null-alien e)
   16.95+            db
   16.96+            (error 'unable-to-open-db
   16.97+                   :db-path db-path
   16.98+                   :error-message e))))))
   16.99 
  16.100-;; (defmacro clone-octets-to-foreign (lisp-array foreign-array)
  16.101-;;   (let ((i (gensym)))
  16.102-;;     `(loop for ,i from 0 below (length ,lisp-array)
  16.103-;;            do (setf (deref ,foreign-array ,i)
  16.104-;;                     (aref ,lisp-array ,i)))))
  16.105-
  16.106-;; (defmacro clone-octets-from-foreign (foreign-array lisp-array len)
  16.107-;;   (let ((i (gensym)))
  16.108-;;     `(loop for ,i from 0 below ,len
  16.109-;;            do (setf (aref ,lisp-array ,i)
  16.110-;;                     (deref ,foreign-array ,i)))))
  16.111+(defun put-kv (db key val &optional opts)
  16.112+  (let ((opts (or opts (rocksdb-writeoptions-create)))
  16.113+	(klen (length key))
  16.114+	(vlen (length val)))
  16.115+    (with-alien ((errptr rocksdb-errptr nil)
  16.116+		 (k (* char) (make-alien char klen))
  16.117+		 (v (* char) (make-alien char vlen)))
  16.118+      (loop for x across key
  16.119+	    for i from 0 below klen
  16.120+	    do (setf (deref k i) x))
  16.121+      (loop for y across val
  16.122+	    for i from 0 below vlen
  16.123+	    do (setf (deref v i) y))
  16.124+      (rocksdb-put db
  16.125+		   opts
  16.126+		   k
  16.127+		   klen
  16.128+		   v
  16.129+		   vlen
  16.130+		   errptr)
  16.131+      (unless (null-alien errptr)
  16.132+        (error 'unable-to-put-key-value-to-db
  16.133+                :db db
  16.134+                :key key
  16.135+                :val val
  16.136+                :error-message (alien-sap errptr))))))
  16.137 
  16.138-;; (defun put-kv (db key val &optional opt)
  16.139-;;   (unless opt
  16.140-;;     (setq opt (create-writeoptions)))
  16.141-;;   (with-alien ((errptr (* t))
  16.142-;;                          (key* unsigned-char (length key))
  16.143-;;                          (val* unsigned-char (length val)))
  16.144-;;     (clone-octets-to-foreign key key*)
  16.145-;;     (clone-octets-to-foreign val val*)
  16.146-;;     (put* db
  16.147-;;           opt
  16.148-;;           key*
  16.149-;;           (length key)
  16.150-;;           val*
  16.151-;;           (length val)
  16.152-;;           errptr)
  16.153-;;     (let ((err errptr))
  16.154-;;       (unless (null-alien err)
  16.155-;;         (error 'unable-to-put-key-value-to-db
  16.156-;;                :db db
  16.157-;;                :key key
  16.158-;;                :val val
  16.159-;;                :error-message (sap-alien err c-string))))))
  16.160+(defun put-kv-str (db key val &optional opt)
  16.161+  (let ((key-octets (string-to-octets key))
  16.162+        (val-octets (string-to-octets val)))
  16.163+    (put-kv db key-octets val-octets opt)))
  16.164 
  16.165-;; (defun put-kv-str (db key val &optional opt)
  16.166-;;   (let ((key-octets (string-to-octets key))
  16.167-;;         (val-octets (string-to-octets val)))
  16.168-;;     (put-kv db key-octets val-octets opt)))
  16.169+(defun get-kv (db key &optional opt)
  16.170+  (let ((opt (or opt (rocksdb-readoptions-create)))
  16.171+	(key (string-to-octets key))
  16.172+	(klen (length key)))
  16.173+    (with-alien ((vlen (* size-t))
  16.174+		 (errptr rocksdb-errptr nil)
  16.175+		 (k (* char) (make-alien char klen)))
  16.176+      (loop for x across key
  16.177+	    for i from 0 below klen
  16.178+	    do (setf (deref k i) x))
  16.179 
  16.180-;; (defun get-kv (db key &optional opt)
  16.181-;;   (unless opt
  16.182-;;     (setq opt (create-readoptions)))
  16.183+      (let* ((val (rocksdb-get db
  16.184+			      opt
  16.185+			      k
  16.186+			      klen
  16.187+			      vlen
  16.188+			      errptr))
  16.189+	     (vlen (deref vlen)))
  16.190+	(unless (null-alien errptr)
  16.191+          (error 'unable-to-get-value-to-db
  16.192+		 :db db
  16.193+		 :key key
  16.194+		 :error-message (alien-sap errptr)))
  16.195+	;; helps if we know the vlen beforehand, would need a custom
  16.196+	;; C-side function probably.
  16.197+	(let ((v (make-array vlen :element-type 'unsigned-byte)))
  16.198+	  (loop for i from 0 below vlen
  16.199+		with x = (deref val i) 
  16.200+		do (setf (aref v i) x))
  16.201+	  (map 'vector #'code-char v))))))
  16.202 
  16.203-;;   (with-alien ((val-len-ptr unsigned-int)
  16.204-;;                (errptr system-area-pointer)
  16.205-;;                (key* unsigned-char (length key)))
  16.206-;;     (clone-octets-to-foreign key key*)
  16.207-;;     ;; (setf (mem-ref errptr :pointer) (null-pointer))
  16.208-;;     (let ((val (get* db
  16.209-;;                      opt
  16.210-;;                      key*
  16.211-;;                      (length key)
  16.212-;;                      val-len-ptr
  16.213-;;                      errptr)))
  16.214-;;       (let ((err errptr))
  16.215-;;         (unless (null-alien err)
  16.216-;;           (error 'unable-to-get-value-to-db
  16.217-;;                  :db db
  16.218-;;                  :key key
  16.219-;;                  :error-message (sap-alien err c-string)))
  16.220-        
  16.221-;;         (unless (null-alien val)
  16.222-;;           (let* ((val-len val-len-ptr)
  16.223-;;                  (val* (make-array val-len
  16.224-;;                                       :element-type '(unsigned-byte 8))))
  16.225-;;             (clone-octets-from-foreign val val* val-len)
  16.226-;;             val*))))))
  16.227-
  16.228-;; (defun get-kv-str (db key &optional opt)
  16.229-;;   (let ((key-octets (string-to-octets key)))
  16.230-;;     (let ((#1=val-octets (get-kv db key-octets opt)))
  16.231-;;       (when #1#
  16.232-;;         (octets-to-string #1#)))))
  16.233+ (defun get-kv-str (db key &optional opt)
  16.234+   (let ((k (string-to-octets key)))
  16.235+     (let ((v (get-kv db k opt)))
  16.236+       (when v (print v)))))
  16.237 
  16.238 (defun create-iter (db &optional opt)
  16.239   (unless opt
  16.240     (setq opt (rocksdb-readoptions-create)))
  16.241   (rocksdb-create-iterator db opt))
  16.242 
  16.243-;; (defun iter-key (iter)
  16.244-;;   (with-alien ((klen-ptr unsigned-int 0))
  16.245-;;     (let* ((key-ptr (rocksdb-iter-key iter klen-ptr))
  16.246-;;            (klen klen-ptr)
  16.247-;;            (key (make-array klen :element-type '(unsigned-byte 8))))
  16.248-;;       (clone-octets-from-foreign key-ptr key klen)
  16.249-;;       key)))
  16.250+(defun iter-key (iter)
  16.251+  (with-alien ((klen-ptr (* unsigned-int)))
  16.252+    (let* ((key-ptr (rocksdb-iter-key iter klen-ptr))
  16.253+           (klen (deref klen-ptr))
  16.254+           (k (make-array klen :element-type '(unsigned-byte 8))))
  16.255+      (loop for i from 0 below klen with x = (deref key-ptr i) do (setf (aref k i) x))
  16.256+      k)))
  16.257 
  16.258-;; (defun iter-key-str (iter)
  16.259-;;   (when-let ((key-octets (iter-key iter)))
  16.260-;;     (octets-to-string key-octets)))
  16.261+(defun iter-key-str (iter)
  16.262+  (when-let ((k (iter-key iter)))
  16.263+    (octets-to-string k)))
  16.264 
  16.265-;; (defun iter-val (iter)
  16.266-;;   (with-alien ((len-ptr unsigned-int 0))
  16.267-;;     (let* ((value-ptr (rocksdb-iter-value iter len-ptr))
  16.268-;;            (vlen len-ptr)
  16.269-;;            (value* (make-array vlen :element-type '(unsigned-byte 8))))
  16.270-;;       (clone-octets-from-foreign value-ptr value* vlen)
  16.271-;;       value*)))
  16.272+ (defun iter-val (iter)
  16.273+   (with-alien ((vlen-ptr (* unsigned-int)))
  16.274+     (let* ((val-ptr (rocksdb-iter-value iter vlen-ptr))
  16.275+            (vlen (deref vlen-ptr))
  16.276+            (v (make-array vlen :element-type '(unsigned-byte 8))))
  16.277+       (loop for i from 0 below vlen with x = (deref val-ptr i) do (setf (aref v i) x))
  16.278+       v)))
  16.279 
  16.280-;; (defun iter-val-str (iter)
  16.281-;;   (let ((#1=val-octets (iter-value iter)))
  16.282-;;     (when #1#
  16.283-;;       (octets-to-string #1#))))
  16.284+ (defun iter-val-str (iter)
  16.285+   (when-let ((v (iter-val iter)))
  16.286+     (octets-to-string v)))
    17.1--- a/lisp/rocksdb.asd	Thu Oct 12 22:51:00 2023 -0400
    17.2+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    17.3@@ -1,28 +0,0 @@
    17.4-;;; rocksdb.asd --- ROCKSDB SYSTEMS
    17.5-
    17.6-;; rocksdb for lisp.
    17.7-
    17.8-;;; Commentary:
    17.9-
   17.10-;; based on Vee's cl-rocksdb: https://github.com/veer66/cl-rocksdb/tree/main
   17.11-
   17.12-;;; Code:
   17.13-(defsystem "rocksdb"
   17.14-  :version "0.1.0"
   17.15-  :license (:file "LICENSE")
   17.16-  :maintainer "ellis <ellis@rwest.io>"
   17.17-  :homepage "https://nas-t.net"
   17.18-  :bug-tracker "https://lab.rwest.io/comp/startup/nas-t/issues"
   17.19-  :depends-on (:macs)
   17.20-  :in-order-to ((test-op (test-op "rocksdb/tests")))
   17.21-  :components ((:file "rocksdb/rocksdb")))
   17.22-
   17.23-(defsystem "rocksdb/tests"
   17.24-  :version "0.1.0"
   17.25-  :license (:file "LICENSE")
   17.26-  :maintainer "ellis <ellis@rwest.io>"
   17.27-  :homepage "https://nas-t.net"
   17.28-  :bug-tracker "https://lab.rwest.io/comp/nas-t/issues"
   17.29-  :depends-on (:rocksdb :rt)
   17.30-  :components ((:file "rocksdb/tests"))
   17.31-  :perform (test-op (op c) (uiop:symbol-call '#:rt '#:do-tests :rocksdb)))
    18.1--- a/lisp/rocksdb/rocksdb.lisp	Thu Oct 12 22:51:00 2023 -0400
    18.2+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    18.3@@ -1,181 +0,0 @@
    18.4-;;; rocksdb.lisp --- low-level bindings to the RocksDB C API
    18.5-
    18.6-;; for the high-level interface, see rdb.lisp.
    18.7-
    18.8-;;; Commentary:
    18.9-
   18.10-;; if ur on archlinux and installed rocksdb via AUR you may receive an error from
   18.11-;; jemalloc: cannot allocate memory in static TLS block:
   18.12-
   18.13-;; https://github.com/veer66/cl-rocksdb/issues/1
   18.14-
   18.15-;; for best results, you should compile rocksdb from source - use j0ni's snippet as a
   18.16-;; starting point.
   18.17-
   18.18-;; make shared_lib DISABLE_JEMALLOC=1 && 
   18.19-;; sudo cp librocksdb.so.* /usr/local/lib/ && 
   18.20-;; sudo cp -rf include/* /usr/local/include/
   18.21-
   18.22-;; https://github.com/facebook/rocksdb/blob/main/Makefile
   18.23-
   18.24-;; check /usr/local/include/rocksdb/c.h for the C API header, the source is under
   18.25-;; db/c.cc
   18.26-
   18.27-;;; Code:
   18.28-(defpackage :rocksdb
   18.29-  (:use :cl :sb-alien :macs.alien)
   18.30-  (:export
   18.31-   :load-rocksdb
   18.32-   :rocksdb-open
   18.33-   :rocksdb-close
   18.34-   :rocksdb-destroy-db
   18.35-   :rocksdb-put
   18.36-   :rocksdb-get
   18.37-   :rocksdb-delete
   18.38-   :rocksdb-cancel-all-background-work
   18.39-   :rocksdb-errptr
   18.40-   ;; LRU CACHE
   18.41-   :rocksdb-cache-create-lru
   18.42-   ;; BLOCK-BASED OPTIONS
   18.43-   :rocksdb-block-based-options-create
   18.44-   :rocksdb-block-based-options-destroy
   18.45-   :rocksdb-block-based-options-set-block-cache
   18.46-   :set-block-based-options-cache-index-and-filter-blocks
   18.47-   ;; OPTIONS
   18.48-   :rocksdb-options-create
   18.49-   :rocksdb-options-destroy
   18.50-   :rocksdb-options-increase-parallelism
   18.51-   :rocksdb-options-optimize-level-style-compaction
   18.52-   :rocksdb-options-set-create-if-missing
   18.53-   :rocksdb-options-set-block-based-table-factory
   18.54-   :rocksdb-writeoptions-create
   18.55-   :rocksdb-writeoptions-destroy
   18.56-   :rocksdb-readoptions-create
   18.57-   :rocksdb-readoptions-destroy
   18.58-   ;; ITERATOR
   18.59-   :rocksdb-iter-seek-to-first
   18.60-   :rocksdb-iter-next
   18.61-   :rocksdb-iter-prev
   18.62-   :rocksdb-iter-valid
   18.63-   :rocksdb-create-iterator
   18.64-   :rocksdb-iter-key
   18.65-   :rocksdb-iter-value
   18.66-   :rocksdb-iter-destroy))
   18.67-
   18.68-(in-package :rocksdb)
   18.69-
   18.70-(defun load-rocksdb () 
   18.71-  (unless (member :rocksdb *features*)
   18.72-    (sb-alien:load-shared-object "librocksdb.so" :dont-save t)
   18.73-    (push :rocksdb *features*)))
   18.74-
   18.75-(load-rocksdb)  
   18.76-
   18.77-;;; Opaque Types
   18.78-(define-alien-type rocksdb (* t))
   18.79-(define-alien-type rocksdb-options (* t))
   18.80-(define-alien-type rocksdb-readoptions (* t))
   18.81-(define-alien-type rocksdb-writeoptions (* t))
   18.82-(define-alien-type rocksdb-compactoptions (* t))
   18.83-(define-alien-type rocksdb-block-based-table-options (* t))
   18.84-(define-alien-type rocksdb-iterator (* t))
   18.85-(define-alien-type rocksdb-column-family-handle (* t))
   18.86-(define-alien-type rocksdb-sstfilewriter (* t))
   18.87-(define-alien-type rocksdb-errptr (* c-string))
   18.88-
   18.89-;;; LRU
   18.90-(define-alien-routine rocksdb-cache-create-lru (* rocksdb) (capacity unsigned-int))
   18.91-
   18.92-;;; Options
   18.93-;;;; block-based
   18.94-(define-alien-routine rocksdb-block-based-options-create (* t))
   18.95-(define-alien-routine rocksdb-block-based-options-destroy void 
   18.96-  (options (* t)))
   18.97-(define-alien-routine rocksdb-block-based-options-set-block-cache void 
   18.98-  (options (* t)) 
   18.99-  (block-cache (* t)))
  18.100-(define-alien-routine rocksdb-block-based-options-set-cache-index-and-filter-blocks void
  18.101-  (options (* t)) 
  18.102-  (val c-string))
  18.103-;;;; db
  18.104-(define-alien-routine rocksdb-options-create rocksdb-options)
  18.105-(define-alien-routine rocksdb-options-destroy void 
  18.106-  (options rocksdb-options))
  18.107-(define-alien-routine rocksdb-options-increase-parallelism void 
  18.108-  (opt rocksdb-options) (total-threads int))
  18.109-(define-alien-routine rocksdb-options-optimize-level-style-compaction void 
  18.110-  (opt rocksdb-options) 
  18.111-  (memtable_memory_budget (unsigned 4)))
  18.112-(define-alien-routine rocksdb-options-set-create-if-missing void 
  18.113-  (opt rocksdb-options) 
  18.114-  (val boolean))
  18.115-(define-alien-routine rocksdb-options-set-block-based-table-factory void
  18.116-  (opt rocksdb-options)
  18.117-  (table-options rocksdb-block-based-table-options))
  18.118-;;;; write
  18.119-(define-alien-routine rocksdb-writeoptions-create rocksdb-writeoptions)
  18.120-(define-alien-routine rocksdb-writeoptions-destroy void
  18.121-  (opt rocksdb-writeoptions))
  18.122-;;;; read
  18.123-(define-alien-routine rocksdb-readoptions-create rocksdb-readoptions)
  18.124-(define-alien-routine rocksdb-readoptions-destroy void
  18.125-  (opt rocksdb-readoptions))
  18.126-
  18.127-;;; DB
  18.128-(define-alien-routine rocksdb-open rocksdb 
  18.129-  (opt rocksdb-options) 
  18.130-  (name c-string) 
  18.131-  (errptr rocksdb-errptr))
  18.132-(define-alien-routine rocksdb-close void 
  18.133-  (db rocksdb))
  18.134-(define-alien-routine rocksdb-cancel-all-background-work void 
  18.135-  (db rocksdb) 
  18.136-  (wait boolean))
  18.137-
  18.138-(define-alien-routine rocksdb-put void 
  18.139-  (db rocksdb) 
  18.140-  (options rocksdb-writeoptions) 
  18.141-  (key c-string)
  18.142-  (keylen size-t) 
  18.143-  (val c-string)
  18.144-  (vallen size-t) 
  18.145-  (errptr rocksdb-errptr))
  18.146-
  18.147-(define-alien-routine rocksdb-get c-string
  18.148-  (db rocksdb) 
  18.149-  (options rocksdb-readoptions) 
  18.150-  (key c-string) 
  18.151-  (keylen size-t) 
  18.152-  (vallen (* size-t))
  18.153-  (errptr rocksdb-errptr))
  18.154-
  18.155-(define-alien-routine rocksdb-delete void
  18.156-  (db rocksdb)
  18.157-  (options rocksdb-writeoptions)
  18.158-  (key c-string)
  18.159-  (keylen size-t)
  18.160-  (errptr rocksdb-errptr))
  18.161-
  18.162-;;; Iterators
  18.163-(define-alien-routine rocksdb-create-iterator rocksdb-iterator 
  18.164-  (db rocksdb) 
  18.165-  (opt rocksdb-readoptions))
  18.166-(define-alien-routine rocksdb-iter-destroy void 
  18.167-  (iter rocksdb-iterator))
  18.168-(define-alien-routine rocksdb-iter-seek-to-first void 
  18.169-  (iter rocksdb-iterator))
  18.170-(define-alien-routine rocksdb-iter-valid boolean 
  18.171-  (iter rocksdb-iterator))
  18.172-(define-alien-routine rocksdb-iter-next void 
  18.173-  (iter rocksdb-iterator))
  18.174-(define-alien-routine rocksdb-iter-prev void 
  18.175-  (iter rocksdb-iterator))
  18.176-(define-alien-routine rocksdb-iter-key (* t) 
  18.177-  (iter rocksdb-iterator) 
  18.178-  (klen-ptr (* size-t)))
  18.179-(define-alien-routine rocksdb-iter-value (* t) 
  18.180-  (iter rocksdb-iterator) (vlen-ptr (* size-t)))
  18.181-(define-alien-routine rocksdb-destroy-db void
  18.182-  (options rocksdb-options)
  18.183-  (name c-string) 
  18.184-  (errptr rocksdb-errptr))
    19.1--- a/lisp/rocksdb/tests.lisp	Thu Oct 12 22:51:00 2023 -0400
    19.2+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    19.3@@ -1,60 +0,0 @@
    19.4-;;; rocksdb/tests.lisp --- RocksDB tests
    19.5-
    19.6-;;; Code:
    19.7-(defpackage :rocksdb.tests
    19.8-  (:use :cl :rt :rocksdb :rdb :sb-alien :alien :sb-ext)
    19.9-  (:export :rocksdb-test-dir))
   19.10-
   19.11-(in-package :rocksdb.tests)
   19.12-
   19.13-(defun rocksdb-test-dir ()
   19.14-  (format nil "/tmp/~A/" (gensym "rocksdb-tests-")))
   19.15-
   19.16-(defun test-opts () 
   19.17-  (let ((opts (rocksdb-options-create)))
   19.18-    (rocksdb-options-set-create-if-missing opts t)
   19.19-    opts))
   19.20-
   19.21-(defsuite :rocksdb)
   19.22-
   19.23-(in-suite :rocksdb)
   19.24-
   19.25-(deftest set-opts ()
   19.26-  (let ((opts (rocksdb-options-create))
   19.27-        (wopts (rocksdb-writeoptions-create))
   19.28-        (ropts (rocksdb-readoptions-create))
   19.29-        (bopts (rocksdb-block-based-options-create)))
   19.30-    (rocksdb-options-set-create-if-missing opts t)
   19.31-    (rocksdb-options-destroy opts)
   19.32-    (rocksdb-writeoptions-destroy wopts)
   19.33-    (rocksdb-readoptions-destroy ropts)
   19.34-    (rocksdb-block-based-options-destroy bopts)))
   19.35-
   19.36-(deftest db ()
   19.37-  (let* ((opts (test-opts))
   19.38-         (path (rocksdb-test-dir))
   19.39-         (db (rocksdb-open opts path nil)))
   19.40-    (let ((k "key")
   19.41-          (v "val")
   19.42-          (wopts (rocksdb-writeoptions-create))
   19.43-          (ropts (rocksdb-readoptions-create)))
   19.44-      (with-alien ((key (* char) (make-alien-string k))
   19.45-                   (val (* char) (make-alien-string v :external-format :ascii :null-terminate t))
   19.46-                   (errptr rocksdb-errptr nil)
   19.47-                   (vlen (* size-t) (make-alien size-t 1))
   19.48-                   (ar (array char)))
   19.49-        (rocksdb-put db 
   19.50-                     wopts
   19.51-                     key
   19.52-                     (length k) 
   19.53-                     val
   19.54-                     (length v)
   19.55-                     errptr)
   19.56-        (is (string= v (rocksdb-get db ropts key (length k) vlen errptr)))
   19.57-
   19.58-        (rocksdb-delete db wopts key (length k) errptr)
   19.59-        (rocksdb-writeoptions-destroy wopts)
   19.60-        (rocksdb-readoptions-destroy ropts)))
   19.61-    (rocksdb-close db)
   19.62-    (rocksdb-options-destroy opts)
   19.63-    (sb-ext:delete-directory path :recursive t)))
    20.1--- a/readme.org	Thu Oct 12 22:51:00 2023 -0400
    20.2+++ b/readme.org	Sun Oct 15 01:50:27 2023 -0400
    20.3@@ -3,6 +3,71 @@
    20.4 ** btrfs
    20.5 ** btrfsutil
    20.6 * lisp
    20.7-** btrfs
    20.8-** rocksdb
    20.9+#+begin_src lisp :results silent
   20.10+  (let ((asds '("lisp/rdb.asd" "lisp/sxp.asd" 
   20.11+		"lisp/organ/organ.asd" "lisp/macs/macs.asd" "lisp/skel/skel.asd"
   20.12+		"lisp/ffi/rocksdb.asd" "lisp/ffi/btrfs.asd")))
   20.13+    (mapc (lambda (x) (asdf:load-asd x)) asds))
   20.14+#+end_src
   20.15+** rdb
   20.16+*** tests
   20.17+#+begin_src lisp :package rdb.tests :results output replace :exports results
   20.18+  (asdf:load-system :rdb/tests)
   20.19+  (in-package :rdb.tests)
   20.20+  (load "lisp/rdb/tests.lisp")
   20.21+  (setq log:*log-level* :debug)
   20.22+  (rt:do-tests :rdb)
   20.23+#+end_src
   20.24+
   20.25+#+RESULTS:
   20.26+: in suite RDB with 0/0 tests:
   20.27+: No tests failed.
   20.28+
   20.29+** sxp
   20.30+** ffi
   20.31+*** btrfs
   20.32+**** tests
   20.33+#+begin_src lisp :package rocksdb.tests :results output replace :exports results
   20.34+  (asdf:load-system :btrfs/tests)
   20.35+  (in-package :btrfs.tests)
   20.36+  (load "lisp/ffi/btrfs/tests.lisp")
   20.37+  (setq log:*log-level* :debug)
   20.38+  (rt:do-tests :btrfs)
   20.39+#+end_src
   20.40 
   20.41+#+RESULTS:
   20.42+: in suite BTRFS with 0/0 tests:
   20.43+: No tests failed.
   20.44+
   20.45+*** rocksdb
   20.46+**** tests
   20.47+#+begin_src lisp :package rocksdb.tests :results output replace :exports results
   20.48+  (asdf:load-system :rocksdb/tests)
   20.49+  (in-package :rocksdb.tests)
   20.50+  (load "lisp/ffi/rocksdb/tests.lisp")
   20.51+  (setq log:*log-level* :debug)
   20.52+  (rt:do-tests :rocksdb)
   20.53+#+end_src
   20.54+
   20.55+#+RESULTS:
   20.56+#+begin_example
   20.57+in suite ROCKSDB with 2/2 tests:
   20.58+:DEBUG @ 7288.927  
   20.59+; running test: 
   20.60+; #<TEST DB :fn DB-test991 :args NIL :persist NIL {10032DD7C3}>
   20.61+:DEBUG @ 7288.98  
   20.62+; #<PASS (NULL-ALIEN ERRPTR)>
   20.63+:DEBUG @ 7288.98  
   20.64+; #<PASS (NULL-ALIEN ERRPTR)>
   20.65+:DEBUG @ 7288.98  
   20.66+; #<PASS (STRING= (OCTETS-TO-STRING VAL)
   20.67+                  (CONCATENATE 'STRING (MAP 'VECTOR #'CODE-CHAR RVAL)))>
   20.68+:DEBUG @ 7288.98  
   20.69+; #<PASS (NULL-ALIEN ERRPTR)>
   20.70+#<PASS DB-TEST991> 
   20.71+:DEBUG @ 7288.987  
   20.72+; running test: 
   20.73+; #<TEST SET-OPTS :fn SET-OPTS-test990 :args NIL :persist NIL {10031F2E43}>
   20.74+#<PASS SET-OPTS-TEST990> 
   20.75+No tests failed.
   20.76+#+end_example