showfts5.tcl 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #-------------------------------------------------------------------------
  2. # Process command line arguments.
  3. #
  4. proc usage {} {
  5. puts stderr "usage: $::argv0 ?OPTIONS? database table"
  6. puts stderr ""
  7. puts stderr " -nterm (count number of terms in each segment)"
  8. puts stderr " -segments (output segment contents)"
  9. puts stderr ""
  10. exit 1
  11. }
  12. set O(nterm) 0
  13. set O(segments) 0
  14. if {[llength $argv]<2} usage
  15. foreach a [lrange $argv 0 end-2] {
  16. switch -- $a {
  17. -nterm {
  18. set O(nterm) 1
  19. }
  20. -segments {
  21. set O(segments) 1
  22. }
  23. default {
  24. usage
  25. }
  26. }
  27. }
  28. set database [lindex $argv end-1]
  29. set tbl [lindex $argv end]
  30. #-------------------------------------------------------------------------
  31. # Count the number of terms in each segment of fts5 table $tbl. Store the
  32. # counts in the array variable in the parent context named by parameter
  33. # $arrayname, indexed by segment-id. Example:
  34. #
  35. # count_terms fts_tbl A
  36. # foreach {k v} [array get A] { puts "segid=$k nTerm=$v" }
  37. #
  38. proc count_terms {tbl arrayname} {
  39. upvar A $arrayname
  40. array unset A
  41. db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data" {
  42. set desc [lindex $d 0]
  43. if {[regexp {^segid=([0-9]*)} $desc -> id]} {
  44. foreach i [lrange $d 1 end] {
  45. if {[string match {term=*} $i]} { incr A($id) }
  46. }
  47. }
  48. }
  49. }
  50. #-------------------------------------------------------------------------
  51. # Start of main program.
  52. #
  53. sqlite3 db $database
  54. catch { load_static_extension db fts5 }
  55. if {$O(nterm)} { count_terms $tbl A }
  56. db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data WHERE id=10" {
  57. foreach lvl [lrange $d 1 end] {
  58. puts [lrange $lvl 0 2]
  59. foreach seg [lrange $lvl 3 end] {
  60. if {$::O(nterm)} {
  61. regexp {^id=([0-9]*)} $seg -> id
  62. set nTerm 0
  63. catch { set nTerm $A($id) }
  64. puts [format " % -28s nTerm=%d" $seg $nTerm]
  65. } else {
  66. puts [format " % -28s" $seg]
  67. }
  68. }
  69. }
  70. }
  71. if {$O(segments)} {
  72. puts ""
  73. db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data WHERE id>10" {
  74. puts $d
  75. }
  76. }