| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 ** 2010 July 12 | |
| 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 ** | |
| 13 ** This file contains an implementation of the "dbstat" virtual table. | |
| 14 ** | |
| 15 ** The dbstat virtual table is used to extract low-level formatting | |
| 16 ** information from an SQLite database in order to implement the | |
| 17 ** "sqlite3_analyzer" utility. See the ../tool/spaceanal.tcl script | |
| 18 ** for an example implementation. | |
| 19 ** | |
| 20 ** Additional information is available on the "dbstat.html" page of the | |
| 21 ** official SQLite documentation. | |
| 22 */ | |
| 23 | |
| 24 #include "sqliteInt.h" /* Requires access to internal data structures */ | |
| 25 #if (defined(SQLITE_ENABLE_DBSTAT_VTAB) || defined(SQLITE_TEST)) \ | |
| 26 && !defined(SQLITE_OMIT_VIRTUALTABLE) | |
| 27 | |
| 28 /* | |
| 29 ** Page paths: | |
| 30 ** | |
| 31 ** The value of the 'path' column describes the path taken from the | |
| 32 ** root-node of the b-tree structure to each page. The value of the | |
| 33 ** root-node path is '/'. | |
| 34 ** | |
| 35 ** The value of the path for the left-most child page of the root of | |
| 36 ** a b-tree is '/000/'. (Btrees store content ordered from left to right | |
| 37 ** so the pages to the left have smaller keys than the pages to the right.) | |
| 38 ** The next to left-most child of the root page is | |
| 39 ** '/001', and so on, each sibling page identified by a 3-digit hex | |
| 40 ** value. The children of the 451st left-most sibling have paths such | |
| 41 ** as '/1c2/000/, '/1c2/001/' etc. | |
| 42 ** | |
| 43 ** Overflow pages are specified by appending a '+' character and a | |
| 44 ** six-digit hexadecimal value to the path to the cell they are linked | |
| 45 ** from. For example, the three overflow pages in a chain linked from | |
| 46 ** the left-most cell of the 450th child of the root page are identified | |
| 47 ** by the paths: | |
| 48 ** | |
| 49 ** '/1c2/000+000000' // First page in overflow chain | |
| 50 ** '/1c2/000+000001' // Second page in overflow chain | |
| 51 ** '/1c2/000+000002' // Third page in overflow chain | |
| 52 ** | |
| 53 ** If the paths are sorted using the BINARY collation sequence, then | |
| 54 ** the overflow pages associated with a cell will appear earlier in the | |
| 55 ** sort-order than its child page: | |
| 56 ** | |
| 57 ** '/1c2/000/' // Left-most child of 451st child of root | |
| 58 */ | |
| 59 #define VTAB_SCHEMA \ | |
| 60 "CREATE TABLE xx( " \ | |
| 61 " name STRING, /* Name of table or index */" \ | |
| 62 " path INTEGER, /* Path to page from root */" \ | |
| 63 " pageno INTEGER, /* Page number */" \ | |
| 64 " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \ | |
| 65 " ncell INTEGER, /* Cells on page (0 for overflow) */" \ | |
| 66 " payload INTEGER, /* Bytes of payload on this page */" \ | |
| 67 " unused INTEGER, /* Bytes of unused space on this page */" \ | |
| 68 " mx_payload INTEGER, /* Largest payload size of all cells */" \ | |
| 69 " pgoffset INTEGER, /* Offset of page in file */" \ | |
| 70 " pgsize INTEGER, /* Size of the page */" \ | |
| 71 " schema TEXT HIDDEN /* Database schema being analyzed */" \ | |
| 72 ");" | |
| 73 | |
| 74 | |
| 75 typedef struct StatTable StatTable; | |
| 76 typedef struct StatCursor StatCursor; | |
| 77 typedef struct StatPage StatPage; | |
| 78 typedef struct StatCell StatCell; | |
| 79 | |
| 80 struct StatCell { | |
| 81 int nLocal; /* Bytes of local payload */ | |
| 82 u32 iChildPg; /* Child node (or 0 if this is a leaf) */ | |
| 83 int nOvfl; /* Entries in aOvfl[] */ | |
| 84 u32 *aOvfl; /* Array of overflow page numbers */ | |
| 85 int nLastOvfl; /* Bytes of payload on final overflow page */ | |
| 86 int iOvfl; /* Iterates through aOvfl[] */ | |
| 87 }; | |
| 88 | |
| 89 struct StatPage { | |
| 90 u32 iPgno; | |
| 91 DbPage *pPg; | |
| 92 int iCell; | |
| 93 | |
| 94 char *zPath; /* Path to this page */ | |
| 95 | |
| 96 /* Variables populated by statDecodePage(): */ | |
| 97 u8 flags; /* Copy of flags byte */ | |
| 98 int nCell; /* Number of cells on page */ | |
| 99 int nUnused; /* Number of unused bytes on page */ | |
| 100 StatCell *aCell; /* Array of parsed cells */ | |
| 101 u32 iRightChildPg; /* Right-child page number (or 0) */ | |
| 102 int nMxPayload; /* Largest payload of any cell on this page */ | |
| 103 }; | |
| 104 | |
| 105 struct StatCursor { | |
| 106 sqlite3_vtab_cursor base; | |
| 107 sqlite3_stmt *pStmt; /* Iterates through set of root pages */ | |
| 108 int isEof; /* After pStmt has returned SQLITE_DONE */ | |
| 109 int iDb; /* Schema used for this query */ | |
| 110 | |
| 111 StatPage aPage[32]; | |
| 112 int iPage; /* Current entry in aPage[] */ | |
| 113 | |
| 114 /* Values to return. */ | |
| 115 char *zName; /* Value of 'name' column */ | |
| 116 char *zPath; /* Value of 'path' column */ | |
| 117 u32 iPageno; /* Value of 'pageno' column */ | |
| 118 char *zPagetype; /* Value of 'pagetype' column */ | |
| 119 int nCell; /* Value of 'ncell' column */ | |
| 120 int nPayload; /* Value of 'payload' column */ | |
| 121 int nUnused; /* Value of 'unused' column */ | |
| 122 int nMxPayload; /* Value of 'mx_payload' column */ | |
| 123 i64 iOffset; /* Value of 'pgOffset' column */ | |
| 124 int szPage; /* Value of 'pgSize' column */ | |
| 125 }; | |
| 126 | |
| 127 struct StatTable { | |
| 128 sqlite3_vtab base; | |
| 129 sqlite3 *db; | |
| 130 int iDb; /* Index of database to analyze */ | |
| 131 }; | |
| 132 | |
| 133 #ifndef get2byte | |
| 134 # define get2byte(x) ((x)[0]<<8 | (x)[1]) | |
| 135 #endif | |
| 136 | |
| 137 /* | |
| 138 ** Connect to or create a statvfs virtual table. | |
| 139 */ | |
| 140 static int statConnect( | |
| 141 sqlite3 *db, | |
| 142 void *pAux, | |
| 143 int argc, const char *const*argv, | |
| 144 sqlite3_vtab **ppVtab, | |
| 145 char **pzErr | |
| 146 ){ | |
| 147 StatTable *pTab = 0; | |
| 148 int rc = SQLITE_OK; | |
| 149 int iDb; | |
| 150 | |
| 151 if( argc>=4 ){ | |
| 152 iDb = sqlite3FindDbName(db, argv[3]); | |
| 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 } | |
| 165 | |
| 166 assert( rc==SQLITE_OK || pTab==0 ); | |
| 167 if( rc==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; | |
| 175 } | |
| 176 | |
| 177 /* | |
| 178 ** Disconnect from or destroy a statvfs virtual table. | |
| 179 */ | |
| 180 static int statDisconnect(sqlite3_vtab *pVtab){ | |
| 181 sqlite3_free(pVtab); | |
| 182 return SQLITE_OK; | |
| 183 } | |
| 184 | |
| 185 /* | |
| 186 ** There is no "best-index". This virtual table always does a linear | |
| 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. | |
| 191 */ | |
| 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 | |
| 213 | |
| 214 /* Records are always returned in ascending order of (name, path). | |
| 215 ** If this will satisfy the client, set the orderByConsumed flag so that | |
| 216 ** SQLite does not do an external sort. | |
| 217 */ | |
| 218 if( ( pIdxInfo->nOrderBy==1 | |
| 219 && pIdxInfo->aOrderBy[0].iColumn==0 | |
| 220 && pIdxInfo->aOrderBy[0].desc==0 | |
| 221 ) || | |
| 222 ( pIdxInfo->nOrderBy==2 | |
| 223 && pIdxInfo->aOrderBy[0].iColumn==0 | |
| 224 && pIdxInfo->aOrderBy[0].desc==0 | |
| 225 && pIdxInfo->aOrderBy[1].iColumn==1 | |
| 226 && pIdxInfo->aOrderBy[1].desc==0 | |
| 227 ) | |
| 228 ){ | |
| 229 pIdxInfo->orderByConsumed = 1; | |
| 230 } | |
| 231 | |
| 232 return SQLITE_OK; | |
| 233 } | |
| 234 | |
| 235 /* | |
| 236 ** Open a new statvfs cursor. | |
| 237 */ | |
| 238 static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ | |
| 239 StatTable *pTab = (StatTable *)pVTab; | |
| 240 StatCursor *pCsr; | |
| 241 | |
| 242 pCsr = (StatCursor *)sqlite3_malloc64(sizeof(StatCursor)); | |
| 243 if( pCsr==0 ){ | |
| 244 return SQLITE_NOMEM; | |
| 245 }else{ | |
| 246 memset(pCsr, 0, sizeof(StatCursor)); | |
| 247 pCsr->base.pVtab = pVTab; | |
| 248 pCsr->iDb = pTab->iDb; | |
| 249 } | |
| 250 | |
| 251 *ppCursor = (sqlite3_vtab_cursor *)pCsr; | |
| 252 return SQLITE_OK; | |
| 253 } | |
| 254 | |
| 255 static void statClearPage(StatPage *p){ | |
| 256 int i; | |
| 257 if( p->aCell ){ | |
| 258 for(i=0; i<p->nCell; i++){ | |
| 259 sqlite3_free(p->aCell[i].aOvfl); | |
| 260 } | |
| 261 sqlite3_free(p->aCell); | |
| 262 } | |
| 263 sqlite3PagerUnref(p->pPg); | |
| 264 sqlite3_free(p->zPath); | |
| 265 memset(p, 0, sizeof(StatPage)); | |
| 266 } | |
| 267 | |
| 268 static void statResetCsr(StatCursor *pCsr){ | |
| 269 int i; | |
| 270 sqlite3_reset(pCsr->pStmt); | |
| 271 for(i=0; i<ArraySize(pCsr->aPage); i++){ | |
| 272 statClearPage(&pCsr->aPage[i]); | |
| 273 } | |
| 274 pCsr->iPage = 0; | |
| 275 sqlite3_free(pCsr->zPath); | |
| 276 pCsr->zPath = 0; | |
| 277 pCsr->isEof = 0; | |
| 278 } | |
| 279 | |
| 280 /* | |
| 281 ** Close a statvfs cursor. | |
| 282 */ | |
| 283 static int statClose(sqlite3_vtab_cursor *pCursor){ | |
| 284 StatCursor *pCsr = (StatCursor *)pCursor; | |
| 285 statResetCsr(pCsr); | |
| 286 sqlite3_finalize(pCsr->pStmt); | |
| 287 sqlite3_free(pCsr); | |
| 288 return SQLITE_OK; | |
| 289 } | |
| 290 | |
| 291 static void getLocalPayload( | |
| 292 int nUsable, /* Usable bytes per page */ | |
| 293 u8 flags, /* Page flags */ | |
| 294 int nTotal, /* Total record (payload) size */ | |
| 295 int *pnLocal /* OUT: Bytes stored locally */ | |
| 296 ){ | |
| 297 int nLocal; | |
| 298 int nMinLocal; | |
| 299 int nMaxLocal; | |
| 300 | |
| 301 if( flags==0x0D ){ /* Table leaf node */ | |
| 302 nMinLocal = (nUsable - 12) * 32 / 255 - 23; | |
| 303 nMaxLocal = nUsable - 35; | |
| 304 }else{ /* Index interior and leaf nodes */ | |
| 305 nMinLocal = (nUsable - 12) * 32 / 255 - 23; | |
| 306 nMaxLocal = (nUsable - 12) * 64 / 255 - 23; | |
| 307 } | |
| 308 | |
| 309 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4); | |
| 310 if( nLocal>nMaxLocal ) nLocal = nMinLocal; | |
| 311 *pnLocal = nLocal; | |
| 312 } | |
| 313 | |
| 314 static int statDecodePage(Btree *pBt, StatPage *p){ | |
| 315 int nUnused; | |
| 316 int iOff; | |
| 317 int nHdr; | |
| 318 int isLeaf; | |
| 319 int szPage; | |
| 320 | |
| 321 u8 *aData = sqlite3PagerGetData(p->pPg); | |
| 322 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0]; | |
| 323 | |
| 324 p->flags = aHdr[0]; | |
| 325 p->nCell = get2byte(&aHdr[3]); | |
| 326 p->nMxPayload = 0; | |
| 327 | |
| 328 isLeaf = (p->flags==0x0A || p->flags==0x0D); | |
| 329 nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100; | |
| 330 | |
| 331 nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; | |
| 332 nUnused += (int)aHdr[7]; | |
| 333 iOff = get2byte(&aHdr[1]); | |
| 334 while( iOff ){ | |
| 335 nUnused += get2byte(&aData[iOff+2]); | |
| 336 iOff = get2byte(&aData[iOff]); | |
| 337 } | |
| 338 p->nUnused = nUnused; | |
| 339 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); | |
| 340 szPage = sqlite3BtreeGetPageSize(pBt); | |
| 341 | |
| 342 if( p->nCell ){ | |
| 343 int i; /* Used to iterate through cells */ | |
| 344 int nUsable; /* Usable bytes per page */ | |
| 345 | |
| 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; | |
| 351 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell)); | |
| 352 | |
| 353 for(i=0; i<p->nCell; i++){ | |
| 354 StatCell *pCell = &p->aCell[i]; | |
| 355 | |
| 356 iOff = get2byte(&aData[nHdr+i*2]); | |
| 357 if( !isLeaf ){ | |
| 358 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); | |
| 359 iOff += 4; | |
| 360 } | |
| 361 if( p->flags==0x05 ){ | |
| 362 /* A table interior node. nPayload==0. */ | |
| 363 }else{ | |
| 364 u32 nPayload; /* Bytes of payload total (local+overflow) */ | |
| 365 int nLocal; /* Bytes of payload stored locally */ | |
| 366 iOff += getVarint32(&aData[iOff], nPayload); | |
| 367 if( p->flags==0x0D ){ | |
| 368 u64 dummy; | |
| 369 iOff += sqlite3GetVarint(&aData[iOff], &dummy); | |
| 370 } | |
| 371 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload; | |
| 372 getLocalPayload(nUsable, p->flags, nPayload, &nLocal); | |
| 373 pCell->nLocal = nLocal; | |
| 374 assert( nLocal>=0 ); | |
| 375 assert( nPayload>=(u32)nLocal ); | |
| 376 assert( nLocal<=(nUsable-35) ); | |
| 377 if( nPayload>(u32)nLocal ){ | |
| 378 int j; | |
| 379 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); | |
| 380 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); | |
| 381 pCell->nOvfl = nOvfl; | |
| 382 pCell->aOvfl = sqlite3_malloc64(sizeof(u32)*nOvfl); | |
| 383 if( pCell->aOvfl==0 ) return SQLITE_NOMEM; | |
| 384 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); | |
| 385 for(j=1; j<nOvfl; j++){ | |
| 386 int rc; | |
| 387 u32 iPrev = pCell->aOvfl[j-1]; | |
| 388 DbPage *pPg = 0; | |
| 389 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg, 0); | |
| 390 if( rc!=SQLITE_OK ){ | |
| 391 assert( pPg==0 ); | |
| 392 return rc; | |
| 393 } | |
| 394 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg)); | |
| 395 sqlite3PagerUnref(pPg); | |
| 396 } | |
| 397 } | |
| 398 } | |
| 399 } | |
| 400 } | |
| 401 | |
| 402 return SQLITE_OK; | |
| 403 } | |
| 404 | |
| 405 /* | |
| 406 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on | |
| 407 ** the current value of pCsr->iPageno. | |
| 408 */ | |
| 409 static void statSizeAndOffset(StatCursor *pCsr){ | |
| 410 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab; | |
| 411 Btree *pBt = pTab->db->aDb[pTab->iDb].pBt; | |
| 412 Pager *pPager = sqlite3BtreePager(pBt); | |
| 413 sqlite3_file *fd; | |
| 414 sqlite3_int64 x[2]; | |
| 415 | |
| 416 /* The default page size and offset */ | |
| 417 pCsr->szPage = sqlite3BtreeGetPageSize(pBt); | |
| 418 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1); | |
| 419 | |
| 420 /* If connected to a ZIPVFS backend, override the page size and | |
| 421 ** offset with actual values obtained from ZIPVFS. | |
| 422 */ | |
| 423 fd = sqlite3PagerFile(pPager); | |
| 424 x[0] = pCsr->iPageno; | |
| 425 if( fd->pMethods!=0 && sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ | |
| 426 pCsr->iOffset = x[0]; | |
| 427 pCsr->szPage = (int)x[1]; | |
| 428 } | |
| 429 } | |
| 430 | |
| 431 /* | |
| 432 ** Move a statvfs cursor to the next entry in the file. | |
| 433 */ | |
| 434 static int statNext(sqlite3_vtab_cursor *pCursor){ | |
| 435 int rc; | |
| 436 int nPayload; | |
| 437 char *z; | |
| 438 StatCursor *pCsr = (StatCursor *)pCursor; | |
| 439 StatTable *pTab = (StatTable *)pCursor->pVtab; | |
| 440 Btree *pBt = pTab->db->aDb[pCsr->iDb].pBt; | |
| 441 Pager *pPager = sqlite3BtreePager(pBt); | |
| 442 | |
| 443 sqlite3_free(pCsr->zPath); | |
| 444 pCsr->zPath = 0; | |
| 445 | |
| 446 statNextRestart: | |
| 447 if( pCsr->aPage[0].pPg==0 ){ | |
| 448 rc = sqlite3_step(pCsr->pStmt); | |
| 449 if( rc==SQLITE_ROW ){ | |
| 450 int nPage; | |
| 451 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1); | |
| 452 sqlite3PagerPagecount(pPager, &nPage); | |
| 453 if( nPage==0 ){ | |
| 454 pCsr->isEof = 1; | |
| 455 return sqlite3_reset(pCsr->pStmt); | |
| 456 } | |
| 457 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg, 0); | |
| 458 pCsr->aPage[0].iPgno = iRoot; | |
| 459 pCsr->aPage[0].iCell = 0; | |
| 460 pCsr->aPage[0].zPath = z = sqlite3_mprintf("/"); | |
| 461 pCsr->iPage = 0; | |
| 462 if( z==0 ) rc = SQLITE_NOMEM; | |
| 463 }else{ | |
| 464 pCsr->isEof = 1; | |
| 465 return sqlite3_reset(pCsr->pStmt); | |
| 466 } | |
| 467 }else{ | |
| 468 | |
| 469 /* Page p itself has already been visited. */ | |
| 470 StatPage *p = &pCsr->aPage[pCsr->iPage]; | |
| 471 | |
| 472 while( p->iCell<p->nCell ){ | |
| 473 StatCell *pCell = &p->aCell[p->iCell]; | |
| 474 if( pCell->iOvfl<pCell->nOvfl ){ | |
| 475 int nUsable; | |
| 476 sqlite3BtreeEnter(pBt); | |
| 477 nUsable = sqlite3BtreeGetPageSize(pBt) - | |
| 478 sqlite3BtreeGetReserveNoMutex(pBt); | |
| 479 sqlite3BtreeLeave(pBt); | |
| 480 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); | |
| 481 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl]; | |
| 482 pCsr->zPagetype = "overflow"; | |
| 483 pCsr->nCell = 0; | |
| 484 pCsr->nMxPayload = 0; | |
| 485 pCsr->zPath = z = sqlite3_mprintf( | |
| 486 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl | |
| 487 ); | |
| 488 if( pCell->iOvfl<pCell->nOvfl-1 ){ | |
| 489 pCsr->nUnused = 0; | |
| 490 pCsr->nPayload = nUsable - 4; | |
| 491 }else{ | |
| 492 pCsr->nPayload = pCell->nLastOvfl; | |
| 493 pCsr->nUnused = nUsable - 4 - pCsr->nPayload; | |
| 494 } | |
| 495 pCell->iOvfl++; | |
| 496 statSizeAndOffset(pCsr); | |
| 497 return z==0 ? SQLITE_NOMEM : SQLITE_OK; | |
| 498 } | |
| 499 if( p->iRightChildPg ) break; | |
| 500 p->iCell++; | |
| 501 } | |
| 502 | |
| 503 if( !p->iRightChildPg || p->iCell>p->nCell ){ | |
| 504 statClearPage(p); | |
| 505 if( pCsr->iPage==0 ) return statNext(pCursor); | |
| 506 pCsr->iPage--; | |
| 507 goto statNextRestart; /* Tail recursion */ | |
| 508 } | |
| 509 pCsr->iPage++; | |
| 510 assert( p==&pCsr->aPage[pCsr->iPage-1] ); | |
| 511 | |
| 512 if( p->iCell==p->nCell ){ | |
| 513 p[1].iPgno = p->iRightChildPg; | |
| 514 }else{ | |
| 515 p[1].iPgno = p->aCell[p->iCell].iChildPg; | |
| 516 } | |
| 517 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg, 0); | |
| 518 p[1].iCell = 0; | |
| 519 p[1].zPath = z = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); | |
| 520 p->iCell++; | |
| 521 if( z==0 ) rc = SQLITE_NOMEM; | |
| 522 } | |
| 523 | |
| 524 | |
| 525 /* Populate the StatCursor fields with the values to be returned | |
| 526 ** by the xColumn() and xRowid() methods. | |
| 527 */ | |
| 528 if( rc==SQLITE_OK ){ | |
| 529 int i; | |
| 530 StatPage *p = &pCsr->aPage[pCsr->iPage]; | |
| 531 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); | |
| 532 pCsr->iPageno = p->iPgno; | |
| 533 | |
| 534 rc = statDecodePage(pBt, p); | |
| 535 if( rc==SQLITE_OK ){ | |
| 536 statSizeAndOffset(pCsr); | |
| 537 | |
| 538 switch( p->flags ){ | |
| 539 case 0x05: /* table internal */ | |
| 540 case 0x02: /* index internal */ | |
| 541 pCsr->zPagetype = "internal"; | |
| 542 break; | |
| 543 case 0x0D: /* table leaf */ | |
| 544 case 0x0A: /* index leaf */ | |
| 545 pCsr->zPagetype = "leaf"; | |
| 546 break; | |
| 547 default: | |
| 548 pCsr->zPagetype = "corrupted"; | |
| 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; | |
| 561 } | |
| 562 } | |
| 563 | |
| 564 return rc; | |
| 565 } | |
| 566 | |
| 567 static int statEof(sqlite3_vtab_cursor *pCursor){ | |
| 568 StatCursor *pCsr = (StatCursor *)pCursor; | |
| 569 return pCsr->isEof; | |
| 570 } | |
| 571 | |
| 572 static int statFilter( | |
| 573 sqlite3_vtab_cursor *pCursor, | |
| 574 int idxNum, const char *idxStr, | |
| 575 int argc, sqlite3_value **argv | |
| 576 ){ | |
| 577 StatCursor *pCsr = (StatCursor *)pCursor; | |
| 578 StatTable *pTab = (StatTable*)(pCursor->pVtab); | |
| 579 char *zSql; | |
| 580 int rc = SQLITE_OK; | |
| 581 char *zMaster; | |
| 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 } | |
| 594 statResetCsr(pCsr); | |
| 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; | |
| 615 } | |
| 616 | |
| 617 static int statColumn( | |
| 618 sqlite3_vtab_cursor *pCursor, | |
| 619 sqlite3_context *ctx, | |
| 620 int i | |
| 621 ){ | |
| 622 StatCursor *pCsr = (StatCursor *)pCursor; | |
| 623 switch( i ){ | |
| 624 case 0: /* name */ | |
| 625 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_TRANSIENT); | |
| 626 break; | |
| 627 case 1: /* path */ | |
| 628 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); | |
| 629 break; | |
| 630 case 2: /* pageno */ | |
| 631 sqlite3_result_int64(ctx, pCsr->iPageno); | |
| 632 break; | |
| 633 case 3: /* pagetype */ | |
| 634 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); | |
| 635 break; | |
| 636 case 4: /* ncell */ | |
| 637 sqlite3_result_int(ctx, pCsr->nCell); | |
| 638 break; | |
| 639 case 5: /* payload */ | |
| 640 sqlite3_result_int(ctx, pCsr->nPayload); | |
| 641 break; | |
| 642 case 6: /* unused */ | |
| 643 sqlite3_result_int(ctx, pCsr->nUnused); | |
| 644 break; | |
| 645 case 7: /* mx_payload */ | |
| 646 sqlite3_result_int(ctx, pCsr->nMxPayload); | |
| 647 break; | |
| 648 case 8: /* pgoffset */ | |
| 649 sqlite3_result_int64(ctx, pCsr->iOffset); | |
| 650 break; | |
| 651 case 9: /* pgsize */ | |
| 652 sqlite3_result_int(ctx, pCsr->szPage); | |
| 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 } | |
| 660 } | |
| 661 return SQLITE_OK; | |
| 662 } | |
| 663 | |
| 664 static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ | |
| 665 StatCursor *pCsr = (StatCursor *)pCursor; | |
| 666 *pRowid = pCsr->iPageno; | |
| 667 return SQLITE_OK; | |
| 668 } | |
| 669 | |
| 670 /* | |
| 671 ** Invoke this routine to register the "dbstat" virtual table module | |
| 672 */ | |
| 673 int sqlite3DbstatRegister(sqlite3 *db){ | |
| 674 static sqlite3_module dbstat_module = { | |
| 675 0, /* iVersion */ | |
| 676 statConnect, /* xCreate */ | |
| 677 statConnect, /* xConnect */ | |
| 678 statBestIndex, /* xBestIndex */ | |
| 679 statDisconnect, /* xDisconnect */ | |
| 680 statDisconnect, /* xDestroy */ | |
| 681 statOpen, /* xOpen - open a cursor */ | |
| 682 statClose, /* xClose - close a cursor */ | |
| 683 statFilter, /* xFilter - configure scan constraints */ | |
| 684 statNext, /* xNext - advance a cursor */ | |
| 685 statEof, /* xEof - check for end of scan */ | |
| 686 statColumn, /* xColumn - read data */ | |
| 687 statRowid, /* xRowid - read data */ | |
| 688 0, /* xUpdate */ | |
| 689 0, /* xBegin */ | |
| 690 0, /* xSync */ | |
| 691 0, /* xCommit */ | |
| 692 0, /* xRollback */ | |
| 693 0, /* xFindMethod */ | |
| 694 0, /* xRename */ | |
| 695 }; | |
| 696 return sqlite3_create_module(db, "dbstat", &dbstat_module, 0); | |
| 697 } | |
| 698 #elif defined(SQLITE_ENABLE_DBSTAT_VTAB) | |
| 699 int sqlite3DbstatRegister(sqlite3 *db){ return SQLITE_OK; } | |
| 700 #endif /* SQLITE_ENABLE_DBSTAT_VTAB */ | |
| OLD | NEW |