tcatest.rb

Path: tcatest.rb
Last Update: Sun Sep 19 11:46:52 +0200 2010
dot/f_0.png

Required files

./tokyocabinet  

Methods

eprint   main   procmisc   procread   procremove   procwrite   runmisc   runread   runremove   runwrite   usage  

Included Modules

TokyoCabinet

Public Instance methods

print error message of abstract database

[Source]

    # File tcatest.rb, line 57
57: def eprint(adb, func)
58:   path = adb.path
59:   STDERR.printf("%s: %s: %s: error\n", $progname, path ? path : "-", func)
60: end

main routine

[Source]

    # File tcatest.rb, line 24
24: def main
25:   ARGV.length >= 1 || usage
26:   if ARGV[0] == "write"
27:     rv = runwrite
28:   elsif ARGV[0] == "read"
29:     rv = runread
30:   elsif ARGV[0] == "remove"
31:     rv = runremove
32:   elsif ARGV[0] == "misc"
33:     rv = runmisc
34:   else
35:     usage
36:   end
37:   GC.start
38:   return rv
39: end

perform misc command

[Source]

     # File tcatest.rb, line 260
260: def procmisc(name, rnum)
261:   printf("<Miscellaneous Test>\n  name=%s  rnum=%d\n\n", name, rnum)
262:   err = false
263:   stime = Time.now
264:   adb = ADB::new
265:   if !adb.open(name)
266:     eprint(adb, "open")
267:     err = true
268:   end
269:   printf("writing:\n")
270:   for i in 1..rnum
271:     buf = sprintf("%08d", i)
272:     if !adb.put(buf, buf)
273:       eprint(adb, "put")
274:       err = true
275:       break
276:     end
277:     if rnum > 250 && i % (rnum / 250) == 0
278:       print('.')
279:       if i == rnum || i % (rnum / 10) == 0
280:         printf(" (%08d)\n", i)
281:       end
282:     end
283:   end
284:   printf("reading:\n")
285:   for i in 1..rnum
286:     buf = sprintf("%08d", i)
287:     if !adb.get(buf)
288:       eprint(adb, "get")
289:       err = true
290:       break
291:     end
292:     if rnum > 250 && i % (rnum / 250) == 0
293:       print('.')
294:       if i == rnum || i % (rnum / 10) == 0
295:         printf(" (%08d)\n", i)
296:       end
297:     end
298:   end
299:   printf("removing:\n")
300:   for i in 1..rnum
301:     buf = sprintf("%08d", i)
302:     if rand(2) == 0 && !adb.out(buf)
303:       eprint(adb, "out")
304:       err = true
305:       break
306:     end
307:     if rnum > 250 && i % (rnum / 250) == 0
308:       print('.')
309:       if i == rnum || i % (rnum / 10) == 0
310:         printf(" (%08d)\n", i)
311:       end
312:     end
313:   end
314:   printf("checking iterator:\n")
315:   if !adb.iterinit
316:     eprint(adb, "iterinit")
317:     err = true
318:   end
319:   inum = 0
320:   while key = adb.iternext
321:     value = adb.get(key)
322:     if !value
323:       eprint(adb, "get")
324:       err = true
325:     end
326:     if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0
327:       print('.')
328:       if inum == rnum || inum % (rnum / 10) == 0
329:         printf(" (%08d)\n", inum)
330:       end
331:     end
332:     inum += 1
333:   end
334:   printf(" (%08d)\n", inum) if rnum > 250
335:   if inum != adb.rnum
336:     eprint(adb, "(validation)")
337:     err = true
338:   end
339:   keys = adb.fwmkeys("0", 10)
340:   if adb.rnum >= 10 && keys.size != 10
341:     eprint(adb, "fwmkeys")
342:     err = true
343:   end
344:   printf("checking counting:\n")
345:   for i in 1..rnum
346:     buf = sprintf("[%d]", rand(rnum))
347:     if rand(2) == 0
348:       adb.addint(buf, 1)
349:     else
350:       adb.adddouble(buf, 1)
351:     end
352:     if i > 0 && rnum > 250 && i % (rnum / 250) == 0
353:       print('.')
354:       if i == rnum || i % (rnum / 10) == 0
355:         printf(" (%08d)\n", i)
356:       end
357:     end
358:   end
359:   printf("checking versatile functions:\n")
360:   for i in 1..rnum
361:     rnd = rand(3)
362:     if rnd == 0
363:       name = "putlist"
364:     elsif rnd == 1
365:       name = "outlist"
366:     else
367:       name = "getlist"
368:     end
369:     if !adb.misc(name, [rand(rnum), rand(rnum)])
370:       eprint(adb, "(validation)")
371:       err = true
372:     end
373:     if i > 0 && rnum > 250 && i % (rnum / 250) == 0
374:       print('.')
375:       if i == rnum || i % (rnum / 10) == 0
376:         printf(" (%08d)\n", i)
377:       end
378:     end
379:   end
380:   if !adb.sync
381:     eprint(adb, "sync")
382:     err = true
383:   end
384:   if !adb.optimize
385:     eprint(adb, "optimize")
386:     err = true
387:   end
388:   npath = adb.path + "-tmp"
389:   if !adb.copy(npath)
390:     eprint(adb, "copy")
391:     err = true
392:   end
393:   File::unlink(npath)
394:   if !adb.vanish
395:     eprint(adb, "vanish")
396:     err = true
397:   end
398:   printf("checking transaction commit:\n")
399:   if !adb.tranbegin
400:     eprint(adb, "tranbegin")
401:     err = true
402:   end
403:   for i in 1..rnum
404:     buf = sprintf("%d", rand(rnum))
405:     if rand(2) == 0
406:       if !adb.putcat(buf, buf)
407:         eprint(adb, "putcat")
408:         err = true
409:         break
410:       end
411:     else
412:       adb.out(buf)
413:     end
414:     if rnum > 250 && i % (rnum / 250) == 0
415:       print('.')
416:       if i == rnum || i % (rnum / 10) == 0
417:         printf(" (%08d)\n", i)
418:       end
419:     end
420:   end
421:   if !adb.trancommit
422:     eprint(adb, "trancommit")
423:     err = true
424:   end
425:   printf("checking transaction abort:\n")
426:   ornum = adb.rnum
427:   osize = adb.size
428:   if !adb.tranbegin
429:     eprint(adb, "tranbegin")
430:     err = true
431:   end
432:   for i in 1..rnum
433:     buf = sprintf("%d", rand(rnum))
434:     if rand(2) == 0
435:       if !adb.putcat(buf, buf)
436:         eprint(adb, "putcat")
437:         err = true
438:         break
439:       end
440:     else
441:       adb.out(buf)
442:     end
443:     if rnum > 250 && i % (rnum / 250) == 0
444:       print('.')
445:       if i == rnum || i % (rnum / 10) == 0
446:         printf(" (%08d)\n", i)
447:       end
448:     end
449:   end
450:   if !adb.tranabort
451:     eprint(adb, "trancommit")
452:     err = true
453:   end
454:   if adb.rnum != ornum || adb.size != osize
455:     eprint(adb, "(validation)")
456:     err = true
457:   end
458:   printf("checking hash-like updating:\n")
459:   for i in 1..rnum
460:     buf = sprintf("[%d]", rand(rnum))
461:     rnd = rand(4)
462:     if rnd == 0
463:       adb[buf] = buf
464:     elsif rnd == 1
465:       value = adb[buf]
466:     elsif rnd == 2
467:       res = adb.key?(buf)
468:     elsif rnd == 3
469:       adb.delete(buf)
470:     end
471:     if rnum > 250 && i % (rnum / 250) == 0
472:       print('.')
473:       if i == rnum || i % (rnum / 10) == 0
474:         printf(" (%08d)\n", i)
475:       end
476:     end
477:   end
478:   printf("checking hash-like iterator:\n")
479:   inum = 0
480:   adb.each do |tkey, tvalue|
481:     if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0
482:       print('.')
483:       if inum == rnum || inum % (rnum / 10) == 0
484:         printf(" (%08d)\n", inum)
485:       end
486:     end
487:     inum += 1
488:   end
489:   printf(" (%08d)\n", inum) if rnum > 250
490:   adb.clear
491:   printf("record number: %d\n", adb.rnum)
492:   printf("size: %d\n", adb.size)
493:   if !adb.close
494:     eprint(adb, "close")
495:     err = true
496:   end
497:   printf("time: %.3f\n", Time.now - stime)
498:   printf("%s\n\n", err ? "error" : "ok")
499:   return err ? 1 : 0
500: end

