Path: | tcttest.rb |
Last Update: | Sun Oct 03 15:07:10 +0000 2010 |
print error message of table database
# File tcttest.rb, line 58 58: def eprint(tdb, func) 59: path = tdb.path 60: STDERR.printf("%s: %s: %s: %s\n", $progname, path ? path : "-", func, tdb.errmsg) 61: end
main routine
# File tcttest.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 tcttest.rb, line 376 376: def procmisc(path, rnum, opts, omode) 377: printf("<Miscellaneous Test>\n path=%s rnum=%d opts=%d omode=%d\n\n", 378: path, rnum, opts, omode) 379: err = false 380: stime = Time.now 381: tdb = TDB::new 382: if !tdb.tune(rnum / 50, 2, -1, opts) 383: eprint(tdb, "tune") 384: err = true 385: end 386: if !tdb.setcache(rnum / 10, 128, 256) 387: eprint(tdb, "setcache") 388: err = true 389: end 390: if !tdb.setxmsiz(rnum * 4) 391: eprint(tdb, "setxmsiz") 392: err = true 393: end 394: if !tdb.setdfunit(8) 395: eprint(tdb, "setdfunit") 396: err = true 397: end 398: if !tdb.open(path, TDB::OWRITER | TDB::OCREAT | TDB::OTRUNC | omode) 399: eprint(tdb, "open") 400: err = true 401: end 402: if !tdb.setindex("", TDB::ITDECIMAL) 403: eprint(tdb, "setindex") 404: err = true 405: end 406: if !tdb.setindex("str", TDB::ITLEXICAL) 407: eprint(tdb, "setindex") 408: err = true 409: end 410: if !tdb.setindex("num", TDB::ITDECIMAL) 411: eprint(tdb, "setindex") 412: err = true 413: end 414: if !tdb.setindex("type", TDB::ITDECIMAL) 415: eprint(tdb, "setindex") 416: err = true 417: end 418: if !tdb.setindex("flag", TDB::ITTOKEN) 419: eprint(tdb, "setindex") 420: err = true 421: end 422: if !tdb.setindex("text", TDB::ITQGRAM) 423: eprint(tdb, "setindex") 424: err = true 425: end 426: printf("writing:\n") 427: for i in 1..rnum 428: id = tdb.genuid 429: cols = { 430: "str" => id, 431: "num" => rand(id) + 1, 432: "type" => rand(32) + 1, 433: } 434: vbuf = "" 435: num = rand(5) 436: pt = 0 437: for j in 1..num 438: pt += rand(5) + 1 439: vbuf += "," if vbuf.length > 0 440: vbuf += pt.to_s 441: end 442: if vbuf.length > 0 443: cols["flag"] = vbuf 444: cols["text"] = vbuf 445: end 446: if !tdb.put(id, cols) 447: eprint(tdb, "put") 448: err = true 449: break 450: end 451: if rnum > 250 && i % (rnum / 250) == 0 452: print('.') 453: if i == rnum || i % (rnum / 10) == 0 454: printf(" (%08d)\n", i) 455: end 456: end 457: end 458: printf("reading:\n") 459: for i in 1..rnum 460: if !tdb.get(i) 461: eprint(tdb, "get") 462: err = true 463: break 464: end 465: if rnum > 250 && i % (rnum / 250) == 0 466: print('.') 467: if i == rnum || i % (rnum / 10) == 0 468: printf(" (%08d)\n", i) 469: end 470: end 471: end 472: printf("removing:\n") 473: for i in 1..rnum 474: if rand(2) == 0 && !tdb.out(i) 475: eprint(tdb, "out") 476: err = true 477: break 478: end 479: if rnum > 250 && i % (rnum / 250) == 0 480: print('.') 481: if i == rnum || i % (rnum / 10) == 0 482: printf(" (%08d)\n", i) 483: end 484: end 485: end 486: printf("checking iterator:\n") 487: if !tdb.iterinit 488: eprint(tdb, "iterinit") 489: err = true 490: end 491: inum = 0 492: while pkey = tdb.iternext 493: inum += 1 494: if !tdb.get(pkey) 495: eprint(tdb, "get") 496: err = true 497: end 498: if rnum > 250 && inum % (rnum / 250) == 0 499: print('.') 500: if inum == rnum || inum % (rnum / 10) == 0 501: printf(" (%08d)\n", i) 502: end 503: end 504: end 505: printf(" (%08d)\n", inum) if rnum > 250 506: if tdb.ecode != TDB::ENOREC || inum != tdb.rnum 507: eprint(tdb, "(validation)") 508: err = true 509: end 510: pkeys = tdb.fwmkeys("1", 10) 511: printf("checking counting:\n") 512: for i in 1..rnum 513: buf = sprintf("i:%d", rand(rnum)) 514: if rand(2) == 0 515: if !tdb.addint(buf, 1) 516: eprint(tdb, "addint") 517: err = true 518: break 519: end 520: else 521: if !tdb.adddouble(buf, 1) 522: eprint(tdb, "adddouble") 523: err = true 524: break 525: end 526: end 527: if rnum > 250 && i % (rnum / 250) == 0 528: print('.') 529: if i == rnum || i % (rnum / 10) == 0 530: printf(" (%08d)\n", i) 531: end 532: end 533: end 534: if !tdb.sync 535: eprint(tdb, "sync") 536: err = true 537: end 538: if !tdb.optimize 539: eprint(tdb, "optimize") 540: err = true 541: end 542: npath = path + "-tmp" 543: if !tdb.copy(npath) 544: eprint(tdb, "copy") 545: err = true 546: end 547: Dir.glob("#{npath}.idx.*").each do |tpath| 548: File.unlink(tpath) 549: end 550: File.unlink(npath) 551: printf("searching:\n") 552: qry = TDBQRY::new(tdb) 553: names = [ "", "str", "num", "type", "flag", "text", "c1" ] 554: ops = [ TDBQRY::QCSTREQ, TDBQRY::QCSTRINC, TDBQRY::QCSTRBW, TDBQRY::QCSTREW, TDBQRY::QCSTRAND, 555: TDBQRY::QCSTROR, TDBQRY::QCSTROREQ, TDBQRY::QCSTRRX, TDBQRY::QCNUMEQ, TDBQRY::QCNUMGT, 556: TDBQRY::QCNUMGE, TDBQRY::QCNUMLT, TDBQRY::QCNUMLE, TDBQRY::QCNUMBT, TDBQRY::QCNUMOREQ ] 557: ftsops = [ TDBQRY::QCFTSPH, TDBQRY::QCFTSAND, TDBQRY::QCFTSOR, TDBQRY::QCFTSEX ] 558: types = [ TDBQRY::QOSTRASC, TDBQRY::QOSTRDESC, TDBQRY::QONUMASC, TDBQRY::QONUMDESC ] 559: for i in 1..rnum 560: qry = TDBQRY::new(tdb) if rand(10) > 0 561: cnum = rand(4) 562: for j in 1..cnum 563: name = names[rand(names.length)] 564: op = ops[rand(ops.length)] 565: op = ftsops[rand(ftsops.length)] if rand(10) == 0 566: op |= TDBQRY::QCNEGATE if rand(20) == 0 567: op |= TDBQRY::QCNOIDX if rand(20) == 0 568: expr = rand(i).to_s 569: expr += "," + rand(i).to_s if rand(10) == 0 570: expr += "," + rand(i).to_s if rand(10) == 0 571: qry.addcond(name, op, expr) 572: end 573: if rand(3) != 0 574: name = names[rand(names.length)] 575: type = types[rand(types.length)] 576: qry.setorder(name, type) 577: end 578: qry.setlimit(rand(i), rand(10)) if rand(3) != 0 579: res = qry.search 580: if rnum > 250 && i % (rnum / 250) == 0 581: print('.') 582: if i == rnum || i % (rnum / 10) == 0 583: printf(" (%08d)\n", i) 584: end 585: end 586: end 587: qry = TDBQRY::new(tdb) 588: qry.addcond("", TDBQRY::QCSTRBW, "i:") 589: qry.setorder("_num", TDBQRY::QONUMDESC) 590: ires = qry.search 591: irnum = ires.length 592: itnum = tdb.rnum 593: icnt = 0 594: rv = qry.proc do |tpkey, tcols| 595: icnt += 1 596: tcols["icnt"] = icnt 597: TDBQRY::QPPUT 598: end 599: if !rv 600: eprint(tdb, "qry::proc") 601: err = true 602: end 603: qry.addcond("icnt", TDBQRY::QCNUMGT, 0) 604: mures = qry.metasearch([ qry, qry ], TDBQRY::MSUNION) 605: if mures.length != irnum 606: eprint(tdb, "qry::metasearch") 607: err = true 608: end 609: mires = qry.metasearch([ qry, qry ], TDBQRY::MSISECT) 610: if mires.length != irnum 611: eprint(tdb, "qry::metasearch") 612: err = true 613: end 614: mdres = qry.metasearch([ qry, qry ], TDBQRY::MSDIFF) 615: if mdres.length != 0 616: eprint(tdb, "qry::metasearch") 617: err = true 618: end 619: if !qry.searchout 620: eprint(tdb, "qry::searchout") 621: err = true 622: end 623: if tdb.rnum != itnum - irnum 624: eprint(tdb, "(validation)") 625: err = true 626: end 627: qry = TDBQRY::new(tdb) 628: qry.addcond("text", TDBQRY::QCSTRBW, "1") 629: qry.setlimit(100, 1) 630: qry.search().each do |tpkey| 631: cols = tdb.get(tpkey) 632: if cols 633: texts = qry.kwic(cols, "text", -1, TDBQRY::KWMUBRCT) 634: if texts.length > 0 635: texts.each do |text| 636: if !text.index("1") 637: eprint(tdb, "(validation)") 638: err = true 639: break 640: end 641: end 642: else 643: eprint(tdb, "(validation)") 644: err = true 645: break 646: end 647: else 648: eprint(tdb, "get") 649: err = true 650: break 651: end 652: end 653: if !tdb.vanish 654: eprint(tdb, "vanish") 655: err = true 656: end 657: printf("checking transaction commit:\n") 658: if !tdb.tranbegin 659: eprint(tdb, "tranbegin") 660: err = true 661: end 662: for i in 1..rnum 663: id = rand(rnum) + 1 664: if rand(2) == 0 665: if !tdb.addint(id, 1) 666: eprint(tdb, "addint") 667: err = true 668: break 669: end 670: else 671: if !tdb.out(id) && tdb.ecode != TDB::ENOREC 672: eprint(tdb, "out") 673: err = true 674: break 675: end 676: end 677: if rnum > 250 && i % (rnum / 250) == 0 678: print('.') 679: if i == rnum || i % (rnum / 10) == 0 680: printf(" (%08d)\n", i) 681: end 682: end 683: end 684: if !tdb.trancommit 685: eprint(tdb, "trancommit") 686: err = true 687: end 688: printf("checking transaction abort:\n") 689: ornum = tdb.rnum 690: ofsiz = tdb.fsiz 691: if !tdb.tranbegin 692: eprint(tdb, "tranbegin") 693: err = true 694: end 695: for i in 1..rnum 696: id = rand(rnum) + 1 697: if rand(2) == 0 698: if !tdb.addint(id, 1) 699: eprint(tdb, "addint") 700: err = true 701: break 702: end 703: else 704: if !tdb.out(id) && tdb.ecode != TDB::ENOREC 705: eprint(tdb, "out") 706: err = true 707: break 708: end 709: end 710: if rnum > 250 && i % (rnum / 250) == 0 711: print('.') 712: if i == rnum || i % (rnum / 10) == 0 713: printf(" (%08d)\n", i) 714: end 715: end 716: end 717: if !tdb.tranabort 718: eprint(tdb, "tranabort") 719: err = true 720: end 721: if tdb.rnum != ornum || tdb.fsiz != ofsiz 722: eprint(tdb, "(validation)") 723: err = true 724: end 725: printf("checking hash-like updating:\n") 726: for i in 1..rnum 727: buf = sprintf("[%d]", rand(rnum)) 728: rnd = rand(4) 729: if rnd == 0 730: cols = { 731: "name" => buf, 732: "num" => i, 733: } 734: tdb[buf] = cols 735: elsif rnd == 1 736: value = tdb[buf] 737: elsif rnd == 2 738: tdb.key?(buf) 739: elsif rnd == 3 740: tdb.delete(buf) 741: end 742: if rnum > 250 && i % (rnum / 250) == 0 743: print('.') 744: if i == rnum || i % (rnum / 10) == 0 745: printf(" (%08d)\n", i) 746: end 747: end 748: end 749: printf("checking hash-like iterator:\n") 750: inum = 0 751: tdb.each do |tkey, tvalue| 752: if inum > 0 && rnum > 250 && inum % (rnum / 250) == 0 753: print('.') 754: if inum == rnum || inum % (rnum / 10) == 0 755: printf(" (%08d)\n", inum) 756: end 757: end 758: inum += 1 759: end 760: printf(" (%08d)\n", inum) if rnum > 250 761: tdb.clear 762: printf("record number: %d\n", tdb.rnum) 763: printf("size: %d\n", tdb.fsiz) 764: if !tdb.close 765: eprint(tdb, "close") 766: err = true 767: end 768: printf("time: %.3f\n", Time.now - stime) 769: printf("%s\n\n", err ? "error" : "ok") 770: return err ? 1 : 0 771: end
perform read command
# File tcttest.rb, line 304 304: def procread(path, omode) 305: printf("<Reading Test>\n path=%s omode=%d\n\n", path, omode) 306: err = false 307: stime = Time.now 308: tdb = TDB::new 309: if !tdb.open(path, TDB::OREADER | omode) 310: eprint(tdb, "open") 311: err = true 312: end 313: rnum = tdb.rnum 314: for i in 1..rnum 315: if !tdb.get(i) 316: eprint(tdb, "get") 317: err = true 318: break 319: end 320: if rnum > 250 && i % (rnum / 250) == 0 321: print('.') 322: if i == rnum || i % (rnum / 10) == 0 323: printf(" (%08d)\n", i) 324: end 325: end 326: end 327: printf("record number: %d\n", tdb.rnum) 328: printf("size: %d\n", tdb.fsiz) 329: if !tdb.close 330: eprint(tdb, "close") 331: err = true 332: end 333: printf("time: %.3f\n", Time.now - stime) 334: printf("%s\n\n", err ? "error" : "ok") 335: return err ? 1 : 0 336: end
perform remove command
# File tcttest.rb, line 340 340: def procremove(path, omode) 341: printf("<Removing Test>\n path=%s omode=%d\n\n", path, omode) 342: err = false 343: stime = Time.now 344: tdb = TDB::new 345: if !tdb.open(path, TDB::OWRITER | omode) 346: eprint(tdb, "open") 347: err = true 348: end 349: rnum = tdb.rnum 350: for i in 1..rnum 351: if !tdb.out(i) 352: eprint(tdb, "out") 353: err = true 354: break 355: end 356: if rnum > 250 && i % (rnum / 250) == 0 357: print('.') 358: if i == rnum || i % (rnum / 10) == 0 359: printf(" (%08d)\n", i) 360: end 361: end 362: end 363: printf("record number: %d\n", tdb.rnum) 364: printf("size: %d\n", tdb.fsiz) 365: if !tdb.close 366: eprint(tdb, "close") 367: err = true 368: end 369: printf("time: %.3f\n", Time.now - stime) 370: printf("%s\n\n", err ? "error" : "ok") 371: return err ? 1 : 0 372: end
perform write command
# File tcttest.rb, line 222 222: def procwrite(path, rnum, bnum, apow, fpow, opts, iflags, omode) 223: printf("<Writing Test>\n path=%s rnum=%d bnum=%d apow=%d fpow=%d opts=%d iflags=%d" + 224: " omode=%d\n\n", path, rnum, bnum, apow, fpow, opts, iflags, omode) 225: err = false 226: stime = Time.now 227: tdb = TDB::new 228: if !tdb.tune(bnum, apow, fpow, opts) 229: eprint(tdb, "tune") 230: err = true 231: end 232: if !tdb.open(path, TDB::OWRITER | TDB::OCREAT | TDB::OTRUNC | omode) 233: eprint(tdb, "open") 234: err = true 235: end 236: if (iflags & (1 << 0)) != 0 && !tdb.setindex("", TDB::ITDECIMAL) 237: eprint(tdb, "setindex") 238: err = true 239: end 240: if (iflags & (1 << 1)) != 0 && !tdb.setindex("str", TDB::ITLEXICAL) 241: eprint(tdb, "setindex") 242: err = true 243: end 244: if (iflags & (1 << 2)) != 0 && !tdb.setindex("num", TDB::ITDECIMAL) 245: eprint(tdb, "setindex") 246: err = true 247: end 248: if (iflags & (1 << 3)) != 0 && !tdb.setindex("type", TDB::ITDECIMAL) 249: eprint(tdb, "setindex") 250: err = true 251: end 252: if (iflags & (1 << 4)) != 0 && !tdb.setindex("flag", TDB::ITTOKEN) 253: eprint(tdb, "setindex") 254: err = true 255: end 256: if (iflags & (1 << 5)) != 0 && !tdb.setindex("text", TDB::ITQGRAM) 257: eprint(tdb, "setindex") 258: err = true 259: end 260: for i in 1..rnum 261: id = tdb.genuid 262: cols = { 263: "str" => id, 264: "num" => rand(id) + 1, 265: "type" => rand(32) + 1, 266: } 267: vbuf = "" 268: num = rand(5) 269: pt = 0 270: for j in 1..num 271: pt += rand(5) + 1 272: vbuf += "," if vbuf.length > 0 273: vbuf += pt.to_s 274: end 275: if vbuf.length > 0 276: cols["flag"] = vbuf 277: cols["text"] = vbuf 278: end 279: if !tdb.put(id, cols) 280: eprint(tdb, "put") 281: err = true 282: break 283: end 284: if rnum > 250 && i % (rnum / 250) == 0 285: print('.') 286: if i == rnum || i % (rnum / 10) == 0 287: printf(" (%08d)\n", i) 288: end 289: end 290: end 291: printf("record number: %d\n", tdb.rnum) 292: printf("size: %d\n", tdb.fsiz) 293: if !tdb.close 294: eprint(tdb, "close") 295: err = true 296: end 297: printf("time: %.3f\n", Time.now - stime) 298: printf("%s\n\n", err ? "error" : "ok") 299: return err ? 1 : 0 300: end
parse arguments of misc command
# File tcttest.rb, line 183 183: def runmisc 184: path = nil 185: rnum = nil 186: opts = 0 187: omode = 0 188: i = 1 189: while i < ARGV.length 190: if !path && ARGV[i] =~ /^-/ 191: if ARGV[i] == "-tl" 192: opts |= TDB::TLARGE 193: elsif ARGV[i] == "-td" 194: opts |= TDB::TDEFLATE 195: elsif ARGV[i] == "-tb" 196: opts |= TDB::TBZIP 197: elsif ARGV[i] == "-tt" 198: opts |= TDB::TTCBS 199: elsif ARGV[i] == "-nl" 200: omode |= TDB::ONOLCK 201: elsif ARGV[i] == "-nb" 202: omode |= TDB::OLCKNB 203: else 204: usage 205: end 206: elsif !path 207: path = ARGV[i] 208: elsif !rnum 209: rnum = ARGV[i].to_i 210: else 211: usage 212: end 213: i += 1 214: end 215: usage if !path || !rnum || rnum < 1 216: rv = procmisc(path, rnum, opts, omode) 217: return rv 218: end
parse arguments of read command
# File tcttest.rb, line 129 129: def runread 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 |= TDB::ONOLCK 137: elsif ARGV[i] == "-nb" 138: omode |= TDB::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 = procread(path, omode) 151: return rv 152: end
parse arguments of remove command
# File tcttest.rb, line 156 156: def runremove 157: path = nil 158: omode = 0 159: i = 1 160: while i < ARGV.length 161: if !path && ARGV[i] =~ /^-/ 162: if ARGV[i] == "-nl" 163: omode |= TDB::ONOLCK 164: elsif ARGV[i] == "-nb" 165: omode |= TDB::OLCKNB 166: else 167: usage 168: end 169: elsif !path 170: path = ARGV[i] 171: else 172: usage 173: end 174: i += 1 175: end 176: usage if !path 177: rv = procremove(path, omode) 178: return rv 179: end
parse arguments of write command
# File tcttest.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: iflags = 0 73: omode = 0 74: i = 1 75: while i < ARGV.length 76: if !path && ARGV[i] =~ /^-/ 77: if ARGV[i] == "-tl" 78: opts |= TDB::TLARGE 79: elsif ARGV[i] == "-td" 80: opts |= TDB::TDEFLATE 81: elsif ARGV[i] == "-tb" 82: opts |= TDB::TBZIP 83: elsif ARGV[i] == "-tt" 84: opts |= TDB::TTCBS 85: elsif ARGV[i] == "-ip" 86: iflags |= 1 << 0 87: elsif ARGV[i] == "-is" 88: iflags |= 1 << 1 89: elsif ARGV[i] == "-in" 90: iflags |= 1 << 2 91: elsif ARGV[i] == "-it" 92: iflags |= 1 << 3 93: elsif ARGV[i] == "-if" 94: iflags |= 1 << 4 95: elsif ARGV[i] == "-ix" 96: iflags |= 1 << 5 97: elsif ARGV[i] == "-nl" 98: omode |= TDB::ONOLCK 99: elsif ARGV[i] == "-nb" 100: omode |= TDB::OLCKNB 101: else 102: usage 103: end 104: elsif !path 105: path = ARGV[i] 106: elsif !rnum 107: rnum = ARGV[i].to_i 108: elsif !bnum 109: bnum = ARGV[i].to_i 110: elsif !apow 111: apow = ARGV[i].to_i 112: elsif !fpow 113: fpow = ARGV[i].to_i 114: else 115: usage 116: end 117: i += 1 118: end 119: usage if !path || !rnum || rnum < 1 120: bnum = bnum ? bnum : -1 121: apow = apow ? apow : -1 122: fpow = fpow ? fpow : -1 123: rv = procwrite(path, rnum, bnum, apow, fpow, opts, iflags, omode) 124: return rv 125: end
print the usage and exit
# File tcttest.rb, line 43 43: def usage 44: STDERR.printf("%s: test cases of the table database API\n", $progname) 45: STDERR.printf("\n") 46: STDERR.printf("usage:\n") 47: STDERR.printf(" %s write [-tl] [-td|-tb|-tt] [-ip|-is|-in|-it|-if|-ix] [-nl|-nb] 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