PageRenderTime 73ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/doc/index.html

https://github.com/xach/zcdb
HTML | 245 lines | 190 code | 55 blank | 0 comment | 0 complexity | 29301dc92b02f7b7ffe775d6f186239b MD5 | raw file
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  4. <link rel='stylesheet' type='text/css' href='style.css'>
  5. <title>ZCDB - Read and write cdb files from Common Lisp</title>
  6. </head>
  7. <body>
  8. <div id='content'>
  9. <h2>ZCDB - Read and write cdb files from Common Lisp</h2>
  10. <p>ZCDB is a Common Lisp library for reading and writing
  11. D. J. Bernstein's fast and simple
  12. <a href="http://cr.yp.to/cdb.html">cdb database file format</a>. It
  13. is available under a BSD-style <a href='#license'>license</a>.
  14. The latest version is 1.0.4, released on March 12th, 2015.
  15. <p>Download shortcut: <a href='http://www.xach.com/lisp/zcdb.tgz'>http://www.xach.com/lisp/zcdb.tgz</a>
  16. <h2>Contents</h2>
  17. <ul>
  18. <li> <a href='#overview'>Overview</a>
  19. <li> <a href='#example'>Example Use</a>
  20. <li> <a href='#limitations'>Limitations</a>
  21. <li> <a href='#dictionary'>The ZCDB Dictionary</a>
  22. <ul>
  23. <li> <a href='#with-output-to-cdb'><code>with-output-to-cdb</code></a>
  24. <li> <a href='#add-record'><code>add-record</code></a>
  25. <li> <a href='#lookup'><code>lookup</code></a>
  26. <li> <a href='#map-cdb'><code>map-cdb</code></a>
  27. </ul>
  28. <li> <a href='#feedback'>Feedback</a>
  29. </ul>
  30. <a name='overview'><h2>Overview</h2></a>
  31. <p>ZCDB is for reading and writing from CDB files. CDB files are
  32. described in more detail on
  33. D.J. Bernstein's <a href="http://cr.yp.to/cdb.html">CDB information
  34. page</a>. The CDB file format makes it easy to quickly retrieve
  35. data associated with an arbitrary binary key.
  36. <p>Once written, CDB files cannot be updated with new records. In
  37. ZCDB, a CDB file is written out
  38. with <a href='#with-output-to-cdb'><tt>WITH-OUTPUT-TO-CDB</tt></a>
  39. and <a href='#add-record'><tt>ADD-RECORD</tt></a>, then read
  40. with <a href='#lookup'><tt>LOOKUP</tt></a>.
  41. <a name='example'><h2>Example Use</h2></a>
  42. <p>Here is a simple way to convert a Unix system password file to a
  43. CDB file:
  44. <pre class='code'>
  45. (defpackage #:pwdb
  46. (:use #:cl #:zcdb))
  47. (defun username (password-line)
  48. (let ((end (position #\: password-line)))
  49. (subseq password-line 0 end)))
  50. (defun string-octets (string)
  51. (babel:string-to-octets string :encoding :utf-8))
  52. (defun octets-string (octets)
  53. (babel:octets-to-string octets :encoding :utf-8))
  54. (defun make-password-cdb (password-file cdb-file temp-file)
  55. (with-open-file (stream password-file)
  56. (<a href='#with-output-to-cdb'>with-output-to-cdb</a> (cdb cdb-file temp-file)
  57. (loop for line = (read-line stream nil)
  58. while line do
  59. (let ((username (username line)))
  60. (<a href='#add-record'>add-record</a> (string-octets username)
  61. (string-octets line)
  62. cdb))))))
  63. (defun user-info (username cdb-file)
  64. (let ((entry (<a href='#lookup'>lookup</a> (string-octets username) cdb-file)))
  65. (when entry
  66. (octets-string entry))))
  67. </pre>
  68. <p>Then the database can be created and queried:
  69. <pre class='code'>
  70. * <b>(make-password-cdb "/etc/passwd" "/tmp/pwdb.cdb" "/tmp/pwdb.cdb.tmp")</b>
  71. #P"/tmp/pwdb.cdb"
  72. * <b>(user-info "root" "/tmp/pwdb.cdb")</b>
  73. "root:*:0:0:System Administrator:/var/root:/bin/sh"
  74. </pre>
  75. <a name='limitations'><h2>Limitations</h2></a>
  76. <p>The CDB file format offers many opportunities for low-level
  77. interaction, including writing and reading records that don't fit in
  78. memory by writing or reading them incrementally. ZCDB currently only
  79. offers a simplified interface that works with keys and values that
  80. are fully loaded in memory as <tt>(unsigned-byte&nbsp;8)</tt>
  81. vectors.
  82. <p>ZCDB does not provide any functions for serializing various data
  83. types (such as strings) to vectors. There are many other third-party
  84. libraries for that purpose.
  85. <a name='dictionary'><h2>The ZCDB Dictionary</h2></a>
  86. <p>ZCDB exports the following symbols.
  87. <div class='item'>
  88. <div class='type'><a name='with-output-to-cdb'>[Macro]</a></div>
  89. <div class='signature'>
  90. <code class='name'>with-output-to-cdb</code>
  91. <span class='args'>(<var>cdb</var> <var>cdb-pathname</var>
  92. <var>temporary-pathname</var>)
  93. <code class='llkw'>&amp;body</code> <var>body</var>
  94. </span>
  95. <span class='result'>=> |</span>
  96. </div>
  97. <blockquote class='description'>
  98. <p>Evaluates <var>body</var> with <var>cdb</var> bound to a cdb
  99. writer object. <var>cdb</var> may be used as the target
  100. of <a href='#add-record'><tt>ADD-RECORD</tt></a> operations.
  101. <p>The cdb in progress is written
  102. to <var>temporary-pathname</var>. Any existing file with that
  103. pathname is overwritten. When writing completes
  104. successfully, <var>temporary-pathname</var> is renamed
  105. to <var>cdb-pathname</var>
  106. with <a href='http://l1sp.org/cl/rename-file'><tt>CL:RENAME-FILE</tt></a>. For
  107. atomic operation, both files should be on the same filesystem.
  108. </blockquote>
  109. <div class='item'>
  110. <div class='type'><a name='add-record'>[Function]</a></div>
  111. <div class='signature'>
  112. <code class='name'>add-record</code>
  113. <span class='args'>
  114. <var>key</var> <var>value</var> <var>cdb</var>
  115. </span>
  116. <span class='result'>=> |</span>
  117. </div>
  118. <blockquote class='description'>
  119. <p>Adds a record for <var>key</var> and <var>value</var>
  120. to <var>cdb</var>, which should be a cdb writer object created in
  121. the dynamic scope
  122. of <a href='#with-output-to-cdb'><tt>WITH-OUTPUT-TO-CDB</tt></a>.
  123. <p><var>key</var> and <var>value</var> must both be vectors
  124. specialized to hold
  125. <tt>(unsigned-byte 8)</tt> data.
  126. </blockquote>
  127. </div>
  128. <div class='item'>
  129. <div class='type'><a name='lookup'>[Function]</a></div>
  130. <div class='signature'>
  131. <code class='name'>lookup</code>
  132. <span class='args'>
  133. <var>key</var> <var>cdb</var>
  134. </span>
  135. <span class='result'>=> value</span>
  136. </div>
  137. <blockquote class='description'>
  138. <p>Looks up <var>key</var> in <var>cdb</var> and returns its
  139. value, or nil if no record in <var>cdb</var> has the given key.
  140. <p><var>key</var> must be a specialized <tt>(unsigned-byte&nbsp;8)</tt>
  141. vector. If the value is not null, it will also be a
  142. specialized <tt>(unsigned-byte&nbsp;8)</tt> vector.
  143. <p><var>cdb</var> must be either a pathname, or an input stream
  144. of element-type <tt>(unsigned-byte&nbsp;8)</tt> that can be
  145. repositioned
  146. with <a href='http://l1sp.org/cl/file-position'><tt>CL:FILE-POSITION</tt></a>.
  147. </blockquote>
  148. </div>
  149. <div class='item'>
  150. <div class='type'><a name='map-cdb'>[Function]</a></div>
  151. <div class='signature'>
  152. <code class='name'>map-cdb</code>
  153. <span class='args'>
  154. <var>function</var> <var>cdb</var>
  155. </span>
  156. <span class='result'>=> |</span>
  157. </div>
  158. <blockquote class='description'>
  159. <p>For each record in <var>cdb</var>, <var>function</var> is called with
  160. two arguments, the key vector and the value vector,
  161. respectively.
  162. <p><var>cdb</var> must be either a pathname, or an input stream
  163. of element-type <tt>(unsigned-byte&nbsp;8)</tt> that can be
  164. repositioned
  165. with <a href='http://l1sp.org/cl/file-position'><tt>CL:FILE-POSITION</tt></a>.
  166. </blockquote>
  167. </div>
  168. <a name='feedback'><h2>Feedback</h2></a>
  169. <p>If you have any questions or comments about ZCDB, please email
  170. me, <a href='mailto:xach@xach.com'>Zach Beane</a>.
  171. <a name='license'><h2>License</h2></a>
  172. <p>Copyright &copy; 2010 Zachary Beane
  173. <p>Permission is hereby granted, free of charge, to any person obtaining a copy
  174. of this software and associated documentation files (the "Software"), to deal
  175. in the Software without restriction, including without limitation the rights
  176. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  177. copies of the Software, and to permit persons to whom the Software is
  178. furnished to do so, subject to the following conditions:
  179. <p>The above copyright notice and this permission notice shall be included in
  180. all copies or substantial portions of the Software.
  181. <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  182. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  183. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  184. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  185. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  186. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  187. THE SOFTWARE.
  188. <p><i>2010-09-21</i>
  189. <p class='copyright'>Copyright &copy; 2010 Zachary Beane, All Rights Reserved
  190. </div>