tcftest.rb

Path: tcftest.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 fixed-length database

[Source]

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

main routine

[Source]

    # File tcftest.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 tcftest.rb, line 301
301: def procmisc(path, rnum, omode)
302:   printf("<Miscellaneous Test>\n  path=%s  rnum=%d  omode=%d\n\n", path, rnum, omode)
303:   err = false
304:   stime = Time.now
305:   fdb = FDB::new
306:   if !fdb.tune(10, 1024 + 32 * rnum)
307:     eprint(fdb, "tune")
308:     err = true
309:   end
310:   if !fdb.open(path, FDB::OWRITER | FDB::OCREAT | FDB::OTRUNC | omode)
311:     eprint(fdb, "open")
312:     err = true
313:   end
314:   printf("writing:\n")
315:   for i in 1..rnum
316:     buf = sprintf("%08d", i)
317:     if !fdb.put(buf, buf)
318:       eprint(fdb, "put")
319:       err = true
320:       break
321:     end
322:     if rnum > 250 && i % (rnum / 250) == 0
323:       print('.')
324:       if i == rnum || i % (rnum / 10) == 0
325:         printf(" (%08d)\n", i)
326:       end
327:     end
328:   end
329:   printf("reading:\n")
330:   for i in 1..rnum
331:     buf = sprintf("%08d", i)
332:     if !fdb.get(buf)
333:       eprint(fdb, "get")
334:       err = true
335:       break
336:     end
337:     if rnum > 250 && i % (rnum / 250) == 0
338:       print('.')
339:       if i == rnum || i % (rnum / 10) == 0
340:         printf(" (%08d)\n", i)
341:       end
342:     end
343:   end
344:   printf("removing:\n")
345:   for i in 1..rnum
346:     buf = sprintf("%08d", i)
347:     if rand(2) == 0 && !fdb.out(buf)
348:       eprint(fdb, "out")
349:       err = true
350:       break
351:     end
352:     if 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 iterator:\n")
360:   if !fdb.iterinit
361:     eprint(fdb, "iterinit")
362:     err = true
363:   end
364:   inum = 0
365:   while key = fdb.iternext
366:     value = fdb.get(key)
367:     if !value
368:       eprint(fdb, "get")
369:       err = true
370:     end
371:     if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0
372:       print('.')
373:       if inum == rnum || inum % (rnum / 10) == 0
374:         printf(" (%08d)\n", inum)
375:       end
376:     end
377:     inum += 1
378:   end
379:   printf(" (%08d)\n", inum) if rnum > 250
380:   if fdb.ecode != FDB::ENOREC || inum != fdb.rnum
381:     eprint(fdb, "(validation)")
382:     err = true
383:   end
384:   keys = fdb.range("[min,max]", 10)
385:   if fdb.rnum >= 10 && keys.size != 10
386:     eprint(fdb, "range")
387:     err = true
388:   end
389:   printf("checking counting:\n")
390:   for i in 1..rnum
391:     buf = sprintf("[%d]", rand(rnum) + 1)
392:     if rand(2) == 0
393:       if !fdb.addint(buf, 1) && fdb.ecode != FDB::EKEEP
394:         eprint(fdb, "addint")
395:         err = true
396:         break
397:       end
398:     else
399:       if !fdb.adddouble(buf, 1) && fdb.ecode != FDB::EKEEP
400:         eprint(fdb, "adddouble")
401:         err = true
402:         break
403:       end
404:     end
405:     if i > 0 && rnum > 250 && i % (rnum / 250) == 0
406:       print('.')
407:       if i == rnum || i % (rnum / 10) == 0
408:         printf(" (%08d)\n", i)
409:       end
410:     end
411:   end
412:   if !fdb.sync
413:     eprint(fdb, "sync")
414:     err = true
415:   end
416:   if !fdb.optimize
417:     eprint(fdb, "optimize")
418:     err = true
419:   end
420:   npath = path + "-tmp"
421:   if !fdb.copy(npath)
422:     eprint(fdb, "copy")
423:     err = true
424:   end
425:   File::unlink(npath)
426:   if !fdb.vanish
427:     eprint(fdb, "vanish")
428:     err = true
429:   end
430:   printf("checking transaction commit:\n")
431:   if !fdb.tranbegin
432:     eprint(fdb, "tranbegin")
433:     err = true
434:   end
435:   for i in 1..rnum
436:     buf = sprintf("%d", rand(rnum) + 1)
437:     if rand(2) == 0
438:       if !fdb.putcat(buf, buf)
439:         eprint(fdb, "putcat")
440:         err = true
441:         break
442:       end
443:     else
444:       if !fdb.out(buf) && fdb.ecode != FDB::ENOREC
445:         eprint(fdb, "out")
446:         err = true
447:         break
448:       end
449:     end
450:     if rnum > 250 && i % (rnum / 250) == 0
451:       print('.')
452:       if i == rnum || i % (rnum / 10) == 0
453:         printf(" (%08d)\n", i)
454:       end
455:     end
456:   end
457:   if !fdb.trancommit
458:     eprint(fdb, "trancommit")
459:     err = true
460:   end
461:   printf("checking transaction abort:\n")
462:   ornum = fdb.rnum
463:   ofsiz = fdb.fsiz
464:   if !fdb.tranbegin
465:     eprint(fdb, "tranbegin")
466:     err = true
467:   end
468:   for i in 1..rnum
469:     buf = sprintf("%d", rand(rnum) + 1)
470:     if rand(2) == 0
471:       if !fdb.putcat(buf, buf)
472:         eprint(fdb, "putcat")
473:         err = true
474:         break
475:       end
476:     else
477:       if !fdb.out(buf) && fdb.ecode != FDB::ENOREC
478:         eprint(fdb, "out")
479:         err = true
480:         break
481:       end
482:     end
483:     if rnum > 250 && i % (rnum / 250) == 0
484:       print('.')
485:       if i == rnum || i % (rnum / 10) == 0
486:         printf(" (%08d)\n", i)
487:       end
488:     end
489:   end
490:   if !fdb.tranabort
491:     eprint(fdb, "trancommit")
492:     err = true
493:   end
494:   if fdb.rnum != ornum || fdb.fsiz != ofsiz
495:     eprint(fdb, "(validation)")
496:     err = true
497:   end
498:   printf("checking hash-like updating:\n")
499:   for i in 1..rnum
500:     buf = sprintf("[%d]", rand(rnum) + 1)
501:     rnd = rand(4)
502:     if rnd == 0
503:       fdb[buf] = buf
504:     elsif rnd == 1
505:       value = fdb[buf]
506:     elsif rnd == 2
507:       res = fdb.key?(buf)
508:     elsif rnd == 3
509:       fdb.delete(buf)
510:     end
511:     if rnum > 250 && i % (rnum / 250) == 0
512:       print('.')
513:       if i == rnum || i % (rnum / 10) == 0
514:         printf(" (%08d)\n", i)
515:       end
516:     end
517:   end
518:   printf("checking hash-like iterator:\n")
519:   inum = 0
520:   fdb.each do |tkey, tvalue|
521:     if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0
522:       print('.')
523:       if inum == rnum || inum % (rnum / 10) == 0
524:         printf(" (%08d)\n", inum)
525:       end
526:     end
527:     inum += 1
528:   end
529:   printf(" (%08d)\n", inum) if rnum > 250
530:   fdb.clear
531:   printf("record number: %d\n", fdb.rnum)
532:   printf("size: %d\n", fdb.fsiz)
533:   if !fdb.close
534:     eprint(fdb, "close")
535:     err = true
536:   end
537:   printf("time: %.3f\n", Time.now - stime)
538:   printf("%s\n\n", err ? "error" : "ok")
539:   return err ? 1 : 0
540: end