perform read command

[Source]

     # File tcatest.rb, line 186
186: def procread(name)
187:   printf("<Reading Test>\n  name=%s\n\n", name)
188:   err = false
189:   stime = Time.now
190:   adb = ADB::new
191:   if !adb.open(name)
192:     eprint(adb, "open")
193:     err = true
194:   end
195:   rnum = adb.rnum
196:   for i in 1..rnum
197:     buf = sprintf("%08d", i)
198:     if !adb.get(buf)
199:       eprint(adb, "get")
200:       err = true
201:       break
202:     end
203:     if rnum > 250 && i % (rnum / 250) == 0
204:       print('.')
205:       if i == rnum || i % (rnum / 10) == 0
206:         printf(" (%08d)\n", i)
207:       end
208:     end
209:   end
210:   printf("record number: %d\n", adb.rnum)
211:   printf("size: %d\n", adb.size)
212:   if !adb.close
213:     eprint(adb, "close")
214:     err = true
215:   end
216:   printf("time: %.3f\n", Time.now - stime)
217:   printf("%s\n\n", err ? "error" : "ok")
218:   return err ? 1 : 0
219: end

perform remove command

[Source]

     # File tcatest.rb, line 223
223: def procremove(name)
224:   printf("<Removing Test>\n  name=%s\n\n", name)
225:   err = false
226:   stime = Time.now
227:   adb = ADB::new
228:   if !adb.open(name)
229:     eprint(adb, "open")
230:     err = true
231:   end
232:   rnum = adb.rnum
233:   for i in 1..rnum
234:     buf = sprintf("%08d", i)
235:     if !adb.out(buf)
236:       eprint(adb, "out")
237:       err = true
238:       break
239:     end
240:     if rnum > 250 && i % (rnum / 250) == 0
241:       print('.')
242:       if i == rnum || i % (rnum / 10) == 0
243:         printf(" (%08d)\n", i)
244:       end
245:     end
246:   end
247:   printf("record number: %d\n", adb.rnum)
248:   printf("size: %d\n", adb.size)
249:   if !adb.close
250:     eprint(adb, "close")
251:     err = true
252:   end
253:   printf("time: %.3f\n", Time.now - stime)
254:   printf("%s\n\n", err ? "error" : "ok")
255:   return err ? 1 : 0
256: end

