1# 2014 Dec 20
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#
13
14source [file join [file dirname [info script]] fts5_common.tcl]
15set testprefix fts5optimize
16
17# If SQLITE_ENABLE_FTS5 is defined, omit this file.
18ifcapable !fts5 {
19  finish_test
20  return
21}
22
23#
24# 1.* - Warm body tests for index optimization using ('optimize')
25#
26# 2.* - Warm body tests for index optimization using ('merge', -1)
27#
28
29proc rnddoc {nWord} {
30  set vocab {a b c d e f g h i j k l m n o p q r s t u v w x y z}
31  set nVocab [llength $vocab]
32  set ret [list]
33  for {set i 0} {$i < $nWord} {incr i} {
34    lappend ret [lindex $vocab [expr {int(rand() * $nVocab)}]]
35  }
36  return $ret
37}
38
39foreach {tn nStep} {
40  1 2
41  2 10
42  3 50
43  4 500
44} {
45  reset_db
46  db func rnddoc rnddoc
47  do_execsql_test 1.$tn.1 {
48    CREATE VIRTUAL TABLE t1 USING fts5(x, y);
49  }
50  do_test 1.$tn.2 {
51    for {set i 0} {$i < $nStep} {incr i} {
52      execsql { INSERT INTO t1 VALUES( rnddoc(5), rnddoc(5) ) }
53    }
54  } {}
55
56  do_execsql_test 1.$tn.3 {
57    INSERT INTO t1(t1) VALUES('integrity-check');
58  }
59
60  do_execsql_test 1.$tn.4 {
61    INSERT INTO t1(t1) VALUES('optimize');
62  }
63
64  do_execsql_test 1.$tn.5 {
65    INSERT INTO t1(t1) VALUES('integrity-check');
66  }
67
68  do_test 1.$tn.6 { fts5_segcount t1 } 1
69}
70
71foreach {tn nStep} {
72  1 2
73  2 10
74  3 50
75  4 500
76} {
77  reset_db
78  db func rnddoc rnddoc
79  do_execsql_test 1.$tn.1 {
80    CREATE VIRTUAL TABLE t1 USING fts5(x, y);
81  }
82  do_test 2.$tn.2 {
83    for {set i 0} {$i < $nStep} {incr i} {
84      execsql { INSERT INTO t1 VALUES( rnddoc(5), rnddoc(5) ) }
85    }
86  } {}
87
88  do_execsql_test 2.$tn.3 {
89    INSERT INTO t1(t1) VALUES('integrity-check');
90  }
91
92  do_test 2.$tn.4 {
93    execsql { INSERT INTO t1(t1, rank) VALUES('merge', -1) }
94    while 1 {
95      set c [db total_changes]
96      execsql { INSERT INTO t1(t1, rank) VALUES('merge', 1) }
97      set c [expr [db total_changes]-$c]
98      if {$c<2} break
99    }
100  } {}
101
102  do_execsql_test 2.$tn.5 {
103    INSERT INTO t1(t1) VALUES('integrity-check');
104  }
105
106  do_test 2.$tn.6 { fts5_segcount t1 } 1
107}
108finish_test
109