changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 36: 0f678bfd8699
parent: f54f7cc7458b
child: c6d0a37a046a
author: ellis <ellis@rwest.io>
date: Tue, 19 Dec 2023 16:52:10 -0500
permissions: -rw-r--r--
description: added sample output of cl-simple-example vs c_simple_example
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 # real 0m0.030s
18 # user 0m0.012s
19 # sys 0m0.017s
20 |#
21 
22 ;; Compare to C:
23 #|
24 # in rocksdb/examples
25 gcc -lrocksdb c_simple_example.c -oc_simple_example
26 
27 time ./c_simple_example
28 
29 # real 0m0.021s
30 # user 0m0.006s
31 # sys 0m0.015s
32 |#
33 
34 ;;; Code:
35 (defpackage :examples/rdb/cl-simple-example
36  (:nicknames :cl-simple-example)
37  (:use :cl :std :cli :rdb :sb-alien :rocksdb)
38  (:export :main))
39 
40 (rocksdb:load-rocksdb :save t)
41 
42 (in-package :cl-simple-example)
43 
44 (in-readtable :std)
45 
46 (defvar *num-cpus* (alien-funcall (extern-alien "sysconf" (function long integer)) sb-unix:sc-nprocessors-onln)
47  "CPU count.")
48 
49 (defparameter *db-path* "/tmp/rocksdb-cl-simple-example")
50 
51 (defparameter *db-backup-path* "/tmp/rocksdb-cl-simple-example-backup")
52 
53 (defmain ()
54  ;; open Backup Engine that we will use for backing up our database
55  (let ((options
56  (make-rocksdb-options
57  (lambda (opt)
58  (rocksdb-options-increase-parallelism opt *num-cpus*) ;; set # of online cores
59  (rocksdb-options-optimize-level-style-compaction opt 0)
60  (rocksdb-options-set-create-if-missing opt 1)))))
61  (with-open-backup-engine-raw (be *db-backup-path* options)
62  ;; open DB
63  (with-open-db-raw (db *db-path* options)
64  ;; put key-value
65  (put-kv-str-raw db "key" "value")
66  ;; get value
67  (string= (get-kv-str-raw db "key") "value")
68  ;; create new backup in a directory specified by *db-backup-path*
69  (create-new-backup-raw be db))
70  ;; if something is wrong, you might want to restore data from last backup
71  (restore-from-latest-backup-raw be *db-path* *db-backup-path*))))