File: TokyoCabinet.html

package info (click to toggle)
ruby-tokyocabinet 1.31-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,628 kB
  • sloc: ruby: 3,594; ansic: 3,387; sh: 29; makefile: 2
file content (505 lines) | stat: -rw-r--r-- 13,443 bytes parent folder | download | duplicates (5)
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>Module: TokyoCabinet [Tokyo Cabinet]</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta http-equiv="Content-Script-Type" content="text/javascript" />
  <link rel="stylesheet" href=".././rdoc-style.css" type="text/css" media="screen" />
  <script type="text/javascript">
  // <![CDATA[

  function popupCode( url ) {
    window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
  }

  function toggleCode( id ) {
    if ( document.getElementById )
      elem = document.getElementById( id );
    else if ( document.all )
      elem = eval( "document.all." + id );
    else
      return false;

    elemStyle = elem.style;

    if ( elemStyle.display != "block" ) {
      elemStyle.display = "block"
    } else {
      elemStyle.display = "none"
    }

    return true;
  }

  // Make codeblocks hidden by default
  document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }<\/style>" )

  // ]]>
  </script>

</head>
<body>


    <div id="classHeader">
        <table class="header-table">
        <tr class="top-aligned-row">
          <td><strong>Module</strong></td>
          <td class="class-name-in-header">TokyoCabinet</td>
        </tr>
        <tr class="top-aligned-row">
            <td><strong>In:</strong></td>
            <td>


                <a href="../files/tokyocabinet-doc_rb.html">

                tokyocabinet-doc.rb

                </a>


        <br />

            </td>
        </tr>


        </table>
    </div>
  <!-- banner header -->

  <div id="bodyContent">

  <div id="contextContent">

    <div id="description">
      <h1>Ruby Binding of Tokyo Cabinet</h1>
<p>
Tokyo Cabinet: a modern implementation of DBM
</p>
<h2>INTRODUCTION</h2>
<p>
Tokyo Cabinet is a library of routines for managing a database. The
database is a simple data file containing records, each is a pair of a key
and a value. Every key and value is serial bytes with variable length. Both
binary data and character string can be used as a key and a value. There is
neither concept of data tables nor data types. Records are organized in
hash table, B+ tree, or fixed-length array.
</p>
<p>
As for database of hash table, each key must be unique within a database,
so it is impossible to store two or more records with a key overlaps. The
following access methods are provided to the database: storing a record
with a key and a value, deleting a record by a key, retrieving a record by
a key. Moreover, traversal access to every key are provided, although the
order is arbitrary. These access methods are similar to ones of DBM (or its
followers: NDBM and GDBM) library defined in the UNIX standard. Tokyo
Cabinet is an alternative for DBM because of its higher performance.
</p>
<p>
As for database of B+ tree, records whose keys are duplicated can be
stored. Access methods of storing, deleting, and retrieving are provided as
with the database of hash table. Records are stored in order by a
comparison function assigned by a user. It is possible to access each
record with the cursor in ascending or descending order. According to this
mechanism, forward matching search for strings and range search for
integers are realized.
</p>
<p>
As for database of fixed-length array, records are stored with unique
natural numbers. It is impossible to store two or more records with a key
overlaps. Moreover, the length of each record is limited by the specified
length. Provided operations are the same as ones of hash database.
</p>
<p>
Table database is also provided as a variant of hash database. Each record
is identified by the primary key and has a set of named columns. Although
there is no concept of data schema, it is possible to search for records
with complex conditions efficiently by using indices of arbitrary columns.
</p>
<h3>Setting</h3>
<p>
Install the latest version of Tokyo Cabinet beforehand and get the package
of the Ruby binding of Tokyo Cabinet.
</p>
<p>
Enter the directory of the extracted package then perform installation.
</p>
<pre>
 ruby extconf.rb
 make
 su
 make install
</pre>
<p>
The package `tokyocabinet' should be loaded in each source file of
application programs.
</p>
<pre>
 require 'tokyocabinet'
</pre>
<p>
All symbols of Tokyo Cabinet are defined in the module
`TokyoCabinet'. You can access them without any prefix by including
the module.
</p>
<pre>
 include TokyoCabinet
</pre>
<h1>EXAMPLE</h1>
<p>
The following code is an example to use a hash database.
</p>
<pre>
 require 'tokyocabinet'
 include TokyoCabinet

 # create the object
 hdb = HDB::new

 # open the database
 if !hdb.open(&quot;casket.tch&quot;, HDB::OWRITER | HDB::OCREAT)
   ecode = hdb.ecode
   STDERR.printf(&quot;open error: %s\n&quot;, hdb.errmsg(ecode))
 end

 # store records
 if !hdb.put(&quot;foo&quot;, &quot;hop&quot;) ||
     !hdb.put(&quot;bar&quot;, &quot;step&quot;) ||
     !hdb.put(&quot;baz&quot;, &quot;jump&quot;)
   ecode = hdb.ecode
   STDERR.printf(&quot;put error: %s\n&quot;, hdb.errmsg(ecode))
 end

 # retrieve records
 value = hdb.get(&quot;foo&quot;)
 if value
   printf(&quot;%s\n&quot;, value)
 else
   ecode = hdb.ecode
   STDERR.printf(&quot;get error: %s\n&quot;, hdb.errmsg(ecode))
 end

 # traverse records
 hdb.iterinit
 while key = hdb.iternext
   value = hdb.get(key)
   if value
     printf(&quot;%s:%s\n&quot;, key, value)
   end
 end

 # hash-like usage
 hdb[&quot;quux&quot;] = &quot;touchdown&quot;
 printf(&quot;%s\n&quot;, hdb[&quot;quux&quot;])
 hdb.each do |key, value|
   printf(&quot;%s:%s\n&quot;, key, value)
 end

 # close the database
 if !hdb.close
   ecode = hdb.ecode
   STDERR.printf(&quot;close error: %s\n&quot;, hdb.errmsg(ecode))
 end