perform read command

[Source]

     # File tcftest.rb, line 227
227: def procread(path, omode)
228:   printf("<Reading Test>\n  path=%s  omode=%d\n\n", path, omode)
229:   err = false
230:   stime = Time.now
231:   fdb = FDB::new
232:   if !fdb.open(path, FDB::OREADER | omode)
233:     eprint(fdb, "open")
234:     err = true
235:   end
236:   rnum = fdb.rnum
237:   for i in 1..rnum
238:     buf = sprintf("%08d", i)
239:     if !fdb.get(buf)
240:       eprint(fdb, "get")
241:       err = true
242:       break
243:     end
244:     if rnum > 250 && i % (rnum / 250) == 0
245:       print('.')
246:       if i == rnum || i % (rnum / 10) == 0
247:         printf(" (%08d)\n", i)
248:       end
249:     end
250:   end
251:   printf("record number: %d\n", fdb.rnum)
252:   printf("size: %d\n", fdb.fsiz)
253:   if !fdb.close
254:     eprint(fdb, "close")
255:     err = true
256:   end
257:   printf("time: %.3f\n", Time.now - stime)
258:   printf("%s\n\n", err ? "error" : "ok")
259:   return err ? 1 : 0
260: end

perform remove command

[Source]

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

perform write command

[Source]

     # File tcftest.rb, line 186
