modernc.org/cc@v1.0.1/v2/testdata/_sqlite/test/fts4growth.test (about) 1 # 2014 May 12 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 # This file implements regression tests for SQLite library. The 12 # focus of this script is testing the FTS4 module. 13 # 14 # 15 16 set testdir [file dirname $argv0] 17 source $testdir/tester.tcl 18 set testprefix fts4growth 19 20 # If SQLITE_ENABLE_FTS3 is defined, omit this file. 21 ifcapable !fts3 { 22 finish_test 23 return 24 } 25 26 source $testdir/genesis.tcl 27 28 do_execsql_test 1.1 { CREATE VIRTUAL TABLE x1 USING fts3; } 29 30 do_test 1.2 { 31 foreach L { 32 {"See here, young man," said Mulga Bill, "from Walgett to the sea,} 33 {From Conroy's Gap to Castlereagh, there's none can ride like me.} 34 {I'm good all round at everything as everybody knows,} 35 {Although I'm not the one to talk -- I hate a man that blows.} 36 } { 37 execsql { INSERT INTO x1 VALUES($L) } 38 } 39 execsql { SELECT end_block, length(root) FROM x1_segdir } 40 } {{0 114} 114 {0 118} 118 {0 95} 95 {0 115} 115} 41 42 do_execsql_test 1.3 { 43 INSERT INTO x1(x1) VALUES('optimize'); 44 SELECT level, end_block, length(root) FROM x1_segdir; 45 } {0 {0 394} 394} 46 47 do_test 1.4 { 48 foreach L { 49 {But riding is my special gift, my chiefest, sole delight;} 50 {Just ask a wild duck can it swim, a wildcat can it fight.} 51 {There's nothing clothed in hair or hide, or built of flesh or steel,} 52 {There's nothing walks or jumps, or runs, on axle, hoof, or wheel,} 53 {But what I'll sit, while hide will hold and girths and straps are tight:} 54 {I'll ride this here two-wheeled concern right straight away at sight."} 55 } { 56 execsql { INSERT INTO x1 VALUES($L) } 57 } 58 execsql { 59 INSERT INTO x1(x1) VALUES('merge=4,4'); 60 SELECT level, end_block, length(root) FROM x1_segdir; 61 } 62 } {1 {224 921} 2} 63 64 do_execsql_test 1.5 { 65 SELECT length(block) FROM x1_segments; 66 } {921 {}} 67 68 do_test 1.6 { 69 foreach L { 70 {'Twas Mulga Bill, from Eaglehawk, that sought his own abode,} 71 {That perched above Dead Man's Creek, beside the mountain road.} 72 {He turned the cycle down the hill and mounted for the fray,} 73 {But 'ere he'd gone a dozen yards it bolted clean away.} 74 75 {It left the track, and through the trees, just like a silver steak,} 76 {It whistled down the awful slope towards the Dead Man's Creek.} 77 {It shaved a stump by half an inch, it dodged a big white-box:} 78 {The very wallaroos in fright went scrambling up the rocks,} 79 80 {The wombats hiding in their caves dug deeper underground,} 81 {As Mulga Bill, as white as chalk, sat tight to every bound.} 82 {It struck a stone and gave a spring that cleared a fallen tree,} 83 {It raced beside a precipice as close as close could be;} 84 85 {And then as Mulga Bill let out one last despairing shriek} 86 {It made a leap of twenty feet into the Dead Man's Creek.} 87 {It shaved a stump by half an inch, it dodged a big white-box:} 88 {The very wallaroos in fright went scrambling up the rocks,} 89 {The wombats hiding in their caves dug deeper underground,} 90 } { 91 execsql { INSERT INTO x1 VALUES($L) } 92 } 93 execsql { 94 SELECT level, end_block, length(root) FROM x1_segdir; 95 } 96 } {1 {224 921} 2 1 {226 1230} 7 0 {0 98} 98} 97 98 do_execsql_test 1.7 { 99 SELECT sum(length(block)) FROM x1_segments WHERE blockid IN (224,225,226) 100 } {1230} 101 102 #------------------------------------------------------------------------- 103 # 104 do_execsql_test 2.1 { 105 CREATE TABLE t1(docid, words); 106 CREATE VIRTUAL TABLE x2 USING fts4; 107 } 108 fts_kjv_genesis 109 do_test 2.2 { 110 foreach id [db eval {SELECT docid FROM t1}] { 111 execsql { 112 INSERT INTO x2(docid, content) SELECT $id, words FROM t1 WHERE docid=$id 113 } 114 } 115 foreach id [db eval {SELECT docid FROM t1}] { 116 execsql { 117 INSERT INTO x2(docid, content) SELECT NULL, words FROM t1 WHERE docid=$id 118 } 119 if {[db one {SELECT count(*) FROM x2_segdir WHERE level<2}]==2} break 120 } 121 } {} 122 123 do_execsql_test 2.3 { 124 SELECT count(*) FROM x2_segdir WHERE level=2; 125 SELECT count(*) FROM x2_segdir WHERE level=3; 126 } {6 0} 127 128 do_execsql_test 2.4 { 129 INSERT INTO x2(x2) VALUES('merge=4,4'); 130 SELECT count(*) FROM x2_segdir WHERE level=2; 131 SELECT count(*) FROM x2_segdir WHERE level=3; 132 } {6 1} 133 134 do_execsql_test 2.5 { 135 SELECT end_block FROM x2_segdir WHERE level=3; 136 INSERT INTO x2(x2) VALUES('merge=4,4'); 137 SELECT end_block FROM x2_segdir WHERE level=3; 138 INSERT INTO x2(x2) VALUES('merge=4,4'); 139 SELECT end_block FROM x2_segdir WHERE level=3; 140 } {{5588 -3950} {5588 -11766} {5588 -15541}} 141 142 do_execsql_test 2.6 { 143 SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE 144 blockid BETWEEN start_block AND leaves_end_block 145 AND level=3 146 } {15541} 147 148 do_execsql_test 2.7 { 149 INSERT INTO x2(x2) VALUES('merge=1000,4'); 150 SELECT end_block FROM x2_segdir WHERE level=3; 151 } {{5588 127563}} 152 153 do_execsql_test 2.8 { 154 SELECT sum(length(block)) FROM x2_segdir, x2_segments WHERE 155 blockid BETWEEN start_block AND leaves_end_block 156 AND level=3 157 } {127563} 158 159 #-------------------------------------------------------------------------- 160 # Test that delete markers are removed from FTS segments when possible. 161 # It is only possible to remove delete markers when the output of the 162 # merge operation will become the oldest segment in the index. 163 # 164 # 3.1 - when the oldest segment is created by an 'optimize'. 165 # 3.2 - when the oldest segment is created by an incremental merge. 166 # 3.3 - by a crisis merge. 167 # 168 169 proc insert_doc {args} { 170 foreach iDoc $args { 171 set L [lindex { 172 {In your eagerness to engage the Trojans,} 173 {don’t any of you charge ahead of others,} 174 {trusting in your strength and horsemanship.} 175 {And don’t lag behind. That will hurt our charge.} 176 {Any man whose chariot confronts an enemy’s} 177 {should thrust with his spear at him from there.} 178 {That’s the most effective tactic, the way} 179 {men wiped out city strongholds long ago —} 180 {their chests full of that style and spirit.} 181 } [expr $iDoc%9]] 182 execsql { REPLACE INTO x3(docid, content) VALUES($iDoc, $L) } 183 } 184 } 185 186 proc delete_doc {args} { 187 foreach iDoc $args { 188 execsql { DELETE FROM x3 WHERE docid = $iDoc } 189 } 190 } 191 192 proc second {x} { lindex $x 1 } 193 db func second second 194 195 do_execsql_test 3.0 { CREATE VIRTUAL TABLE x3 USING fts4 } 196 197 do_test 3.1.1 { 198 db transaction { insert_doc 1 2 3 4 5 6 } 199 execsql { SELECT level, idx, second(end_block) FROM x3_segdir } 200 } {0 0 412} 201 do_test 3.1.2 { 202 delete_doc 1 2 3 4 5 6 203 execsql { SELECT count(*) FROM x3_segdir } 204 } {0} 205 do_test 3.1.3 { 206 db transaction { 207 insert_doc 1 2 3 4 5 6 7 8 9 208 delete_doc 9 8 7 209 } 210 execsql { SELECT level, idx, second(end_block) FROM x3_segdir } 211 } {0 0 591 0 1 65 0 2 72 0 3 76} 212 do_test 3.1.4 { 213 execsql { INSERT INTO x3(x3) VALUES('optimize') } 214 execsql { SELECT level, idx, second(end_block) FROM x3_segdir } 215 } {0 0 412} 216 217 do_test 3.2.1 { 218 execsql { DELETE FROM x3 } 219 insert_doc 8 7 6 5 4 3 2 1 220 delete_doc 7 8 221 execsql { SELECT count(*) FROM x3_segdir } 222 } {10} 223 do_test 3.2.2 { 224 execsql { INSERT INTO x3(x3) VALUES('merge=500,10') } 225 execsql { SELECT level, idx, second(end_block) FROM x3_segdir } 226 } {1 0 412} 227 228 # This assumes the crisis merge happens when there are already 16 229 # segments and one more is added. 230 # 231 do_test 3.3.1 { 232 execsql { DELETE FROM x3 } 233 insert_doc 1 2 3 4 5 6 7 8 9 10 11 234 delete_doc 11 10 9 8 7 235 execsql { SELECT count(*) FROM x3_segdir } 236 } {16} 237 238 do_test 3.3.2 { 239 insert_doc 12 240 execsql { SELECT level, idx, second(end_block) FROM x3_segdir WHERE level=1 } 241 } {1 0 412} 242 243 #-------------------------------------------------------------------------- 244 # Check a theory on a bug in fts4 - that segments with idx==0 were not 245 # being incrementally merged correctly. Theory turned out to be false. 246 # 247 do_execsql_test 4.1 { 248 DROP TABLE IF EXISTS x4; 249 DROP TABLE IF EXISTS t1; 250 CREATE TABLE t1(docid, words); 251 CREATE VIRTUAL TABLE x4 USING fts4(words); 252 } 253 do_test 4.2 { 254 fts_kjv_genesis 255 execsql { INSERT INTO x4 SELECT words FROM t1 } 256 execsql { INSERT INTO x4 SELECT words FROM t1 } 257 } {} 258 259 do_execsql_test 4.3 { 260 SELECT level, idx, second(end_block) FROM x4_segdir 261 } {0 0 117483 0 1 118006} 262 263 do_execsql_test 4.4 { 264 INSERT INTO x4(x4) VALUES('merge=10,2'); 265 SELECT count(*) FROM x4_segdir; 266 } {3} 267 268 do_execsql_test 4.5 { 269 INSERT INTO x4(x4) VALUES('merge=10,2'); 270 SELECT count(*) FROM x4_segdir; 271 } {3} 272 273 do_execsql_test 4.6 { 274 INSERT INTO x4(x4) VALUES('merge=1000,2'); 275 SELECT count(*) FROM x4_segdir; 276 } {1} 277 278 279 280 #-------------------------------------------------------------------------- 281 # Check that segments are not promoted if the "end_block" field does not 282 # contain a size. 283 # 284 do_execsql_test 5.1 { 285 DROP TABLE IF EXISTS x2; 286 DROP TABLE IF EXISTS t1; 287 CREATE TABLE t1(docid, words); 288 CREATE VIRTUAL TABLE x2 USING fts4; 289 } 290 fts_kjv_genesis 291 292 proc first {L} {lindex $L 0} 293 db func first first 294 295 do_test 5.2 { 296 foreach r [db eval { SELECT rowid FROM t1 }] { 297 execsql { 298 INSERT INTO x2(docid, content) SELECT docid, words FROM t1 WHERE rowid=$r 299 } 300 } 301 foreach d [db eval { SELECT docid FROM t1 LIMIT -1 OFFSET 20 }] { 302 execsql { DELETE FROM x2 WHERE docid = $d } 303 } 304 305 execsql { 306 INSERT INTO x2(x2) VALUES('optimize'); 307 SELECT level, idx, end_block FROM x2_segdir 308 } 309 } {2 0 {752 1926}} 310 311 do_execsql_test 5.3 { 312 UPDATE x2_segdir SET end_block = CAST( first(end_block) AS INTEGER ); 313 SELECT end_block, typeof(end_block) FROM x2_segdir; 314 } {752 integer} 315 316 do_execsql_test 5.4 { 317 INSERT INTO x2 SELECT words FROM t1 LIMIT 50; 318 SELECT level, idx, end_block FROM x2_segdir 319 } {2 0 752 0 0 {758 5174}} 320 321 do_execsql_test 5.5 { 322 UPDATE x2_segdir SET end_block = end_block || ' 1926' WHERE level=2; 323 INSERT INTO x2 SELECT words FROM t1 LIMIT 40; 324 SELECT level, idx, end_block FROM x2_segdir 325 } {0 0 {752 1926} 0 1 {758 5174} 0 2 {763 4170}} 326 327 proc t1_to_x2 {} { 328 foreach id [db eval {SELECT docid FROM t1 LIMIT 2}] { 329 execsql { 330 DELETE FROM x2 WHERE docid=$id; 331 INSERT INTO x2(docid, content) SELECT $id, words FROM t1 WHERE docid=$id; 332 } 333 } 334 } 335 336 #-------------------------------------------------------------------------- 337 # Check that segments created by auto-merge are not promoted until they 338 # are completed. 339 # 340 341 do_execsql_test 6.1 { 342 CREATE VIRTUAL TABLE x5 USING fts4; 343 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 0; 344 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 25; 345 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 50; 346 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 75; 347 SELECT count(*) FROM x5_segdir 348 } {4} 349 350 do_execsql_test 6.2 { 351 INSERT INTO x5(x5) VALUES('merge=2,4'); 352 SELECT level, idx, end_block FROM x5_segdir; 353 } {0 0 {10 9216} 0 1 {21 9330} 0 2 {31 8850} 0 3 {40 8689} 1 0 {1320 -3117}} 354 355 do_execsql_test 6.3 { 356 INSERT INTO x5 SELECT words FROM t1 LIMIT 100 OFFSET 100; 357 SELECT level, idx, end_block FROM x5_segdir; 358 } { 359 0 0 {10 9216} 0 1 {21 9330} 0 2 {31 8850} 360 0 3 {40 8689} 1 0 {1320 -3117} 0 4 {1329 8297} 361 } 362 363 do_execsql_test 6.4 { 364 INSERT INTO x5(x5) VALUES('merge=200,4'); 365 SELECT level, idx, end_block FROM x5_segdir; 366 } {0 0 {1329 8297} 1 0 {1320 28009}} 367 368 do_execsql_test 6.5 { 369 INSERT INTO x5 SELECT words FROM t1; 370 SELECT level, idx, end_block FROM x5_segdir; 371 } { 372 0 1 {1329 8297} 0 0 {1320 28009} 0 2 {1449 118006} 373 } 374 375 #-------------------------------------------------------------------------- 376 # Ensure that if part of an incremental merge is performed by an old 377 # version that does not support storing segment sizes in the end_block 378 # field, no size is stored in the final segment (as it would be incorrect). 379 # 380 do_execsql_test 7.1 { 381 CREATE VIRTUAL TABLE x6 USING fts4; 382 INSERT INTO x6 SELECT words FROM t1; 383 INSERT INTO x6 SELECT words FROM t1; 384 INSERT INTO x6 SELECT words FROM t1; 385 INSERT INTO x6 SELECT words FROM t1; 386 INSERT INTO x6 SELECT words FROM t1; 387 INSERT INTO x6 SELECT words FROM t1; 388 SELECT level, idx, end_block FROM x6_segdir; 389 } { 390 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 391 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} 392 } 393 394 do_execsql_test 7.2 { 395 INSERT INTO x6(x6) VALUES('merge=25,4'); 396 SELECT level, idx, end_block FROM x6_segdir; 397 } { 398 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 399 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} 400 1 0 {23694 -69477} 401 } 402 403 do_execsql_test 7.3 { 404 UPDATE x6_segdir SET end_block = first(end_block) WHERE level=1; 405 SELECT level, idx, end_block FROM x6_segdir; 406 } { 407 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 408 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} 409 1 0 23694 410 } 411 412 do_execsql_test 7.4 { 413 INSERT INTO x6(x6) VALUES('merge=25,4'); 414 SELECT level, idx, end_block FROM x6_segdir; 415 } { 416 0 0 {118 117483} 0 1 {238 118006} 0 2 {358 118006} 417 0 3 {478 118006} 0 4 {598 118006} 0 5 {718 118006} 418 1 0 23694 419 } 420 421 do_execsql_test 7.5 { 422 INSERT INTO x6(x6) VALUES('merge=2500,4'); 423 SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir; 424 } { 425 1 0 719 1171 23694 426 } 427 428 do_execsql_test 7.6 { 429 INSERT INTO x6(x6) VALUES('merge=2500,2'); 430 SELECT level, idx, start_block, leaves_end_block, end_block FROM x6_segdir; 431 } { 432 1 0 719 1171 23694 433 } 434 435 do_execsql_test 7.7 { 436 SELECT sum(length(block)) FROM x6_segments 437 } {635247} 438 439 440 finish_test