Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/sqlite/blob/master/ext/fts3/tool/fts3view.c # sqlite [Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `sqlite / ext / fts3 / tool / fts3view.c` 905 lines ยท 26871 bytes Raw Blame History 1 /* 2 ** This program is a debugging and analysis utility that displays 3 ** information about an FTS3 or FTS4 index. 4 ** 5 ** Link this program against the SQLite3 amalgamation with the 6 ** SQLITE_ENABLE_FTS4 compile-time option. Then run it as: 7 ** 8 ** fts3view DATABASE 9 ** 10 ** to get a list of all FTS3/4 tables in DATABASE, or do 11 ** 12 ** fts3view DATABASE TABLE COMMAND .... 13 ** 14 ** to see various aspects of the TABLE table. Type fts3view with no 15 ** arguments for a list of available COMMANDs. 16 */ 17 #include 18 #include 19 #include 20 #include 21 #include 22 #include "sqlite3.h" 23 24 /* 25 ** Extra command-line arguments: 26 */ 27 int nExtra; 28 char **azExtra; 29 30 /* 31 ** Look for a command-line argument. 32 */ 33 const char *findOption(const char *zName, int hasArg, const char *zDefault){ 34 int i; 35 const char *zResult = zDefault; 36 for(i=0; i=2000 ){ 237 n = 0; 238 pStmt = prepare(db, "SELECT count(*) FROM %s" 239 " WHERE col='*' AND occurrences<=%d", zAux, nDoc/1000); 240 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 241 n = sqlite3_column_int(pStmt, 0); 242 } 243 sqlite3_finalize(pStmt); 244 printf("Tokens used in 0.1%% or less of docs...... %9d %5.2f%%\n", 245 n, n*100.0/nToken); 246 } 247 248 if( nDoc>=200 ){ 249 n = 0; 250 pStmt = prepare(db, "SELECT count(*) FROM %s" 251 " WHERE col='*' AND occurrences<=%d", zAux, nDoc/100); 252 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 253 n = sqlite3_column_int(pStmt, 0); 254 } 255 sqlite3_finalize(pStmt); 256 printf("Tokens used in 1%% or less of docs........ %9d %5.2f%%\n", 257 n, n*100.0/nToken); 258 } 259 260 nTop = atoi(findOption("top", 1, "25")); 261 printf("The %d most common tokens:\n", nTop); 262 pStmt = prepare(db, 263 "SELECT term, documents FROM %s" 264 " WHERE col='*'" 265 " ORDER BY documents DESC, term" 266 " LIMIT %d", zAux, nTop); 267 i = 0; 268 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 269 i++; 270 n = sqlite3_column_int(pStmt, 1); 271 printf(" %2d. %-30s %9d docs %5.2f%%\n", i, 272 sqlite3_column_text(pStmt, 0), n, n*100.0/nDoc); 273 } 274 sqlite3_finalize(pStmt); 275 276 end_vocab: 277 runSql(db, "ROLLBACK"); 278 sqlite3_free(zAux); 279 } 280 281 /* 282 ** Report on the number and sizes of segments 283 */ 284 static void showSegmentStats(sqlite3 *db, const char *zTab){ 285 sqlite3_stmt *pStmt; 286 int nSeg = 0; 287 sqlite3_int64 szSeg = 0, mxSeg = 0; 288 int nIdx = 0; 289 sqlite3_int64 szIdx = 0, mxIdx = 0; 290 int nRoot = 0; 291 sqlite3_int64 szRoot = 0, mxRoot = 0; 292 sqlite3_int64 mx; 293 int nLeaf; 294 int n; 295 int pgsz; 296 int mxLevel; 297 int i; 298 299 pStmt = prepare(db, 300 "SELECT count(*), sum(length(block)), max(length(block))" 301 " FROM '%q_segments'", 302 zTab); 303 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 304 nSeg = sqlite3_column_int(pStmt, 0); 305 szSeg = sqlite3_column_int64(pStmt, 1); 306 mxSeg = sqlite3_column_int64(pStmt, 2); 307 } 308 sqlite3_finalize(pStmt); 309 pStmt = prepare(db, 310 "SELECT count(*), sum(length(block)), max(length(block))" 311 " FROM '%q_segments' a JOIN '%q_segdir' b" 312 " WHERE a.blockid BETWEEN b.leaves_end_block+1 AND b.end_block", 313 zTab, zTab); 314 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 315 nIdx = sqlite3_column_int(pStmt, 0); 316 szIdx = sqlite3_column_int64(pStmt, 1); 317 mxIdx = sqlite3_column_int64(pStmt, 2); 318 } 319 sqlite3_finalize(pStmt); 320 pStmt = prepare(db, 321 "SELECT count(*), sum(length(root)), max(length(root))" 322 " FROM '%q_segdir'", 323 zTab); 324 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 325 nRoot = sqlite3_column_int(pStmt, 0); 326 szRoot = sqlite3_column_int64(pStmt, 1); 327 mxRoot = sqlite3_column_int64(pStmt, 2); 328 } 329 sqlite3_finalize(pStmt); 330 331 printf("Number of segments....................... %9d\n", nSeg+nRoot); 332 printf("Number of leaf segments.................. %9d\n", nSeg-nIdx); 333 printf("Number of index segments................. %9d\n", nIdx); 334 printf("Number of root segments.................. %9d\n", nRoot); 335 printf("Total size of all segments............... %9lld\n", szSeg+szRoot); 336 printf("Total size of all leaf segments.......... %9lld\n", szSeg-szIdx); 337 printf("Total size of all index segments......... %9lld\n", szIdx); 338 printf("Total size of all root segments.......... %9lld\n", szRoot); 339 if( nSeg>0 ){ 340 printf("Average size of all segments............. %11.1f\n", 341 (double)(szSeg+szRoot)/(double)(nSeg+nRoot)); 342 printf("Average size of leaf segments............ %11.1f\n", 343 (double)(szSeg-szIdx)/(double)(nSeg-nIdx)); 344 } 345 if( nIdx>0 ){ 346 printf("Average size of index segments........... %11.1f\n", 347 (double)szIdx/(double)nIdx); 348 } 349 if( nRoot>0 ){ 350 printf("Average size of root segments............ %11.1f\n", 351 (double)szRoot/(double)nRoot); 352 } 353 mx = mxSeg; 354 if( mx%d", 371 zTab, zTab, pgsz-45); 372 n = 0; 373 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 374 n = sqlite3_column_int(pStmt, 0); 375 } 376 sqlite3_finalize(pStmt); 377 nLeaf = nSeg - nIdx; 378 printf("Leaf segments larger than %5d bytes.... %9d %5.2f%%\n", 379 pgsz-45, n, nLeaf>0 ? n*100.0/nLeaf : 0.0); 380 381 pStmt = prepare(db, "SELECT max(level%%1024) FROM '%q_segdir'", zTab); 382 mxLevel = 0; 383 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 384 mxLevel = sqlite3_column_int(pStmt, 0); 385 } 386 sqlite3_finalize(pStmt); 387 388 for(i=0; i<=mxLevel; i++){ 389 pStmt = prepare(db, 390 "SELECT count(*), sum(len), avg(len), max(len), sum(len>%d)," 391 " count(distinct idx)" 392 " FROM (SELECT length(a.block) AS len, idx" 393 " FROM '%q_segments' a JOIN '%q_segdir' b" 394 " WHERE (a.blockid BETWEEN b.start_block" 395 " AND b.leaves_end_block)" 396 " AND (b.level%%1024)==%d)", 397 pgsz-45, zTab, zTab, i); 398 if( sqlite3_step(pStmt)==SQLITE_ROW 399 && (nLeaf = sqlite3_column_int(pStmt, 0))>0 400 ){ 401 sqlite3_int64 sz; 402 nIdx = sqlite3_column_int(pStmt, 5); 403 printf("For level %d:\n", i); 404 printf(" Number of indexes...................... %9d\n", nIdx); 405 printf(" Number of leaf segments................ %9d\n", nLeaf); 406 if( nIdx>1 ){ 407 printf(" Average leaf segments per index........ %11.1f\n", 408 (double)nLeaf/(double)nIdx); 409 } 410 printf(" Total size of all leaf segments........ %9lld\n", 411 (sz = sqlite3_column_int64(pStmt, 1))); 412 printf(" Average size of leaf segments.......... %11.1f\n", 413 sqlite3_column_double(pStmt, 2)); 414 if( nIdx>1 ){ 415 printf(" Average leaf segment size per index.... %11.1f\n", 416 (double)sz/(double)nIdx); 417 } 418 printf(" Maximum leaf segment size.............. %9lld\n", 419 sqlite3_column_int64(pStmt, 3)); 420 n = sqlite3_column_int(pStmt, 4); 421 printf(" Leaf segments larger than %5d bytes.. %9d %5.2f%%\n", 422 pgsz-45, n, n*100.0/nLeaf); 423 } 424 sqlite3_finalize(pStmt); 425 } 426 } 427 428 /* 429 ** Print a single "tree" line of the segdir map output. 430 */ 431 static void printTreeLine(sqlite3_int64 iLower, sqlite3_int64 iUpper){ 432 printf(" tree %9lld", iLower); 433 if( iUpper>iLower ){ 434 printf(" thru %9lld (%lld blocks)", iUpper, iUpper-iLower+1); 435 } 436 printf("\n"); 437 } 438 439 /* 440 ** Check to see if the block of a %_segments entry is NULL. 441 */ 442 static int isNullSegment(sqlite3 *db, const char *zTab, sqlite3_int64 iBlockId){ 443 sqlite3_stmt *pStmt; 444 int rc = 1; 445 446 pStmt = prepare(db, "SELECT block IS NULL FROM '%q_segments'" 447 " WHERE blockid=%lld", zTab, iBlockId); 448 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 449 rc = sqlite3_column_int(pStmt, 0); 450 } 451 sqlite3_finalize(pStmt); 452 return rc; 453 } 454 455 /* 456 ** Show a map of segments derived from the %_segdir table. 457 */ 458 static void showSegdirMap(sqlite3 *db, const char *zTab){ 459 int mxIndex, iIndex; 460 sqlite3_stmt *pStmt = 0; 461 sqlite3_stmt *pStmt2 = 0; 462 int prevLevel; 463 464 pStmt = prepare(db, "SELECT max(level/1024) FROM '%q_segdir'", zTab); 465 if( sqlite3_step(pStmt)==SQLITE_ROW ){ 466 mxIndex = sqlite3_column_int(pStmt, 0); 467 }else{ 468 mxIndex = 0; 469 } 470 sqlite3_finalize(pStmt); 471 472 printf("Number of inverted indices............... %3d\n", mxIndex+1); 473 pStmt = prepare(db, 474 "SELECT level, idx, start_block, leaves_end_block, end_block, rowid" 475 " FROM '%q_segdir'" 476 " WHERE level/1024==?" 477 " ORDER BY level DESC, idx", 478 zTab); 479 pStmt2 = prepare(db, 480 "SELECT blockid FROM '%q_segments'" 481 " WHERE blockid BETWEEN ? AND ? ORDER BY blockid", 482 zTab); 483 for(iIndex=0; iIndex<=mxIndex; iIndex++){ 484 if( mxIndex>0 ){ 485 printf("**************************** Index %d " 486 "****************************\n", iIndex); 487 } 488 sqlite3_bind_int(pStmt, 1, iIndex); 489 prevLevel = -1; 490 while( sqlite3_step(pStmt)==SQLITE_ROW ){ 491 int iLevel = sqlite3_column_int(pStmt, 0)%1024; 492 int iIdx = sqlite3_column_int(pStmt, 1); 493 sqlite3_int64 iStart = sqlite3_column_int64(pStmt, 2); 494 sqlite3_int64 iLEnd = sqlite3_column_int64(pStmt, 3); 495 sqlite3_int64 iEnd = sqlite3_column_int64(pStmt, 4); 496 char rtag[20]; 497 if( iLevel!=prevLevel ){ 498 printf("level %2d idx %2d", iLevel, iIdx); 499 prevLevel = iLevel; 500 }else{ 501 printf(" idx %2d", iIdx); 502 } 503 sqlite3_snprintf(sizeof(rtag), rtag, "r%lld", 504 sqlite3_column_int64(pStmt,5)); 505 printf(" root %9s\n", rtag); 506 if( iLEnd>iStart ){ 507 sqlite3_int64 iLower, iPrev = 0, iX; 508 if( iLEnd+1<=iEnd ){ 509 sqlite3_bind_int64(pStmt2, 1, iLEnd+1); 510 sqlite3_bind_int64(pStmt2, 2, iEnd); 511 iLower = -1; 512 while( sqlite3_step(pStmt2)==SQLITE_ROW ){ 513 iX = sqlite3_column_int64(pStmt2, 0); 514 if( iLower<0 ){ 515 iLower = iPrev = iX; 516 }else if( iX==iPrev+1 ){ 517 iPrev = iX; 518 }else{ 519 printTreeLine(iLower, iPrev); 520 iLower = iPrev = iX; 521 } 522 } 523 sqlite3_reset(pStmt2); 524 if( iLower>=0 ){ 525 if( iLower==iPrev && iLower==iEnd 526 && isNullSegment(db,zTab,iLower) 527 ){ 528 printf(" null %9lld\n", iLower); 529 }else{ 530 printTreeLine(iLower, iPrev); 531 } 532 } 533 } 534 printf(" leaves %9lld thru %9lld (%lld blocks)\n", 535 iStart, iLEnd, iLEnd - iStart + 1); 536 } 537 } 538 sqlite3_reset(pStmt); 539 } 540 sqlite3_finalize(pStmt); 541 sqlite3_finalize(pStmt2); 542 } 543 544 /* 545 ** Decode a single segment block and display the results on stdout. 546 */ 547 static void decodeSegment( 548 const unsigned char *aInput, /* Content to print */ 549 int nData /* Number of bytes of content */ 550 ){ 551 sqlite3_int64 iChild = 0; 552 sqlite3_int64 iPrefix; 553 sqlite3_int64 nTerm; 554 sqlite3_int64 n; 555 sqlite3_int64 iDocsz; 556 int iHeight; 557 sqlite3_int64 i = 0; 558 int cnt = 0; 559 char zTerm[1000]; 560 561 unsigned char *aData = sqlite3_malloc64((sqlite3_int64)nData+64); 562 if( aData==0 ){ 563 fprintf(stderr, "failed to allocate %d bytes\n", nData+64); 564 exit(1); 565 } 566 memcpy(aData, aInput, nData); 567 568 i += getVarint(aData, &n); 569 iHeight = (int)n; 570 printf("height: %d\n", iHeight); 571 if( iHeight>0 ){ 572 i += getVarint(aData+i, &iChild); 573 printf("left-child: %lld\n", iChild); 574 } 575 while( i0 ){ 577 i += getVarint(aData+i, &iPrefix); 578 }else{ 579 iPrefix = 0; 580 } 581 i += getVarint(aData+i, &nTerm); 582 if( iPrefix<0 || nTerm<0 ){ 583 fprintf(stderr, "segment is corrupt\n"); 584 exit(1); 585 } 586 if( iPrefix+nTerm+1 >= (sqlite3_int64)sizeof(zTerm) ){ 587 fprintf(stderr, "term too long\n"); 588 exit(1); 589 } 590 if( (i + nTerm)>nData ){ 591 fprintf(stderr, "segment is corrupt\n"); 592 exit(1); 593 } 594 memcpy(zTerm+iPrefix, aData+i, (size_t)nTerm); 595 zTerm[iPrefix+nTerm] = 0; 596 i += nTerm; 597 if( iHeight==0 ){ 598 i += getVarint(aData+i, &iDocsz); 599 if( iDocsz<0 || iDocsz>0x7FFFFFFF ){ 600 fprintf(stderr, "segment is corrupt\n"); 601 exit(1); 602 } 603 printf("term: %-25s doclist %7lld bytes offset %lld\n", zTerm, iDocsz, i); 604 i += iDocsz; 605 }else{ 606 printf("term: %-25s child %lld\n", zTerm, ++iChild); 607 } 608 } 609 610 sqlite3_free(aData); 611 } 612 613 614 /* 615 ** Print a a blob as hex and ascii. 616 */ 617 static void printBlob( 618 const unsigned char *aData, /* Content to print */ 619 int nData /* Number of bytes of content */ 620 ){ 621 int i, j; 622 const char *zOfstFmt; 623 const int perLine = 16; 624 625 if( (nData&~0xfff)==0 ){ 626 zOfstFmt = " %03x: "; 627 }else if( (nData&~0xffff)==0 ){ 628 zOfstFmt = " %04x: "; 629 }else if( (nData&~0xfffff)==0 ){ 630 zOfstFmt = " %05x: "; 631 }else if( (nData&~0xffffff)==0 ){ 632 zOfstFmt = " %06x: "; 633 }else{ 634 zOfstFmt = " %08x: "; 635 } 636 637 for(i=0; inData ){ 641 fprintf(stdout, " "); 642 }else{ 643 fprintf(stdout,"%02x ", aData[i+j]); 644 } 645 } 646 for(j=0; jnData ){ 648 fprintf(stdout, " "); 649 }else{ 650 fprintf(stdout,"%c", isprint(aData[i+j]) ? aData[i+j] : '.'); 651 } 652 } 653 fprintf(stdout,"\n"); 654 } 655 } 656 657 /* 658 ** Convert text to a 64-bit integer 659 */ 660 static sqlite3_int64 atoi64(const char *z){ 661 sqlite3_int64 v = 0; 662 while( z[0]>='0' && z[0]<='9' ){ 663 v = v*10 + z[0] - '0'; 664 z++; 665 } 666 return v; 667 } 668 669 /* 670 ** Return a prepared statement which, when stepped, will return in its 671 ** first column the blob associated with segment zId. If zId begins with 672 ** 'r' then it is a rowid of a %_segdir entry. Otherwise it is a 673 ** %_segment entry. 674 */ 675 static sqlite3_stmt *prepareToGetSegment( 676 sqlite3 *db, /* The database */ 677 const char *zTab, /* The FTS3/4 table name */ 678 const char *zId /* ID of the segment to open */ 679 ){ 680 sqlite3_stmt *pStmt; 681 if( zId[0]=='r' ){ 682 pStmt = prepare(db, "SELECT root FROM '%q_segdir' WHERE rowid=%lld", 683 zTab, atoi64(zId+1)); 684 }else{ 685 pStmt = prepare(db, "SELECT block FROM '%q_segments' WHERE blockid=%lld", 686 zTab, atoi64(zId)); 687 } 688 return pStmt; 689 } 690 691 /* 692 ** Print the content of a segment or of the root of a segdir. The segment 693 ** or root is identified by azExtra[0]. If the first character of azExtra[0] 694 ** is 'r' then the remainder is the integer rowid of the %_segdir entry. 695 ** If the first character of azExtra[0] is not 'r' then, then all of 696 ** azExtra[0] is an integer which is the block number. 697 ** 698 ** If the --raw option is present in azExtra, then a hex dump is provided. 699 ** Otherwise a decoding is shown. 700 */ 701 static void showSegment(sqlite3 *db, const char *zTab){ 702 const unsigned char *aData; 703 int nData; 704 sqlite3_stmt *pStmt; 705 706 pStmt = prepareToGetSegment(db, zTab, azExtra[0]); 707 if( sqlite3_step(pStmt)!=SQLITE_ROW ){ 708 sqlite3_finalize(pStmt); 709 return; 710 } 711 nData = sqlite3_column_bytes(pStmt, 0); 712 aData = sqlite3_column_blob(pStmt, 0); 713 printf("Segment %s of size %d bytes:\n", azExtra[0], nData); 714 if( findOption("raw", 0, 0)!=0 ){ 715 printBlob(aData, nData); 716 }else{ 717 decodeSegment(aData, nData); 718 } 719 sqlite3_finalize(pStmt); 720 } 721 722 /* 723 ** Decode a single doclist and display the results on stdout. 724 */ 725 static void decodeDoclist( 726 const unsigned char *aInput, /* Content to print */ 727 int nData /* Number of bytes of content */ 728 ){ 729 sqlite3_int64 iPrevDocid = 0; 730 sqlite3_int64 iDocid; 731 sqlite3_int64 iPos; 732 sqlite3_int64 iPrevPos = 0; 733 sqlite3_int64 iCol; 734 int i = 0; 735 736 unsigned char *aData = sqlite3_malloc64((sqlite3_int64)nData+64); 737 if( aData==0 ){ 738 fprintf(stderr, "failed to allocate %d bytes\n", nData+64); 739 exit(1); 740 } 741 memcpy(aData, aInput, nData); 742 743 while( i