changelog shortlog graph tags branches changeset files revisions annotate raw help

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

changeset 44: 99d4ab4f8d53
parent: 1ef551e24009
author: Richard Westhaver <ellis@rwest.io>
date: Sun, 11 Aug 2024 01:50:18 -0400
permissions: -rw-r--r--
description: update
1 ;;; cl-simple-example.lisp --- Common Lisp port of rocksdb/examples/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/cl-simple-example-raw
36  (:use :cl :std :cli :rdb :sb-alien :rocksdb)
37  (:export :main))
38 
39 (in-package :examples/cl-simple-example-raw)
40 (declaim (optimize (speed 3)))
41 
42 (defparameter *num-cpus* (num-cpus)
43  "CPU count.")
44 
45 (defparameter *db-path* "/tmp/rocksdb-cl-simple-example-raw")
46 
47 (defparameter *db-backup-path* "/tmp/rocksdb-cl-simple-example-backup-raw")
48 
49 (defmain ()
50  ;; open Backup Engine that we will use for backing up our database
51  (let ((options (make-rocksdb-options
52  (lambda (opt)
53  (rocksdb-options-increase-parallelism opt *num-cpus*) ;; set # of online cores
54  (rocksdb-options-optimize-level-style-compaction opt 0)
55  (rocksdb-options-set-create-if-missing opt 1)))))
56  (with-open-backup-engine-raw (be *db-backup-path* options)
57  ;; open DB
58  (with-open-db-raw (db *db-path* options)
59  ;; put key-value
60  (put-kv-str-raw db "key" "value")
61  ;; get value
62  (string= (get-kv-str-raw db "key") "value")
63  ;; create new backup in a directory specified by *db-backup-path*
64  (create-new-backup-raw be db))
65  ;; if something is wrong, you might want to restore data from last backup
66  (restore-from-latest-backup-raw be *db-path* *db-backup-path*))))