rbuprogress.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. # 2016 March 18
  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. source [file join [file dirname [info script]] rbu_common.tcl]
  13. if_no_rbu_support { finish_test ; return }
  14. set ::testprefix rbuprogress
  15. proc create_db_file {filename sql} {
  16. forcedelete $filename
  17. sqlite3 tmpdb $filename
  18. tmpdb eval $sql
  19. tmpdb close
  20. }
  21. # Create a simple RBU database. That expects to write to a table:
  22. #
  23. # CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  24. #
  25. proc create_rbu1 {filename} {
  26. create_db_file $filename {
  27. CREATE TABLE data_t1(a, b, c, rbu_control);
  28. INSERT INTO data_t1 VALUES(1, 2, 3, 0);
  29. INSERT INTO data_t1 VALUES(2, 'two', 'three', 0);
  30. INSERT INTO data_t1 VALUES(3, NULL, 8.2, 0);
  31. CREATE TABLE rbu_count(tbl, cnt);
  32. INSERT INTO rbu_count VALUES('data_t1', 3);
  33. }
  34. return $filename
  35. }
  36. do_execsql_test 1.0 {
  37. PRAGMA page_size = 4096;
  38. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  39. }
  40. do_test 1.1 {
  41. create_rbu1 rbu.db
  42. sqlite3rbu rbu test.db rbu.db
  43. rbu bp_progress
  44. } {0 0}
  45. do_test 1.2 { rbu step ; rbu bp_progress } {3333 0}
  46. do_test 1.3 { rbu step ; rbu bp_progress } {6666 0}
  47. do_test 1.4 { rbu step ; rbu bp_progress } {10000 0}
  48. do_test 1.5 { rbu step ; rbu bp_progress } {10000 0}
  49. do_test 1.6 { rbu step ; rbu bp_progress } {10000 0}
  50. do_test 1.7 { rbu step ; rbu bp_progress } {10000 5000}
  51. do_test 1.8 { rbu step ; rbu bp_progress } {10000 10000}
  52. do_test 1.9 { rbu step ; rbu bp_progress } {10000 10000}
  53. do_test 1.10 {
  54. rbu close
  55. } {SQLITE_DONE}
  56. #-------------------------------------------------------------------------
  57. #
  58. proc do_sp_test {tn bReopen target rbu reslist} {
  59. uplevel [list do_test $tn [subst -nocommands {
  60. if {$bReopen==0} { sqlite3rbu rbu $target $rbu }
  61. set res [list]
  62. while 1 {
  63. if {$bReopen} { sqlite3rbu rbu $target $rbu }
  64. set rc [rbu step]
  65. if {[set rc] != "SQLITE_OK"} { rbu close ; error "error 1" }
  66. lappend res [lindex [rbu bp_progress] 0]
  67. if {[lindex [set res] end]==10000} break
  68. if {$bReopen} { rbu close }
  69. }
  70. if {[set res] != [list $reslist]} {
  71. rbu close
  72. error "1. reslist incorrect (expect=$reslist got=[set res])"
  73. }
  74. # One step to clean up the temporary tables used to update the only
  75. # target table in the rbu database. And one more to move the *-oal
  76. # file to *-wal. After each of these steps, the progress remains
  77. # at "10000 0".
  78. #
  79. if {[lindex [list $reslist] 0]!=-1} {
  80. rbu step
  81. set res [rbu bp_progress]
  82. if {[set res] != [list 10000 0]} {
  83. rbu close
  84. error "2. reslist incorrect (expect=10000 0 got=[set res])"
  85. }
  86. }
  87. rbu step
  88. set res [rbu bp_progress]
  89. if {[set res] != [list 10000 0]} {
  90. rbu close
  91. error "3. reslist incorrect (expect=10000 0 got=[set res])"
  92. }
  93. # Do the checkpoint.
  94. while {[rbu step]=="SQLITE_OK"} {
  95. foreach {a b} [rbu bp_progress] {}
  96. if {[set a]!=10000 || [set b]<=0 || [set b]>10000} {
  97. rbu close
  98. error "4. reslist incorrect (expect=10000 1..10000 got=[set a] [set b])"
  99. }
  100. }
  101. set res [rbu bp_progress]
  102. if {[set res] != [list 10000 10000]} {
  103. rbu close
  104. error "5. reslist is incorrect (expect=10000 10000 got=[set res])"
  105. }
  106. rbu close
  107. }] {SQLITE_DONE}]
  108. }
  109. foreach {bReopen} { 0 1 } {
  110. reset_db
  111. do_test 2.$bReopen.1.0 {
  112. execsql {
  113. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  114. }
  115. create_db_file rbu.db {
  116. CREATE TABLE data_t1(a, b, c, rbu_control);
  117. INSERT INTO data_t1 VALUES(4, 4, 4, 0);
  118. INSERT INTO data_t1 VALUES(5, 5, 5, 0);
  119. CREATE TABLE rbu_count(tbl, cnt);
  120. INSERT INTO rbu_count VALUES('data_t1', 2);
  121. }
  122. } {}
  123. do_sp_test 2.$bReopen.1.1 $bReopen test.db rbu.db {5000 10000}
  124. reset_db
  125. do_test 2.$bReopen.2.0 {
  126. execsql { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c) }
  127. create_rbu1 rbu.db
  128. } {rbu.db}
  129. do_sp_test 2.$bReopen.2.1 $bReopen test.db rbu.db {3333 6666 10000}
  130. reset_db
  131. do_test 2.$bReopen.3.0 {
  132. execsql {
  133. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  134. CREATE INDEX i1 ON t1(b);
  135. INSERT INTO t1 VALUES(1, 1, 1);
  136. INSERT INTO t1 VALUES(2, 2, 2);
  137. INSERT INTO t1 VALUES(3, 3, 3);
  138. }
  139. create_db_file rbu.db {
  140. CREATE TABLE data_t1(a, b, c, rbu_control);
  141. INSERT INTO data_t1 VALUES(4, 4, 4, 0);
  142. INSERT INTO data_t1 VALUES(2, NULL, NULL, 1);
  143. INSERT INTO data_t1 VALUES(5, NULL, NULL, 1);
  144. CREATE TABLE rbu_count(tbl, cnt);
  145. INSERT INTO rbu_count VALUES('data_t1', 3);
  146. }
  147. } {}
  148. do_sp_test 2.$bReopen.3.1 $bReopen test.db rbu.db {1666 3333 6000 8000 10000}
  149. reset_db
  150. do_test 2.$bReopen.4.0 {
  151. execsql {
  152. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  153. CREATE INDEX i1 ON t1(b);
  154. INSERT INTO t1 VALUES(1, 1, 1);
  155. INSERT INTO t1 VALUES(2, 2, 2);
  156. INSERT INTO t1 VALUES(3, 3, 3);
  157. }
  158. create_db_file rbu.db {
  159. CREATE TABLE data_t1(a, b, c, rbu_control);
  160. INSERT INTO data_t1 VALUES(2, 4, 4, '.xx');
  161. CREATE TABLE rbu_count(tbl, cnt);
  162. INSERT INTO rbu_count VALUES('data_t1', 1);
  163. }
  164. } {}
  165. do_sp_test 2.$bReopen.4.1 $bReopen test.db rbu.db {3333 6666 10000}
  166. reset_db
  167. do_test 2.$bReopen.5.0 {
  168. execsql {
  169. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  170. CREATE INDEX i1 ON t1(b);
  171. INSERT INTO t1 VALUES(1, 1, 1);
  172. INSERT INTO t1 VALUES(2, 2, 2);
  173. INSERT INTO t1 VALUES(3, 3, 3);
  174. }
  175. create_db_file rbu.db {
  176. CREATE TABLE data_t1(a, b, c, rbu_control);
  177. INSERT INTO data_t1 VALUES(4, NULL, 4, '.xx');
  178. CREATE TABLE rbu_count(tbl, cnt);
  179. INSERT INTO rbu_count VALUES('data_t1', 1);
  180. }
  181. } {}
  182. do_sp_test 2.$bReopen.5.1 $bReopen test.db rbu.db {10000}
  183. reset_db
  184. do_test 2.$bReopen.6.0 {
  185. execsql {
  186. CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c);
  187. CREATE INDEX i1 ON t1(b);
  188. INSERT INTO t1 VALUES(1, 1, 1);
  189. INSERT INTO t1 VALUES(2, 2, 2);
  190. INSERT INTO t1 VALUES(3, 3, 3);
  191. }
  192. create_db_file rbu.db {
  193. CREATE TABLE data_t1(a, b, c, rbu_control);
  194. INSERT INTO data_t1 VALUES(4, 4, 4, 0);
  195. INSERT INTO data_t1 VALUES(2, NULL, NULL, 1);
  196. INSERT INTO data_t1 VALUES(5, NULL, NULL, 1);
  197. }
  198. } {}
  199. do_sp_test 2.$bReopen.6.1 $bReopen test.db rbu.db {-1 -1 -1 -1 -1 10000}
  200. }
  201. #-------------------------------------------------------------------------
  202. # The following tests verify that the API works when resuming an update
  203. # during the incremental checkpoint stage.
  204. #
  205. proc do_phase2_test {tn bReopen target rbu nStep} {
  206. uplevel [list do_test $tn [subst -nocommands {
  207. # Build the OAL/WAL file:
  208. sqlite3rbu rbu $target $rbu
  209. while {[lindex [rbu bp_progress] 0]<10000} {
  210. set rc [rbu step]
  211. if {"SQLITE_OK" != [set rc]} { rbu close }
  212. }
  213. # Clean up the temp tables and move the *-oal file to *-wal.
  214. rbu step
  215. rbu step
  216. for {set i 0} {[set i] < $nStep} {incr i} {
  217. if {$bReopen} {
  218. rbu close
  219. sqlite3rbu rbu $target $rbu
  220. }
  221. rbu step
  222. set res [rbu bp_progress]
  223. set expect [expr (1 + [set i]) * 10000 / $nStep]
  224. if {[lindex [set res] 1] != [set expect]} {
  225. error "Have [set res], expected 10000 [set expect]"
  226. }
  227. }
  228. set rc [rbu step]
  229. if {[set rc] != "SQLITE_DONE"} {
  230. error "Have [set rc], expected SQLITE_DONE"
  231. }
  232. rbu close
  233. }] {SQLITE_DONE}]
  234. }
  235. foreach bReopen {0 1} {
  236. do_test 3.$bReopen.1.0 {
  237. reset_db
  238. execsql {
  239. PRAGMA page_size = 4096;
  240. CREATE TABLE t1(a INTEGER PRIMARY KEY, b);
  241. CREATE TABLE t2(a INTEGER PRIMARY KEY, b);
  242. CREATE TABLE t3(a INTEGER PRIMARY KEY, b);
  243. CREATE TABLE t4(a INTEGER PRIMARY KEY, b);
  244. }
  245. create_db_file rbu.db {
  246. CREATE TABLE data_t1(a, b, rbu_control);
  247. CREATE TABLE data_t2(a, b, rbu_control);
  248. CREATE TABLE data_t3(a, b, rbu_control);
  249. CREATE TABLE data_t4(a, b, rbu_control);
  250. INSERT INTO data_t1 VALUES(1, 2, 0);
  251. INSERT INTO data_t2 VALUES(1, 2, 0);
  252. INSERT INTO data_t3 VALUES(1, 2, 0);
  253. INSERT INTO data_t4 VALUES(1, 2, 0);
  254. CREATE TABLE rbu_count(tbl, cnt);
  255. INSERT INTO rbu_count VALUES('data_t1', 1);
  256. INSERT INTO rbu_count VALUES('data_t2', 1);
  257. INSERT INTO rbu_count VALUES('data_t3', 1);
  258. INSERT INTO rbu_count VALUES('data_t4', 1);
  259. }
  260. } {}
  261. do_phase2_test 3.$bReopen.1.1 $bReopen test.db rbu.db 5
  262. }
  263. foreach {bReopen} { 0 1 } {
  264. foreach {tn tbl} {
  265. ipk { CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c) }
  266. wr { CREATE TABLE t1(a INT PRIMARY KEY, b, c) WITHOUT ROWID }
  267. pk { CREATE TABLE t1(a INT PRIMARY KEY, b, c) }
  268. } {
  269. foreach {tn2 rbusql r1 r3} {
  270. 1 {
  271. CREATE TABLE data0_t1(a, b, c, rbu_control);
  272. INSERT INTO data0_t1 VALUES(15, 15, 15, 0);
  273. INSERT INTO data0_t1 VALUES(20, 20, 20, 0);
  274. CREATE TABLE rbu_count(tbl, cnt);
  275. INSERT INTO rbu_count VALUES('data0_t1', 2);
  276. }
  277. {2500 5000 7500 10000}
  278. {1666 3333 5000 6666 8333 10000}
  279. 2 {
  280. CREATE TABLE data0_t1(a, b, c, rbu_control);
  281. INSERT INTO data0_t1 VALUES(10, 10, 10, 2);
  282. CREATE TABLE rbu_count(tbl, cnt);
  283. INSERT INTO rbu_count VALUES('data0_t1', 1);
  284. }
  285. {3333 6666 10000}
  286. {2000 4000 6000 8000 10000}
  287. 3 {
  288. CREATE TABLE data0_t1(a, b, c, rbu_control);
  289. INSERT INTO data0_t1 VALUES(7, 7, 7, 2);
  290. INSERT INTO data0_t1 VALUES(10, 10, 10, 2);
  291. CREATE TABLE rbu_count(tbl, cnt);
  292. INSERT INTO rbu_count VALUES('data0_t1', 2);
  293. }
  294. {2500 4000 6000 8000 10000}
  295. {1666 2500 3750 5000 6250 7500 8750 10000}
  296. } {
  297. reset_db ; execsql $tbl
  298. do_test 4.$tn.$bReopen.$tn2.0 {
  299. execsql {
  300. CREATE INDEX t1c ON t1(c);
  301. INSERT INTO t1 VALUES(1, 1, 1);
  302. INSERT INTO t1 VALUES(5, 5, 5);
  303. INSERT INTO t1 VALUES(10, 10, 10);
  304. }
  305. create_db_file rbu.db $rbusql
  306. } {}
  307. set R(ipk) $r1
  308. set R(wr) $r1
  309. set R(pk) $r3
  310. do_sp_test 4.$tn.$bReopen.$tn2.1 $bReopen test.db rbu.db $R($tn)
  311. }
  312. }
  313. }
  314. foreach {bReopen} { 0 1 } {
  315. foreach {tn tbl} {
  316. nopk {
  317. CREATE TABLE t1(a, b, c);
  318. CREATE INDEX t1c ON t1(c);
  319. }
  320. vtab {
  321. CREATE VIRTUAL TABLE t1 USING fts5(a, b, c);
  322. }
  323. } {
  324. if {$tn=="vtab"} { ifcapable !fts5 break }
  325. foreach {tn2 rbusql r1 r2} {
  326. 1 {
  327. CREATE TABLE data0_t1(a, b, c, rbu_rowid, rbu_control);
  328. INSERT INTO data0_t1 VALUES(15, 15, 15, 4, 0);
  329. INSERT INTO data0_t1 VALUES(20, 20, 20, 5, 0);
  330. CREATE TABLE rbu_count(tbl, cnt);
  331. INSERT INTO rbu_count VALUES('data0_t1', 2);
  332. }
  333. {2500 5000 7500 10000}
  334. {5000 10000}
  335. 2 {
  336. CREATE TABLE data0_t1(rbu_rowid, a, b, c, rbu_control);
  337. INSERT INTO data0_t1 VALUES(0, 7, 7, 7, 2);
  338. INSERT INTO data0_t1 VALUES(2, 10, 10, 10, 2);
  339. CREATE TABLE rbu_count(tbl, cnt);
  340. INSERT INTO rbu_count VALUES('data0_t1', 2);
  341. }
  342. {2500 4000 6000 8000 10000}
  343. {5000 10000}
  344. 3 {
  345. CREATE TABLE data0_t1(rbu_rowid, a, b, c, rbu_control);
  346. INSERT INTO data0_t1 VALUES(1, NULL, NULL, NULL, 1);
  347. INSERT INTO data0_t1 VALUES(2, NULL, NULL, 7, '..x');
  348. CREATE TABLE rbu_count(tbl, cnt);
  349. INSERT INTO rbu_count VALUES('data0_t1', 2);
  350. }
  351. {2500 4000 6000 8000 10000}
  352. {5000 10000}
  353. } {
  354. reset_db ; execsql $tbl
  355. do_test 5.$tn.$bReopen.$tn2.0 {
  356. execsql {
  357. INSERT INTO t1 VALUES(1, 1, 1);
  358. INSERT INTO t1 VALUES(5, 5, 5);
  359. INSERT INTO t1 VALUES(10, 10, 10);
  360. }
  361. create_db_file rbu.db $rbusql
  362. } {}
  363. set R(nopk) $r1
  364. set R(vtab) $r2
  365. do_sp_test 5.$tn.$bReopen.$tn2.1 $bReopen test.db rbu.db $R($tn)
  366. }
  367. }
  368. }
  369. #-------------------------------------------------------------------------
  370. # Test that sqlite3_bp_progress() works with an RBU vacuum if there
  371. # is an rbu_count table in the db being vacuumed.
  372. #
  373. reset_db
  374. do_execsql_test 6.0 {
  375. CREATE TABLE t1(a, b, c);
  376. CREATE INDEX i1 ON t1(a);
  377. CREATE INDEX i2 ON t1(b);
  378. WITH s(i) AS (
  379. SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<100
  380. )
  381. INSERT INTO t1 SELECT i, i, i FROM s;
  382. CREATE TABLE rbu_count(tbl TEXT PRIMARY KEY, cnt INTEGER) WITHOUT ROWID;
  383. INSERT INTO rbu_count VALUES('t1', (SELECT count(*) FROM t1));
  384. INSERT INTO rbu_count VALUES('rbu_count', 2);
  385. }
  386. forcedelete state.db
  387. do_test 6.1 {
  388. set maxA 0
  389. set maxB 0
  390. sqlite3rbu_vacuum rbu test.db state.db
  391. while {[rbu step]=="SQLITE_OK"} {
  392. foreach {a b} [rbu bp_progress] {
  393. if {$a > $maxA} { set maxA $a }
  394. if {$b > $maxB} { set maxB $b }
  395. }
  396. }
  397. list [rbu close] $maxA $maxB
  398. } {SQLITE_DONE 10000 10000}
  399. finish_test