github.com/jdgcs/sqlite3@v1.12.1-0.20210908114423-bc5f96e4dd51/testdata/tcl/fts3defer3.test (about)

     1  # 2010 October 23
     2  #
     3  # The author disclaims copyright to this source code.  In place of
     4  # a legal notice, here is a blessing:
     5  #
     6  #    May you do good and not evil.
     7  #    May you find forgiveness for yourself and forgive others.
     8  #    May you share freely, never taking more than you give.
     9  #
    10  #***********************************************************************
    11  #
    12  # This file contains a very simple test to show that the deferred tokens
    13  # optimization is doing something.
    14  #
    15  
    16  set testdir [file dirname $argv0]
    17  source $testdir/tester.tcl
    18  source $testdir/malloc_common.tcl
    19  ifcapable !fts3||!fts4_deferred {
    20    finish_test 
    21    return
    22  }
    23  set testprefix fts3defer3
    24  
    25  set nDoclist 3204
    26  set nDoc     800
    27  
    28  # Set up a database that contains 800 rows. Each row contains the document
    29  # "b b", except for the row with docid=200, which contains "a b". Hence
    30  # token "b" is extremely common and token "a" is not.
    31  #
    32  do_test 1.1 {
    33    execsql {
    34      CREATE VIRTUAL TABLE t1 USING fts4;
    35        BEGIN;
    36    }
    37    for {set i 1} {$i <= $nDoc} {incr i} {
    38      set document "b b"
    39      if {$i==200} { set document "a b" }
    40      execsql { INSERT INTO t1 (docid, content) VALUES($i, $document) }
    41    }
    42    execsql COMMIT
    43  } {}
    44  
    45  # Check that the db contains two doclists. A small one for "a" and a 
    46  # larger one for "b".
    47  #
    48  do_execsql_test 1.2 {
    49    SELECT blockid, length(block) FROM t1_segments;
    50  } [list 1 8 2 $nDoclist]
    51  
    52  # Query for 'a b'. Although this test doesn't prove so, token "b" will 
    53  # be deferred because of the very large associated doclist.
    54  #
    55  do_execsql_test 1.3 {
    56    SELECT docid, content FROM t1 WHERE t1 MATCH 'a b';
    57  } {200 {a b}}
    58  
    59  # Zero out the doclist for token "b" within the database file. Now the 
    60  # only queries that use token "b" that will work are those that defer
    61  # it. Any query that tries to use the doclist belonging to token "b"
    62  # will fail.
    63  #
    64  do_test 1.4 {
    65    set fd [db incrblob t1_segments block 2]
    66    puts -nonewline $fd [string repeat "\00" $nDoclist]
    67    close $fd
    68  } {}
    69  
    70  # The first two queries succeed, as they defer token "b". The last one
    71  # fails, as it tries to load the corrupt doclist.
    72  #
    73  do_execsql_test 1.5 {
    74    SELECT docid, content FROM t1 WHERE t1 MATCH 'a b';
    75  } {200 {a b}}
    76  do_execsql_test 1.6 {
    77    SELECT count(*) FROM t1 WHERE t1 MATCH 'a b';
    78  } {1}
    79  do_catchsql_test 1.7 {
    80    SELECT count(*) FROM t1 WHERE t1 MATCH 'b';
    81  } {1 {database disk image is malformed}}
    82  
    83  
    84  finish_test