</pre>
<p>
The following code is an example to use a B+ tree database.
</p>
<pre>
 require 'tokyocabinet'
 include TokyoCabinet

 # create the object
 bdb = BDB::new

 # open the database
 if !bdb.open(&quot;casket.tcb&quot;, BDB::OWRITER | BDB::OCREAT)
   ecode = bdb.ecode
   STDERR.printf(&quot;open error: %s\n&quot;, bdb.errmsg(ecode))
 end

 # store records
 if !bdb.put(&quot;foo&quot;, &quot;hop&quot;) ||
     !bdb.put(&quot;bar&quot;, &quot;step&quot;) ||
     !bdb.put(&quot;baz&quot;, &quot;jump&quot;)
   ecode = bdb.ecode
   STDERR.printf(&quot;put error: %s\n&quot;, bdb.errmsg(ecode))
 end

 # retrieve records
 value = bdb.get(&quot;foo&quot;)
 if value
   printf(&quot;%s\n&quot;, value)
 else
   ecode = bdb.ecode
   STDERR.printf(&quot;get error: %s\n&quot;, bdb.errmsg(ecode))
 end

 # traverse records
 cur = BDBCUR::new(bdb)
 cur.first
 while key = cur.key
   value = cur.val
   if value
     printf(&quot;%s:%s\n&quot;, key, value)
   end
   cur.next
 end

 # hash-like usage
 bdb[&quot;quux&quot;] = &quot;touchdown&quot;
 printf(&quot;%s\n&quot;, bdb[&quot;quux&quot;])
 bdb.each do |key, value|
   printf(&quot;%s:%s\n&quot;, key, value)
 end

 # close the database
 if !bdb.close
   ecode = bdb.ecode
   STDERR.printf(&quot;close error: %s\n&quot;, bdb.errmsg(ecode))
 end
</pre>
<p>
The following code is an example to use a fixed-length database.
</p>
<pre>
 require 'tokyocabinet'
 include TokyoCabinet

 # create the object
 fdb = FDB::new

 # open the database
 if !fdb.open(&quot;casket.tcf&quot;, FDB::OWRITER | FDB::OCREAT)
   ecode = fdb.ecode
   STDERR.printf(&quot;open error: %s\n&quot;, fdb.errmsg(ecode))
 end

 # store records
 if !fdb.put(1, &quot;one&quot;) ||
     !fdb.put(12, &quot;twelve&quot;) ||
     !fdb.put(144, &quot;one forty four&quot;)
   ecode = fdb.ecode
   STDERR.printf(&quot;put error: %s\n&quot;, fdb.errmsg(ecode))
 end

 # retrieve records
 value = fdb.get(1)
 if value
   printf(&quot;%s\n&quot;, value)
 else
   ecode = fdb.ecode
   STDERR.printf(&quot;get error: %s\n&quot;, fdb.errmsg(ecode))
 end

 # traverse records
 fdb.iterinit
 while key = fdb.iternext
   value = fdb.get(key)
   if value
     printf(&quot;%s:%s\n&quot;, key, value)
   end
 end

 # hash-like usage
 fdb[1728] = &quot;seventeen twenty eight&quot;
 printf(&quot;%s\n&quot;, fdb[1728])
 fdb.each do |key, value|
   printf(&quot;%s:%s\n&quot;, key, value)
 end

 # close the database
 if !fdb.close
   ecode = fdb.ecode
   STDERR.printf(&quot;close error: %s\n&quot;, fdb.errmsg(ecode))
 end
