Path: | tcbtest.rb |
Last Update: | Sun Sep 19 11:46:52 +0200 2010 |
print error message of B+ tree database
# File tcbtest.rb, line 58 58: def eprint(bdb, func) 59: path = bdb.path 60: STDERR.printf("%s: %s: %s: %s\n", $progname, path ? path : "-", func, bdb.errmsg) 61: end
main routine
# File tcbtest.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
# File tcbtest.rb, line 333 333: def procmisc(path, rnum, opts, omode) 334: printf("<Miscellaneous Test>\n path=%s rnum=%d opts=%d omode=%d\n\n", 335: path, rnum, opts, omode) 336: err = false 337: stime = Time.now 338: bdb = BDB::new 339: if !bdb.tune(10, 10, rnum / 50, 2, -1, opts) 340: eprint(bdb, "tune") 341: err = true 342: end 343: if !bdb.setcache(128, 256) 344: eprint(bdb, "setcache") 345: err = true 346: end 347: if !bdb.setxmsiz(rnum * 4) 348: eprint(bdb, "setxmsiz") 349: err = true 350: end 351: if !bdb.setdfunit(8) 352: eprint(bdb, "setdfunit") 353: err = true 354: end 355: if !bdb.open(path, BDB::OWRITER | BDB::OCREAT | BDB::OTRUNC | omode) 356: eprint(bdb, "open") 357: err = true 358: end 359: printf("writing:\n") 360: for i in 1..rnum 361: buf = sprintf("%08d", i) 362: if !bdb.put(buf, buf) 363: eprint(bdb, "put") 364: err = true 365: break 366: end 367: if rnum > 250 && i % (rnum / 250) == 0 368: print('.') 369: if i == rnum || i % (rnum / 10) == 0 370: printf(" (%08d)\n", i) 371: end 372: end 373: end 374: printf("reading:\n") 375: for i in 1..rnum 376: buf = sprintf("%08d", i) 377: if !bdb.get(buf) 378: eprint(bdb, "get") 379: err = true 380: break 381: end 382: if rnum > 250 && i % (rnum / 250) == 0 383: print('.') 384: if i == rnum || i % (rnum / 10) == 0 385: printf(" (%08d)\n", i) 386: end 387: end 388: end 389: printf("removing:\n") 390: for i in 1..rnum 391: buf = sprintf("%08d", i) 392: if rand(2) == 0 && !bdb.out(buf) 393: eprint(bdb, "out") 394: err = true 395: break 396: end 397: if rnum > 250 && i % (rnum / 250) == 0 398: print('.') 399: if i == rnum || i % (rnum / 10) == 0 400: printf(" (%08d)\n", i) 401: end 402: end 403: end 404: printf("checking cursor:\n") 405: cur = BDBCUR::new(bdb) 406: if !cur.first && bdb.ecode != BDB::ENOREC 407: eprint(bdb, "cur::first") 408: err = true 409: end 410: inum = 0 411: while key = cur.key 412: value = cur.val 413: if !value 414: eprint(bdb, "cur::val") 415: err = true 416: end 417: cur.next 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 bdb.ecode != BDB::ENOREC || inum != bdb.rnum 428: eprint(bdb, "(validation)") 429: err = true 430: end 431: keys = bdb.fwmkeys("0", 10) 432: if bdb.rnum >= 10 && keys.size != 10 433: eprint(bdb, "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 !bdb.addint(buf, 1) && bdb.ecode != BDB::EKEEP 441: eprint(bdb, "addint") 442: err = true 443: break 444: end 445: else 446: if !bdb.adddouble(buf, 1) && bdb.ecode != BDB::EKEEP 447: eprint(bdb, "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 !bdb.sync 460: eprint(bdb, "sync") 461: err = true 462: end 463: if !bdb.optimize 464: eprint(bdb, "optimize") 465: err = true 466: end 467: npath = path + "-tmp" 468: if !bdb.copy(npath) 469: eprint(bdb, "copy") 470: err = true 471: end 472: File::unlink(npath) 473: if !bdb.vanish 474: eprint(bdb, "vanish") 475: err = true 476: end 477: printf("random writing:\n") 478: for i in 1..rnum 479: buf = sprintf("%08d", rand(i)) 480: if !bdb.putdup(buf, buf) 481: eprint(bdb, "putdup") 482: err = true 483: break 484: end 485: if rnum > 250 && i % (rnum / 250) == 0 486: print('.') 487: if i == rnum || i % (rnum / 10) == 0 488: printf(" (%08d)\n", i) 489: end 490: end 491: end 492: printf("cursor updating:\n") 493: for i in 1..rnum 494: if rand(10) == 0 495: buf = sprintf("%08d", rand(rnum)) 496: cur.jump(buf) 497: for j in 1..10 498: key = cur.key 499: break if !key 500: if rand(3) == 0 501: cur.out 502: else 503: cpmode = BDBCUR::CPCURRENT + rand(3) 504: cur.put(buf, cpmode) 505: end 506: cur.next 507: j += 1 508: end 509: end 510: if rnum > 250 && i % (rnum / 250) == 0 511: print('.') 512: if i == rnum || i % (rnum / 10) == 0 513: printf(" (%08d)\n", i) 514: end 515: end 516: end 517: if !bdb.tranbegin 518: eprint(bdb, "tranbegin") 519: err = true 520: end 521: bdb.putdup("::1", "1") 522: bdb.putdup("::2", "2a") 523: bdb.putdup("::2", "2b") 524: bdb.putdup("::3", "3") 525: cur.jump("::2") 526: cur.put("2A") 527: cur.put("2-", BDBCUR::CPBEFORE) 528: cur.put("2+") 529: cur.next 530: cur.next 531: cur.put("mid", BDBCUR::CPBEFORE) 532: cur.put("2C", BDBCUR::CPAFTER) 533: cur.prev 534: cur.out 535: vals = bdb.getlist("::2") 536: if !vals || vals.size != 4 537: eprint(bdb, "getlist") 538: err = true 539: end 540: pvals = [ "hop", "step", "jump" ] 541: if !bdb.putlist("::1", pvals) 542: eprint(bdb, "putlist") 543: err = true 544: end 545: if !bdb.outlist("::1") 546: eprint(bdb, "outlist") 547: err = true 548: end 549: if !bdb.trancommit 550: eprint(bdb, "trancommit") 551: err = true 552: end 553: if !bdb.tranbegin || !bdb.tranabort 554: eprint(bdb, "tranbegin") 555: err = true 556: end 557: printf("checking hash-like updating:\n") 558: for i in 1..rnum 559: buf = sprintf("[%d]", rand(rnum)) 560: rnd = rand(4) 561: if rnd == 0 562: bdb[buf] = buf + "hoge" 563: elsif rnd == 1 564: value = bdb[buf] 565: elsif rnd == 2 566: res = bdb.key?(buf) 567: elsif rnd == 3 568: bdb.delete(buf) 569: end 570: if rnum > 250 && i % (rnum / 250) == 0 571: print('.') 572: if i == rnum || i % (rnum / 10) == 0 573: printf(" (%08d)\n", i) 574: end 575: end 576: end 577: printf("checking hash-like iterator:\n") 578: inum = 0 579: bdb.each do |tkey, tvalue| 580: if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0 581: print('.') 582: if inum == rnum || inum % (rnum / 10) == 0 583: printf(" (%08d)\n", inum) 584: end 585: end 586: inum += 1 587: end 588: printf(" (%08d)\n", inum) if rnum > 250 589: bdb.clear 590: printf("record number: %d\n", bdb.rnum) 591: printf("size: %d\n", bdb.fsiz) 592: if !bdb.close 593: eprint(bdb, "close") 594: err = true 595: end 596: printf("time: %.3f\n", Time.now - stime) 597: printf("%s\n\n", err ? "error" : "ok") 598: return err ? 1 : 0 599: end
perform read command
# File tcbtest.rb, line 259 259: def procread(path, omode) 260: printf("<Reading Test>\n path=%s omode=%d\n\n", path, omode) 261: err = false 262: stime = Time.now 263: bdb = BDB::new 264: if !bdb.open(path, BDB::OREADER | omode) 265: eprint(bdb, "open") 266: err = true 267: end 268: rnum = bdb.rnum 269: for i in 1..rnum 270: buf = sprintf("%08d", i) 271: if !bdb.get(buf) 272: eprint(bdb, "get") 273: err = true 274: break 275: end 276: if rnum > 250 && i % (rnum / 250) == 0 277: print('.') 278: if i == rnum || i % (rnum / 10) == 0 279: printf(" (%08d)\n", i) 280: end 281: end 282: end 283: printf("record number: %d\n", bdb.rnum) 284: printf("size: %d\n", bdb.fsiz) 285: if !bdb.close 286: eprint(bdb, "close") 287: err = true 288: end 289: printf("time: %.3f\n", Time.now - stime) 290: printf("%s\n\n", err ? "error" : "ok") 291: return err ? 1 : 0 292: end
perform remove command
# File tcbtest.rb, line 296 296: def procremove(path, omode) 297: printf("<Removing Test>\n path=%s omode=%d\n\n", path, omode) 298: err = false 299: stime = Time.now 300: bdb = BDB::new 301: if !bdb.open(path, BDB::OWRITER | omode) 302: eprint(bdb, "open") 303: err = true 304: end 305: rnum = bdb.rnum 306: for i in 1..rnum 307: buf = sprintf("%08d", i) 308: if !bdb.out(buf) 309: eprint(bdb, "out") 310: err = true 311: break 312: end 313: if rnum > 250 && i % (rnum / 250) == 0 314: print('.') 315: if i == rnum || i % (rnum / 10) == 0 316: printf(" (%08d)\n", i) 317: end 318: end 319: end 320: printf("record number: %d\n", bdb.rnum) 321: printf("size: %d\n", bdb.fsiz) 322: if !bdb.close 323: eprint(bdb, "close") 324: err = true 325: end 326: printf("time: %.3f\n", Time.now - stime) 327: printf("%s\n\n", err ? "error" : "ok") 328: return err ? 1 : 0 329: end
perform write command
# File tcbtest.rb, line 217 217: def procwrite(path, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode) 218: printf("<Writing Test>\n path=%s rnum=%d lmemb=%d nmemb=%d bnum=%d apow=%d fpow=%d" + 219: " opts=%d omode=%d\n\n", 220: path, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode) 221: err = false 222: stime = Time.now 223: bdb = BDB::new 224: if !bdb.tune(lmemb, nmemb, bnum, apow, fpow, opts) 225: eprint(bdb, "tune") 226: err = true 227: end 228: if !bdb.open(path, BDB::OWRITER | BDB::OCREAT | BDB::OTRUNC | omode) 229: eprint(bdb, "open") 230: err = true 231: end 232: for i in 1..rnum 233: buf = sprintf("%08d", i) 234: if !bdb.put(buf, buf) 235: eprint(bdb, "put") 236: err = true 237: break 238: end 239: if rnum > 250 && i % (rnum / 250) == 0 240: print('.') 241: if i == rnum || i % (rnum / 10) == 0 242: printf(" (%08d)\n", i) 243: end 244: end 245: end 246: printf("record number: %d\n", bdb.rnum) 247: printf("size: %d\n", bdb.fsiz) 248: if !bdb.close 249: eprint(bdb, "close") 250: err = true 251: end 252: printf("time: %.3f\n", Time.now - stime) 253: printf("%s\n\n", err ? "error" : "ok") 254: return err ? 1 : 0 255: end
parse arguments of misc command
# File tcbtest.rb, line 178 178: def runmisc 179: path = nil 180: rnum = nil 181: opts = 0 182: omode = 0 183: i = 1 184: while i < ARGV.length 185: if !path && ARGV[i] =~ /^-/ 186: if ARGV[i] == "-tl" 187: opts |= BDB::TLARGE 188: elsif ARGV[i] == "-td" 189: opts |= BDB::TDEFLATE 190: elsif ARGV[i] == "-tb" 191: opts |= BDB::TBZIP 192: elsif ARGV[i] == "-tt" 193: opts |= BDB::TTCBS 194: elsif ARGV[i] == "-nl" 195: omode |= BDB::ONOLCK 196: elsif ARGV[i] == "-nb" 197: omode |= BDB::OLCKNB 198: else 199: usage 200: end 201: elsif !path 202: path = ARGV[i] 203: elsif !rnum 204: rnum = ARGV[i].to_i 205: else 206: usage 207: end 208: i += 1 209: end 210: usage if !path || !rnum || rnum < 1 211: rv = procmisc(path, rnum, opts, omode) 212: return rv 213: end
parse arguments of read command
# File tcbtest.rb, line 124 124: def runread 125: path = nil 126: omode = 0 127: i = 1 128: while i < ARGV.length 129: if !path && ARGV[i] =~ /^-/ 130: if ARGV[i] == "-nl" 131: omode |= BDB::ONOLCK 132: elsif ARGV[i] == "-nb" 133: omode |= BDB::OLCKNB 134: else 135: usage 136: end 137: elsif !path 138: path = ARGV[i] 139: else 140: usage 141: end 142: i += 1 143: end 144: usage if !path 145: rv = procread(path, omode) 146: return rv 147: end
parse arguments of remove command
# File tcbtest.rb, line 151 151: def runremove 152: path = nil 153: omode = 0 154: i = 1 155: while i < ARGV.length 156: if !path && ARGV[i] =~ /^-/ 157: if ARGV[i] == "-nl" 158: omode |= BDB::ONOLCK 159: elsif ARGV[i] == "-nb" 160: omode |= BDB::OLCKNB 161: else 162: usage 163: end 164: elsif !path 165: path = ARGV[i] 166: else 167: usage 168: end 169: i += 1 170: end 171: usage if !path 172: rv = procremove(path, omode) 173: return rv 174: end
parse arguments of write command
# File tcbtest.rb, line 65 65: def runwrite 66: path = nil 67: rnum = nil 68: lmemb = nil 69: nmemb = nil 70: bnum = nil 71: apow = nil 72: fpow = nil 73: opts = 0 74: omode = 0 75: i = 1 76: while i < ARGV.length 77: if !path && ARGV[i] =~ /^-/ 78: if ARGV[i] == "-tl" 79: opts |= BDB::TLARGE 80: elsif ARGV[i] == "-td" 81: opts |= BDB::TDEFLATE 82: elsif ARGV[i] == "-tb" 83: opts |= BDB::TBZIP 84: elsif ARGV[i] == "-tt" 85: opts |= BDB::TTCBS 86: elsif ARGV[i] == "-nl" 87: omode |= BDB::ONOLCK 88: elsif ARGV[i] == "-nb" 89: omode |= BDB::OLCKNB 90: else 91: usage 92: end 93: elsif !path 94: path = ARGV[i] 95: elsif !rnum 96: rnum = ARGV[i].to_i 97: elsif !lmemb 98: lmemb = ARGV[i].to_i 99: elsif !nmemb 100: nmemb = ARGV[i].to_i 101: elsif !bnum 102: bnum = ARGV[i].to_i 103: elsif !apow 104: apow = ARGV[i].to_i 105: elsif !fpow 106: fpow = ARGV[i].to_i 107: else 108: usage 109: end 110: i += 1 111: end 112: usage if !path || !rnum || rnum < 1 113: lmemb = lmemb ? lmemb : -1 114: nmemb = nmemb ? nmemb : -1 115: bnum = bnum ? bnum : -1 116: apow = apow ? apow : -1 117: fpow = fpow ? fpow : -1 118: rv = procwrite(path, rnum, lmemb, nmemb, bnum, apow, fpow, opts, omode) 119: return rv 120: end
print the usage and exit
# File tcbtest.rb, line 43 43: def usage 44: STDERR.printf("%s: test cases of the B+ tree database API\n", $progname) 45: STDERR.printf("\n") 46: STDERR.printf("usage:\n") 47: STDERR.printf(" %s write [-tl] [-td|-tb|-tt] [-nl|-nb] path rnum" + 48: " [lmemb [nmemb [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