186: def procwrite(path, rnum, width, limsiz, omode)
187:   printf("<Writing Test>\n  path=%s  rnum=%d  width=%d  limsiz=%d  omode=%d\n\n",
188:          path, rnum, width, limsiz, omode)
189:   err = false
190:   stime = Time.now
191:   fdb = FDB::new
192:   if !fdb.tune(width, limsiz)
193:     eprint(fdb, "tune")
194:     err = true
195:   end
196:   if !fdb.open(path, FDB::OWRITER | FDB::OCREAT | FDB::OTRUNC | omode)
197:     eprint(fdb, "open")
198:     err = true
199:   end
200:   for i in 1..rnum
201:     buf = sprintf("%08d", i)
202:     if !fdb.put(buf, buf)
203:       eprint(fdb, "put")
204:       err = true
205:       break
206:     end
207:     if rnum > 250 && i % (rnum / 250) == 0
208:       print('.')
209:       if i == rnum || i % (rnum / 10) == 0
210:         printf(" (%08d)\n", i)
211:       end
212:     end
213:   end
214:   printf("record number: %d\n", fdb.rnum)
215:   printf("size: %d\n", fdb.fsiz)
216:   if !fdb.close
217:     eprint(fdb, "close")
218:     err = true
219:   end
220:   printf("time: %.3f\n", Time.now - stime)
221:   printf("%s\n\n", err ? "error" : "ok")
222:   return err ? 1 : 0
223: end

parse arguments of misc command

[Source]

     # File tcftest.rb, line 156
156: def runmisc
157:   path = nil
158:   rnum = nil
159:   omode = 0
160:   i = 1
161:   while i < ARGV.length
162:     if !path && ARGV[i] =~ /^-/
163:       if ARGV[i] == "-nl"
164:         omode |= FDB::ONOLCK
165:       elsif ARGV[i] == "-nb"
166:         omode |= FDB::OLCKNB
167:       else
168:         usage
169:       end
170:     elsif !path
171:       path = ARGV[i]
172:     elsif !rnum
173:       rnum = ARGV[i].to_i
174:     else
175:       usage
176:     end
177:     i += 1
178:   end
179:   usage if !path || !rnum || rnum < 1
180:   rv = procmisc(path, rnum, omode)
181:   return rv
182: end

parse arguments of read command

[Source]

     # File tcftest.rb, line 102
102: def runread
103:   path = nil
104:   omode = 0
105:   i = 1
106:   while i < ARGV.length
107:     if !path && ARGV[i] =~ /^-/
108:       if ARGV[i] == "-nl"
109:         omode |= FDB::ONOLCK
110:       elsif ARGV[i] == "-nb"
111:         omode |= FDB::OLCKNB
112:       else
113:         usage
114:       end
115:     elsif !path
116:       path = ARGV[i]
117:     else
118:       usage
119:     end
120:     i += 1
121:   end
122:   usage if !path
123:   rv = procread(path, omode)
124:   return rv
125: end

parse arguments of remove command

[Source]

     # File tcftest.rb, line 129
129: def runremove
130:   path = nil
131:   omode = 0
132:   i = 1
133:   while i < ARGV.length
134:     if !path && ARGV[i] =~ /^-/
135:       if ARGV[i] == "-nl"
136:         omode |= FDB::ONOLCK
137:       elsif ARGV[i] == "-nb"
138:         omode |= FDB::OLCKNB
139:       else
140:         usage
141:       end
142:     elsif !path
143:       path = ARGV[i]
144:     else
145:       usage
146:     end
147:     i += 1
148:   end
149:   usage if !path
150:   rv = procremove(path, omode)
151:   return rv
152: end

parse arguments of write command

[Source]

    # File tcftest.rb, line 64
64: def runwrite
65:   path = nil
66:   rnum = nil
67:   width = nil
68:   limsiz = nil
69:   omode = 0
70:   i = 1
71:   while i < ARGV.length
72:     if !path && ARGV[i] =~ /^-/
73:       if ARGV[i] == "-nl"
74:         omode |= FDB::ONOLCK
75:       elsif ARGV[i] == "-nb"
76:         omode |= FDB::OLCKNB
77:       else
78:         usage
79:       end
80:     elsif !path
81:       path = ARGV[i]
82:     elsif !rnum
83:       rnum = ARGV[i].to_i
84:     elsif !width
85:       width = ARGV[i].to_i
86:     elsif !limsiz
87:       limsiz = ARGV[i].to_i
88:     else
89:       usage
90:     end
91:     i += 1
92:   end
93:   usage if !path || !rnum || rnum < 1
94:   width = width ? width : -1
95:   limsiz = limsiz ? limsiz : -1
96:   rv = procwrite(path, rnum, width, limsiz, omode)
97:   return rv
98: end

print the usage and exit

[Source]

    # File tcftest.rb, line 43
43: def usage
44:   STDERR.printf("%s: test cases of the fixed-length database API\n", $progname)
45:   STDERR.printf("\n")
46:   STDERR.printf("usage:\n")
47:   STDERR.printf("  %s write [-nl|-nb] path rnum [width [limsiz]]\n", $progname)
48:   STDERR.printf("  %s read [-nl|-nb] path\n", $progname)
49:   STDERR.printf("  %s remove [-nl|-nb] path\n", $progname)
50:   STDERR.printf("  %s misc [-nl|-nb] path rnum\n", $progname)
51:   STDERR.printf("\n")
52:   exit(1)
53: end

[Validate]