| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: SYSTEM -*- |
| 2 |
;;;; |
| 3 |
;;;; Copyright (c) 2011, Juan Jose Garcia-Ripoll. |
| 4 |
;;;; |
| 5 |
;;;; This program is free software; you can redistribute it and/or |
| 6 |
;;;; modify it under the terms of the GNU Library General Public |
| 7 |
;;;; License as published by the Free Software Foundation; either |
| 8 |
;;;; version 2 of the License, or (at your option) any later version. |
| 9 |
;;;; |
| 10 |
;;;; See file '../Copyright' for full details. |
| 11 |
;;;; |
| 12 |
;;;; This file implements the CDB specification, which you find here |
| 13 |
;;;; http://cr.yp.to/cdb/cdb.txt and is reproduced below, replicating |
| 14 |
;;;; the interface developed by Zach Beane under the name ZCDB. |
| 15 |
;;;; |
| 16 |
;;;; A structure for constant databases |
| 17 |
;;;; 19960914 |
| 18 |
;;;; Copyright 1996 |
| 19 |
;;;; D. J. Bernstein, [email protected] |
| 20 |
;;;; |
| 21 |
;;;; A cdb is an associative array: it maps strings (``keys'') to strings |
| 22 |
;;;; (``data''). |
| 23 |
;;;; |
| 24 |
;;;; A cdb contains 256 pointers to linearly probed open hash tables. The |
| 25 |
;;;; hash tables contain pointers to (key,data) pairs. A cdb is stored in |
| 26 |
;;;; a single file on disk: |
| 27 |
;;;; |
| 28 |
;;;; +----------------+---------+-------+-------+-----+---------+ |
| 29 |
;;;; | p0 p1 ... p255 | records | hash0 | hash1 | ... | hash255 | |
| 30 |
;;;; +----------------+---------+-------+-------+-----+---------+ |
| 31 |
;;;; |
| 32 |
;;;; Each of the 256 initial pointers states a position and a length. The |
| 33 |
;;;; position is the starting byte position of the hash table. The length |
| 34 |
;;;; is the number of slots in the hash table. |
| 35 |
;;;; |
| 36 |
;;;; Records are stored sequentially, without special alignment. A record |
| 37 |
;;;; states a key length, a data length, the key, and the data. |
| 38 |
;;;; |
| 39 |
;;;; Each hash table slot states a hash value and a byte position. If the |
| 40 |
;;;; byte position is 0, the slot is empty. Otherwise, the slot points to |
| 41 |
;;;; a record whose key has that hash value. |
| 42 |
;;;; |
| 43 |
;;;; Positions, lengths, and hash values are 32-bit quantities, stored in |
| 44 |
;;;; little-endian form in 4 bytes. Thus a cdb must fit into 4 gigabytes. |
| 45 |
;;;; |
| 46 |
;;;; A record is located as follows. Compute the hash value of the key in |
| 47 |
;;;; the record. The hash value modulo 256 is the number of a hash table. |
| 48 |
;;;; The hash value divided by 256, modulo the length of that table, is a |
| 49 |
;;;; slot number. Probe that slot, the next higher slot, and so on, until |
| 50 |
;;;; you find the record or run into an empty slot. |
| 51 |
;;;; |
| 52 |
;;;; The cdb hash function is ``h = ((h << 5) + h) ^ c'', with a starting |
| 53 |
;;;; hash of 5381. |
| 54 |
;;;; |
| 55 |
|
| 56 |
(defpackage "ECL-CDB" |
| 57 |
(:use "CL") |
| 58 |
(:export "WITH-OUTPUT-TO-CDB" "ADD-RECORD" "LOOKUP-CDB")) |
| 59 |
|
| 60 |
(in-package "ECL-CDB") |
| 61 |
|
| 62 |
(defstruct cdb |
| 63 |
stream |
| 64 |
pathname |
| 65 |
word-buffer |
| 66 |
tables |
| 67 |
temporary-pathname) |
| 68 |
|
| 69 |
(defun read-word (stream) |
| 70 |
(logior (read-byte stream) |
| 71 |
(ash (read-byte stream) 8) |
| 72 |
(ash (read-byte stream) 16) |
| 73 |
(ash (read-byte stream) 24))) |
| 74 |
|
| 75 |
(defun write-word (byte stream) |
| 76 |
(declare (type (unsigned-byte 32) byte) |
| 77 |
(stream stream) |
| 78 |
(optimize speed (safety 0))) |
| 79 |
(write-byte (logand #xff byte) stream) |
| 80 |
(write-byte (logand #xff (ash byte -8)) stream) |
| 81 |
(write-byte (logand #xff (ash byte -16)) stream) |
| 82 |
(write-byte (logand #xff (ash byte -24)) stream)) |
| 83 |
|
| 84 |
(defun write-vector (vector stream) |
| 85 |
(declare (type (array (unsigned-byte 8) (*)) vector)) |
| 86 |
(loop for v across vector |
| 87 |
do (write-word v stream))) |
| 88 |
|
| 89 |
(defun to-cdb-hash (key-vector) |
| 90 |
(declare (type (array (unsigned-byte 8) (*)) key-vector)) |
| 91 |
(loop with h of-type (unsigned-byte 32) = 5381 |
| 92 |
for byte of-type (unsigned-byte 8) across key-vector |
| 93 |
do (setf h (logxor (logand #xffffffff |
| 94 |
(+ (ash (logand #.(ash #xffffffff -5) h) |
| 95 |
5) |
| 96 |
h)) |
| 97 |
byte)) |
| 98 |
finally (return h))) |
| 99 |
|
| 100 |
(defun %make-cdb (cdb-pathname temporary-pathname) |
| 101 |
(let ((stream (open temporary-pathname |
| 102 |
:direction :output |
| 103 |
:if-exists :supersede |
| 104 |
:if-does-not-exist :create |
| 105 |
:element-type '(unsigned-byte 8)))) |
| 106 |
(if stream |
| 107 |
(progn |
| 108 |
(file-position stream 0) |
| 109 |
(dotimes (i (* 256 2)) |
| 110 |
(write-word 0 stream)) |
| 111 |
(make-cdb :stream stream |
| 112 |
:pathname cdb-pathname |
| 113 |
:tables (make-array 256 :initial-element nil) |
| 114 |
:temporary-pathname temporary-pathname)) |
| 115 |
(error "Unable to create CDB at filename ~A" temporary-pathname)))) |
| 116 |
|
| 117 |
(defmacro with-output-to-cdb ((cdb cdb-pathname temporary-pathname) &body body) |
| 118 |
`(let (,cdb) |
| 119 |
(unwind-protect |
| 120 |
(progn |
| 121 |
(setf ,cdb (%make-cdb ,cdb-pathname ,temporary-pathname)) |
| 122 |
,@body) |
| 123 |
(close-cdb ,cdb)))) |
| 124 |
|
| 125 |
(defun add-record (key value cdb) |
| 126 |
;; This routine dumps the data of this record, storing a small |
| 127 |
;; reference in the CDB structure itself. This reference will be |
| 128 |
;; used to create the hash. |
| 129 |
(let* ((hash-key (to-cdb-hash key)) |
| 130 |
(table-index (logand #xff hash-key)) |
| 131 |
(stream (cdb-stream cdb))) |
| 132 |
(push (cons hash-key (file-position stream)) |
| 133 |
(aref (cdb-tables cdb) table-index)) |
| 134 |
(write-word (length key) stream) |
| 135 |
(write-word (length value) stream) |
| 136 |
(write-sequence key stream) |
| 137 |
(write-sequence value stream))) |
| 138 |
|
| 139 |
(defun dump-table (table stream) |
| 140 |
(declare (optimize speed (safety 0))) |
| 141 |
;; TABLE is an association list of (HASH-KEY . FILE-POSITION) |
| 142 |
;; We dump it at the end of the file. The length of the actual |
| 143 |
;; file table can be a bit larger, to avoid many coincidences. |
| 144 |
;; Here we use a factor 2. |
| 145 |
(loop with length = (* 2 (length table)) |
| 146 |
with vector = (make-array (* 2 length) :initial-element 0 |
| 147 |
:element-type '(unsigned-byte 32)) |
| 148 |
for (hash-key . pos) in table |
| 149 |
for index = (mod (ash hash-key -8) length) |
| 150 |
do (loop for disp from 0 below length |
| 151 |
for i = (* 2 (mod (+ disp index) length)) |
| 152 |
for record-pos = (aref vector (1+ i)) |
| 153 |
until (zerop record-pos) |
| 154 |
finally (setf (aref vector i) hash-key (aref vector (1+ i)) pos)) |
| 155 |
finally (progn (write-vector vector stream) |
| 156 |
(return length)))) |
| 157 |
|
| 158 |
(defun dump-cdb (cdb) |
| 159 |
;; After we have dumped all the records in the file, we append the |
| 160 |
;; hash tables and recreate the index table at the beginning. |
| 161 |
(let* ((stream (cdb-stream cdb)) |
| 162 |
(index (make-array (* 2 256) :element-type '(unsigned-byte 32)))) |
| 163 |
(loop for table across (cdb-tables cdb) |
| 164 |
for i of-type fixnum from 0 by 2 |
| 165 |
do (setf (aref index i) (file-position stream) |
| 166 |
(aref index (1+ i)) (dump-table table stream))) |
| 167 |
(file-position stream 0) |
| 168 |
(write-vector index stream))) |
| 169 |
|
| 170 |
(defun close-cdb (cdb) |
| 171 |
(let ((stream (cdb-stream cdb))) |
| 172 |
(when (open-stream-p stream) |
| 173 |
(dump-cdb cdb) |
| 174 |
(close stream) |
| 175 |
(when (cdb-pathname cdb) |
| 176 |
(rename-file (cdb-temporary-pathname cdb) |
| 177 |
(cdb-pathname cdb)))))) |
| 178 |
|
| 179 |
(defun cdb-error (stream) |
| 180 |
(error "Error when reading CDB database ~A" stream)) |
| 181 |
|
| 182 |
(defun values-coincide (position key-vector stream return-position-p) |
| 183 |
(unless (file-position stream position) |
| 184 |
(cdb-error stream)) |
| 185 |
(let ((key-length (read-word stream))) |
| 186 |
(when (= key-length (length key-vector)) |
| 187 |
(let* ((value-length (read-word stream)) |
| 188 |
(other-key (make-array key-length :element-type '(unsigned-byte 8)))) |
| 189 |
(read-sequence other-key stream) |
| 190 |
(when (equalp other-key key-vector) |
| 191 |
(if return-position-p |
| 192 |
(file-position stream) |
| 193 |
(let ((value (make-array value-length :element-type '(unsigned-byte 8)))) |
| 194 |
(read-sequence value stream) |
| 195 |
value) |
| 196 |
)))))) |
| 197 |
|
| 198 |
(defun lookup-cdb (key stream &optional return-position-p) |
| 199 |
(if (streamp stream) |
| 200 |
(let* ((hash (to-cdb-hash key)) |
| 201 |
(table (logand #xFF hash))) |
| 202 |
(unless (file-position stream (* table 8)) |
| 203 |
(cdb-error stream)) |
| 204 |
(let* ((start (read-word stream)) |
| 205 |
(length (read-word stream)) |
| 206 |
(index (mod (ash hash -8) length))) |
| 207 |
(loop for reset = t |
| 208 |
for i from 0 below length |
| 209 |
for rounded-i = (mod (+ index i) length) |
| 210 |
for position = (+ start (* 8 rounded-i)) |
| 211 |
do (progn |
| 212 |
(when reset |
| 213 |
(unless (file-position stream position) |
| 214 |
(cdb-error stream)) |
| 215 |
(setf reset nil)) |
| 216 |
(let* ((other-hash (read-word stream)) |
| 217 |
(record-position (read-word stream))) |
| 218 |
(when (zerop record-position) |
| 219 |
(return nil)) |
| 220 |
(when (= other-hash hash) |
| 221 |
(let ((output (values-coincide record-position key stream |
| 222 |
return-position-p))) |
| 223 |
(if output |
| 224 |
(return output) |
| 225 |
(setf reset t))))))))) |
| 226 |
(with-open-file (s stream :direction :input |
| 227 |
:element-type '(unsigned-byte 8)) |
| 228 |
(lookup-cdb key s return-position-p)))) |
| 229 |
|
| 230 |
(defun map-cdb (function stream) |
| 231 |
(if (streamp stream) |
| 232 |
(let* ((index (make-array (* 256 2) :element-type '(unsigned-byte 32)))) |
| 233 |
(unless (file-position stream 0) |
| 234 |
(cdb-error stream)) |
| 235 |
(unless (= (read-sequence index stream) (length index)) |
| 236 |
(cdb-error stream)) |
| 237 |
(loop for i from 0 by 2 below (length index) |
| 238 |
for table-position = (aref index i) |
| 239 |
for table-length = (aref index (1+ i)) |
| 240 |
do (progn |
| 241 |
(unless (file-position stream table-position) |
| 242 |
(cdb-error stream)) |
| 243 |
(loop for i from 0 below table-length |
| 244 |
for position from table-position by 8 |
| 245 |
for record-hash = (read-word stream) |
| 246 |
for record-position = (read-word stream) |
| 247 |
unless (zerop record-position) |
| 248 |
do (progn |
| 249 |
(unless (file-position stream record-position) |
| 250 |
(cdb-error stream)) |
| 251 |
(let* ((key-length (read-word stream)) |
| 252 |
(value-length (read-word stream)) |
| 253 |
(key (make-array key-length |
| 254 |
:element-type '(unsigned-byte 8))) |
| 255 |
(value (make-array value-length |
| 256 |
:element-type '(unsigned-byte 8)))) |
| 257 |
(unless (and (= (read-sequence key stream) |
| 258 |
key-length) |
| 259 |
(= (read-sequence value stream) |
| 260 |
value-length)) |
| 261 |
(cdb-error stream)) |
| 262 |
(funcall function key value))))))) |
| 263 |
(with-open-file (s stream :direction :input :element-type '(unsigned-byte 8)) |
| 264 |
(map-cdb function s)))) |
| 265 |
|
| 266 |
(provide '#:ecl-cdb) |