tchtest.rb

Path: tchtest.rb
Last Update: Sun Oct 03 15:07:10 +0000 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 hash database

[Source]

    # File tchtest.rb, line 58
58: def eprint(hdb, func)
59:   path = hdb.path
60:   STDERR.printf("%s: %s: %s: %s\n", $progname, path ? path : "-", func, hdb.errmsg)
61: end

main routine

[Source]

    # File tchtest.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 tchtest.rb, line 335
335: def procmisc(path, rnum, opts, omode)
336:   printf("<Miscellaneous Test>\n  path=%s  rnum=%d  opts=%d  omode=%d\n\n",
337:          path, rnum, opts, omode)
338:   err = false
339:   stime = Time.now
340:   hdb = HDB::new
341:   if !hdb.tune(rnum / 50, 2, -1, opts)
342:     eprint(hdb, "tune")
343:     err = true
344:   end
345:   if !hdb.setcache(rnum / 10)
346:     eprint(hdb, "setcache")
347:     err = true
348:   end
349:   if !hdb.setxmsiz(rnum * 4)
350:     eprint(hdb, "setxmsiz")
351:     err = true
352:   end
353:   if !hdb.setdfunit(8)
354:     eprint(hdb, "setdfunit")
355:     err = true
356:   end
357:   if !hdb.open(path, HDB::OWRITER | HDB::OCREAT | HDB::OTRUNC | omode)
358:     eprint(hdb, "open")
359:     err = true
360:   end
361:   printf("writing:\n")
362:   for i in 1..rnum
363:     buf = sprintf("%08d", i)
364:     if !hdb.put(buf, buf)
365:       eprint(hdb, "put")
366:       err = true
367:       break
368:     end
369:     if rnum > 250 && i % (rnum / 250) == 0
370:       print('.')
371:       if i == rnum || i % (rnum / 10) == 0
372:         printf(" (%08d)\n", i)
373:       end
374:     end
375:   end
376:   printf("reading:\n")
377:   for i in 1..rnum
378:     buf = sprintf("%08d", i)
379:     if !hdb.get(buf)
380:       eprint(hdb, "get")
381:       err = true
382:       break
383:     end
384:     if rnum > 250 && i % (rnum / 250) == 0
385:       print('.')
386:       if i == rnum || i % (rnum / 10) == 0
387:         printf(" (%08d)\n", i)
388:       end
389:     end
390:   end
391:   printf("removing:\n")
392:   for i in 1..rnum
393:     buf = sprintf("%08d", i)
394:     if rand(2) == 0 && !hdb.out(buf)
395:       eprint(hdb, "out")
396:       err = true
397:       break
398:     end
399:     if rnum > 250 && i % (rnum / 250) == 0
400:       print('.')
401:       if i == rnum || i % (rnum / 10) == 0
402:         printf(" (%08d)\n", i)
403:       end
404:     end
405:   end
406:   printf("checking iterator:\n")
407:   if !hdb.iterinit
408:     eprint(hdb, "iterinit")
409:     err = true
410:   end
411:   inum = 0
412:   while key = hdb.iternext
413:     value = hdb.get(key)
414:     if !value
415:       eprint(hdb, "get")
416:       err = true
417:     end
418:     if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0
419:       print('.')
420:       if inum == rnum || inum % (rnum / 10) == 0
421:         printf(" (%08d)\n", inum)
422:       end
423:     end
424:     inum += 1
425:   end
426:   printf(" (%08d)\n", inum) if rnum > 250
427:   if hdb.ecode != HDB::ENOREC || inum != hdb.rnum
428:     eprint(hdb, "(validation)")
429:     err = true
430:   end
431:   keys = hdb.fwmkeys("0", 10)
432:   if hdb.rnum >= 10 && keys.size != 10
433:     eprint(hdb, "fwmkeys")
434:     err = true
435:   end
436:   printf("checking counting:\n")
437:   for i in 1..rnum
438:     buf = sprintf("[%d]", rand(rnum))
439:     if rand(2) == 0
440:       if !hdb.addint(buf, 1) && hdb.ecode != HDB::EKEEP
441:         eprint(hdb, "addint")
442:         err = true
443:         break
444:       end
445:     else
446:       if !hdb.adddouble(buf, 1) && hdb.ecode != HDB::EKEEP
447:         eprint(hdb, "adddouble")
448:         err = true
449:         break
450:       end
451:     end
452:     if i > 0 && rnum > 250 && i % (rnum / 250) == 0
453:       print('.')
454:       if i == rnum || i % (rnum / 10) == 0
455:         printf(" (%08d)\n", i)
456:       end
457:     end
458:   end
459:   if !hdb.sync
460:     eprint(hdb, "sync")
461:     err = true
462:   end
463:   if !hdb.optimize
464:     eprint(hdb, "optimize")
465:     err = true
466:   end
467:   npath = path + "-tmp"
468:   if !hdb.copy(npath)
469:     eprint(hdb, "copy")
470:     err = true
471:   end
472:   File::unlink(npath)
473:   if !hdb.vanish
474:     eprint(hdb, "vanish")
475:     err = true
476:   end
477:   printf("checking transaction commit:\n")
478:   if !hdb.tranbegin
479:     eprint(hdb, "tranbegin")
480:     err = true
481:   end
482:   for i in 1..rnum
483:     buf = sprintf("%d", rand(rnum))
484:     if rand(2) == 0
485:       if !hdb.putcat(buf, buf)
486:         eprint(hdb, "putcat")
487:         err = true
488:         break
489:       end
490:     else
491:       if !hdb.out(buf) && hdb.ecode != HDB::ENOREC
492:         eprint(hdb, "out")
493:         err = true
494:         break
495:       end
496:     end
497:     if rnum > 250 && i % (rnum / 250) == 0
498:       print('.')
499:       if i == rnum || i % (rnum / 10) == 0
500:         printf(" (%08d)\n", i)
501:       end
502:     end
503:   end
504:   if !hdb.trancommit
505:     eprint(hdb, "trancommit")
506:     err = true
507:   end
508:   printf("checking transaction abort:\n")
509:   ornum = hdb.rnum
510:   ofsiz = hdb.fsiz
511:   if !hdb.tranbegin
512:     eprint(hdb, "tranbegin")
513:     err = true
514:   end
515:   for i in 1..rnum
516:     buf = sprintf("%d", rand(rnum))
517:     if rand(2) == 0
518:       if !hdb.putcat(buf, buf)
519:         eprint(hdb, "putcat")
520:         err = true
521:         break
522:       end
523:     else
524:       if !hdb.out(buf) && hdb.ecode != HDB::ENOREC
525:         eprint(hdb, "out")
526:         err = true
527:         break
528:       end
529:     end
530:     if rnum > 250 && i % (rnum / 250) == 0
531:       print('.')
532:       if i == rnum || i % (rnum / 10) == 0
533:         printf(" (%08d)\n", i)
534:       end
535:     end
536:   end
537:   if !hdb.tranabort
538:     eprint(hdb, "trancommit")
539:     err = true
540:   end
541:   if hdb.rnum != ornum || hdb.fsiz != ofsiz
542:     eprint(hdb, "(validation)")
543:     err = true
544:   end
545:   printf("checking hash-like updating:\n")
546:   for i in 1..rnum
547:     buf = sprintf("[%d]", rand(rnum))
548:     rnd = rand(4)
549:     if rnd == 0
550:       hdb[buf] = buf
551:     elsif rnd == 1
552:       value = hdb[buf]
553:     elsif rnd == 2
554:       res = hdb.key?(buf)
555:     elsif rnd == 3
556:       hdb.delete(buf)
557:     end
558:     if rnum > 250 && i % (rnum / 250) == 0
559:       print('.')
560:       if i == rnum || i % (rnum / 10) == 0
561:         printf(" (%08d)\n", i)
562:       end
563:     end
564:   end
565:   printf("checking hash-like iterator:\n")
566:   inum = 0
567:   hdb.each do |tkey, tvalue|
568:     if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0
569:       print('.')
570:       if inum == rnum || inum % (rnum / 10) == 0
571:         printf(" (%08d)\n", inum)
572:       end
573:     end
574:     inum += 1
575:   end
576:   printf(" (%08d)\n", inum) if rnum > 250
577:   hdb.clear
578:   printf("record number: %d\n", hdb.rnum)
579:   printf("size: %d\n", hdb.fsiz)
580:   if !hdb.close
581:     eprint(hdb, "close")
582:     err = true
583:   end
584:   printf("time: %.3f\n", Time.now - stime)
585:   printf("%s\n\n", err ? "error" : "ok")
586:   return err ? 1 : 0
587: end

