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

Side by Side Diff: third_party/sqlite/sqlite-src-3100200/src/dbstat.c

Issue 1610543003: [sql] Import reference version of SQLite 3.10.2. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 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 ** 2010 July 12 2 ** 2010 July 12
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 **
11 ****************************************************************************** 11 ******************************************************************************
12 ** 12 **
13 ** This file contains an implementation of the "dbstat" virtual table. 13 ** This file contains an implementation of the "dbstat" virtual table.
14 ** 14 **
15 ** The dbstat virtual table is used to extract low-level formatting 15 ** The dbstat virtual table is used to extract low-level formatting
16 ** information from an SQLite database in order to implement the 16 ** information from an SQLite database in order to implement the
17 ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script 17 ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script
18 ** for an example implementation. 18 ** for an example implementation.
19 **
20 ** Additional information is available on the "dbstat.html" page of the
21 ** official SQLite documentation.
19 */ 22 */
20 23
21 #ifndef SQLITE_AMALGAMATION 24 #include "sqliteInt.h" /* Requires access to internal data structures */
22 # include "sqliteInt.h" 25 #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \
23 #endif 26 && !defined(SQLITE_OMIT_VIRTUALTABLE)
24
25 #ifndef SQLITE_OMIT_VIRTUALTABLE
26 27
27 /* 28 /*
28 ** Page paths: 29 ** Page paths:
29 ** 30 **
30 ** The value of the 'path' column describes the path taken from the 31 ** The value of the 'path' column describes the path taken from the
31 ** root-node of the b-tree structure to each page. The value of the 32 ** root-node of the b-tree structure to each page. The value of the
32 ** root-node path is '/'. 33 ** root-node path is '/'.
33 ** 34 **
34 ** The value of the path for the left-most child page of the root of 35 ** The value of the path for the left-most child page of the root of
35 ** a b-tree is '/000/'. (Btrees store content ordered from left to right 36 ** a b-tree is '/000/'. (Btrees store content ordered from left to right
(...skipping 23 matching lines...) Expand all
59 "CREATE TABLE xx( " \ 60 "CREATE TABLE xx( " \
60 " name STRING, /* Name of table or index */" \ 61 " name STRING, /* Name of table or index */" \
61 " path INTEGER, /* Path to page from root */" \ 62 " path INTEGER, /* Path to page from root */" \
62 " pageno INTEGER, /* Page number */" \ 63 " pageno INTEGER, /* Page number */" \
63 " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \ 64 " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \
64 " ncell INTEGER, /* Cells on page (0 for overflow) */" \ 65 " ncell INTEGER, /* Cells on page (0 for overflow) */" \
65 " payload INTEGER, /* Bytes of payload on this page */" \ 66 " payload INTEGER, /* Bytes of payload on this page */" \
66 " unused INTEGER, /* Bytes of unused space on this page */" \ 67 " unused INTEGER, /* Bytes of unused space on this page */" \
67 " mx_payload INTEGER, /* Largest payload size of all cells */" \ 68 " mx_payload INTEGER, /* Largest payload size of all cells */" \
68 " pgoffset INTEGER, /* Offset of page in file */" \ 69 " pgoffset INTEGER, /* Offset of page in file */" \
69 " pgsize INTEGER /* Size of the page */" \ 70 " pgsize INTEGER, /* Size of the page */" \
71 " schema TEXT HIDDEN /* Database schema being analyzed */" \
70 ");" 72 ");"
71 73
72 74
73 typedef struct StatTable StatTable; 75 typedef struct StatTable StatTable;
74 typedef struct StatCursor StatCursor; 76 typedef struct StatCursor StatCursor;
75 typedef struct StatPage StatPage; 77 typedef struct StatPage StatPage;
76 typedef struct StatCell StatCell; 78 typedef struct StatCell StatCell;
77 79
78 struct StatCell { 80 struct StatCell {
79 int nLocal; /* Bytes of local payload */ 81 int nLocal; /* Bytes of local payload */
(...skipping 17 matching lines...) Expand all
97 int nUnused; /* Number of unused bytes on page */ 99 int nUnused; /* Number of unused bytes on page */
98 StatCell *aCell; /* Array of parsed cells */ 100 StatCell *aCell; /* Array of parsed cells */
99 u32 iRightChildPg; /* Right-child page number (or 0) */ 101 u32 iRightChildPg; /* Right-child page number (or 0) */
100 int nMxPayload; /* Largest payload of any cell on this page */ 102 int nMxPayload; /* Largest payload of any cell on this page */
101 }; 103 };
102 104
103 struct StatCursor { 105 struct StatCursor {
104 sqlite3_vtab_cursor base; 106 sqlite3_vtab_cursor base;
105 sqlite3_stmt *pStmt; /* Iterates through set of root pages */ 107 sqlite3_stmt *pStmt; /* Iterates through set of root pages */
106 int isEof; /* After pStmt has returned SQLITE_DONE */ 108 int isEof; /* After pStmt has returned SQLITE_DONE */
109 int iDb; /* Schema used for this query */
107 110
108 StatPage aPage[32]; 111 StatPage aPage[32];
109 int iPage; /* Current entry in aPage[] */ 112 int iPage; /* Current entry in aPage[] */
110 113
111 /* Values to return. */ 114 /* Values to return. */
112 char *zName; /* Value of 'name' column */ 115 char *zName; /* Value of 'name' column */
113 char *zPath; /* Value of 'path' column */ 116 char *zPath; /* Value of 'path' column */
114 u32 iPageno; /* Value of 'pageno' column */ 117 u32 iPageno; /* Value of 'pageno' column */
115 char *zPagetype; /* Value of 'pagetype' column */ 118 char *zPagetype; /* Value of 'pagetype' column */
116 int nCell; /* Value of 'ncell' column */ 119 int nCell; /* Value of 'ncell' column */
117 int nPayload; /* Value of 'payload' column */ 120 int nPayload; /* Value of 'payload' column */
118 int nUnused; /* Value of 'unused' column */ 121 int nUnused; /* Value of 'unused' column */
119 int nMxPayload; /* Value of 'mx_payload' column */ 122 int nMxPayload; /* Value of 'mx_payload' column */
120 i64 iOffset; /* Value of 'pgOffset' column */ 123 i64 iOffset; /* Value of 'pgOffset' column */
121 int szPage; /* Value of 'pgSize' column */ 124 int szPage; /* Value of 'pgSize' column */
122 }; 125 };
123 126
124 struct StatTable { 127 struct StatTable {
125 sqlite3_vtab base; 128 sqlite3_vtab base;
126 sqlite3 *db; 129 sqlite3 *db;
130 int iDb; /* Index of database to analyze */
127 }; 131 };
128 132
129 #ifndef get2byte 133 #ifndef get2byte
130 # define get2byte(x) ((x)[0]<<8 | (x)[1]) 134 # define get2byte(x) ((x)[0]<<8 | (x)[1])
131 #endif 135 #endif
132 136
133 /* 137 /*
134 ** Connect to or create a statvfs virtual table. 138 ** Connect to or create a statvfs virtual table.
135 */ 139 */
136 static int statConnect( 140 static int statConnect(
137 sqlite3 *db, 141 sqlite3 *db,
138 void *pAux, 142 void *pAux,
139 int argc, const char *const*argv, 143 int argc, const char *const*argv,
140 sqlite3_vtab **ppVtab, 144 sqlite3_vtab **ppVtab,
141 char **pzErr 145 char **pzErr
142 ){ 146 ){
143 StatTable *pTab; 147 StatTable *pTab = 0;
148 int rc = SQLITE_OK;
149 int iDb;
144 150
145 pTab = (StatTable *)sqlite3_malloc(sizeof(StatTable)); 151 if( argc>=4 ){
146 memset(pTab, 0, sizeof(StatTable)); 152 iDb = sqlite3FindDbName(db, argv[3]);
147 pTab->db = db; 153 if( iDb<0 ){
154 *pzErr = sqlite3_mprintf("no such database: %s", argv[3]);
155 return SQLITE_ERROR;
156 }
157 }else{
158 iDb = 0;
159 }
160 rc = sqlite3_declare_vtab(db, VTAB_SCHEMA);
161 if( rc==SQLITE_OK ){
162 pTab = (StatTable *)sqlite3_malloc64(sizeof(StatTable));
163 if( pTab==0 ) rc = SQLITE_NOMEM;
164 }
148 165
149 sqlite3_declare_vtab(db, VTAB_SCHEMA); 166 assert( rc==SQLITE_OK || pTab==0 );
150 *ppVtab = &pTab->base; 167 if( rc==SQLITE_OK ){
151 return SQLITE_OK; 168 memset(pTab, 0, sizeof(StatTable));
169 pTab->db = db;
170 pTab->iDb = iDb;
171 }
172
173 *ppVtab = (sqlite3_vtab*)pTab;
174 return rc;
152 } 175 }
153 176
154 /* 177 /*
155 ** Disconnect from or destroy a statvfs virtual table. 178 ** Disconnect from or destroy a statvfs virtual table.
156 */ 179 */
157 static int statDisconnect(sqlite3_vtab *pVtab){ 180 static int statDisconnect(sqlite3_vtab *pVtab){
158 sqlite3_free(pVtab); 181 sqlite3_free(pVtab);
159 return SQLITE_OK; 182 return SQLITE_OK;
160 } 183 }
161 184
162 /* 185 /*
163 ** There is no "best-index". This virtual table always does a linear 186 ** There is no "best-index". This virtual table always does a linear
164 ** scan of the binary VFS log file. 187 ** scan. However, a schema=? constraint should cause this table to
188 ** operate on a different database schema, so check for it.
189 **
190 ** idxNum is normally 0, but will be 1 if a schema=? constraint exists.
165 */ 191 */
166 static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ 192 static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
193 int i;
194
195 pIdxInfo->estimatedCost = 1.0e6; /* Initial cost estimate */
196
197 /* Look for a valid schema=? constraint. If found, change the idxNum to
198 ** 1 and request the value of that constraint be sent to xFilter. And
199 ** lower the cost estimate to encourage the constrained version to be
200 ** used.
201 */
202 for(i=0; i<pIdxInfo->nConstraint; i++){
203 if( pIdxInfo->aConstraint[i].usable==0 ) continue;
204 if( pIdxInfo->aConstraint[i].op!=SQLITE_INDEX_CONSTRAINT_EQ ) continue;
205 if( pIdxInfo->aConstraint[i].iColumn!=10 ) continue;
206 pIdxInfo->idxNum = 1;
207 pIdxInfo->estimatedCost = 1.0;
208 pIdxInfo->aConstraintUsage[i].argvIndex = 1;
209 pIdxInfo->aConstraintUsage[i].omit = 1;
210 break;
211 }
212
167 213
168 /* Records are always returned in ascending order of (name, path). 214 /* Records are always returned in ascending order of (name, path).
169 ** If this will satisfy the client, set the orderByConsumed flag so that 215 ** If this will satisfy the client, set the orderByConsumed flag so that
170 ** SQLite does not do an external sort. 216 ** SQLite does not do an external sort.
171 */ 217 */
172 if( ( pIdxInfo->nOrderBy==1 218 if( ( pIdxInfo->nOrderBy==1
173 && pIdxInfo->aOrderBy[0].iColumn==0 219 && pIdxInfo->aOrderBy[0].iColumn==0
174 && pIdxInfo->aOrderBy[0].desc==0 220 && pIdxInfo->aOrderBy[0].desc==0
175 ) || 221 ) ||
176 ( pIdxInfo->nOrderBy==2 222 ( pIdxInfo->nOrderBy==2
177 && pIdxInfo->aOrderBy[0].iColumn==0 223 && pIdxInfo->aOrderBy[0].iColumn==0
178 && pIdxInfo->aOrderBy[0].desc==0 224 && pIdxInfo->aOrderBy[0].desc==0
179 && pIdxInfo->aOrderBy[1].iColumn==1 225 && pIdxInfo->aOrderBy[1].iColumn==1
180 && pIdxInfo->aOrderBy[1].desc==0 226 && pIdxInfo->aOrderBy[1].desc==0
181 ) 227 )
182 ){ 228 ){
183 pIdxInfo->orderByConsumed = 1; 229 pIdxInfo->orderByConsumed = 1;
184 } 230 }
185 231
186 pIdxInfo->estimatedCost = 10.0;
187 return SQLITE_OK; 232 return SQLITE_OK;
188 } 233 }
189 234
190 /* 235 /*
191 ** Open a new statvfs cursor. 236 ** Open a new statvfs cursor.
192 */ 237 */
193 static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ 238 static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
194 StatTable *pTab = (StatTable *)pVTab; 239 StatTable *pTab = (StatTable *)pVTab;
195 StatCursor *pCsr; 240 StatCursor *pCsr;
196 int rc;
197 241
198 pCsr = (StatCursor *)sqlite3_malloc(sizeof(StatCursor)); 242 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor));
199 memset(pCsr, 0, sizeof(StatCursor)); 243 if( pCsr==0 ){
200 pCsr->base.pVtab = pVTab; 244 return SQLITE_NOMEM;
201 245 }else{
202 rc = sqlite3_prepare_v2(pTab->db, 246 memset(pCsr, 0, sizeof(StatCursor));
203 "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type" 247 pCsr->base.pVtab = pVTab;
204 " UNION ALL " 248 pCsr->iDb = pTab->iDb;
205 "SELECT name, rootpage, type FROM sqlite_master WHERE rootpage!=0"
206 " ORDER BY name", -1,
207 &pCsr->pStmt, 0
208 );
209 if( rc!=SQLITE_OK ){
210 sqlite3_free(pCsr);
211 return rc;
212 } 249 }
213 250
214 *ppCursor = (sqlite3_vtab_cursor *)pCsr; 251 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
215 return SQLITE_OK; 252 return SQLITE_OK;
216 } 253 }
217 254
218 static void statClearPage(StatPage *p){ 255 static void statClearPage(StatPage *p){
219 int i; 256 int i;
220 for(i=0; i<p->nCell; i++){ 257 if( p->aCell ){
221 sqlite3_free(p->aCell[i].aOvfl); 258 for(i=0; i<p->nCell; i++){
259 sqlite3_free(p->aCell[i].aOvfl);
260 }
261 sqlite3_free(p->aCell);
222 } 262 }
223 sqlite3PagerUnref(p->pPg); 263 sqlite3PagerUnref(p->pPg);
224 sqlite3_free(p->aCell);
225 sqlite3_free(p->zPath); 264 sqlite3_free(p->zPath);
226 memset(p, 0, sizeof(StatPage)); 265 memset(p, 0, sizeof(StatPage));
227 } 266 }
228 267
229 static void statResetCsr(StatCursor *pCsr){ 268 static void statResetCsr(StatCursor *pCsr){
230 int i; 269 int i;
231 sqlite3_reset(pCsr->pStmt); 270 sqlite3_reset(pCsr->pStmt);
232 for(i=0; i<ArraySize(pCsr->aPage); i++){ 271 for(i=0; i<ArraySize(pCsr->aPage); i++){
233 statClearPage(&pCsr->aPage[i]); 272 statClearPage(&pCsr->aPage[i]);
234 } 273 }
235 pCsr->iPage = 0; 274 pCsr->iPage = 0;
236 sqlite3_free(pCsr->zPath); 275 sqlite3_free(pCsr->zPath);
237 pCsr->zPath = 0; 276 pCsr->zPath = 0;
277 pCsr->isEof = 0;
238 } 278 }
239 279
240 /* 280 /*
241 ** Close a statvfs cursor. 281 ** Close a statvfs cursor.
242 */ 282 */
243 static int statClose(sqlite3_vtab_cursor *pCursor){ 283 static int statClose(sqlite3_vtab_cursor *pCursor){
244 StatCursor *pCsr = (StatCursor *)pCursor; 284 StatCursor *pCsr = (StatCursor *)pCursor;
245 statResetCsr(pCsr); 285 statResetCsr(pCsr);
246 sqlite3_finalize(pCsr->pStmt); 286 sqlite3_finalize(pCsr->pStmt);
247 sqlite3_free(pCsr); 287 sqlite3_free(pCsr);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 while( iOff ){ 334 while( iOff ){
295 nUnused += get2byte(&aData[iOff+2]); 335 nUnused += get2byte(&aData[iOff+2]);
296 iOff = get2byte(&aData[iOff]); 336 iOff = get2byte(&aData[iOff]);
297 } 337 }
298 p->nUnused = nUnused; 338 p->nUnused = nUnused;
299 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); 339 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
300 szPage = sqlite3BtreeGetPageSize(pBt); 340 szPage = sqlite3BtreeGetPageSize(pBt);
301 341
302 if( p->nCell ){ 342 if( p->nCell ){
303 int i; /* Used to iterate through cells */ 343 int i; /* Used to iterate through cells */
304 int nUsable = szPage - sqlite3BtreeGetReserve(pBt); 344 int nUsable; /* Usable bytes per page */
305 345
306 p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell)); 346 sqlite3BtreeEnter(pBt);
347 nUsable = szPage - sqlite3BtreeGetReserveNoMutex(pBt);
348 sqlite3BtreeLeave(pBt);
349 p->aCell = sqlite3_malloc64((p->nCell+1) * sizeof(StatCell));
350 if( p->aCell==0 ) return SQLITE_NOMEM;
307 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell)); 351 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
308 352
309 for(i=0; i<p->nCell; i++){ 353 for(i=0; i<p->nCell; i++){
310 StatCell *pCell = &p->aCell[i]; 354 StatCell *pCell = &p->aCell[i];
311 355
312 iOff = get2byte(&aData[nHdr+i*2]); 356 iOff = get2byte(&aData[nHdr+i*2]);
313 if( !isLeaf ){ 357 if( !isLeaf ){
314 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); 358 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
315 iOff += 4; 359 iOff += 4;
316 } 360 }
(...skipping 11 matching lines...) Expand all
328 getLocalPayload(nUsable, p->flags, nPayload, &nLocal); 372 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
329 pCell->nLocal = nLocal; 373 pCell->nLocal = nLocal;
330 assert( nLocal>=0 ); 374 assert( nLocal>=0 );
331 assert( nPayload>=(u32)nLocal ); 375 assert( nPayload>=(u32)nLocal );
332 assert( nLocal<=(nUsable-35) ); 376 assert( nLocal<=(nUsable-35) );
333 if( nPayload>(u32)nLocal ){ 377 if( nPayload>(u32)nLocal ){
334 int j; 378 int j;
335 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); 379 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
336 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); 380 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
337 pCell->nOvfl = nOvfl; 381 pCell->nOvfl = nOvfl;
338 pCell->aOvfl = sqlite3_malloc(sizeof(u32)*nOvfl); 382 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl);
383 if( pCell->aOvfl==0 ) return SQLITE_NOMEM;
339 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); 384 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
340 for(j=1; j<nOvfl; j++){ 385 for(j=1; j<nOvfl; j++){
341 int rc; 386 int rc;
342 u32 iPrev = pCell->aOvfl[j-1]; 387 u32 iPrev = pCell->aOvfl[j-1];
343 DbPage *pPg = 0; 388 DbPage *pPg = 0;
344 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg); 389 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0);
345 if( rc!=SQLITE_OK ){ 390 if( rc!=SQLITE_OK ){
346 assert( pPg==0 ); 391 assert( pPg==0 );
347 return rc; 392 return rc;
348 } 393 }
349 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg)); 394 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
350 sqlite3PagerUnref(pPg); 395 sqlite3PagerUnref(pPg);
351 } 396 }
352 } 397 }
353 } 398 }
354 } 399 }
355 } 400 }
356 401
357 return SQLITE_OK; 402 return SQLITE_OK;
358 } 403 }
359 404
360 /* 405 /*
361 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on 406 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
362 ** the current value of pCsr->iPageno. 407 ** the current value of pCsr->iPageno.
363 */ 408 */
364 static void statSizeAndOffset(StatCursor *pCsr){ 409 static void statSizeAndOffset(StatCursor *pCsr){
365 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab; 410 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
366 Btree *pBt = pTab->db->aDb[0].pBt; 411 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt;
367 Pager *pPager = sqlite3BtreePager(pBt); 412 Pager *pPager = sqlite3BtreePager(pBt);
368 sqlite3_file *fd; 413 sqlite3_file *fd;
369 sqlite3_int64 x[2]; 414 sqlite3_int64 x[2];
370 415
371 /* The default page size and offset */ 416 /* The default page size and offset */
372 pCsr->szPage = sqlite3BtreeGetPageSize(pBt); 417 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
373 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1); 418 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
374 419
375 /* If connected to a ZIPVFS backend, override the page size and 420 /* If connected to a ZIPVFS backend, override the page size and
376 ** offset with actual values obtained from ZIPVFS. 421 ** offset with actual values obtained from ZIPVFS.
377 */ 422 */
378 fd = sqlite3PagerFile(pPager); 423 fd = sqlite3PagerFile(pPager);
379 x[0] = pCsr->iPageno; 424 x[0] = pCsr->iPageno;
380 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ 425 if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
381 pCsr->iOffset = x[0]; 426 pCsr->iOffset = x[0];
382 pCsr->szPage = (int)x[1]; 427 pCsr->szPage = (int)x[1];
383 } 428 }
384 } 429 }
385 430
386 /* 431 /*
387 ** Move a statvfs cursor to the next entry in the file. 432 ** Move a statvfs cursor to the next entry in the file.
388 */ 433 */
389 static int statNext(sqlite3_vtab_cursor *pCursor){ 434 static int statNext(sqlite3_vtab_cursor *pCursor){
390 int rc; 435 int rc;
391 int nPayload; 436 int nPayload;
437 char *z;
392 StatCursor *pCsr = (StatCursor *)pCursor; 438 StatCursor *pCsr = (StatCursor *)pCursor;
393 StatTable *pTab = (StatTable *)pCursor->pVtab; 439 StatTable *pTab = (StatTable *)pCursor->pVtab;
394 Btree *pBt = pTab->db->aDb[0].pBt; 440 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt;
395 Pager *pPager = sqlite3BtreePager(pBt); 441 Pager *pPager = sqlite3BtreePager(pBt);
396 442
397 sqlite3_free(pCsr->zPath); 443 sqlite3_free(pCsr->zPath);
398 pCsr->zPath = 0; 444 pCsr->zPath = 0;
399 445
400 statNextRestart: 446 statNextRestart:
401 if( pCsr->aPage[0].pPg==0 ){ 447 if( pCsr->aPage[0].pPg==0 ){
402 rc = sqlite3_step(pCsr->pStmt); 448 rc = sqlite3_step(pCsr->pStmt);
403 if( rc==SQLITE_ROW ){ 449 if( rc==SQLITE_ROW ){
404 int nPage; 450 int nPage;
405 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1); 451 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
406 sqlite3PagerPagecount(pPager, &nPage); 452 sqlite3PagerPagecount(pPager, &nPage);
407 if( nPage==0 ){ 453 if( nPage==0 ){
408 pCsr->isEof = 1; 454 pCsr->isEof = 1;
409 return sqlite3_reset(pCsr->pStmt); 455 return sqlite3_reset(pCsr->pStmt);
410 } 456 }
411 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg); 457 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0);
412 pCsr->aPage[0].iPgno = iRoot; 458 pCsr->aPage[0].iPgno = iRoot;
413 pCsr->aPage[0].iCell = 0; 459 pCsr->aPage[0].iCell = 0;
414 pCsr->aPage[0].zPath = sqlite3_mprintf("/"); 460 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/");
415 pCsr->iPage = 0; 461 pCsr->iPage = 0;
462 if( z==0 ) rc = SQLITE_NOMEM;
416 }else{ 463 }else{
417 pCsr->isEof = 1; 464 pCsr->isEof = 1;
418 return sqlite3_reset(pCsr->pStmt); 465 return sqlite3_reset(pCsr->pStmt);
419 } 466 }
420 }else{ 467 }else{
421 468
422 /* Page p itself has already been visited. */ 469 /* Page p itself has already been visited. */
423 StatPage *p = &pCsr->aPage[pCsr->iPage]; 470 StatPage *p = &pCsr->aPage[pCsr->iPage];
424 471
425 while( p->iCell<p->nCell ){ 472 while( p->iCell<p->nCell ){
426 StatCell *pCell = &p->aCell[p->iCell]; 473 StatCell *pCell = &p->aCell[p->iCell];
427 if( pCell->iOvfl<pCell->nOvfl ){ 474 if( pCell->iOvfl<pCell->nOvfl ){
428 int nUsable = sqlite3BtreeGetPageSize(pBt)-sqlite3BtreeGetReserve(pBt); 475 int nUsable;
476 sqlite3BtreeEnter(pBt);
477 nUsable = sqlite3BtreeGetPageSize(pBt) -
478 sqlite3BtreeGetReserveNoMutex(pBt);
479 sqlite3BtreeLeave(pBt);
429 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); 480 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
430 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl]; 481 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
431 pCsr->zPagetype = "overflow"; 482 pCsr->zPagetype = "overflow";
432 pCsr->nCell = 0; 483 pCsr->nCell = 0;
433 pCsr->nMxPayload = 0; 484 pCsr->nMxPayload = 0;
434 pCsr->zPath = sqlite3_mprintf( 485 pCsr->zPath = z = sqlite3_mprintf(
435 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl 486 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
436 ); 487 );
437 if( pCell->iOvfl<pCell->nOvfl-1 ){ 488 if( pCell->iOvfl<pCell->nOvfl-1 ){
438 pCsr->nUnused = 0; 489 pCsr->nUnused = 0;
439 pCsr->nPayload = nUsable - 4; 490 pCsr->nPayload = nUsable - 4;
440 }else{ 491 }else{
441 pCsr->nPayload = pCell->nLastOvfl; 492 pCsr->nPayload = pCell->nLastOvfl;
442 pCsr->nUnused = nUsable - 4 - pCsr->nPayload; 493 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
443 } 494 }
444 pCell->iOvfl++; 495 pCell->iOvfl++;
445 statSizeAndOffset(pCsr); 496 statSizeAndOffset(pCsr);
446 return SQLITE_OK; 497 return z==0 ? SQLITE_NOMEM : SQLITE_OK;
447 } 498 }
448 if( p->iRightChildPg ) break; 499 if( p->iRightChildPg ) break;
449 p->iCell++; 500 p->iCell++;
450 } 501 }
451 502
452 if( !p->iRightChildPg || p->iCell>p->nCell ){ 503 if( !p->iRightChildPg || p->iCell>p->nCell ){
453 statClearPage(p); 504 statClearPage(p);
454 if( pCsr->iPage==0 ) return statNext(pCursor); 505 if( pCsr->iPage==0 ) return statNext(pCursor);
455 pCsr->iPage--; 506 pCsr->iPage--;
456 goto statNextRestart; /* Tail recursion */ 507 goto statNextRestart; /* Tail recursion */
457 } 508 }
458 pCsr->iPage++; 509 pCsr->iPage++;
459 assert( p==&pCsr->aPage[pCsr->iPage-1] ); 510 assert( p==&pCsr->aPage[pCsr->iPage-1] );
460 511
461 if( p->iCell==p->nCell ){ 512 if( p->iCell==p->nCell ){
462 p[1].iPgno = p->iRightChildPg; 513 p[1].iPgno = p->iRightChildPg;
463 }else{ 514 }else{
464 p[1].iPgno = p->aCell[p->iCell].iChildPg; 515 p[1].iPgno = p->aCell[p->iCell].iChildPg;
465 } 516 }
466 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg); 517 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0);
467 p[1].iCell = 0; 518 p[1].iCell = 0;
468 p[1].zPath = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); 519 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
469 p->iCell++; 520 p->iCell++;
521 if( z==0 ) rc = SQLITE_NOMEM;
470 } 522 }
471 523
472 524
473 /* Populate the StatCursor fields with the values to be returned 525 /* Populate the StatCursor fields with the values to be returned
474 ** by the xColumn() and xRowid() methods. 526 ** by the xColumn() and xRowid() methods.
475 */ 527 */
476 if( rc==SQLITE_OK ){ 528 if( rc==SQLITE_OK ){
477 int i; 529 int i;
478 StatPage *p = &pCsr->aPage[pCsr->iPage]; 530 StatPage *p = &pCsr->aPage[pCsr->iPage];
479 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); 531 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
480 pCsr->iPageno = p->iPgno; 532 pCsr->iPageno = p->iPgno;
481 533
482 statDecodePage(pBt, p); 534 rc = statDecodePage(pBt, p);
483 statSizeAndOffset(pCsr); 535 if( rc==SQLITE_OK ){
536 statSizeAndOffset(pCsr);
484 537
485 switch( p->flags ){ 538 switch( p->flags ){
486 case 0x05: /* table internal */ 539 case 0x05: /* table internal */
487 case 0x02: /* index internal */ 540 case 0x02: /* index internal */
488 pCsr->zPagetype = "internal"; 541 pCsr->zPagetype = "internal";
489 break; 542 break;
490 case 0x0D: /* table leaf */ 543 case 0x0D: /* table leaf */
491 case 0x0A: /* index leaf */ 544 case 0x0A: /* index leaf */
492 pCsr->zPagetype = "leaf"; 545 pCsr->zPagetype = "leaf";
493 break; 546 break;
494 default: 547 default:
495 pCsr->zPagetype = "corrupted"; 548 pCsr->zPagetype = "corrupted";
496 break; 549 break;
550 }
551 pCsr->nCell = p->nCell;
552 pCsr->nUnused = p->nUnused;
553 pCsr->nMxPayload = p->nMxPayload;
554 pCsr->zPath = z = sqlite3_mprintf("%s", p->zPath);
555 if( z==0 ) rc = SQLITE_NOMEM;
556 nPayload = 0;
557 for(i=0; i<p->nCell; i++){
558 nPayload += p->aCell[i].nLocal;
559 }
560 pCsr->nPayload = nPayload;
497 } 561 }
498 pCsr->nCell = p->nCell;
499 pCsr->nUnused = p->nUnused;
500 pCsr->nMxPayload = p->nMxPayload;
501 pCsr->zPath = sqlite3_mprintf("%s", p->zPath);
502 nPayload = 0;
503 for(i=0; i<p->nCell; i++){
504 nPayload += p->aCell[i].nLocal;
505 }
506 pCsr->nPayload = nPayload;
507 } 562 }
508 563
509 return rc; 564 return rc;
510 } 565 }
511 566
512 static int statEof(sqlite3_vtab_cursor *pCursor){ 567 static int statEof(sqlite3_vtab_cursor *pCursor){
513 StatCursor *pCsr = (StatCursor *)pCursor; 568 StatCursor *pCsr = (StatCursor *)pCursor;
514 return pCsr->isEof; 569 return pCsr->isEof;
515 } 570 }
516 571
517 static int statFilter( 572 static int statFilter(
518 sqlite3_vtab_cursor *pCursor, 573 sqlite3_vtab_cursor *pCursor,
519 int idxNum, const char *idxStr, 574 int idxNum, const char *idxStr,
520 int argc, sqlite3_value **argv 575 int argc, sqlite3_value **argv
521 ){ 576 ){
522 StatCursor *pCsr = (StatCursor *)pCursor; 577 StatCursor *pCsr = (StatCursor *)pCursor;
578 StatTable *pTab = (StatTable*)(pCursor->pVtab);
579 char *zSql;
580 int rc = SQLITE_OK;
581 char *zMaster;
523 582
583 if( idxNum==1 ){
584 const char *zDbase = (const char*)sqlite3_value_text(argv[0]);
585 pCsr->iDb = sqlite3FindDbName(pTab->db, zDbase);
586 if( pCsr->iDb<0 ){
587 sqlite3_free(pCursor->pVtab->zErrMsg);
588 pCursor->pVtab->zErrMsg = sqlite3_mprintf("no such schema: %s", zDbase);
589 return pCursor->pVtab->zErrMsg ? SQLITE_ERROR : SQLITE_NOMEM;
590 }
591 }else{
592 pCsr->iDb = pTab->iDb;
593 }
524 statResetCsr(pCsr); 594 statResetCsr(pCsr);
525 return statNext(pCursor); 595 sqlite3_finalize(pCsr->pStmt);
596 pCsr->pStmt = 0;
597 zMaster = pCsr->iDb==1 ? "sqlite_temp_master" : "sqlite_master";
598 zSql = sqlite3_mprintf(
599 "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type"
600 " UNION ALL "
601 "SELECT name, rootpage, type"
602 " FROM \"%w\".%s WHERE rootpage!=0"
603 " ORDER BY name", pTab->db->aDb[pCsr->iDb].zName, zMaster);
604 if( zSql==0 ){
605 return SQLITE_NOMEM;
606 }else{
607 rc = sqlite3_prepare_v2(pTab->db, zSql, -1, &pCsr->pStmt, 0);
608 sqlite3_free(zSql);
609 }
610
611 if( rc==SQLITE_OK ){
612 rc = statNext(pCursor);
613 }
614 return rc;
526 } 615 }
527 616
528 static int statColumn( 617 static int statColumn(
529 sqlite3_vtab_cursor *pCursor, 618 sqlite3_vtab_cursor *pCursor,
530 sqlite3_context *ctx, 619 sqlite3_context *ctx,
531 int i 620 int i
532 ){ 621 ){
533 StatCursor *pCsr = (StatCursor *)pCursor; 622 StatCursor *pCsr = (StatCursor *)pCursor;
534 switch( i ){ 623 switch( i ){
535 case 0: /* name */ 624 case 0: /* name */
536 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_STATIC); 625 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT);
537 break; 626 break;
538 case 1: /* path */ 627 case 1: /* path */
539 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); 628 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
540 break; 629 break;
541 case 2: /* pageno */ 630 case 2: /* pageno */
542 sqlite3_result_int64(ctx, pCsr->iPageno); 631 sqlite3_result_int64(ctx, pCsr->iPageno);
543 break; 632 break;
544 case 3: /* pagetype */ 633 case 3: /* pagetype */
545 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); 634 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
546 break; 635 break;
547 case 4: /* ncell */ 636 case 4: /* ncell */
548 sqlite3_result_int(ctx, pCsr->nCell); 637 sqlite3_result_int(ctx, pCsr->nCell);
549 break; 638 break;
550 case 5: /* payload */ 639 case 5: /* payload */
551 sqlite3_result_int(ctx, pCsr->nPayload); 640 sqlite3_result_int(ctx, pCsr->nPayload);
552 break; 641 break;
553 case 6: /* unused */ 642 case 6: /* unused */
554 sqlite3_result_int(ctx, pCsr->nUnused); 643 sqlite3_result_int(ctx, pCsr->nUnused);
555 break; 644 break;
556 case 7: /* mx_payload */ 645 case 7: /* mx_payload */
557 sqlite3_result_int(ctx, pCsr->nMxPayload); 646 sqlite3_result_int(ctx, pCsr->nMxPayload);
558 break; 647 break;
559 case 8: /* pgoffset */ 648 case 8: /* pgoffset */
560 sqlite3_result_int64(ctx, pCsr->iOffset); 649 sqlite3_result_int64(ctx, pCsr->iOffset);
561 break; 650 break;
562 case 9: /* pgsize */ 651 case 9: /* pgsize */
563 sqlite3_result_int(ctx, pCsr->szPage); 652 sqlite3_result_int(ctx, pCsr->szPage);
564 break; 653 break;
654 default: { /* schema */
655 sqlite3 *db = sqlite3_context_db_handle(ctx);
656 int iDb = pCsr->iDb;
657 sqlite3_result_text(ctx, db->aDb[iDb].zName, -1, SQLITE_STATIC);
658 break;
659 }
565 } 660 }
566 return SQLITE_OK; 661 return SQLITE_OK;
567 } 662 }
568 663
569 static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ 664 static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
570 StatCursor *pCsr = (StatCursor *)pCursor; 665 StatCursor *pCsr = (StatCursor *)pCursor;
571 *pRowid = pCsr->iPageno; 666 *pRowid = pCsr->iPageno;
572 return SQLITE_OK; 667 return SQLITE_OK;
573 } 668 }
574 669
575 int sqlite3_dbstat_register(sqlite3 *db){ 670 /*
671 ** Invoke this routine to register the "dbstat" virtual table module
672 */
673 int sqlite3DbstatRegister(sqlite3 *db){
576 static sqlite3_module dbstat_module = { 674 static sqlite3_module dbstat_module = {
577 0, /* iVersion */ 675 0, /* iVersion */
578 statConnect, /* xCreate */ 676 statConnect, /* xCreate */
579 statConnect, /* xConnect */ 677 statConnect, /* xConnect */
580 statBestIndex, /* xBestIndex */ 678 statBestIndex, /* xBestIndex */
581 statDisconnect, /* xDisconnect */ 679 statDisconnect, /* xDisconnect */
582 statDisconnect, /* xDestroy */ 680 statDisconnect, /* xDestroy */
583 statOpen, /* xOpen - open a cursor */ 681 statOpen, /* xOpen - open a cursor */
584 statClose, /* xClose - close a cursor */ 682 statClose, /* xClose - close a cursor */
585 statFilter, /* xFilter - configure scan constraints */ 683 statFilter, /* xFilter - configure scan constraints */
586 statNext, /* xNext - advance a cursor */ 684 statNext, /* xNext - advance a cursor */
587 statEof, /* xEof - check for end of scan */ 685 statEof, /* xEof - check for end of scan */
588 statColumn, /* xColumn - read data */ 686 statColumn, /* xColumn - read data */
589 statRowid, /* xRowid - read data */ 687 statRowid, /* xRowid - read data */
590 0, /* xUpdate */ 688 0, /* xUpdate */
591 0, /* xBegin */ 689 0, /* xBegin */
592 0, /* xSync */ 690 0, /* xSync */
593 0, /* xCommit */ 691 0, /* xCommit */
594 0, /* xRollback */ 692 0, /* xRollback */
595 0, /* xFindMethod */ 693 0, /* xFindMethod */
596 0, /* xRename */ 694 0, /* xRename */
597 }; 695 };
598 sqlite3_create_module(db, "dbstat", &dbstat_module, 0); 696 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
599 return SQLITE_OK;
600 } 697 }
601 698 #elif defined(SQLITE_ENABLE_DBSTAT_VTAB)
602 #endif 699 int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; }
603 700 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */
604 #if defined(SQLITE_TEST) || TCLSH==2
605 #include <tcl.h>
606
607 static int test_dbstat(
608 void *clientData,
609 Tcl_Interp *interp,
610 int objc,
611 Tcl_Obj *CONST objv[]
612 ){
613 #ifdef SQLITE_OMIT_VIRTUALTABLE
614 Tcl_AppendResult(interp, "dbstat not available because of "
615 "SQLITE_OMIT_VIRTUALTABLE", (void*)0);
616 return TCL_ERROR;
617 #else
618 struct SqliteDb { sqlite3 *db; };
619 char *zDb;
620 Tcl_CmdInfo cmdInfo;
621
622 if( objc!=2 ){
623 Tcl_WrongNumArgs(interp, 1, objv, "DB");
624 return TCL_ERROR;
625 }
626
627 zDb = Tcl_GetString(objv[1]);
628 if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){
629 sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db;
630 sqlite3_dbstat_register(db);
631 }
632 return TCL_OK;
633 #endif
634 }
635
636 int SqlitetestStat_Init(Tcl_Interp *interp){
637 Tcl_CreateObjCommand(interp, "register_dbstat_vtab", test_dbstat, 0, 0);
638 return TCL_OK;
639 }
640 #endif /* if defined(SQLITE_TEST) || TCLSH==2 */
OLDNEW
« no previous file with comments | « third_party/sqlite/sqlite-src-3100200/src/date.c ('k') | third_party/sqlite/sqlite-src-3100200/src/delete.c » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698