</pre>
<p>
The following code is an example to use a table database.
</p>
<pre>
 require 'tokyocabinet'
 include TokyoCabinet

 # create the object
 tdb = TDB::new

 # open the database
 if !tdb.open(&quot;casket.tct&quot;, TDB::OWRITER | TDB::OCREAT)
   ecode = tdb.ecode
   STDERR.printf(&quot;open error: %s\n&quot;, tdb.errmsg(ecode))
 end

 # store a record
 pkey = tdb.genuid
 cols = { &quot;name&quot; =&gt; &quot;mikio&quot;, &quot;age&quot; =&gt; &quot;30&quot;, &quot;lang&quot; =&gt; &quot;ja,en,c&quot; }
 if !tdb.put(pkey, cols)
   ecode = tdb.ecode
   STDERR.printf(&quot;get error: %s\n&quot;, tdb.errmsg(ecode))
 end

 # store another record
 cols = { &quot;name&quot; =&gt; &quot;falcon&quot;, &quot;age&quot; =&gt; &quot;31&quot;, &quot;lang&quot; =&gt; &quot;ja&quot;, &quot;skill&quot; =&gt; &quot;cook,blog&quot; }
 if !tdb.put(&quot;x12345&quot;, cols)
   ecode = tdb.ecode
   STDERR.printf(&quot;get error: %s\n&quot;, tdb.errmsg(ecode))
 end

 # search for records
 qry = TDBQRY::new(tdb)
 qry.addcond(&quot;age&quot;, TDBQRY::QCNUMGE, &quot;20&quot;)
 qry.addcond(&quot;lang&quot;, TDBQRY::QCSTROR, &quot;ja,en&quot;)
 qry.setorder(&quot;name&quot;, TDBQRY::QOSTRASC)
 qry.setlimit(10)
 res = qry.search
 res.each do |rkey|
   rcols = tdb.get(rkey)
   printf(&quot;name:%s\n&quot;, rcols[&quot;name&quot;])
 end

 # hash-like usage
 tdb[&quot;joker&quot;] = { &quot;name&quot; =&gt; &quot;ozma&quot;, &quot;lang&quot; =&gt; &quot;en&quot;, &quot;skill&quot; =&gt; &quot;song,dance&quot; }
 printf(&quot;%s\n&quot;, tdb[&quot;joker&quot;][&quot;name&quot;])
 tdb.each do |key, value|
   printf(&quot;%s:%s\n&quot;, key, value[&quot;name&quot;])
 end

 # close the database
 if !tdb.close
   ecode = tdb.ecode
   STDERR.printf(&quot;close error: %s\n&quot;, tdb.errmsg(ecode))
 end
</pre>
<p>
The following code is an example to use an abstract database.
</p>
<pre>
 require 'tokyocabinet'
 include TokyoCabinet

 # create the object
 adb = ADB::new

 # open the database
 if !adb.open(&quot;casket.tch&quot;)
   STDERR.printf(&quot;open error\n&quot;)
 end

 # store records
 if !adb.put(&quot;foo&quot;, &quot;hop&quot;) ||
     !adb.put(&quot;bar&quot;, &quot;step&quot;) ||
     !adb.put(&quot;baz&quot;, &quot;jump&quot;)
   STDERR.printf(&quot;put error\n&quot;)
 end

 # retrieve records
 value = adb.get(&quot;foo&quot;)
 if value
   printf(&quot;%s\n&quot;, value)
 else
   STDERR.printf(&quot;get error\n&quot;)
 end

 # traverse records
 adb.iterinit
 while key = adb.iternext
   value = adb.get(key)
   if value
     printf(&quot;%s:%s\n&quot;, key, value)
   end
 end

 # hash-like usage
 adb[&quot;quux&quot;] = &quot;touchdown&quot;
 printf(&quot;%s\n&quot;, adb[&quot;quux&quot;])
 adb.each do |key, value|
   printf(&quot;%s:%s\n&quot;, key, value)
 end

 # close the database
 if !adb.close
   STDERR.printf(&quot;close error\n&quot;)
 end
</pre>
<h2>LICENSE</h2>
<pre>
 Copyright (C) 2006-2010 FAL Labs
 All rights reserved.
</pre>
<p>
Tokyo Cabinet is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License or any
later version. Tokyo Cabinet is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
General Public License for more details. You should have received a copy of
the GNU Lesser General Public License along with Tokyo Cabinet; if not,
write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA.
</p>

    </div>

   </div>


  </div>

    <!-- if includes -->

    <div id="section">

    <div id="class-list">
      <h3 class="section-bar">Classes and Modules</h3>

      Class <a href="TokyoCabinet/ADB.html" class="link">TokyoCabinet::ADB</a><br />
Class <a href="TokyoCabinet/BDB.html" class="link">TokyoCabinet::BDB</a><br />
Class <a href="TokyoCabinet/BDBCUR.html" class="link">TokyoCabinet::BDBCUR</a><br />
Class <a href="TokyoCabinet/FDB.html" class="link">TokyoCabinet::FDB</a><br />
Class <a href="TokyoCabinet/HDB.html" class="link">TokyoCabinet::HDB</a><br />
Class <a href="TokyoCabinet/TDB.html" class="link">TokyoCabinet::TDB</a><br />
Class <a href="TokyoCabinet/TDBQRY.html" class="link">TokyoCabinet::TDBQRY</a><br />

    </div>

    <div id="constants-list">
      <h3 class="section-bar">Constants</h3>

      <div class="name-list">
        <table summary="Constants">

        <tr class="top-aligned-row context-row">
          <td class="context-item-name">VERSION</td>
          <td>=</td>
          <td class="context-item-value">&quot;x.y.z&quot;</td>

          <td>&nbsp;</td>
          <td class="context-item-desc">
the version information

</td>

        </tr>

        </table>
      </div>
    </div>




    <!-- if method_list -->




  </div>

<div id="validator-badges">
</div>

</body>
</html>