perform read command

[Source]

     # File tchtest.rb, line 261
261: def procread(path, omode)
262:   printf("<Reading Test>\n  path=%s  omode=%d\n\n", path, omode)
263:   err = false
264:   stime = Time.now
265:   hdb = HDB::new
266:   if !hdb.open(path, HDB::OREADER | omode)
267:     eprint(hdb, "open")
268:     err = true
269:   end
270:   rnum = hdb.rnum
271:   for i in 1..rnum
272:     buf = sprintf("%08d", i)
273:     if !hdb.get(buf)
274:       eprint(hdb, "get")
275:       err = true
276:       break
277:     end
278:     if rnum > 250 && i % (rnum / 250) == 0
279:       print('.')
280:       if i == rnum || i % (rnum / 10) == 0
281:         printf(" (%08d)\n", i)
282:       end
283:     end
284:   end
285:   printf("record number: %d\n", hdb.rnum)
286:   printf("size: %d\n", hdb.fsiz)
287:   if !hdb.close
288:     eprint(hdb, "close")
289:     err = true
290:   end
291:   printf("time: %.3f\n", Time.now - stime)
292:   printf("%s\n\n", err ? "error" : "ok")
293:   return err ? 1 : 0
294: end

perform remove command

[Source]

     # File tchtest.rb, line 298
