3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
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.
10 #*************************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
15 source $testdir/fts3_common.tcl
16 set ::testprefix fts4opt
18 # If SQLITE_ENABLE_FTS3 is defined, omit this file.
24 # Create the fts_kjv_genesis procedure which fills and FTS3/4 table
25 # with the complete text of the Book of Genesis.
27 source $testdir/genesis.tcl
29 do_execsql_test 1.0 { CREATE TABLE t1(docid, words) }
32 #-------------------------------------------------------------------------
33 # Argument $db is an open database handle. $tbl is the name of an FTS3/4
34 # table with the database. This command rearranges the contents of the
35 # %_segdir table so that all segments within each index are on the same
36 # level. This means that the 'merge' command can then be used for an
37 # incremental optimize routine.
39 proc prepare_for_optimize {db tbl} {
40 $db eval [string map [list % $tbl] {
42 CREATE TEMP TABLE tmp_segdir(
43 level, idx, start_block, leaves_end_block, end_block, root
46 INSERT INTO temp.tmp_segdir
48 1024*(o.level / 1024) + 32, -- level
49 sum(o.level<i.level OR (o.level=i.level AND o.idx>i.idx)), -- idx
50 o.start_block, o.leaves_end_block, o.end_block, o.root -- other
51 FROM %_segdir o, %_segdir i
52 WHERE (o.level / 1024) = (i.level / 1024)
53 GROUP BY o.level, o.idx;
56 INSERT INTO %_segdir SELECT * FROM temp.tmp_segdir;
57 DROP TABLE temp.tmp_segdir;
64 execsql { CREATE VIRTUAL TABLE t2 USING fts4(words, prefix="1,2,3") }
65 foreach {docid words} [db eval { SELECT * FROM t1 }] {
66 execsql { INSERT INTO t2(docid, words) VALUES($docid, $words) }
71 SELECT level, count(*) FROM t2_segdir GROUP BY level
74 1024 13 1025 15 1026 5
75 2048 13 2049 15 2050 5
76 3072 13 3073 15 3074 5
79 do_execsql_test 1.3 { INSERT INTO t2(t2) VALUES('integrity-check') }
80 prepare_for_optimize db t2
81 do_execsql_test 1.4 { INSERT INTO t2(t2) VALUES('integrity-check') }
84 SELECT level, count(*) FROM t2_segdir GROUP BY level
94 set tc1 [db total_changes]
95 execsql { INSERT INTO t2(t2) VALUES('merge=5,2') }
96 set tc2 [db total_changes]
97 if {($tc2 - $tc1) < 2} break
99 execsql { SELECT level, count(*) FROM t2_segdir GROUP BY level }
100 } {33 1 1057 1 2081 1 3105 1}
101 do_execsql_test 1.7 { INSERT INTO t2(t2) VALUES('integrity-check') }
103 do_execsql_test 1.8 {
104 INSERT INTO t2(words) SELECT words FROM t1;
105 SELECT level, count(*) FROM t2_segdir GROUP BY level;
106 } {0 2 1024 2 2048 2 3072 2}
108 #-------------------------------------------------------------------------
110 do_execsql_test 2.0 {
114 foreach {docid words} [db eval { SELECT * FROM t1 }] {
115 execsql { INSERT INTO t2(docid, words) VALUES($docid, $words) }
119 foreach {docid words} [db eval { SELECT * FROM t1 }] {
120 if {[incr i] % 2} { execsql { DELETE FROM t2 WHERE docid = $docid } }
124 foreach {docid words} [db eval { SELECT * FROM t1 }] {
126 execsql { INSERT OR REPLACE INTO t2(docid, words) VALUES($docid, $words) }
131 do_execsql_test 2.2 {
132 SELECT level, count(*) FROM t2_segdir GROUP BY level
135 1024 10 1025 15 1026 12
136 2048 10 2049 15 2050 12
137 3072 10 3073 15 3074 12
140 do_execsql_test 2.3 { INSERT INTO t2(t2) VALUES('integrity-check') }
141 prepare_for_optimize db t2
142 do_execsql_test 2.4 { INSERT INTO t2(t2) VALUES('integrity-check') }
144 do_execsql_test 2.5 {
145 SELECT level, count(*) FROM t2_segdir GROUP BY level
155 set tc1 [db total_changes]
156 execsql { INSERT INTO t2(t2) VALUES('merge=5,2') }
157 set tc2 [db total_changes]
158 if {($tc2 - $tc1) < 2} break
160 execsql { SELECT level, count(*) FROM t2_segdir GROUP BY level }
161 } {33 1 1057 1 2081 1 3105 1}
162 do_execsql_test 2.7 { INSERT INTO t2(t2) VALUES('integrity-check') }
164 do_execsql_test 2.8 {
165 INSERT INTO t2(words) SELECT words FROM t1;
166 SELECT level, count(*) FROM t2_segdir GROUP BY level;
167 } {0 2 1024 2 2048 2 3072 2}
169 #-------------------------------------------------------------------------
170 # Check that 'optimize' works when there is data in the in-memory hash
171 # table, but no segments at all on disk.
173 do_execsql_test 3.1 {
174 CREATE VIRTUAL TABLE fts USING fts4 (t);
175 INSERT INTO fts (fts) VALUES ('optimize');
177 do_execsql_test 3.2 {
178 INSERT INTO fts(fts) VALUES('integrity-check');
179 SELECT count(*) FROM fts_segdir;
181 do_execsql_test 3.3 {
183 INSERT INTO fts (rowid, t) VALUES (2, 'test');
184 INSERT INTO fts (fts) VALUES ('optimize');
186 SELECT level, idx FROM fts_segdir;
188 do_execsql_test 3.4 {
189 INSERT INTO fts(fts) VALUES('integrity-check');
190 SELECT rowid FROM fts WHERE fts MATCH 'test';
192 do_execsql_test 3.5 {
193 INSERT INTO fts (fts) VALUES ('optimize');
194 INSERT INTO fts(fts) VALUES('integrity-check');
197 set c1 [db total_changes]
198 execsql { INSERT INTO fts (fts) VALUES ('optimize') }
199 expr {[db total_changes] - $c1}
202 execsql { INSERT INTO fts (rowid, t) VALUES (3, 'xyz') }
203 set c1 [db total_changes]
204 execsql { INSERT INTO fts (fts) VALUES ('optimize') }
205 expr {([db total_changes] - $c1) > 1}
208 set c1 [db total_changes]
209 execsql { INSERT INTO fts (fts) VALUES ('optimize') }
210 expr {[db total_changes] - $c1}