xref: /sqlite-3.40.0/test/fts3defer3.test (revision 07490d2b)
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
16set testdir [file dirname $argv0]
17source $testdir/tester.tcl
18source $testdir/malloc_common.tcl
19ifcapable !fts3||!fts4_deferred {
20  finish_test
21  return
22}
23set testprefix fts3defer3
24
25set nDoclist 3204
26set 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#
32do_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#
48do_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#
55do_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#
64do_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#
73do_execsql_test 1.5 {
74  SELECT docid, content FROM t1 WHERE t1 MATCH 'a b';
75} {200 {a b}}
76do_execsql_test 1.6 {
77  SELECT count(*) FROM t1 WHERE t1 MATCH 'a b';
78} {1}
79do_catchsql_test 1.7 {
80  SELECT count(*) FROM t1 WHERE t1 MATCH 'b';
81} {1 {database disk image is malformed}}
82
83
84finish_test
85