| 1 |
;;; |
| 2 |
;;; ndbm - ndbm interface |
| 3 |
;;; |
| 4 |
;;; Copyright (c) 2000-2024 Shiro Kawai <[email protected]> |
| 5 |
;;; |
| 6 |
;;; Redistribution and use in source and binary forms, with or without |
| 7 |
;;; modification, are permitted provided that the following conditions |
| 8 |
;;; are met: |
| 9 |
;;; |
| 10 |
;;; 1. Redistributions of source code must retain the above copyright |
| 11 |
;;; notice, this list of conditions and the following disclaimer. |
| 12 |
;;; |
| 13 |
;;; 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
;;; notice, this list of conditions and the following disclaimer in the |
| 15 |
;;; documentation and/or other materials provided with the distribution. |
| 16 |
;;; |
| 17 |
;;; 3. Neither the name of the authors nor the names of its contributors |
| 18 |
;;; may be used to endorse or promote products derived from this |
| 19 |
;;; software without specific prior written permission. |
| 20 |
;;; |
| 21 |
;;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
;;; "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
;;; LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
;;; A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
;;; OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
;;; SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
;;; TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
;;; PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
;;; LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
;;; |
| 33 |
|
| 34 |
(define-module dbm.ndbm |
| 35 |
(extend dbm) |
| 36 |
(use scheme.list) |
| 37 |
(use util.match) |
| 38 |
(export <ndbm> |
| 39 |
;; low level funcions |
| 40 |
ndbm-open ndbm-close ndbm-closed? |
| 41 |
ndbm-store ndbm-fetch ndbm-exists? |
| 42 |
ndbm-delete |
| 43 |
ndbm-firstkey ndbm-nextkey ndbm-error |
| 44 |
ndbm-clearerror |
| 45 |
DBM_INSERT DBM_REPLACE) |
| 46 |
) |
| 47 |
(select-module dbm.ndbm) |
| 48 |
|
| 49 |
;;; |
| 50 |
;;; High-level dbm interface |
| 51 |
;;; |
| 52 |
|
| 53 |
(define-class <ndbm-meta> (<dbm-meta>) |
| 54 |
()) |
| 55 |
|
| 56 |
(define-class <ndbm> (<dbm>) |
| 57 |
((ndbm-file :accessor ndbm-file-of :initform #f) |
| 58 |
) |
| 59 |
:metaclass <ndbm-meta>) |
| 60 |
|
| 61 |
(define-method dbm-open ((self <ndbm>)) |
| 62 |
(next-method) |
| 63 |
(unless (slot-bound? self 'path) |
| 64 |
(error "path must be set to open ndbm database")) |
| 65 |
(when (ndbm-file-of self) |
| 66 |
(errorf "ndbm ~S already opened" self)) |
| 67 |
(let* ([path (slot-ref self 'path)] |
| 68 |
[rwmode (slot-ref self 'rw-mode)] |
| 69 |
[rwopt (case rwmode |
| 70 |
[(:read) O_RDONLY] |
| 71 |
[(:write) (+ O_RDWR O_CREAT)] |
| 72 |
[(:create) (+ O_RDWR O_CREAT O_TRUNC)])] |
| 73 |
[fp (ndbm-open path |
| 74 |
rwopt |
| 75 |
(slot-ref self 'file-mode))]) |
| 76 |
(slot-set! self 'ndbm-file fp) |
| 77 |
self)) |
| 78 |
|
| 79 |
;; |
| 80 |
;; close operation |
| 81 |
;; |
| 82 |
|
| 83 |
(define-method dbm-close ((self <ndbm>)) |
| 84 |
(let1 ndbm (ndbm-file-of self) |
| 85 |
(and ndbm (ndbm-close ndbm)))) |
| 86 |
|
| 87 |
(define-method dbm-closed? ((self <ndbm>)) |
| 88 |
(let1 ndbm (ndbm-file-of self) |
| 89 |
(or (not ndbm) (ndbm-closed? ndbm)))) |
| 90 |
|
| 91 |
;; |
| 92 |
;; common operations |
| 93 |
;; |
| 94 |
|
| 95 |
(define-method dbm-put! ((self <ndbm>) key value) |
| 96 |
(next-method) |
| 97 |
(when (positive? (ndbm-store (ndbm-file-of self) |
| 98 |
(%dbm-k2s self key) |
| 99 |
(%dbm-v2s self value) |
| 100 |
DBM_REPLACE)) |
| 101 |
(error "dbm-put! failed" self))) |
| 102 |
|
| 103 |
(define-method dbm-get ((self <ndbm>) key . args) |
| 104 |
(next-method) |
| 105 |
(cond [(ndbm-fetch (ndbm-file-of self) (%dbm-k2s self key)) |
| 106 |
=> (cut %dbm-s2v self <>)] |
| 107 |
[(pair? args) (car args)] ;fall-back value |
| 108 |
[else (errorf "ndbm: no data for key ~s in database ~s" |
| 109 |
key (ndbm-file-of self))])) |
| 110 |
|
| 111 |
(define-method dbm-exists? ((self <ndbm>) key) |
| 112 |
(next-method) |
| 113 |
(ndbm-exists? (ndbm-file-of self) (%dbm-k2s self key))) |
| 114 |
|
| 115 |
(define-method dbm-delete! ((self <ndbm>) key) |
| 116 |
(next-method) |
| 117 |
(when (positive? (ndbm-delete (ndbm-file-of self) (%dbm-k2s self key))) |
| 118 |
(errorf "dbm-delete!: deleteting key ~s from ~s failed" key self))) |
| 119 |
|
| 120 |
(define-method dbm-fold ((self <ndbm>) proc knil) |
| 121 |
(let1 ndbm (ndbm-file-of self) |
| 122 |
(let loop ([key (ndbm-firstkey ndbm)] [r knil]) |
| 123 |
(if key |
| 124 |
(let1 val (ndbm-fetch ndbm key) |
| 125 |
(loop (ndbm-nextkey ndbm) |
| 126 |
(proc (%dbm-s2k self key) (%dbm-s2v self val) r))) |
| 127 |
r)))) |
| 128 |
|
| 129 |
;; *ndbm-suffixes* is defined in the stub file. |
| 130 |
(define (ndbm-files name) |
| 131 |
(map (cut string-append name <>) *ndbm-suffixes*)) |
| 132 |
|
| 133 |
(define-method dbm-db-exists? ((class <ndbm-meta>) name) |
| 134 |
(every file-exists? (ndbm-files name))) |
| 135 |
|
| 136 |
(define-method dbm-db-remove ((class <ndbm-meta>) name) |
| 137 |
(for-each sys-unlink (ndbm-files name))) |
| 138 |
|
| 139 |
(define-method dbm-db-copy ((class <ndbm-meta>) from to . keys) |
| 140 |
(match (ndbm-files from) |
| 141 |
[(f1 f2) |
| 142 |
(match-let1 (t1 t2) (ndbm-files to) |
| 143 |
(apply %dbm-copy2 f1 t1 f2 t2 keys))] |
| 144 |
[(f) |
| 145 |
(match-let1 (t) (ndbm-files to) |
| 146 |
(apply copy-file f t :safe #t keys))] |
| 147 |
[() |
| 148 |
(apply copy-file from to :safe #t keys)] |
| 149 |
[else |
| 150 |
(error "dbm-db-copy: ndbm using more than three files isn't supported")])) |
| 151 |
|
| 152 |
(define-method dbm-db-move ((class <ndbm-meta>) from to . keys) |
| 153 |
(match (ndbm-files from) |
| 154 |
[(f1 f2) |
| 155 |
(match-let1 (t1 t2) (ndbm-files to) |
| 156 |
(apply %dbm-rename2 f1 t1 f2 t2 keys))] |
| 157 |
[(f) |
| 158 |
(match-let1 (t) (ndbm-files to) |
| 159 |
(apply move-file f t :safe #t keys))] |
| 160 |
[() |
| 161 |
(apply move-file from to :safe #t keys)] |
| 162 |
[else |
| 163 |
(error "dbm-db-move: ndbm using more than three files isn't supported")])) |
| 164 |
|
| 165 |
;;; |
| 166 |
;;; Low-level bindings |
| 167 |
;;; |
| 168 |
|
| 169 |
(inline-stub |
| 170 |
(declcode |
| 171 |
(.include <fcntl.h>) |
| 172 |
(.include <gauche/priv/configP.h>) |
| 173 |
(.include "dbmconf.h") |
| 174 |
(.cond |
| 175 |
["HAVE_NDBM_H" (.include <ndbm.h>)] |
| 176 |
["HAVE_GDBM_SLASH_NDBM_H" (.include <gdbm/ndbm.h>)] |
| 177 |
["HAVE_GDBM_MINUS_NDBM_H" (.include <gdbm-ndbm.h>)]) |
| 178 |
(.include "ndbm-suffixes.h")) |
| 179 |
|
| 180 |
(define-ctype ScmNdbmFile::(.struct |
| 181 |
(SCM_HEADER :: "" |
| 182 |
name |
| 183 |
dbf::DBM* ; NULL if closed |
| 184 |
))) |
| 185 |
|
| 186 |
(define-cclass <ndbm-file> :private ScmNdbmFile* "Scm_NdbmFileClass" () |
| 187 |
() |
| 188 |
[printer |
| 189 |
(Scm_Printf port "#<ndbm-file %S>" (-> (SCM_NDBM_FILE obj) name))]) |
| 190 |
|
| 191 |
(define-cfn ndbm_finalize (obj _::void*) ::void :static |
| 192 |
(let* ((n::ScmNdbmFile* (SCM_NDBM_FILE obj))) |
| 193 |
(when (-> n dbf) |
| 194 |
(dbm_close (-> n dbf)) |
| 195 |
(set! (-> n dbf) NULL)))) |
| 196 |
|
| 197 |
;; data conversion macros |
| 198 |
(define-cise-stmt TO_DATUM |
| 199 |
[(_ datum scm) |
| 200 |
(let ((tmp (gensym))) |
| 201 |
`(let* ((,tmp :: (const ScmStringBody*) (SCM_STRING_BODY ,scm))) |
| 202 |
(set! (ref ,datum dptr) (cast char* (SCM_STRING_BODY_START ,tmp))) |
| 203 |
(set! (ref ,datum dsize) (SCM_STRING_BODY_SIZE ,tmp))))]) |
| 204 |
|
| 205 |
(define-cise-stmt FROM_DATUM |
| 206 |
[(_ scm datum) |
| 207 |
`(cond [(ref ,datum dptr) |
| 208 |
(set! ,scm (Scm_MakeString (ref ,datum dptr) (ref ,datum dsize) |
| 209 |
-1 SCM_STRING_COPYING))] |
| 210 |
[else |
| 211 |
(set! ,scm SCM_FALSE)])]) |
| 212 |
|
| 213 |
(define-cise-stmt CHECK_NDBM |
| 214 |
[(_ g) |
| 215 |
`(unless (-> ,g dbf) (Scm_Error "ndbm file already closed: %S" ,g))]) |
| 216 |
|
| 217 |
;; suffixes |
| 218 |
(define-variable *ndbm-suffixes* |
| 219 |
(c (Scm_CStringArrayToList ndbm_suffixes -1 SCM_STRING_IMMUTABLE))) |
| 220 |
|
| 221 |
;; bindings |
| 222 |
(define-cproc ndbm-open (name::<string> flags::<fixnum> mode::<fixnum>) |
| 223 |
(let* ([z::ScmNdbmFile* (SCM_NEW ScmNdbmFile)]) |
| 224 |
(SCM_SET_CLASS z (& Scm_NdbmFileClass)) |
| 225 |
(Scm_RegisterFinalizer (SCM_OBJ z) ndbm_finalize NULL) |
| 226 |
(set! (-> z name) (SCM_OBJ name)) |
| 227 |
(set! (-> z dbf) (dbm_open (Scm_GetString name) flags mode)) |
| 228 |
(when (== (-> z dbf) NULL) |
| 229 |
(Scm_SysError "couldn't open ndbm file %S" name)) |
| 230 |
(return (SCM_OBJ z)))) |
| 231 |
|
| 232 |
(define-cproc ndbm-close (ndbm::<ndbm-file>) ::<void> |
| 233 |
(when (-> ndbm dbf) |
| 234 |
(dbm_close (-> ndbm dbf)) |
| 235 |
(set! (-> ndbm dbf) NULL))) |
| 236 |
|
| 237 |
(define-cproc ndbm-closed? (ndbm::<ndbm-file>) ::<boolean> |
| 238 |
(return (== (-> ndbm dbf) NULL))) |
| 239 |
|
| 240 |
(define-cproc ndbm-store (ndbm::<ndbm-file> key::<string> val::<string> |
| 241 |
:optional (flags::<fixnum> 0)) |
| 242 |
::<int> |
| 243 |
(let* ([dkey::datum] [dval::datum]) |
| 244 |
(CHECK_NDBM ndbm) |
| 245 |
(TO_DATUM dkey key) |
| 246 |
(TO_DATUM dval val) |
| 247 |
(return (dbm_store (-> ndbm dbf) dkey dval flags)))) |
| 248 |
|
| 249 |
(define-cproc ndbm-fetch (ndbm::<ndbm-file> key::<string>) |
| 250 |
(let* ([dkey::datum] [dval::datum]) |
| 251 |
(CHECK_NDBM ndbm) |
| 252 |
(TO_DATUM dkey key) |
| 253 |
(set! dval (dbm_fetch (-> ndbm dbf) dkey)) |
| 254 |
(FROM_DATUM SCM_RESULT dval))) |
| 255 |
|
| 256 |
(define-cproc ndbm-exists? (ndbm::<ndbm-file> key::<string>) ::<boolean> |
| 257 |
(let* ([dkey::datum] [dval::datum]) |
| 258 |
(CHECK_NDBM ndbm) |
| 259 |
(TO_DATUM dkey key) |
| 260 |
(set! dval (dbm_fetch (-> ndbm dbf) dkey)) |
| 261 |
(return (!= (ref dval dptr) NULL)))) |
| 262 |
|
| 263 |
(define-cproc ndbm-delete (ndbm::<ndbm-file> key::<string>) ::<int> |
| 264 |
(let* ([dkey::datum]) |
| 265 |
(CHECK_NDBM ndbm) |
| 266 |
(TO_DATUM dkey key) |
| 267 |
(return (dbm_delete (-> ndbm dbf) dkey)))) |
| 268 |
|
| 269 |
(define-cproc ndbm-firstkey (ndbm::<ndbm-file>) |
| 270 |
(let* ([dkey::datum]) |
| 271 |
(CHECK_NDBM ndbm) |
| 272 |
(set! dkey (dbm_firstkey (-> ndbm dbf))) |
| 273 |
(FROM_DATUM SCM_RESULT dkey))) |
| 274 |
|
| 275 |
(define-cproc ndbm-nextkey (ndbm::<ndbm-file>) |
| 276 |
(let* ([dkey::datum]) |
| 277 |
(CHECK_NDBM ndbm) |
| 278 |
(set! dkey (dbm_nextkey (-> ndbm dbf))) |
| 279 |
(FROM_DATUM SCM_RESULT dkey))) |
| 280 |
|
| 281 |
(define-cproc ndbm-error (ndbm::<ndbm-file>) ::<int> |
| 282 |
(CHECK_NDBM ndbm) |
| 283 |
(return (dbm_error (-> ndbm dbf)))) |
| 284 |
|
| 285 |
(define-cproc ndbm-clearerror (ndbm::<ndbm-file>) ::<void> |
| 286 |
(CHECK_NDBM ndbm) |
| 287 |
(dbm_clearerr (-> ndbm dbf))) |
| 288 |
|
| 289 |
(define-enum DBM_INSERT) |
| 290 |
(define-enum DBM_REPLACE) |
| 291 |
(define-enum O_RDONLY) |
| 292 |
(define-enum O_WRONLY) |
| 293 |
(define-enum O_RDWR) |
| 294 |
(define-enum O_CREAT) |
| 295 |
(define-enum O_TRUNC) |
| 296 |
) |