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