annotate sqlite/vacuum.c @ 1621:fae6e0cfcc77 trunk

[svn] - Reapply r2161
author nhjm449
date Tue, 05 Sep 2006 19:57:18 -0700
parents b6b61becdf4e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1434
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
1 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
2 ** 2003 April 6
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
3 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
4 ** The author disclaims copyright to this source code. In place of
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
5 ** a legal notice, here is a blessing:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
6 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
7 ** May you do good and not evil.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
8 ** May you find forgiveness for yourself and forgive others.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
9 ** May you share freely, never taking more than you give.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
10 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
11 *************************************************************************
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
12 ** This file contains code used to implement the VACUUM command.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
13 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
14 ** Most of the code in this file may be omitted by defining the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
15 ** SQLITE_OMIT_VACUUM macro.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
16 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
17 ** $Id: vacuum.c,v 1.59 2006/02/24 02:53:50 drh Exp $
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
18 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
19 #include "sqliteInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
20 #include "vdbeInt.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
21 #include "os.h"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
22
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
23 #ifndef SQLITE_OMIT_VACUUM
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
24 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
25 ** Generate a random name of 20 character in length.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
26 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
27 static void randomName(unsigned char *zBuf){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
28 static const unsigned char zChars[] =
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
29 "abcdefghijklmnopqrstuvwxyz"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
30 "0123456789";
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
31 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
32 sqlite3Randomness(20, zBuf);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
33 for(i=0; i<20; i++){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
34 zBuf[i] = zChars[ zBuf[i]%(sizeof(zChars)-1) ];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
35 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
36 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
37
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
38 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
39 ** Execute zSql on database db. Return an error code.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
40 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
41 static int execSql(sqlite3 *db, const char *zSql){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
42 sqlite3_stmt *pStmt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
43 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
44 return sqlite3_errcode(db);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
45 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
46 while( SQLITE_ROW==sqlite3_step(pStmt) ){}
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
47 return sqlite3_finalize(pStmt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
48 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
49
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
50 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
51 ** Execute zSql on database db. The statement returns exactly
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
52 ** one column. Execute this as SQL on the same database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
53 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
54 static int execExecSql(sqlite3 *db, const char *zSql){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
55 sqlite3_stmt *pStmt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
56 int rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
57
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
58 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
59 if( rc!=SQLITE_OK ) return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
60
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
61 while( SQLITE_ROW==sqlite3_step(pStmt) ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
62 rc = execSql(db, (char*)sqlite3_column_text(pStmt, 0));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
63 if( rc!=SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
64 sqlite3_finalize(pStmt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
65 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
66 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
67 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
68
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
69 return sqlite3_finalize(pStmt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
70 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
71
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
72 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
73
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
74 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
75 ** The non-standard VACUUM command is used to clean up the database,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
76 ** collapse free space, etc. It is modelled after the VACUUM command
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
77 ** in PostgreSQL.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
78 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
79 ** In version 1.0.x of SQLite, the VACUUM command would call
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
80 ** gdbm_reorganize() on all the database tables. But beginning
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
81 ** with 2.0.0, SQLite no longer uses GDBM so this command has
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
82 ** become a no-op.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
83 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
84 void sqlite3Vacuum(Parse *pParse){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
85 Vdbe *v = sqlite3GetVdbe(pParse);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
86 if( v ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
87 sqlite3VdbeAddOp(v, OP_Vacuum, 0, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
88 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
89 return;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
90 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
91
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
92 /*
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
93 ** This routine implements the OP_Vacuum opcode of the VDBE.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
94 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
95 int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
96 int rc = SQLITE_OK; /* Return code from service routines */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
97 #ifndef SQLITE_OMIT_VACUUM
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
98 const char *zFilename; /* full pathname of the database file */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
99 int nFilename; /* number of characters in zFilename[] */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
100 char *zTemp = 0; /* a temporary file in same directory as zFilename */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
101 Btree *pMain; /* The database being vacuumed */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
102 Btree *pTemp;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
103 char *zSql = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
104 int saved_flags; /* Saved value of the db->flags */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
105 Db *pDb = 0; /* Database to detach at end of vacuum */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
106
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
107 /* Save the current value of the write-schema flag before setting it. */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
108 saved_flags = db->flags;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
109 db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
110
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
111 if( !db->autoCommit ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
112 sqlite3SetString(pzErrMsg, "cannot VACUUM from within a transaction",
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
113 (char*)0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
114 rc = SQLITE_ERROR;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
115 goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
116 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
117
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
118 /* Get the full pathname of the database file and create a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
119 ** temporary filename in the same directory as the original file.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
120 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
121 pMain = db->aDb[0].pBt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
122 zFilename = sqlite3BtreeGetFilename(pMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
123 assert( zFilename );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
124 if( zFilename[0]=='\0' ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
125 /* The in-memory database. Do nothing. Return directly to avoid causing
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
126 ** an error trying to DETACH the vacuum_db (which never got attached)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
127 ** in the exit-handler.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
128 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
129 return SQLITE_OK;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
130 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
131 nFilename = strlen(zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
132 zTemp = sqliteMalloc( nFilename+100 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
133 if( zTemp==0 ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
134 rc = SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
135 goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
136 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
137 strcpy(zTemp, zFilename);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
138
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
139 /* The randomName() procedure in the following loop uses an excellent
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
140 ** source of randomness to generate a name from a space of 1.3e+31
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
141 ** possibilities. So unless the directory already contains on the order
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
142 ** of 1.3e+31 files, the probability that the following loop will
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
143 ** run more than once or twice is vanishingly small. We are certain
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
144 ** enough that this loop will always terminate (and terminate quickly)
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
145 ** that we don't even bother to set a maximum loop count.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
146 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
147 do {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
148 zTemp[nFilename] = '-';
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
149 randomName((unsigned char*)&zTemp[nFilename+1]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
150 } while( sqlite3OsFileExists(zTemp) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
151
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
152 /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
153 ** can be set to 'off' for this file, as it is not recovered if a crash
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
154 ** occurs anyway. The integrity of the database is maintained by a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
155 ** (possibly synchronous) transaction opened on the main database before
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
156 ** sqlite3BtreeCopyFile() is called.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
157 **
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
158 ** An optimisation would be to use a non-journaled pager.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
159 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
160 zSql = sqlite3MPrintf("ATTACH '%q' AS vacuum_db;", zTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
161 if( !zSql ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
162 rc = SQLITE_NOMEM;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
163 goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
164 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
165 rc = execSql(db, zSql);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
166 sqliteFree(zSql);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
167 zSql = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
168 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
169 pDb = &db->aDb[db->nDb-1];
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
170 assert( strcmp(db->aDb[db->nDb-1].zName,"vacuum_db")==0 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
171 pTemp = db->aDb[db->nDb-1].pBt;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
172 sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain),
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
173 sqlite3BtreeGetReserve(pMain));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
174 assert( sqlite3BtreeGetPageSize(pTemp)==sqlite3BtreeGetPageSize(pMain) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
175 rc = execSql(db, "PRAGMA vacuum_db.synchronous=OFF");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
176 if( rc!=SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
177 goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
178 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
179
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
180 #ifndef SQLITE_OMIT_AUTOVACUUM
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
181 sqlite3BtreeSetAutoVacuum(pTemp, sqlite3BtreeGetAutoVacuum(pMain));
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
182 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
183
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
184 /* Begin a transaction */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
185 rc = execSql(db, "BEGIN EXCLUSIVE;");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
186 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
187
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
188 /* Query the schema of the main database. Create a mirror schema
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
189 ** in the temporary database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
190 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
191 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
192 "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14,100000000) "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
193 " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
194 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
195 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
196 "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14,100000000)"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
197 " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
198 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
199 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
200 "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21,100000000) "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
201 " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
202 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
203 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
204 "SELECT 'CREATE VIEW vacuum_db.' || substr(sql,13,100000000) "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
205 " FROM sqlite_master WHERE type='view'"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
206 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
207 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
208
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
209 /* Loop through the tables in the main database. For each, do
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
210 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM xxx;" to copy
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
211 ** the contents to the temporary database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
212 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
213 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
214 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
215 "|| ' SELECT * FROM ' || quote(name) || ';'"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
216 "FROM sqlite_master "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
217 "WHERE type = 'table' AND name!='sqlite_sequence';"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
218 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
219 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
220
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
221 /* Copy over the sequence table
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
222 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
223 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
224 "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
225 "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
226 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
227 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
228 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
229 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
230 "|| ' SELECT * FROM ' || quote(name) || ';' "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
231 "FROM vacuum_db.sqlite_master WHERE name=='sqlite_sequence';"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
232 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
233 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
234
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
235
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
236 /* Copy the triggers from the main database to the temporary database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
237 ** This was deferred before in case the triggers interfered with copying
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
238 ** the data. It's possible the indices should be deferred until this
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
239 ** point also.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
240 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
241 rc = execExecSql(db,
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
242 "SELECT 'CREATE TRIGGER vacuum_db.' || substr(sql, 16, 1000000) "
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
243 "FROM sqlite_master WHERE type='trigger'"
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
244 );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
245 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
246
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
247
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
248 /* At this point, unless the main db was completely empty, there is now a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
249 ** transaction open on the vacuum database, but not on the main database.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
250 ** Open a btree level transaction on the main database. This allows a
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
251 ** call to sqlite3BtreeCopyFile(). The main database btree level
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
252 ** transaction is then committed, so the SQL level never knows it was
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
253 ** opened for writing. This way, the SQL transaction used to create the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
254 ** temporary database never needs to be committed.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
255 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
256 if( rc==SQLITE_OK ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
257 u32 meta;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
258 int i;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
259
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
260 /* This array determines which meta meta values are preserved in the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
261 ** vacuum. Even entries are the meta value number and odd entries
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
262 ** are an increment to apply to the meta value after the vacuum.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
263 ** The increment is used to increase the schema cookie so that other
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
264 ** connections to the same database will know to reread the schema.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
265 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
266 static const unsigned char aCopy[] = {
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
267 1, 1, /* Add one to the old schema cookie */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
268 3, 0, /* Preserve the default page cache size */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
269 5, 0, /* Preserve the default text encoding */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
270 6, 0, /* Preserve the user version */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
271 };
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
272
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
273 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
274 assert( 1==sqlite3BtreeIsInTrans(pMain) );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
275
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
276 /* Copy Btree meta values */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
277 for(i=0; i<sizeof(aCopy)/sizeof(aCopy[0]); i+=2){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
278 rc = sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
279 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
280 rc = sqlite3BtreeUpdateMeta(pTemp, aCopy[i], meta+aCopy[i+1]);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
281 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
282 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
283
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
284 rc = sqlite3BtreeCopyFile(pMain, pTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
285 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
286 rc = sqlite3BtreeCommit(pTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
287 if( rc!=SQLITE_OK ) goto end_of_vacuum;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
288 rc = sqlite3BtreeCommit(pMain);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
289 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
290
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
291 end_of_vacuum:
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
292 /* Restore the original value of db->flags */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
293 db->flags = saved_flags;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
294
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
295 /* Currently there is an SQL level transaction open on the vacuum
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
296 ** database. No locks are held on any other files (since the main file
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
297 ** was committed at the btree level). So it safe to end the transaction
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
298 ** by manually setting the autoCommit flag to true and detaching the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
299 ** vacuum database. The vacuum_db journal file is deleted when the pager
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
300 ** is closed by the DETACH.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
301 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
302 db->autoCommit = 1;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
303
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
304 if( pDb ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
305 sqlite3MallocDisallow();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
306 sqlite3BtreeClose(pDb->pBt);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
307 sqlite3MallocAllow();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
308 pDb->pBt = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
309 pDb->pSchema = 0;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
310 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
311
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
312 /* If one of the execSql() calls above returned SQLITE_NOMEM, then the
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
313 ** mallocFailed flag will be clear (because execSql() calls sqlite3_exec()).
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
314 ** Fix this so the flag and return code match.
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
315 */
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
316 if( rc==SQLITE_NOMEM ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
317 sqlite3MallocFailed();
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
318 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
319
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
320 if( zTemp ){
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
321 sqlite3OsDelete(zTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
322 sqliteFree(zTemp);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
323 }
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
324 sqliteFree( zSql );
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
325 sqlite3ResetInternalSchema(db, 0);
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
326 #endif
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
327
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
328 return rc;
b6b61becdf4e [svn] - add sqlite/ directory
nenolod
parents:
diff changeset
329 }