298: def procremove(path, omode)
299:   printf("<Removing Test>\n  path=%s  omode=%d\n\n", path, omode)
300:   err = false
301:   stime = Time.now
302:   hdb = HDB::new
303:   if !hdb.open(path, HDB::OWRITER | omode)
304:     eprint(hdb, "open")
305:     err = true
306:   end
307:   rnum = hdb.rnum
308:   for i in 1..rnum
309:     buf = sprintf("%08d", i)
310:     if !hdb.out(buf)
311:       eprint(hdb, "out")
312:       err = true
313:       break
314:     end
315:     if rnum > 250 && i % (rnum / 250) == 0
316:       print('.')
317:       if i == rnum || i % (rnum / 10) == 0
318:         printf(" (%08d)\n", i)
319:       end
320:     end
321:   end
322:   printf("record number: %d\n", hdb.rnum)
323:   printf("size: %d\n", hdb.fsiz)
324:   if !hdb.close
325:     eprint(hdb, "close")
326:     err = true
327:   end
328:   printf("time: %.3f\n", Time.now - stime)
329:   printf("%s\n\n", err ? "error" : "ok")
330:   return err ? 1 : 0
331: end

perform write command

[Source]

     # File tchtest.rb, line 212
212: def procwrite(path, rnum, bnum, apow, fpow, opts, omode, as)
213:   printf("<Writing Test>\n  path=%s  rnum=%d  bnum=%d  apow=%d  fpow=%d  opts=%d" +
214:          "  omode=%d  as=%s\n\n", path, rnum, bnum, apow, fpow, opts, omode, as)
215:   err = false
216:   stime = Time.now
217:   hdb = HDB::new
218:   if !hdb.tune(bnum, apow, fpow, opts)
219:     eprint(hdb, "tune")
220:     err = true
221:   end
222:   if !hdb.open(path, HDB::OWRITER | HDB::OCREAT | HDB::OTRUNC | omode)
223:     eprint(hdb, "open")
224:     err = true
225:   end
226:   for i in 1..rnum
227:     buf = sprintf("%08d", i)
228:     if as
229:       if !hdb.putasync(buf, buf)
230:         eprint(hdb, "putasync")
231:         err = true
232:         break
233:       end
234:     else
235:       if !hdb.put(buf, buf)
236:         eprint(hdb, "put")
237:         err = true
238:         break
239:       end
240:     end
241:     if rnum > 250 && i % (rnum / 250) == 0
242:       print('.')
243:       if i == rnum || i % (rnum / 10) == 0
244:         printf(" (%08d)\n", i)
245:       end
246:     end
247:   end
248:   printf("record number: %d\n", hdb.rnum)
249:   printf("size: %d\n", hdb.fsiz)
250:   if !hdb.close
251:     eprint(hdb, "close")
252:     err = true
253:   end
254:   printf("time: %.3f\n", Time.now - stime)
255:   printf("%s\n\n", err ? "error" : "ok")
256:   return err ? 1 : 0
257: end

parse arguments of misc command

[Source]

     # File tchtest.rb, line 173
173: def runmisc
174:   path = nil
175:   rnum = nil
176:   opts = 0
177:   omode = 0
178:   i = 1
179:   while i < ARGV.length
180:     if !path && ARGV[i] =~ /^-/
181:       if ARGV[i] == "-tl"
182:         opts |= HDB::TLARGE
183:       elsif ARGV[i] == "-td"
184:         opts |= HDB::TDEFLATE
185:       elsif ARGV[i] == "-tb"
186:         opts |= HDB::TBZIP
187:       elsif ARGV[i] == "-tt"
188:         opts |= HDB::TTCBS
189:       elsif ARGV[i] == "-nl"
190:         omode |= HDB::ONOLCK
191:       elsif ARGV[i] == "-nb"
192:         omode |= HDB::OLCKNB
193:       else
194:         usage
195:       end
196:     elsif !path
197:       path = ARGV[i]
198:     elsif !rnum
199:       rnum = ARGV[i].to_i
200:     else
201:       usage
202:     end
203:     i += 1
204:   end
205:   usage if !path || !rnum || rnum < 1
206:   rv = procmisc(path, rnum, opts, omode)
207:   return rv
208: end

