1 /*
2 ** 2014 December 9
3 **
4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
6 **
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
10 **
11 *************************************************************************
12 **
13 ** This file contains multi-threaded tests that use shared-cache and
14 ** the VACUUM command.
15 **
16 ** Tests:
17 **
18 ** vacuum1
19 **
20 */
21
22
vacuum1_thread_writer(int iTid,void * pArg)23 static char *vacuum1_thread_writer(int iTid, void *pArg){
24 Error err = {0}; /* Error code and message */
25 Sqlite db = {0}; /* SQLite database connection */
26 i64 i = 0;
27
28 opendb(&err, &db, "test.db", 0);
29 while( !timetostop(&err) ){
30 i++;
31
32 /* Insert lots of rows. Then delete some. */
33 execsql(&err, &db,
34 "WITH loop(i) AS (SELECT 1 UNION ALL SELECT i+1 FROM loop WHERE i<100) "
35 "INSERT INTO t1 SELECT randomblob(50), randomblob(2500) FROM loop"
36 );
37
38 /* Delete lots of rows */
39 execsql(&err, &db, "DELETE FROM t1 WHERE rowid = :i", &i);
40 clear_error(&err, SQLITE_LOCKED);
41
42 /* Select the rows */
43 execsql(&err, &db, "SELECT * FROM t1 ORDER BY x");
44 clear_error(&err, SQLITE_LOCKED);
45 }
46
47 closedb(&err, &db);
48 print_and_free_err(&err);
49 return sqlite3_mprintf("ok");
50 }
51
vacuum1_thread_vacuumer(int iTid,void * pArg)52 static char *vacuum1_thread_vacuumer(int iTid, void *pArg){
53 Error err = {0}; /* Error code and message */
54 Sqlite db = {0}; /* SQLite database connection */
55 opendb(&err, &db, "test.db", 0);
56
57 do{
58 sql_script(&err, &db, "VACUUM");
59 clear_error(&err, SQLITE_LOCKED);
60 }while( !timetostop(&err) );
61
62 closedb(&err, &db);
63 print_and_free_err(&err);
64 return sqlite3_mprintf("ok");
65 }
66
vacuum1(int nMs)67 static void vacuum1(int nMs){
68 Error err = {0};
69 Sqlite db = {0};
70 Threadset threads = {0};
71
72 opendb(&err, &db, "test.db", 1);
73 sql_script(&err, &db,
74 "CREATE TABLE t1(x PRIMARY KEY, y BLOB);"
75 "CREATE INDEX i1 ON t1(y);"
76 );
77 closedb(&err, &db);
78
79 setstoptime(&err, nMs);
80
81 sqlite3_enable_shared_cache(1);
82 launch_thread(&err, &threads, vacuum1_thread_writer, 0);
83 launch_thread(&err, &threads, vacuum1_thread_writer, 0);
84 launch_thread(&err, &threads, vacuum1_thread_writer, 0);
85 launch_thread(&err, &threads, vacuum1_thread_vacuumer, 0);
86 join_all_threads(&err, &threads);
87 sqlite3_enable_shared_cache(0);
88
89 print_and_free_err(&err);
90 }
91