changelog shortlog graph tags branches changeset file revisions annotate raw help

Mercurial > demo / examples/db/cl-simple-example.lisp

revision 35: f54f7cc7458b
child 36: 0f678bfd8699
     1.1--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2+++ b/examples/db/cl-simple-example.lisp	Tue Dec 19 16:46:55 2023 -0500
     1.3@@ -0,0 +1,63 @@
     1.4+;;; cl-simple-example.lisp --- Common Lisp port of rocksdb/example/c_simple_example.c
     1.5+
     1.6+;; ref: https://github.com/facebook/rocksdb/blob/main/examples/c_simple_example.c
     1.7+
     1.8+;;; Usage: 
     1.9+
    1.10+;; To compile and run from the shell:
    1.11+#|
    1.12+sbcl --eval '(ql:quickload :rdb)' \
    1.13+     --eval '(ql:quickload :cli)' \
    1.14+     --eval '(compile-file "cl-simple-example.lisp")' \
    1.15+     --eval '(load "cl-simple-example.fasl")' \
    1.16+     --eval "(sb-ext:save-lisp-and-die \"cl-simple-example\" :toplevel #'cl-simple-example::main :executable t)"
    1.17+
    1.18+time ./cl-simple-example
    1.19+|#
    1.20+
    1.21+;; Compare to C:
    1.22+#|
    1.23+# in rocksdb/examples
    1.24+gcc -lrocksdb c_simple_example.c -oc_simple_example
    1.25+
    1.26+time ./c_simple_example
    1.27+|#
    1.28+
    1.29+;;; Code:
    1.30+(defpackage :examples/rdb/cl-simple-example
    1.31+  (:nicknames :cl-simple-example)
    1.32+  (:use :cl :std :cli :rdb :sb-alien :rocksdb)
    1.33+  (:export :main))
    1.34+
    1.35+(rocksdb:load-rocksdb :save t)
    1.36+
    1.37+(in-package :cl-simple-example)
    1.38+
    1.39+(in-readtable :std)
    1.40+
    1.41+(defvar *num-cpus* (alien-funcall (extern-alien "sysconf" (function long integer)) sb-unix:sc-nprocessors-onln)
    1.42+  "CPU count.")
    1.43+
    1.44+(defparameter *db-path* "/tmp/rocksdb-cl-simple-example")
    1.45+
    1.46+(defparameter *db-backup-path* "/tmp/rocksdb-cl-simple-example-backup")
    1.47+
    1.48+(defmain ()
    1.49+  ;; open Backup Engine that we will use for backing up our database
    1.50+  (let ((options 
    1.51+          (make-rocksdb-options 
    1.52+                  (lambda (opt)
    1.53+                    (rocksdb-options-increase-parallelism opt *num-cpus*) ;; set # of online cores
    1.54+                    (rocksdb-options-optimize-level-style-compaction opt 0)
    1.55+                    (rocksdb-options-set-create-if-missing opt 1)))))
    1.56+  (with-open-backup-engine-raw (be *db-backup-path* options)
    1.57+    ;; open DB
    1.58+    (with-open-db-raw (db *db-path* options)
    1.59+      ;; put key-value
    1.60+      (put-kv-str-raw db "key" "value")
    1.61+      ;; get value
    1.62+      (string= (get-kv-str-raw db "key") "value")
    1.63+      ;; create new backup in a directory specified by *db-backup-path*
    1.64+      (create-new-backup-raw be db))
    1.65+    ;; if something is wrong, you might want to restore data from last backup
    1.66+    (restore-from-latest-backup-raw be *db-path* *db-backup-path*))))