Mercurial > core / lisp/lib/rdb/macs.lisp
changeset 624: |
97dd03beda03 |
parent: |
ab9e41953ae2
|
child: |
12287fab15d0 |
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Sun, 25 Aug 2024 20:28:57 -0400 |
permissions: |
-rw-r--r-- |
description: |
rocksdb updates in coordination with infra/scripts/org-graph-db-init.lisp |
1 ;;; rdb/macs.lisp --- macros 7 (defmacro with-errptr ((e &optional errtyp params) &body body) 8 `(with-alien ((,e rocksdb-errptr nil)) 10 (handler-bind ((sb-sys:memory-fault-error 12 (handle-errptr ,e ,errtyp ,params))) 15 (handle-errptr ,e ,errtyp ,params)))) 17 (handle-errptr ,e ,errtyp ,params) 22 (defmacro rdb-opt-setter (key) 23 `(find-symbol (format nil "~:@(rocksdb-options-set-~x~)" ,key) :rocksdb)) 25 (defmacro rdb-opt-getter (key) 26 `(find-symbol (format nil "~:@(rocksdb-options-get-~x~)" ,key) :rocksdb)) 29 (defmacro with-open-db-raw ((db-var db-path &optional (opt (default-rocksdb-options))) &body body) 30 `(let ((,db-var (open-db-raw ,db-path ,opt))) 31 (unwind-protect (progn ,@body) 32 (rocksdb-close ,db-var) 33 (with-errptr (err 'rocksdb-error) 34 ;; (rocksdb-destroy-db ,opt ,db-path err) ;; when :destroy only 35 (rocksdb-options-destroy ,opt))))) 37 (defmacro with-db ((db-var db) &body body) 38 "Bind DB-VAR to the database object DB for the lifetime of BODY." 40 (handler-bind ((error (lambda (condition) 43 (format nil "WITH-DB signaled: ~A" condition))))) 47 (defmacro with-cf ((cf-var cf) &body body) 48 "Bind CF to CF-VAR for the lifetime of BODY." 50 (handler-bind ((error (lambda (condition) 53 (format nil "WITH-CF signaled: ~A" condition))))) 56 (defmacro do-cfs ((cf cfs) &body body) 57 "Do BODY for each CF in the array CFS." 59 `(loop for ,%cf across ,cfs 60 do (with-cf (,cf ,%cf) ,@body)))) 63 (defmacro with-iter-raw ((iter-var db &optional (opt (rocksdb-readoptions-create))) &body body) 64 `(let ((,iter-var (create-iter-raw ,db ,opt))) 65 (unwind-protect (progn ,@body) 66 (destroy-iter-raw ,iter-var)))) 68 (defmacro with-iter ((iter-var iter) &body body) 69 "Bind object ITER to ITER-VAR. 71 ((%ITER ITER) BODY) is passed to ROCKSDB:WITH-ITER-RAW, binding the 72 raw handle to the same symbol prefixed with '%'. 74 Errors that occur in the inner body will be handled but the iterator 75 handle will not be freed on exit." 76 (let ((%iter-var (symbolicate '% (symbol-name iter-var)))) 77 `(let ((,iter-var ,iter)) 78 (let ((,%iter-var (rdb-iter-sap ,iter-var))) 79 (declare (ignorable ,%iter-var)) 82 ;; TODO: sb-ext:with-current-source-form ? 84 (defmacro with-open-backup-engine-raw ((be-var be-path &optional (opt (rocksdb-options-create))) 86 `(let ((,be-var (open-backup-engine-raw ,be-path ,opt))) 87 (unwind-protect (progn ,@body) 88 (rocksdb-backup-engine-close ,be-var)))) 91 (defmacro do-db ((db opts) accessors &body body) 92 "Database Iteration construct. OPTS are used to provide top-level 93 options dynamically bound to DB. ACCESSORS is a list of database 94 accessors which are available to call in BODY." 98 (defvar *temp-db-path-generator* 99 (lambda (&optional (name "temp-db")) 100 (make-pathname :directory "tmp" :name (symbol-name (gensym name)))) 101 "A single arg function returning the absolute path to a temp-db path.") 103 (defvar *temp-db-destroy* nil) 105 (defmacro with-temp-db ((db-var (&rest cfs) &key (destroy *temp-db-destroy*) open) &body body) 106 "Bind DB-VAR to a temporary RDB object, arranging for CF-VARS to be 107 created as column-families and destroying the database after executing 112 (setf var (make-rdb-cf (symbol-name var)))) 114 `(with-db (,db-var (make-rdb 115 (namestring (funcall ,*temp-db-path-generator* ,(symbol-name db-var))) 117 (make-array ,(length cfs) :element-type 'rdb-cf :initial-contents ',cfs :adjustable t :fill-pointer ,(length cfs)))) 118 ,@(when open `((open-db ,db-var) 119 (create-cfs ,db-var))) 123 `(destroy-db ,db-var) 124 `(shutdown-db ,db-var))))) 126 (defmacro with-sst ((sst &key file comparator destroy) &body body) 127 "Do BODY with SST bound to a SST-FILE-WRITER. When FILE is supplied 128 the writer will automatically open that file. 130 When COMPARATOR is supplied it is used as the comparator function for 131 the writer. Every key inserted MUST be in ascending order, according 132 to the comparator. By default the ordering is binary 135 It is up to the developer to ensure that the comparator used by a 136 writer is exactly the same as the comparator used when ingesting the 137 file by a RDB instance." 138 `(let ((,sst (make-sst-file-writer ,comparator))) 139 ,@(when file `((open-sst ,sst ,file))) 141 ,@(when destroy `((destroy-sst ,sst)))))