1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #-------------------------------------------------------------------------
- # Process command line arguments.
- #
- proc usage {} {
- puts stderr "usage: $::argv0 ?OPTIONS? database table"
- puts stderr ""
- puts stderr " -nterm (count number of terms in each segment)"
- puts stderr " -segments (output segment contents)"
- puts stderr ""
- exit 1
- }
- set O(nterm) 0
- set O(segments) 0
- if {[llength $argv]<2} usage
- foreach a [lrange $argv 0 end-2] {
- switch -- $a {
- -nterm {
- set O(nterm) 1
- }
- -segments {
- set O(segments) 1
- }
- default {
- usage
- }
- }
- }
- set database [lindex $argv end-1]
- set tbl [lindex $argv end]
- #-------------------------------------------------------------------------
- # Count the number of terms in each segment of fts5 table $tbl. Store the
- # counts in the array variable in the parent context named by parameter
- # $arrayname, indexed by segment-id. Example:
- #
- # count_terms fts_tbl A
- # foreach {k v} [array get A] { puts "segid=$k nTerm=$v" }
- #
- proc count_terms {tbl arrayname} {
- upvar A $arrayname
- array unset A
- db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data" {
- set desc [lindex $d 0]
- if {[regexp {^segid=([0-9]*)} $desc -> id]} {
- foreach i [lrange $d 1 end] {
- if {[string match {term=*} $i]} { incr A($id) }
- }
- }
- }
- }
- #-------------------------------------------------------------------------
- # Start of main program.
- #
- sqlite3 db $database
- catch { load_static_extension db fts5 }
- if {$O(nterm)} { count_terms $tbl A }
- db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data WHERE id=10" {
- foreach lvl [lrange $d 1 end] {
- puts [lrange $lvl 0 2]
- foreach seg [lrange $lvl 3 end] {
- if {$::O(nterm)} {
- regexp {^id=([0-9]*)} $seg -> id
- set nTerm 0
- catch { set nTerm $A($id) }
- puts [format " % -28s nTerm=%d" $seg $nTerm]
- } else {
- puts [format " % -28s" $seg]
- }
- }
- }
- }
- if {$O(segments)} {
- puts ""
- db eval "SELECT fts5_decode(rowid, block) AS d FROM ${tbl}_data WHERE id>10" {
- puts $d
- }
- }
|