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