forked from propublica/daybreak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
363 lines (314 loc) · 16.9 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
<!doctype html>
<html>
<head>
<title>Daybreak</title>
<!--
^^ |
daybreak ^^ \ _ /
-= / \ =-
~^~ ^ ^~^~ ~^~ ~ ~^~~^~^-=~=~=-~^~^~^~
-->
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<style>
/* reset */
div, html, body {
margin: 0;
padding: 0;
border: 0;
vertical-align: baseline;
}
ul { list-style: none; padding-left: 10px;}
li { margin-bottom: 1em; }
/* text styles */
body {
font-family: "Helvetica Nueue", Helvetica, sans-serif;
font-size: 14px;
line-height: 1.7em;
margin-left: auto;
margin-right: auto;
width: 600px;
padding: 20px;
}
p, li {
width: 600px;
margin: 0px 0px 1em;
}
h1, h2, h3 {
text-rendering: optimizeLegibility;
margin-left: -5px;
}
h4 {
margin: 0px;
margin-top: 30px;
margin-left: -5px;
font-weight: normal;
}
h4 code {
padding: 4px;
background-color: #e6f3ff;
}
ol {
padding-left: 0px;
}
code, pre, tt { font-family: Monaco, monospace; font-size: 12px; }
tt { border:1px solid #efefef; padding: 2px;}
dd { margin-left: 0; }
dt { margin-left: 1em; }
a { color: black; }
a:hover { text-decoration: none; }
pre {
padding-left: 10px;
font-size: 12px;
border-left: 5px solid #efefef;
line-height: 1.3;
}
#logo {
border-left: 0px;
}
hr {
border: 0;
border-top: 1px solid #efefef;
height: 1px;
}
table {
border-collapse: collapse;
width: 100%;
margin-bottom: 1em;
}
table td, table th {
border: 1px solid #efefef;
margin: 0px 5px;
text-align: center;
}
table th {
width: 40%;
}
table th.crc {
width: 20%;
}
/* styles stolen from docco */
body .hll { background-color: #ffffcc }
body .c { color: #408080; font-style: italic } /* Comment */
body .err { border: 1px solid #FF0000 } /* Error */
body .k { color: #954121 } /* Keyword */
body .o { color: #666666 } /* Operator */
body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
body .cp { color: #BC7A00 } /* Comment.Preproc */
body .c1 { color: #408080; font-style: italic } /* Comment.Single */
body .cs { color: #408080; font-style: italic } /* Comment.Special */
body .gd { color: #A00000 } /* Generic.Deleted */
body .ge { font-style: italic } /* Generic.Emph */
body .gr { color: #FF0000 } /* Generic.Error */
body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
body .gi { color: #00A000 } /* Generic.Inserted */
body .go { color: #808080 } /* Generic.Output */
body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
body .gs { font-weight: bold } /* Generic.Strong */
body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
body .gt { color: #0040D0 } /* Generic.Traceback */
body .kc { color: #954121 } /* Keyword.Constant */
body .kd { color: #954121; font-weight: bold } /* Keyword.Declaration */
body .kn { color: #954121; font-weight: bold } /* Keyword.Namespace */
body .kp { color: #954121 } /* Keyword.Pseudo */
body .kr { color: #954121; font-weight: bold } /* Keyword.Reserved */
body .kt { color: #B00040 } /* Keyword.Type */
body .m { color: #666666 } /* Literal.Number */
body .s { color: #219161 } /* Literal.String */
body .na { color: #7D9029 } /* Name.Attribute */
body .nb { color: #954121 } /* Name.Builtin */
body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
body .no { color: #880000 } /* Name.Constant */
body .nd { color: #AA22FF } /* Name.Decorator */
body .ni { color: #999999; font-weight: bold } /* Name.Entity */
body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
body .nf { color: #0000FF } /* Name.Function */
body .nl { color: #A0A000 } /* Name.Label */
body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
body .nt { color: #954121; font-weight: bold } /* Name.Tag */
body .nv { color: #19469D } /* Name.Variable */
body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
body .w { color: #bbbbbb } /* Text.Whitespace */
body .mf { color: #666666 } /* Literal.Number.Float */
body .mh { color: #666666 } /* Literal.Number.Hex */
body .mi { color: #666666 } /* Literal.Number.Integer */
body .mo { color: #666666 } /* Literal.Number.Oct */
body .sb { color: #219161 } /* Literal.String.Backtick */
body .sc { color: #219161 } /* Literal.String.Char */
body .sd { color: #219161; font-style: italic } /* Literal.String.Doc */
body .s2 { color: #219161 } /* Literal.String.Double */
body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
body .sh { color: #219161 } /* Literal.String.Heredoc */
body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
body .sx { color: #954121 } /* Literal.String.Other */
body .sr { color: #BB6688 } /* Literal.String.Regex */
body .s1 { color: #219161 } /* Literal.String.Single */
body .ss { color: #19469D } /* Literal.String.Symbol */
body .bp { color: #954121 } /* Name.Builtin.Pseudo */
body .vc { color: #19469D } /* Name.Variable.Class */
body .vg { color: #19469D } /* Name.Variable.Global */
body .vi { color: #19469D } /* Name.Variable.Instance */
body .il { color: #666666 } /* Literal.Number.Integer.Long */
</style>
</head>
<body>
<p>
<pre id="logo">
^^ |
daybreak ^^ \ _ /
-= / \ =-
~^~ ^ ^~^~ ~^~ ~ ~^~~^~^-=~=~=-~^~^~^~
</pre>
<p>
Daybreak is a simple key value store for ruby. It has user defined persistence,
and all data is stored in a table in memory, so ruby niceties are available.
Daybreak's is faster than <tt>pstore</tt> and more convenient than
<tt>dbm</tt>.
</p>
<p>
The souce is on <a href="http://github.com/propublica/daybreak">Github</a> and you can install it with:
</p>
<pre>
$ gem install daybreak
</pre>
<p>(v0.0.1) | <a href="http://rdoc.info/docs/daybreak">API Docs</a> | <a href="http://github.com/propublica/daybreak/issues">Issue Tracker</a></p>
<h2>Overview</h2>
<p>
Daybreak stores data in an append-only file, and values inserted into the
database are marshalled ruby objects. It includes <tt>Enumerable</tt>
for functional methods like <tt>map</tt> and <tt>reduce</tt> and emulates
the interface of a simple ruby hash. Here is the basic api:
</p>
<code>
<div class="highlight"><pre> <span class="nb">require</span> <span class="s1">'daybreak'</span>
<span class="n">db</span> <span class="o">=</span> <span class="no">Daybreak</span><span class="o">::</span><span class="no">DB</span><span class="o">.</span><span class="n">new</span> <span class="s2">"example.db"</span>
<span class="c1"># set the value of a key</span>
<span class="n">db</span><span class="o">[</span><span class="s1">'foo'</span><span class="o">]</span> <span class="o">=</span> <span class="mi">2</span>
<span class="c1"># set the value of a key and flush the change to disk</span>
<span class="n">db</span><span class="o">.</span><span class="n">set!</span> <span class="s1">'bar'</span><span class="p">,</span> <span class="mi">2</span>
<span class="c1"># all keys are cast to strings via #to_s</span>
<span class="n">db</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span> <span class="o">=</span> <span class="mi">2</span>
<span class="n">db</span><span class="o">.</span><span class="n">keys</span><span class="o">.</span><span class="n">include?</span> <span class="mi">1</span> <span class="c1"># => false</span>
<span class="n">db</span><span class="o">.</span><span class="n">keys</span><span class="o">.</span><span class="n">include?</span> <span class="s1">'1'</span> <span class="c1"># => true</span>
<span class="c1"># ensure changes are sent to disk</span>
<span class="n">db</span><span class="o">.</span><span class="n">flush!</span>
<span class="c1"># open up another db client</span>
<span class="n">db2</span> <span class="o">=</span> <span class="no">Daybreak</span><span class="o">::</span><span class="no">DB</span><span class="o">.</span><span class="n">new</span> <span class="s2">"example.db"</span>
<span class="n">db2</span><span class="o">[</span><span class="s1">'foo'</span><span class="o">]</span> <span class="o">=</span> <span class="mi">3</span>
<span class="c1"># Ruby objects work too</span>
<span class="n">db2</span><span class="o">[</span><span class="s1">'baz'</span><span class="o">]</span> <span class="o">=</span> <span class="p">{</span><span class="ss">:one</span> <span class="o">=></span> <span class="mi">1</span><span class="p">}</span>
<span class="n">db2</span><span class="o">.</span><span class="n">flush!</span>
<span class="c1"># Reread the changed file in the first db</span>
<span class="n">db</span><span class="o">.</span><span class="n">read!</span>
<span class="nb">p</span> <span class="n">db</span><span class="o">[</span><span class="s1">'foo'</span><span class="o">]</span> <span class="c1">#=> 3</span>
<span class="nb">p</span> <span class="n">db</span><span class="o">[</span><span class="s1">'baz'</span><span class="o">]</span> <span class="c1">#=> {:one => 1}</span>
<span class="c1"># Enumerable works too!</span>
<span class="mi">1000</span><span class="o">.</span><span class="n">times</span> <span class="p">{</span><span class="o">|</span><span class="n">i</span><span class="o">|</span> <span class="n">db</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="n">i</span> <span class="p">}</span>
<span class="nb">p</span> <span class="n">db</span><span class="o">.</span><span class="n">reduce</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span> <span class="p">{</span><span class="o">|</span><span class="n">m</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="o">|</span> <span class="n">m</span> <span class="o">+</span> <span class="n">k</span><span class="o">.</span><span class="n">last</span> <span class="p">}</span> <span class="c1"># => 499500</span>
<span class="c1"># Compaction is always a good idea. It will cut down on the size of the Database</span>
<span class="n">db</span><span class="o">.</span><span class="n">compact!</span>
<span class="nb">p</span> <span class="n">db</span><span class="o">[</span><span class="s1">'foo'</span><span class="o">]</span> <span class="c1">#=> 1</span>
<span class="n">db2</span><span class="o">.</span><span class="n">read!</span>
<span class="nb">p</span> <span class="n">db2</span><span class="o">[</span><span class="s1">'foo'</span><span class="o">]</span> <span class="c1">#=> 1</span>
<span class="c1"># DBs can have default values</span>
<span class="n">db3</span> <span class="o">=</span> <span class="no">Daybreak</span><span class="o">::</span><span class="no">DB</span><span class="o">.</span><span class="n">new</span> <span class="s2">"example2.db"</span><span class="p">,</span> <span class="s1">'hello!'</span>
<span class="n">db3</span><span class="o">[</span><span class="s1">'bingo'</span><span class="o">]</span> <span class="c1">#=> hello!</span>
<span class="c1"># close the databases</span>
<span class="n">db</span><span class="o">.</span><span class="n">close!</span>
<span class="n">db2</span><span class="o">.</span><span class="n">close!</span>
<span class="n">db3</span><span class="o">.</span><span class="n">close!</span>
</pre></div>
</code>
<p>
<tt>Daybreak::DB</tt> can be subclassed if you want a different serialization
strategy (for example, JSON). If so override <tt>DB#parse</tt> and <tt>DB#serialize</tt>
</p>
<h2>Architecture</h2>
<p>
When a Daybreak database is opened it reads the append only file and mirrors
the data in an in memory hash table for fast reads.
</p>
<p>
Writes to a Daybreak database are asynchronous and each write is queued until
the file is ready for writing, via
<a href="http://www.ruby-doc.org/core-1.9.3/IO.html#method-c-select"><tt>IO.select</tt></a>.
If you want to commit immediately to the file call <tt>flush!</tt> after a
write.
</p>
<p>
Writes with duplicate keys are simply appended to the end of the file.
From time to time you will want to run <tt>compact!</tt> which will remove
old commits from the file and create a smaller logfile. This will shrink the
space necessary to store the data on disk.
</p>
<h2>File Format</h2>
<p>
Daybreak stores its data in the simplest file format imaginable. Each
Daybreak file is an append only log consisting of alternating key and value cells.
Every key-value pair also has an associated 32 bit CRC field to protect against bad data.
Each cell starts with a 32 bit unsigend big-endian integer representing the
length of the value stored in the cell. Here is how a database of one record might
look like:
</p>
<table>
<tr>
<th class="key" colspan="2">Key</th>
<th class="key" colspan="2">Value</th>
<th class="key">CRC</th>
</tr>
<tr>
<td>Encoded Length</td>
<td>String Key</td>
<td>Encoded Length</td>
<td>Marshalled Value</td>
<td rowspan="2"><small>CRC32(key, value)</small></td>
</tr>
<tr>
<td>(...)0000101</td>
<td>hello</td>
<td>(...)0001010</td>
<td><marshalled value></td>
</tr>
</table>
<p>
These values are all read into an in memory hash table and commits to the
database are queued for writing when the OS signals the file is ready.
A reminder: pass <tt>true</tt> as the last argument to <tt>set</tt>, or
call <tt>flush!</tt> if you want commits to block and be written to
the filesystem.
</p>
<h2>In the Wild</h2>
<ul>
<li>
The <a href="http://projects.propublica.org">Message Machine</a> uses
Daybreak to store word frequencies and indexes for search and document
clustering.
</li>
</ul>
<h2>Change Log</h2>
<dl>
<dd><b>0.0.1</b></dd>
<dt>Initial release.</dt>
</dl>
<h2>License</h2>
<pre>
Copyright (c) 2012, ProPublica
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
</pre>
<p><em>Daybreak is a project of ProPublica.</em></p>
</body>
</html>