changelog shortlog graph tags branches changeset files file revisions raw help

Mercurial > demo / annotate 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
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
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
16
 |#
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
17
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
18
 ;; Compare to C:
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
19
 #|
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
20
 # in rocksdb/examples
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
21
 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
22
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
23
 time ./c_simple_example
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
24
 |#
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
25
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
26
 ;;; Code:
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
27
 (defpackage :examples/rdb/cl-simple-example
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
28
   (:nicknames :cl-simple-example)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
29
   (:use :cl :std :cli :rdb :sb-alien :rocksdb)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
30
   (:export :main))
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
31
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
32
 (rocksdb:load-rocksdb :save t)
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
 (in-package :cl-simple-example)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
35
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
36
 (in-readtable :std)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
37
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
38
 (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
39
   "CPU count.")
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
40
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
41
 (defparameter *db-path* "/tmp/rocksdb-cl-simple-example")
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
42
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
43
 (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
44
 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
45
 (defmain ()
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
46
   ;; 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
47
   (let ((options 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
48
           (make-rocksdb-options 
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
49
                   (lambda (opt)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
50
                     (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
51
                     (rocksdb-options-optimize-level-style-compaction opt 0)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
52
                     (rocksdb-options-set-create-if-missing opt 1)))))
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
53
   (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
54
     ;; open DB
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
55
     (with-open-db-raw (db *db-path* options)
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
56
       ;; put key-value
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
57
       (put-kv-str-raw db "key" "value")
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
58
       ;; get value
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
59
       (string= (get-kv-str-raw db "key") "value")
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
60
       ;; 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
61
       (create-new-backup-raw be db))
f54f7cc7458b cl-simple-example implemented, init db/tao.lisp
ellis <ellis@rwest.io>
parents:
diff changeset
62
     ;; 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
63
     (restore-from-latest-backup-raw be *db-path* *db-backup-path*))))