Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(331)

Side by Side Diff: third_party/sqlite/sqlite-src-3080704/src/vacuum.c

Issue 883353008: [sql] Import reference version of SQLite 3.8.7.4. (Closed) Base URL: http://chromium.googlesource.com/chromium/src.git@master
Patch Set: Hold back encoding change which is messing up patch. Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 ** 2003 April 6 2 ** 2003 April 6
3 ** 3 **
4 ** The author disclaims copyright to this source code. In place of 4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing: 5 ** a legal notice, here is a blessing:
6 ** 6 **
7 ** May you do good and not evil. 7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others. 8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give. 9 ** May you share freely, never taking more than you give.
10 ** 10 **
(...skipping 27 matching lines...) Expand all
38 sqlite3_stmt *pStmt; 38 sqlite3_stmt *pStmt;
39 VVA_ONLY( int rc; ) 39 VVA_ONLY( int rc; )
40 if( !zSql ){ 40 if( !zSql ){
41 return SQLITE_NOMEM; 41 return SQLITE_NOMEM;
42 } 42 }
43 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){ 43 if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
44 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db)); 44 sqlite3SetString(pzErrMsg, db, sqlite3_errmsg(db));
45 return sqlite3_errcode(db); 45 return sqlite3_errcode(db);
46 } 46 }
47 VVA_ONLY( rc = ) sqlite3_step(pStmt); 47 VVA_ONLY( rc = ) sqlite3_step(pStmt);
48 assert( rc!=SQLITE_ROW ); 48 assert( rc!=SQLITE_ROW || (db->flags&SQLITE_CountRows) );
49 return vacuumFinalize(db, pStmt, pzErrMsg); 49 return vacuumFinalize(db, pStmt, pzErrMsg);
50 } 50 }
51 51
52 /* 52 /*
53 ** Execute zSql on database db. The statement returns exactly 53 ** Execute zSql on database db. The statement returns exactly
54 ** one column. Execute this as SQL on the same database. 54 ** one column. Execute this as SQL on the same database.
55 */ 55 */
56 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){ 56 static int execExecSql(sqlite3 *db, char **pzErrMsg, const char *zSql){
57 sqlite3_stmt *pStmt; 57 sqlite3_stmt *pStmt;
58 int rc; 58 int rc;
59 59
60 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0); 60 rc = sqlite3_prepare(db, zSql, -1, &pStmt, 0);
61 if( rc!=SQLITE_OK ) return rc; 61 if( rc!=SQLITE_OK ) return rc;
62 62
63 while( SQLITE_ROW==sqlite3_step(pStmt) ){ 63 while( SQLITE_ROW==sqlite3_step(pStmt) ){
64 rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0)); 64 rc = execSql(db, pzErrMsg, (char*)sqlite3_column_text(pStmt, 0));
65 if( rc!=SQLITE_OK ){ 65 if( rc!=SQLITE_OK ){
66 vacuumFinalize(db, pStmt, pzErrMsg); 66 vacuumFinalize(db, pStmt, pzErrMsg);
67 return rc; 67 return rc;
68 } 68 }
69 } 69 }
70 70
71 return vacuumFinalize(db, pStmt, pzErrMsg); 71 return vacuumFinalize(db, pStmt, pzErrMsg);
72 } 72 }
73 73
74 /* 74 /*
75 ** The non-standard VACUUM command is used to clean up the database, 75 ** The VACUUM command is used to clean up the database,
76 ** collapse free space, etc. It is modelled after the VACUUM command 76 ** collapse free space, etc. It is modelled after the VACUUM command
77 ** in PostgreSQL. 77 ** in PostgreSQL. The VACUUM command works as follows:
78 ** 78 **
79 ** In version 1.0.x of SQLite, the VACUUM command would call 79 ** (1) Create a new transient database file
80 ** gdbm_reorganize() on all the database tables. But beginning 80 ** (2) Copy all content from the database being vacuumed into
81 ** with 2.0.0, SQLite no longer uses GDBM so this command has 81 ** the new transient database file
82 ** become a no-op. 82 ** (3) Copy content from the transient database back into the
83 ** original database.
84 **
85 ** The transient database requires temporary disk space approximately
86 ** equal to the size of the original database. The copy operation of
87 ** step (3) requires additional temporary disk space approximately equal
88 ** to the size of the original database for the rollback journal.
89 ** Hence, temporary disk space that is approximately 2x the size of the
90 ** original database is required. Every page of the database is written
91 ** approximately 3 times: Once for step (2) and twice for step (3).
92 ** Two writes per page are required in step (3) because the original
93 ** database content must be written into the rollback journal prior to
94 ** overwriting the database with the vacuumed content.
95 **
96 ** Only 1x temporary space and only 1x writes would be required if
97 ** the copy of step (3) were replace by deleting the original database
98 ** and renaming the transient database as the original. But that will
99 ** not work if other processes are attached to the original database.
100 ** And a power loss in between deleting the original and renaming the
101 ** transient would cause the database file to appear to be deleted
102 ** following reboot.
83 */ 103 */
84 void sqlite3Vacuum(Parse *pParse){ 104 void sqlite3Vacuum(Parse *pParse){
85 Vdbe *v = sqlite3GetVdbe(pParse); 105 Vdbe *v = sqlite3GetVdbe(pParse);
86 if( v ){ 106 if( v ){
87 sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0); 107 sqlite3VdbeAddOp2(v, OP_Vacuum, 0, 0);
108 sqlite3VdbeUsesBtree(v, 0);
88 } 109 }
89 return; 110 return;
90 } 111 }
91 112
92 /* 113 /*
93 ** This routine implements the OP_Vacuum opcode of the VDBE. 114 ** This routine implements the OP_Vacuum opcode of the VDBE.
94 */ 115 */
95 int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){ 116 int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
96 int rc = SQLITE_OK; /* Return code from service routines */ 117 int rc = SQLITE_OK; /* Return code from service routines */
97 Btree *pMain; /* The database being vacuumed */ 118 Btree *pMain; /* The database being vacuumed */
98 Btree *pTemp; /* The temporary database we vacuum into */ 119 Btree *pTemp; /* The temporary database we vacuum into */
99 char *zSql = 0; /* SQL statements */ 120 char *zSql = 0; /* SQL statements */
100 int saved_flags; /* Saved value of the db->flags */ 121 int saved_flags; /* Saved value of the db->flags */
101 int saved_nChange; /* Saved value of db->nChange */ 122 int saved_nChange; /* Saved value of db->nChange */
102 int saved_nTotalChange; /* Saved value of db->nTotalChange */ 123 int saved_nTotalChange; /* Saved value of db->nTotalChange */
103 void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */ 124 void (*saved_xTrace)(void*,const char*); /* Saved db->xTrace */
104 Db *pDb = 0; /* Database to detach at end of vacuum */ 125 Db *pDb = 0; /* Database to detach at end of vacuum */
105 int isMemDb; /* True if vacuuming a :memory: database */ 126 int isMemDb; /* True if vacuuming a :memory: database */
106 int nRes; /* Bytes of reserved space at the end of each page */ 127 int nRes; /* Bytes of reserved space at the end of each page */
107 int nDb; /* Number of attached databases */ 128 int nDb; /* Number of attached databases */
108 129
109 if( !db->autoCommit ){ 130 if( !db->autoCommit ){
110 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction"); 131 sqlite3SetString(pzErrMsg, db, "cannot VACUUM from within a transaction");
111 return SQLITE_ERROR; 132 return SQLITE_ERROR;
112 } 133 }
113 if( db->activeVdbeCnt>1 ){ 134 if( db->nVdbeActive>1 ){
114 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress"); 135 sqlite3SetString(pzErrMsg, db,"cannot VACUUM - SQL statements in progress");
115 return SQLITE_ERROR; 136 return SQLITE_ERROR;
116 } 137 }
117 138
118 /* Save the current value of the database flags so that it can be 139 /* Save the current value of the database flags so that it can be
119 ** restored before returning. Then set the writable-schema flag, and 140 ** restored before returning. Then set the writable-schema flag, and
120 ** disable CHECK and foreign key constraints. */ 141 ** disable CHECK and foreign key constraints. */
121 saved_flags = db->flags; 142 saved_flags = db->flags;
122 saved_nChange = db->nChange; 143 saved_nChange = db->nChange;
123 saved_nTotalChange = db->nTotalChange; 144 saved_nTotalChange = db->nTotalChange;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 #ifdef SQLITE_HAS_CODEC 190 #ifdef SQLITE_HAS_CODEC
170 if( db->nextPagesize ){ 191 if( db->nextPagesize ){
171 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*); 192 extern void sqlite3CodecGetKey(sqlite3*, int, void**, int*);
172 int nKey; 193 int nKey;
173 char *zKey; 194 char *zKey;
174 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey); 195 sqlite3CodecGetKey(db, 0, (void**)&zKey, &nKey);
175 if( nKey ) db->nextPagesize = 0; 196 if( nKey ) db->nextPagesize = 0;
176 } 197 }
177 #endif 198 #endif
178 199
200 rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
201 if( rc!=SQLITE_OK ) goto end_of_vacuum;
202
203 /* Begin a transaction and take an exclusive lock on the main database
204 ** file. This is done before the sqlite3BtreeGetPageSize(pMain) call below,
205 ** to ensure that we do not try to change the page-size on a WAL database.
206 */
207 rc = execSql(db, pzErrMsg, "BEGIN;");
208 if( rc!=SQLITE_OK ) goto end_of_vacuum;
209 rc = sqlite3BtreeBeginTrans(pMain, 2);
210 if( rc!=SQLITE_OK ) goto end_of_vacuum;
211
179 /* Do not attempt to change the page size for a WAL database */ 212 /* Do not attempt to change the page size for a WAL database */
180 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain)) 213 if( sqlite3PagerGetJournalMode(sqlite3BtreePager(pMain))
181 ==PAGER_JOURNALMODE_WAL ){ 214 ==PAGER_JOURNALMODE_WAL ){
182 db->nextPagesize = 0; 215 db->nextPagesize = 0;
183 } 216 }
184 217
185 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0) 218 if( sqlite3BtreeSetPageSize(pTemp, sqlite3BtreeGetPageSize(pMain), nRes, 0)
186 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0)) 219 || (!isMemDb && sqlite3BtreeSetPageSize(pTemp, db->nextPagesize, nRes, 0))
187 || NEVER(db->mallocFailed) 220 || NEVER(db->mallocFailed)
188 ){ 221 ){
189 rc = SQLITE_NOMEM; 222 rc = SQLITE_NOMEM;
190 goto end_of_vacuum; 223 goto end_of_vacuum;
191 } 224 }
192 rc = execSql(db, pzErrMsg, "PRAGMA vacuum_db.synchronous=OFF");
193 if( rc!=SQLITE_OK ){
194 goto end_of_vacuum;
195 }
196 225
197 #ifndef SQLITE_OMIT_AUTOVACUUM 226 #ifndef SQLITE_OMIT_AUTOVACUUM
198 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac : 227 sqlite3BtreeSetAutoVacuum(pTemp, db->nextAutovac>=0 ? db->nextAutovac :
199 sqlite3BtreeGetAutoVacuum(pMain)); 228 sqlite3BtreeGetAutoVacuum(pMain));
200 #endif 229 #endif
201 230
202 /* Begin a transaction */
203 rc = execSql(db, pzErrMsg, "BEGIN EXCLUSIVE;");
204 if( rc!=SQLITE_OK ) goto end_of_vacuum;
205
206 /* Query the schema of the main database. Create a mirror schema 231 /* Query the schema of the main database. Create a mirror schema
207 ** in the temporary database. 232 ** in the temporary database.
208 */ 233 */
209 rc = execExecSql(db, pzErrMsg, 234 rc = execExecSql(db, pzErrMsg,
210 "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) " 235 "SELECT 'CREATE TABLE vacuum_db.' || substr(sql,14) "
211 " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'" 236 " FROM sqlite_master WHERE type='table' AND name!='sqlite_sequence'"
212 " AND rootpage>0" 237 " AND coalesce(rootpage,1)>0"
213 ); 238 );
214 if( rc!=SQLITE_OK ) goto end_of_vacuum; 239 if( rc!=SQLITE_OK ) goto end_of_vacuum;
215 rc = execExecSql(db, pzErrMsg, 240 rc = execExecSql(db, pzErrMsg,
216 "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)" 241 "SELECT 'CREATE INDEX vacuum_db.' || substr(sql,14)"
217 " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' "); 242 " FROM sqlite_master WHERE sql LIKE 'CREATE INDEX %' ");
218 if( rc!=SQLITE_OK ) goto end_of_vacuum; 243 if( rc!=SQLITE_OK ) goto end_of_vacuum;
219 rc = execExecSql(db, pzErrMsg, 244 rc = execExecSql(db, pzErrMsg,
220 "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) " 245 "SELECT 'CREATE UNIQUE INDEX vacuum_db.' || substr(sql,21) "
221 " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'"); 246 " FROM sqlite_master WHERE sql LIKE 'CREATE UNIQUE INDEX %'");
222 if( rc!=SQLITE_OK ) goto end_of_vacuum; 247 if( rc!=SQLITE_OK ) goto end_of_vacuum;
223 248
224 /* Loop through the tables in the main database. For each, do 249 /* Loop through the tables in the main database. For each, do
225 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy 250 ** an "INSERT INTO vacuum_db.xxx SELECT * FROM main.xxx;" to copy
226 ** the contents to the temporary database. 251 ** the contents to the temporary database.
227 */ 252 */
228 rc = execExecSql(db, pzErrMsg, 253 rc = execExecSql(db, pzErrMsg,
229 "SELECT 'INSERT INTO vacuum_db.' || quote(name) " 254 "SELECT 'INSERT INTO vacuum_db.' || quote(name) "
230 "|| ' SELECT * FROM main.' || quote(name) || ';'" 255 "|| ' SELECT * FROM main.' || quote(name) || ';'"
231 "FROM main.sqlite_master " 256 "FROM main.sqlite_master "
232 "WHERE type = 'table' AND name!='sqlite_sequence' " 257 "WHERE type = 'table' AND name!='sqlite_sequence' "
233 " AND rootpage>0" 258 " AND coalesce(rootpage,1)>0"
234 ); 259 );
235 if( rc!=SQLITE_OK ) goto end_of_vacuum; 260 if( rc!=SQLITE_OK ) goto end_of_vacuum;
236 261
237 /* Copy over the sequence table 262 /* Copy over the sequence table
238 */ 263 */
239 rc = execExecSql(db, pzErrMsg, 264 rc = execExecSql(db, pzErrMsg,
240 "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' " 265 "SELECT 'DELETE FROM vacuum_db.' || quote(name) || ';' "
241 "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' " 266 "FROM vacuum_db.sqlite_master WHERE name='sqlite_sequence' "
242 ); 267 );
243 if( rc!=SQLITE_OK ) goto end_of_vacuum; 268 if( rc!=SQLITE_OK ) goto end_of_vacuum;
(...skipping 12 matching lines...) Expand all
256 */ 281 */
257 rc = execSql(db, pzErrMsg, 282 rc = execSql(db, pzErrMsg,
258 "INSERT INTO vacuum_db.sqlite_master " 283 "INSERT INTO vacuum_db.sqlite_master "
259 " SELECT type, name, tbl_name, rootpage, sql" 284 " SELECT type, name, tbl_name, rootpage, sql"
260 " FROM main.sqlite_master" 285 " FROM main.sqlite_master"
261 " WHERE type='view' OR type='trigger'" 286 " WHERE type='view' OR type='trigger'"
262 " OR (type='table' AND rootpage=0)" 287 " OR (type='table' AND rootpage=0)"
263 ); 288 );
264 if( rc ) goto end_of_vacuum; 289 if( rc ) goto end_of_vacuum;
265 290
266 /* At this point, unless the main db was completely empty, there is now a 291 /* At this point, there is a write transaction open on both the
267 ** transaction open on the vacuum database, but not on the main database. 292 ** vacuum database and the main database. Assuming no error occurs,
268 ** Open a btree level transaction on the main database. This allows a 293 ** both transactions are closed by this block - the main database
269 ** call to sqlite3BtreeCopyFile(). The main database btree level 294 ** transaction by sqlite3BtreeCopyFile() and the other by an explicit
270 ** transaction is then committed, so the SQL level never knows it was 295 ** call to sqlite3BtreeCommit().
271 ** opened for writing. This way, the SQL transaction used to create the
272 ** temporary database never needs to be committed.
273 */ 296 */
274 { 297 {
275 u32 meta; 298 u32 meta;
276 int i; 299 int i;
277 300
278 /* This array determines which meta meta values are preserved in the 301 /* This array determines which meta meta values are preserved in the
279 ** vacuum. Even entries are the meta value number and odd entries 302 ** vacuum. Even entries are the meta value number and odd entries
280 ** are an increment to apply to the meta value after the vacuum. 303 ** are an increment to apply to the meta value after the vacuum.
281 ** The increment is used to increase the schema cookie so that other 304 ** The increment is used to increase the schema cookie so that other
282 ** connections to the same database will know to reread the schema. 305 ** connections to the same database will know to reread the schema.
283 */ 306 */
284 static const unsigned char aCopy[] = { 307 static const unsigned char aCopy[] = {
285 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */ 308 BTREE_SCHEMA_VERSION, 1, /* Add one to the old schema cookie */
286 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */ 309 BTREE_DEFAULT_CACHE_SIZE, 0, /* Preserve the default page cache size */
287 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */ 310 BTREE_TEXT_ENCODING, 0, /* Preserve the text encoding */
288 BTREE_USER_VERSION, 0, /* Preserve the user version */ 311 BTREE_USER_VERSION, 0, /* Preserve the user version */
312 BTREE_APPLICATION_ID, 0, /* Preserve the application id */
289 }; 313 };
290 314
291 assert( 1==sqlite3BtreeIsInTrans(pTemp) ); 315 assert( 1==sqlite3BtreeIsInTrans(pTemp) );
292 assert( 1==sqlite3BtreeIsInTrans(pMain) ); 316 assert( 1==sqlite3BtreeIsInTrans(pMain) );
293 317
294 /* Copy Btree meta values */ 318 /* Copy Btree meta values */
295 for(i=0; i<ArraySize(aCopy); i+=2){ 319 for(i=0; i<ArraySize(aCopy); i+=2){
296 /* GetMeta() and UpdateMeta() cannot fail in this context because 320 /* GetMeta() and UpdateMeta() cannot fail in this context because
297 ** we already have page 1 loaded into cache and marked dirty. */ 321 ** we already have page 1 loaded into cache and marked dirty. */
298 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta); 322 sqlite3BtreeGetMeta(pMain, aCopy[i], &meta);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 db->autoCommit = 1; 354 db->autoCommit = 1;
331 355
332 if( pDb ){ 356 if( pDb ){
333 sqlite3BtreeClose(pDb->pBt); 357 sqlite3BtreeClose(pDb->pBt);
334 pDb->pBt = 0; 358 pDb->pBt = 0;
335 pDb->pSchema = 0; 359 pDb->pSchema = 0;
336 } 360 }
337 361
338 /* This both clears the schemas and reduces the size of the db->aDb[] 362 /* This both clears the schemas and reduces the size of the db->aDb[]
339 ** array. */ 363 ** array. */
340 sqlite3ResetInternalSchema(db, -1); 364 sqlite3ResetAllSchemasOfConnection(db);
341 365
342 return rc; 366 return rc;
343 } 367 }
344 368
345 #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */ 369 #endif /* SQLITE_OMIT_VACUUM && SQLITE_OMIT_ATTACH */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3080704/src/util.c ('k') | third_party/sqlite/sqlite-src-3080704/src/vdbe.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698