perform write command

[Source]

     # File tcatest.rb, line 150
150: def procwrite(name, rnum)
151:   printf("<Writing Test>\n  name=%s  rnum=%d\n\n", name, rnum)
152:   err = false
153:   stime = Time.now
154:   adb = ADB::new
155:   if !adb.open(name)
156:     eprint(adb, "open")
157:     err = true
158:   end
159:   for i in 1..rnum
160:     buf = sprintf("%08d", i)
161:     if !adb.put(buf, buf)
162:       eprint(adb, "put")
163:       err = true
164:       break
165:     end
166:     if rnum > 250 && i % (rnum / 250) == 0
167:       print('.')
168:       if i == rnum || i % (rnum / 10) == 0
169:         printf(" (%08d)\n", i)
170:       end
171:     end
172:   end
173:   printf("record number: %d\n", adb.rnum)
174:   printf("size: %d\n", adb.size)
175:   if !adb.close
176:     eprint(adb, "close")
177:     err = true
178:   end
179:   printf("time: %.3f\n", Time.now - stime)
180:   printf("%s\n\n", err ? "error" : "ok")
181:   return err ? 1 : 0
182: end

parse arguments of misc command

[Source]

     # File tcatest.rb, line 127
127: def runmisc
128:   name = nil
129:   rnum = nil
130:   i = 1
131:   while i < ARGV.length
132:     if !name && ARGV[i] =~ /^-/
133:       usage
134:     elsif !name
135:       name = ARGV[i]
136:     elsif !rnum
137:       rnum = ARGV[i].to_i
138:     else
139:       usage
140:     end
141:     i += 1
142:   end
143:   usage if !name || !rnum || rnum < 1
144:   rv = procmisc(name, rnum)
145:   return rv
146: end

parse arguments of read command

[Source]

     # File tcatest.rb, line 87
 87: def runread
 88:   name = nil
 89:   i = 1
 90:   while i < ARGV.length
 91:     if !name && ARGV[i] =~ /^-/
 92:       usage
 93:     elsif !name
 94:       name = ARGV[i]
 95:     else
 96:       usage
 97:     end
 98:     i += 1
 99:   end
100:   usage if !name
101:   rv = procread(name)
102:   return rv
103: end

parse arguments of remove command

[Source]

     # File tcatest.rb, line 107
107: def runremove
108:   name = nil
109:   i = 1
110:   while i < ARGV.length
111:     if !name && ARGV[i] =~ /^-/
112:       usage
113:     elsif !name
114:       name = ARGV[i]
115:     else
116:       usage
117:     end
118:     i += 1
119:   end
120:   usage if !name
121:   rv = procremove(name)
122:   return rv
123: end

parse arguments of write command

[Source]

    # File tcatest.rb, line 64
64: def runwrite
65:   name = nil
66:   rnum = nil
67:   i = 1
68:   while i < ARGV.length
69:     if !name && ARGV[i] =~ /^-/
70:       usage
71:     elsif !name
72:       name = ARGV[i]
73:     elsif !rnum
74:       rnum = ARGV[i].to_i
75:     else
76:       usage
77:     end
78:     i += 1
79:   end
80:   usage if !name || !rnum || rnum < 1
81:   rv = procwrite(name, rnum)
82:   return rv
83: end

print the usage and exit

[Source]

    # File tcatest.rb, line 43
43: def usage
44:   STDERR.printf("%s: test cases of the abstract database API\n", $progname)
45:   STDERR.printf("\n")
46:   STDERR.printf("usage:\n")
47:   STDERR.printf("  %s write name rnum\n", $progname)
48:   STDERR.printf("  %s read name\n", $progname)
49:   STDERR.printf("  %s remove name\n", $progname)
50:   STDERR.printf("  %s misc name rnum\n", $progname)
51:   STDERR.printf("\n")
52:   exit(1)
53: end

[Validate]