parse arguments of read command

[Source]

     # File tchtest.rb, line 119
119: def runread
120:   path = nil
121:   omode = 0
122:   i = 1
123:   while i < ARGV.length
124:     if !path && ARGV[i] =~ /^-/
125:       if ARGV[i] == "-nl"
126:         omode |= HDB::ONOLCK
127:       elsif ARGV[i] == "-nb"
128:         omode |= HDB::OLCKNB
129:       else
130:         usage
131:       end
132:     elsif !path
133:       path = ARGV[i]
134:     else
135:       usage
136:     end
137:     i += 1
138:   end
139:   usage if !path
140:   rv = procread(path, omode)
141:   return rv
142: end

parse arguments of remove command

[Source]

     # File tchtest.rb, line 146
146: def runremove
147:   path = nil
148:   omode = 0
149:   i = 1
150:   while i < ARGV.length
151:     if !path && ARGV[i] =~ /^-/
152:       if ARGV[i] == "-nl"
153:         omode |= HDB::ONOLCK
154:       elsif ARGV[i] == "-nb"
155:         omode |= HDB::OLCKNB
156:       else
157:         usage
158:       end
159:     elsif !path
160:       path = ARGV[i]
161:     else
162:       usage
163:     end
164:     i += 1
165:   end
166:   usage if !path
167:   rv = procremove(path, omode)
168:   return rv
169: end

parse arguments of write command

[Source]

     # File tchtest.rb, line 65
 65: def runwrite
 66:   path = nil
 67:   rnum = nil
 68:   bnum = nil
 69:   apow = nil
 70:   fpow = nil
 71:   opts = 0
 72:   omode = 0
 73:   as = false
 74:   i = 1
 75:   while i < ARGV.length
 76:     if !path && ARGV[i] =~ /^-/
 77:       if ARGV[i] == "-tl"
 78:         opts |= HDB::TLARGE
 79:       elsif ARGV[i] == "-td"
 80:         opts |= HDB::TDEFLATE
 81:       elsif ARGV[i] == "-tb"
 82:         opts |= HDB::TBZIP
 83:       elsif ARGV[i] == "-tt"
 84:         opts |= HDB::TTCBS
 85:       elsif ARGV[i] == "-nl"
 86:         omode |= HDB::ONOLCK
 87:       elsif ARGV[i] == "-nb"
 88:         omode |= HDB::OLCKNB
 89:       elsif ARGV[i] == "-as"
 90:         as = true
 91:       else
 92:         usage
 93:       end
 94:     elsif !path
 95:       path = ARGV[i]
 96:     elsif !rnum
 97:       rnum = ARGV[i].to_i
 98:     elsif !bnum
 99:       bnum = ARGV[i].to_i
100:     elsif !apow
101:       apow = ARGV[i].to_i
102:     elsif !fpow
103:       fpow = ARGV[i].to_i
104:     else
105:       usage
106:     end
107:     i += 1
108:   end
109:   usage if !path || !rnum || rnum < 1
110:   bnum = bnum ? bnum : -1
111:   apow = apow ? apow : -1
112:   fpow = fpow ? fpow : -1
113:   rv = procwrite(path, rnum, bnum, apow, fpow, opts, omode, as)
114:   return rv
115: end

print the usage and exit

[Source]

    # File tchtest.rb, line 43
43: def usage
44:   STDERR.printf("%s: test cases of the hash database API\n", $progname)
45:   STDERR.printf("\n")
46:   STDERR.printf("usage:\n")
47:   STDERR.printf("  %s write [-tl] [-td|-tb|-tt] [-nl|-nb] [-as] path rnum" +
48:                 " [bnum [apow [fpow]]]\n", $progname)
49:   STDERR.printf("  %s read [-nl|-nb] path\n", $progname)
50:   STDERR.printf("  %s remove [-nl|-nb] path\n", $progname)
51:   STDERR.printf("  %s misc [-tl] [-td|-tb|-tt] [-nl|-nb] path rnum\n", $progname)
52:   STDERR.printf("\n")
53:   exit(1)
54: end

[Validate]