forked from careo/tokyocabinet-ruby
-
Notifications
You must be signed in to change notification settings - Fork 1
/
overview.rd
309 lines (243 loc) · 8.72 KB
/
overview.rd
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
= Ruby Binding of Tokyo Cabinet
Tokyo Cabinet: a modern implementation of DBM
== INTRODUCTION
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.
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.
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.
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.
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.
=== Setting
Install the latest version of Tokyo Cabinet beforehand and get the package of the Ruby binding of Tokyo Cabinet.
Enter the directory of the extracted package then perform installation.
ruby extconf.rb
make
su
make install
The package `tokyocabinet' should be loaded in each source file of application programs.
require 'tokyocabinet'
All symbols of Tokyo Cabinet are defined in the module `TokyoCabinet'. You can access them without any prefix by including the module.
include TokyoCabinet
= EXAMPLE
The following code is an example to use a hash database.
require 'tokyocabinet'
include TokyoCabinet
# create the object
hdb = HDB::new
# open the database
if !hdb.open("casket.tch", HDB::OWRITER | HDB::OCREAT)
ecode = hdb.ecode
STDERR.printf("open error: %s\n", hdb.errmsg(ecode))
end
# store records
if !hdb.put("foo", "hop") ||
!hdb.put("bar", "step") ||
!hdb.put("baz", "jump")
ecode = hdb.ecode
STDERR.printf("put error: %s\n", hdb.errmsg(ecode))
end
# retrieve records
value = hdb.get("foo")
if value
printf("%s\n", value)
else
ecode = hdb.ecode
STDERR.printf("get error: %s\n", hdb.errmsg(ecode))
end
# traverse records
hdb.iterinit
while key = hdb.iternext
value = hdb.get(key)
if value
printf("%s:%s\n", key, value)
end
end
# hash-like usage
hdb["quux"] = "touchdown"
printf("%s\n", hdb["quux"])
hdb.each do |key, value|
printf("%s:%s\n", key, value)
end
# close the database
if !hdb.close
ecode = hdb.ecode
STDERR.printf("close error: %s\n", hdb.errmsg(ecode))
end
The following code is an example to use a B+ tree database.
require 'tokyocabinet'
include TokyoCabinet
# create the object
bdb = BDB::new
# open the database
if !bdb.open("casket.tcb", BDB::OWRITER | BDB::OCREAT)
ecode = bdb.ecode
STDERR.printf("open error: %s\n", bdb.errmsg(ecode))
end
# store records
if !bdb.put("foo", "hop") ||
!bdb.put("bar", "step") ||
!bdb.put("baz", "jump")
ecode = bdb.ecode
STDERR.printf("put error: %s\n", bdb.errmsg(ecode))
end
# retrieve records
value = bdb.get("foo")
if value
printf("%s\n", value)
else
ecode = bdb.ecode
STDERR.printf("get error: %s\n", bdb.errmsg(ecode))
end
# traverse records
cur = BDBCUR::new(bdb)
cur.first
while key = cur.key
value = cur.val
if value
printf("%s:%s\n", key, value)
end
cur.next
end
# hash-like usage
bdb["quux"] = "touchdown"
printf("%s\n", bdb["quux"])
bdb.each do |key, value|
printf("%s:%s\n", key, value)
end
# close the database
if !bdb.close
ecode = bdb.ecode
STDERR.printf("close error: %s\n", bdb.errmsg(ecode))
end
The following code is an example to use a fixed-length database.
require 'tokyocabinet'
include TokyoCabinet
# create the object
fdb = FDB::new
# open the database
if !fdb.open("casket.tcf", FDB::OWRITER | FDB::OCREAT)
ecode = fdb.ecode
STDERR.printf("open error: %s\n", fdb.errmsg(ecode))
end
# store records
if !fdb.put(1, "one") ||
!fdb.put(12, "twelve") ||
!fdb.put(144, "one forty four")
ecode = fdb.ecode
STDERR.printf("put error: %s\n", fdb.errmsg(ecode))
end
# retrieve records
value = fdb.get(1)
if value
printf("%s\n", value)
else
ecode = fdb.ecode
STDERR.printf("get error: %s\n", fdb.errmsg(ecode))
end
# traverse records
fdb.iterinit
while key = fdb.iternext
value = fdb.get(key)
if value
printf("%s:%s\n", key, value)
end
end
# hash-like usage
fdb[1728] = "seventeen twenty eight"
printf("%s\n", fdb[1728])
fdb.each do |key, value|
printf("%s:%s\n", key, value)
end
# close the database
if !fdb.close
ecode = fdb.ecode
STDERR.printf("close error: %s\n", fdb.errmsg(ecode))
end
The following code is an example to use a table database.
require 'tokyocabinet'
include TokyoCabinet
# create the object
tdb = TDB::new
# open the database
if !tdb.open("casket.tct", TDB::OWRITER | TDB::OCREAT)
ecode = tdb.ecode
STDERR.printf("open error: %s\n", tdb.errmsg(ecode))
end
# store a record
pkey = tdb.genuid
cols = { "name" => "mikio", "age" => "30", "lang" => "ja,en,c" }
if !tdb.put(pkey, cols)
ecode = tdb.ecode
STDERR.printf("get error: %s\n", tdb.errmsg(ecode))
end
# store another record
cols = { "name" => "falcon", "age" => "31", "lang" => "ja", "skill" => "cook,blog" }
if !tdb.put("x12345", cols)
ecode = tdb.ecode
STDERR.printf("get error: %s\n", tdb.errmsg(ecode))
end
# search for records
qry = TDBQRY::new(tdb)
qry.addcond("age", TDBQRY::QCNUMGE, "20")
qry.addcond("lang", TDBQRY::QCSTROR, "ja,en")
qry.setorder("name", TDBQRY::QOSTRASC)
qry.setlimit(10)
res = qry.search
res.each do |rkey|
rcols = tdb.get(rkey)
printf("name:%s\n", rcols["name"])
end
# hash-like usage
tdb["joker"] = { "name" => "ozma", "lang" => "en", "skill" => "song,dance" }
printf("%s\n", tdb["joker"]["name"])
tdb.each do |key, value|
printf("%s:%s\n", key, value["name"])
end
# close the database
if !tdb.close
ecode = tdb.ecode
STDERR.printf("close error: %s\n", tdb.errmsg(ecode))
end
The following code is an example to use an abstract database.
require 'tokyocabinet'
include TokyoCabinet
# create the object
adb = ADB::new
# open the database
if !adb.open("casket.tch")
STDERR.printf("open error\n")
end
# store records
if !adb.put("foo", "hop") ||
!adb.put("bar", "step") ||
!adb.put("baz", "jump")
STDERR.printf("put error\n")
end
# retrieve records
value = adb.get("foo")
if value
printf("%s\n", value)
else
STDERR.printf("get error\n")
end
# traverse records
adb.iterinit
while key = adb.iternext
value = adb.get(key)
if value
printf("%s:%s\n", key, value)
end
end
# hash-like usage
adb["quux"] = "touchdown"
printf("%s\n", adb["quux"])
adb.each do |key, value|
printf("%s:%s\n", key, value)
end
# close the database
if !adb.close
STDERR.printf("close error\n")
end
== LICENSE
Copyright (C) 2006-2010 FAL Labs
All rights reserved.
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.