1.1--- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2+++ b/examples/db/cl-simple-example-raw.lisp Thu Apr 11 18:58:35 2024 -0400
1.3@@ -0,0 +1,66 @@
1.4+;;; cl-simple-example.lisp --- Common Lisp port of rocksdb/examples/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+# real 0m0.030s
1.21+# user 0m0.012s
1.22+# sys 0m0.017s
1.23+|#
1.24+
1.25+;; Compare to C:
1.26+#|
1.27+# in rocksdb/examples
1.28+gcc -lrocksdb c_simple_example.c -oc_simple_example
1.29+
1.30+time ./c_simple_example
1.31+
1.32+# real 0m0.021s
1.33+# user 0m0.006s
1.34+# sys 0m0.015s
1.35+|#
1.36+
1.37+;;; Code:
1.38+(defpackage :examples/cl-simple-example-raw
1.39+ (:use :cl :std :cli :rdb :sb-alien :rocksdb)
1.40+ (:export :main))
1.41+
1.42+(in-package :examples/cl-simple-example-raw)
1.43+(declaim (optimize (speed 3)))
1.44+
1.45+(defparameter *num-cpus* (num-cpus)
1.46+ "CPU count.")
1.47+
1.48+(defparameter *db-path* "/tmp/rocksdb-cl-simple-example-raw")
1.49+
1.50+(defparameter *db-backup-path* "/tmp/rocksdb-cl-simple-example-backup-raw")
1.51+
1.52+(defmain ()
1.53+ ;; open Backup Engine that we will use for backing up our database
1.54+ (let ((options (make-rocksdb-options
1.55+ (lambda (opt)
1.56+ (rocksdb-options-increase-parallelism opt *num-cpus*) ;; set # of online cores
1.57+ (rocksdb-options-optimize-level-style-compaction opt 0)
1.58+ (rocksdb-options-set-create-if-missing opt 1)))))
1.59+ (with-open-backup-engine-raw (be *db-backup-path* options)
1.60+ ;; open DB
1.61+ (with-open-db-raw (db *db-path* options)
1.62+ ;; put key-value
1.63+ (put-kv-str-raw db "key" "value")
1.64+ ;; get value
1.65+ (string= (get-kv-str-raw db "key") "value")
1.66+ ;; create new backup in a directory specified by *db-backup-path*
1.67+ (create-new-backup-raw be db))
1.68+ ;; if something is wrong, you might want to restore data from last backup
1.69+ (restore-from-latest-backup-raw be *db-path* *db-backup-path*))))