monotone

monotone Mtn Source Tree

Root/sqlite/vacuum.c

1/*
2** 2003 April 6
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** This file contains code used to implement the VACUUM command.
13**
14** Most of the code in this file may be omitted by defining the
15** SQLITE_OMIT_VACUUM macro.
16**
17** $Id: vacuum.c,v 1.1 2003/08/05 23:03:08 graydon Exp $
18*/
19#include "sqliteInt.h"
20#include "os.h"
21
22/*
23** A structure for holding a dynamic string - a string that can grow
24** without bound.
25*/
26typedef struct dynStr dynStr;
27struct dynStr {
28 char *z; /* Text of the string in space obtained from sqliteMalloc() */
29 int nAlloc; /* Amount of space allocated to z[] */
30 int nUsed; /* Next unused slot in z[] */
31};
32
33/*
34** A structure that holds the vacuum context
35*/
36typedef struct vacuumStruct vacuumStruct;
37struct vacuumStruct {
38 sqlite *dbOld; /* Original database */
39 sqlite *dbNew; /* New database */
40 Parse *pParse; /* The parser context */
41 const char *zTable; /* Name of a table being copied */
42 const char *zPragma; /* Pragma to execute with results */
43 dynStr s1, s2; /* Two dynamic strings */
44};
45
46#if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
47/*
48** Append text to a dynamic string
49*/
50static void appendText(dynStr *p, const char *zText, int nText){
51 if( nText<0 ) nText = strlen(zText);
52 if( p->z==0 || p->nUsed + nText + 1 >= p->nAlloc ){
53 char *zNew;
54 p->nAlloc = p->nUsed + nText + 1000;
55 zNew = sqliteRealloc(p->z, p->nAlloc);
56 if( zNew==0 ){
57 sqliteFree(p->z);
58 memset(p, 0, sizeof(*p));
59 return;
60 }
61 p->z = zNew;
62 }
63 memcpy(&p->z[p->nUsed], zText, nText+1);
64 p->nUsed += nText;
65}
66
67/*
68** Append text to a dynamic string, having first put the text in quotes.
69*/
70static void appendQuoted(dynStr *p, const char *zText){
71 int i, j;
72 appendText(p, "'", 1);
73 for(i=j=0; zText[i]; i++){
74 if( zText[i]=='\'' ){
75 appendText(p, &zText[j], i-j+1);
76 j = i + 1;
77 appendText(p, "'", 1);
78 }
79 }
80 if( j<i ){
81 appendText(p, &zText[j], i-j);
82 }
83 appendText(p, "'", 1);
84}
85
86/*
87** Execute statements of SQL. If an error occurs, write the error
88** message into pParse->zErrMsg and return non-zero.
89*/
90static int execsql(Parse *pParse, sqlite *db, const char *zSql){
91 int rc;
92 char *zErrMsg = 0;
93
94 /* printf("***** executing *****\n%s\n", zSql); */
95 rc = sqlite_exec(db, zSql, 0, 0, &zErrMsg);
96 if( rc ){
97 sqliteErrorMsg(pParse, "%s", zErrMsg);
98 sqlite_freemem(zErrMsg);
99 }
100 return rc;
101}
102
103/*
104** This is the second stage callback. Each invocation contains all the
105** data for a single row of a single table in the original database. This
106** routine must write that information into the new database.
107*/
108static int vacuumCallback2(void *pArg, int argc, char **argv, char **NotUsed){
109 vacuumStruct *p = (vacuumStruct*)pArg;
110 int rc = 0;
111 const char *zSep = "(";
112 int i;
113
114 p->s2.nUsed = 0;
115 appendText(&p->s2, "INSERT INTO ", -1);
116 appendQuoted(&p->s2, p->zTable);
117 appendText(&p->s2, " VALUES", -1);
118 for(i=0; i<argc; i++){
119 appendText(&p->s2, zSep, 1);
120 zSep = ",";
121 if( argv[i]==0 ){
122 appendText(&p->s2, "NULL", 4);
123 }else{
124 appendQuoted(&p->s2, argv[i]);
125 }
126 }
127 appendText(&p->s2,")", 1);
128 rc = execsql(p->pParse, p->dbNew, p->s2.z);
129 return rc;
130}
131
132/*
133** This is the first stage callback. Each invocation contains three
134** arguments where are taken from the SQLITE_MASTER table of the original
135** database: (1) the entry type, (2) the entry name, and (3) the SQL for
136** the entry. In all cases, execute the SQL of the third argument.
137** For tables, run a query to select all entries in that table and
138** transfer them to the second-stage callback.
139*/
140static int vacuumCallback1(void *pArg, int argc, char **argv, char **NotUsed){
141 vacuumStruct *p = (vacuumStruct*)pArg;
142 int rc = 0;
143 assert( argc==3 );
144 assert( argv[0]!=0 );
145 assert( argv[1]!=0 );
146 assert( argv[2]!=0 );
147 rc = execsql(p->pParse, p->dbNew, argv[2]);
148 if( rc==SQLITE_OK && strcmp(argv[0],"table")==0 ){
149 char *zErrMsg = 0;
150 p->s1.nUsed = 0;
151 appendText(&p->s1, "SELECT * FROM ", -1);
152 appendQuoted(&p->s1, argv[1]);
153 p->zTable = argv[1];
154 rc = sqlite_exec(p->dbOld, p->s1.z, vacuumCallback2, p, &zErrMsg);
155 if( rc && p->pParse->zErrMsg==0 ){
156 sqliteErrorMsg(p->pParse, "%s", zErrMsg);
157 }
158 }
159 return rc;
160}
161
162/*
163** This callback is used to transfer PRAGMA settings from one database
164** to the other. The value in argv[0] should be passed to a pragma
165** identified by ((vacuumStruct*)pArg)->zPragma.
166*/
167static int vacuumCallback3(void *pArg, int argc, char **argv, char **NotUsed){
168 vacuumStruct *p = (vacuumStruct*)pArg;
169 int rc = 0;
170 char zBuf[200];
171 assert( argc==1 );
172 assert( argv[0]!=0 );
173 assert( strlen(p->zPragma)<100 );
174 assert( strlen(argv[0])<30 );
175 sprintf(zBuf,"PRAGMA %s=%s;", p->zPragma, argv[0]);
176 rc = execsql(p->pParse, p->dbNew, zBuf);
177 return rc;
178}
179
180/*
181** Generate a random name of 20 character in length.
182*/
183static void randomName(char *zBuf){
184 static const char zChars[] =
185 "abcdefghijklmnopqrstuvwxyz"
186 "0123456789";
187 int i;
188 for(i=0; i<20; i++){
189 int n = sqliteRandomByte() % (sizeof(zChars)-1);
190 zBuf[i] = zChars[n];
191 }
192}
193#endif
194
195/*
196** The non-standard VACUUM command is used to clean up the database,
197** collapse free space, etc. It is modelled after the VACUUM command
198** in PostgreSQL.
199**
200** In version 1.0.x of SQLite, the VACUUM command would call
201** gdbm_reorganize() on all the database tables. But beginning
202** with 2.0.0, SQLite no longer uses GDBM so this command has
203** become a no-op.
204*/
205void sqliteVacuum(Parse *pParse, Token *pTableName){
206#if !defined(SQLITE_OMIT_VACUUM) || SQLITE_OMIT_VACUUM
207 const char *zFilename; /* full pathname of the database file */
208 int nFilename; /* number of characters in zFilename[] */
209 char *zTemp = 0; /* a temporary file in same directory as zFilename */
210 sqlite *dbNew = 0; /* The new vacuumed database */
211 sqlite *db; /* The original database */
212 int rc = SQLITE_OK; /* Return code from service routines */
213 int i; /* Loop counter */
214 char *zErrMsg = 0; /* Error messages stored here */
215 int safety = 0; /* TRUE if safety is off */
216 vacuumStruct sVac; /* Information passed to callbacks */
217
218 /* These are all of the pragmas that need to be transferred over
219 ** to the new database */
220 static const char *zPragma[] = {
221 "default_synchronous",
222 "default_cache_size",
223 /* "default_temp_store", */
224 };
225
226 /* Initial error checks
227 */
228 if( pParse->explain ){
229 return;
230 }
231 db = pParse->db;
232 if( db->flags & SQLITE_InTrans ){
233 sqliteErrorMsg(pParse, "cannot VACUUM from within a transaction");
234 return;
235 }
236 memset(&sVac, 0, sizeof(sVac));
237
238 /* Get the full pathname of the database file and create two
239 ** temporary filenames in the same directory as the original file.
240 */
241 zFilename = sqliteBtreeGetFilename(db->aDb[0].pBt);
242 if( zFilename==0 ){
243 /* This only happens with the in-memory database. VACUUM is a no-op
244 ** there, so just return */
245 return;
246 }
247 nFilename = strlen(zFilename);
248 zTemp = sqliteMalloc( nFilename+100 );
249 if( zTemp==0 ) return;
250 strcpy(zTemp, zFilename);
251 for(i=0; i<10; i++){
252 zTemp[nFilename] = '-';
253 randomName(&zTemp[nFilename+1]);
254 if( !sqliteOsFileExists(zTemp) ) break;
255 }
256 if( i>=10 ){
257 sqliteErrorMsg(pParse, "unable to create a temporary database file "
258 "in the same directory as the original database");
259 goto end_of_vacuum;
260 }
261
262
263 dbNew = sqlite_open(zTemp, 0, &zErrMsg);
264 if( dbNew==0 ){
265 sqliteErrorMsg(pParse, "unable to open a temporary database at %s - %s",
266 zTemp, zErrMsg);
267 goto end_of_vacuum;
268 }
269 if( sqliteSafetyOff(db) ){
270 sqliteErrorMsg(pParse, "library routines called out of sequence");
271 goto end_of_vacuum;
272 }
273 safety = 1;
274 if( execsql(pParse, db, "BEGIN") ) goto end_of_vacuum;
275 if( execsql(pParse, dbNew, "PRAGMA synchronous=off; BEGIN") ){
276 goto end_of_vacuum;
277 }
278 sVac.dbOld = db;
279 sVac.dbNew = dbNew;
280 sVac.pParse = pParse;
281 for(i=0; rc==SQLITE_OK && i<sizeof(zPragma)/sizeof(zPragma[0]); i++){
282 char zBuf[200];
283 assert( strlen(zPragma[i])<100 );
284 sprintf(zBuf, "PRAGMA %s;", zPragma[i]);
285 sVac.zPragma = zPragma[i];
286 rc = sqlite_exec(db, zBuf, vacuumCallback3, &sVac, &zErrMsg);
287 }
288 if( !rc ){
289 rc = sqlite_exec(db, "SELECT type, name, sql FROM sqlite_master "
290 "WHERE sql NOT NULL", vacuumCallback1, &sVac, &zErrMsg);
291 }
292 if( !rc ){
293 rc = sqliteBtreeCopyFile(db->aDb[0].pBt, dbNew->aDb[0].pBt);
294 sqlite_exec(db, "COMMIT", 0, 0, 0);
295 sqlite_exec(db, "ROLLBACK", 0, 0, 0); /* In case the COMMIT failed */
296 sqliteResetInternalSchema(db, 0);
297 }
298
299end_of_vacuum:
300 if( rc && pParse->zErrMsg==0 && zErrMsg!=0 ){
301 sqliteErrorMsg(pParse, "unable to vacuum database - %s", zErrMsg);
302 }
303 if( safety ) {
304 sqlite_exec(db, "COMMIT", 0, 0, 0);
305 sqlite_exec(db, "ROLLBACK", 0, 0, 0); /* In case the COMMIT failed */
306 sqliteSafetyOn(db);
307 }
308 if( dbNew ) sqlite_close(dbNew);
309 sqliteOsDelete(zTemp);
310 sqliteFree(zTemp);
311 sqliteFree(sVac.s1.z);
312 sqliteFree(sVac.s2.z);
313 if( zErrMsg ) sqlite_freemem(zErrMsg);
314 return;
315#endif
316}

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status