changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 35: f54f7cc7458b
child: 0f678bfd8699
author: ellis <ellis@rwest.io>
date: Tue, 19 Dec 2023 16:46:55 -0500
permissions: -rw-r--r--
description: cl-simple-example implemented, init db/tao.lisp
1 ;;; cl-simple-example.lisp --- Common Lisp port of rocksdb/example/c_simple_example.c
2 
3 ;; ref: https://github.com/facebook/rocksdb/blob/main/examples/c_simple_example.c
4 
5 ;;; Usage:
6 
7 ;; To compile and run from the shell:
8 #|
9 sbcl --eval '(ql:quickload :rdb)' \
10  --eval '(ql:quickload :cli)' \
11  --eval '(compile-file "cl-simple-example.lisp")' \
12  --eval '(load "cl-simple-example.fasl")' \
13  --eval "(sb-ext:save-lisp-and-die \"cl-simple-example\" :toplevel #'cl-simple-example::main :executable t)"
14 
15 time ./cl-simple-example
16 |#
17 
18 ;; Compare to C:
19 #|
20 # in rocksdb/examples
21 gcc -lrocksdb c_simple_example.c -oc_simple_example
22 
23 time ./c_simple_example
24 |#
25 
26 ;;; Code:
27 (defpackage :examples/rdb/cl-simple-example
28  (:nicknames :cl-simple-example)
29  (:use :cl :std :cli :rdb :sb-alien :rocksdb)
30  (:export :main))
31 
32 (rocksdb:load-rocksdb :save t)
33 
34 (in-package :cl-simple-example)
35 
36 (in-readtable :std)
37 
38 (defvar *num-cpus* (alien-funcall (extern-alien "sysconf" (function long integer)) sb-unix:sc-nprocessors-onln)
39  "CPU count.")
40 
41 (defparameter *db-path* "/tmp/rocksdb-cl-simple-example")
42 
43 (defparameter *db-backup-path* "/tmp/rocksdb-cl-simple-example-backup")
44 
45 (defmain ()
46  ;; open Backup Engine that we will use for backing up our database
47  (let ((options
48  (make-rocksdb-options
49  (lambda (opt)
50  (rocksdb-options-increase-parallelism opt *num-cpus*) ;; set # of online cores
51  (rocksdb-options-optimize-level-style-compaction opt 0)
52  (rocksdb-options-set-create-if-missing opt 1)))))
53  (with-open-backup-engine-raw (be *db-backup-path* options)
54  ;; open DB
55  (with-open-db-raw (db *db-path* options)
56  ;; put key-value
57  (put-kv-str-raw db "key" "value")
58  ;; get value
59  (string= (get-kv-str-raw db "key") "value")
60  ;; create new backup in a directory specified by *db-backup-path*
61  (create-new-backup-raw be db))
62  ;; if something is wrong, you might want to restore data from last backup
63  (restore-from-latest-backup-raw be *db-path* *db-backup-path*))))