dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 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 | ****************************************************************************** |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 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. |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
dan | 0ae479d | 2011-09-21 16:43:07 +0000 | [diff] [blame] | 21 | #ifndef SQLITE_AMALGAMATION |
| 22 | # include "sqliteInt.h" |
| 23 | #endif |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 24 | |
dan | 6d3eb82 | 2010-07-12 18:12:41 +0000 | [diff] [blame] | 25 | #ifndef SQLITE_OMIT_VIRTUALTABLE |
| 26 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 27 | /* |
| 28 | ** Page paths: |
| 29 | ** |
| 30 | ** 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 path is '/'. |
| 33 | ** |
| 34 | ** The value of the path for the left-most child page of the root of |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 35 | ** a b-tree is '/000/'. (Btrees store content ordered from left to right |
| 36 | ** so the pages to the left have smaller keys than the pages to the right.) |
| 37 | ** The next to left-most child of the root page is |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 38 | ** '/001', and so on, each sibling page identified by a 3-digit hex |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 39 | ** value. The children of the 451st left-most sibling have paths such |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 40 | ** as '/1c2/000/, '/1c2/001/' etc. |
| 41 | ** |
| 42 | ** Overflow pages are specified by appending a '+' character and a |
| 43 | ** six-digit hexadecimal value to the path to the cell they are linked |
| 44 | ** from. For example, the three overflow pages in a chain linked from |
| 45 | ** the left-most cell of the 450th child of the root page are identified |
| 46 | ** by the paths: |
| 47 | ** |
| 48 | ** '/1c2/000+000000' // First page in overflow chain |
| 49 | ** '/1c2/000+000001' // Second page in overflow chain |
| 50 | ** '/1c2/000+000002' // Third page in overflow chain |
| 51 | ** |
| 52 | ** If the paths are sorted using the BINARY collation sequence, then |
| 53 | ** the overflow pages associated with a cell will appear earlier in the |
| 54 | ** sort-order than its child page: |
| 55 | ** |
drh | 3a0f13f | 2010-07-12 16:47:48 +0000 | [diff] [blame] | 56 | ** '/1c2/000/' // Left-most child of 451st child of root |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 57 | */ |
| 58 | #define VTAB_SCHEMA \ |
| 59 | "CREATE TABLE xx( " \ |
| 60 | " name STRING, /* Name of table or index */" \ |
| 61 | " path INTEGER, /* Path to page from root */" \ |
| 62 | " pageno INTEGER, /* Page number */" \ |
| 63 | " pagetype STRING, /* 'internal', 'leaf' or 'overflow' */" \ |
| 64 | " ncell INTEGER, /* Cells on page (0 for overflow) */" \ |
| 65 | " payload INTEGER, /* Bytes of payload on this page */" \ |
| 66 | " unused INTEGER, /* Bytes of unused space on this page */" \ |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 67 | " mx_payload INTEGER, /* Largest payload size of all cells */" \ |
| 68 | " pgoffset INTEGER, /* Offset of page in file */" \ |
| 69 | " pgsize INTEGER /* Size of the page */" \ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 70 | ");" |
| 71 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 72 | |
| 73 | typedef struct StatTable StatTable; |
| 74 | typedef struct StatCursor StatCursor; |
| 75 | typedef struct StatPage StatPage; |
| 76 | typedef struct StatCell StatCell; |
| 77 | |
| 78 | struct StatCell { |
| 79 | int nLocal; /* Bytes of local payload */ |
| 80 | u32 iChildPg; /* Child node (or 0 if this is a leaf) */ |
| 81 | int nOvfl; /* Entries in aOvfl[] */ |
| 82 | u32 *aOvfl; /* Array of overflow page numbers */ |
| 83 | int nLastOvfl; /* Bytes of payload on final overflow page */ |
| 84 | int iOvfl; /* Iterates through aOvfl[] */ |
| 85 | }; |
| 86 | |
| 87 | struct StatPage { |
| 88 | u32 iPgno; |
| 89 | DbPage *pPg; |
| 90 | int iCell; |
| 91 | |
| 92 | char *zPath; /* Path to this page */ |
| 93 | |
| 94 | /* Variables populated by statDecodePage(): */ |
| 95 | u8 flags; /* Copy of flags byte */ |
| 96 | int nCell; /* Number of cells on page */ |
| 97 | int nUnused; /* Number of unused bytes on page */ |
| 98 | StatCell *aCell; /* Array of parsed cells */ |
| 99 | u32 iRightChildPg; /* Right-child page number (or 0) */ |
| 100 | int nMxPayload; /* Largest payload of any cell on this page */ |
| 101 | }; |
| 102 | |
| 103 | struct StatCursor { |
| 104 | sqlite3_vtab_cursor base; |
| 105 | sqlite3_stmt *pStmt; /* Iterates through set of root pages */ |
| 106 | int isEof; /* After pStmt has returned SQLITE_DONE */ |
| 107 | |
| 108 | StatPage aPage[32]; |
| 109 | int iPage; /* Current entry in aPage[] */ |
| 110 | |
| 111 | /* Values to return. */ |
| 112 | char *zName; /* Value of 'name' column */ |
| 113 | char *zPath; /* Value of 'path' column */ |
| 114 | u32 iPageno; /* Value of 'pageno' column */ |
| 115 | char *zPagetype; /* Value of 'pagetype' column */ |
| 116 | int nCell; /* Value of 'ncell' column */ |
| 117 | int nPayload; /* Value of 'payload' column */ |
| 118 | int nUnused; /* Value of 'unused' column */ |
| 119 | int nMxPayload; /* Value of 'mx_payload' column */ |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 120 | i64 iOffset; /* Value of 'pgOffset' column */ |
| 121 | int szPage; /* Value of 'pgSize' column */ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | struct StatTable { |
| 125 | sqlite3_vtab base; |
| 126 | sqlite3 *db; |
| 127 | }; |
| 128 | |
| 129 | #ifndef get2byte |
| 130 | # define get2byte(x) ((x)[0]<<8 | (x)[1]) |
| 131 | #endif |
| 132 | |
| 133 | /* |
| 134 | ** Connect to or create a statvfs virtual table. |
| 135 | */ |
| 136 | static int statConnect( |
| 137 | sqlite3 *db, |
| 138 | void *pAux, |
| 139 | int argc, const char *const*argv, |
| 140 | sqlite3_vtab **ppVtab, |
| 141 | char **pzErr |
| 142 | ){ |
| 143 | StatTable *pTab; |
| 144 | |
| 145 | pTab = (StatTable *)sqlite3_malloc(sizeof(StatTable)); |
| 146 | memset(pTab, 0, sizeof(StatTable)); |
| 147 | pTab->db = db; |
| 148 | |
| 149 | sqlite3_declare_vtab(db, VTAB_SCHEMA); |
| 150 | *ppVtab = &pTab->base; |
| 151 | return SQLITE_OK; |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | ** Disconnect from or destroy a statvfs virtual table. |
| 156 | */ |
| 157 | static int statDisconnect(sqlite3_vtab *pVtab){ |
| 158 | sqlite3_free(pVtab); |
| 159 | return SQLITE_OK; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | ** There is no "best-index". This virtual table always does a linear |
| 164 | ** scan of the binary VFS log file. |
| 165 | */ |
| 166 | static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){ |
| 167 | |
| 168 | /* Records are always returned in ascending order of (name, path). |
| 169 | ** If this will satisfy the client, set the orderByConsumed flag so that |
| 170 | ** SQLite does not do an external sort. |
| 171 | */ |
| 172 | if( ( pIdxInfo->nOrderBy==1 |
| 173 | && pIdxInfo->aOrderBy[0].iColumn==0 |
| 174 | && pIdxInfo->aOrderBy[0].desc==0 |
| 175 | ) || |
| 176 | ( pIdxInfo->nOrderBy==2 |
| 177 | && pIdxInfo->aOrderBy[0].iColumn==0 |
| 178 | && pIdxInfo->aOrderBy[0].desc==0 |
| 179 | && pIdxInfo->aOrderBy[1].iColumn==1 |
| 180 | && pIdxInfo->aOrderBy[1].desc==0 |
| 181 | ) |
| 182 | ){ |
| 183 | pIdxInfo->orderByConsumed = 1; |
| 184 | } |
| 185 | |
| 186 | pIdxInfo->estimatedCost = 10.0; |
| 187 | return SQLITE_OK; |
| 188 | } |
| 189 | |
| 190 | /* |
| 191 | ** Open a new statvfs cursor. |
| 192 | */ |
| 193 | static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){ |
| 194 | StatTable *pTab = (StatTable *)pVTab; |
| 195 | StatCursor *pCsr; |
| 196 | int rc; |
| 197 | |
| 198 | pCsr = (StatCursor *)sqlite3_malloc(sizeof(StatCursor)); |
| 199 | memset(pCsr, 0, sizeof(StatCursor)); |
| 200 | pCsr->base.pVtab = pVTab; |
| 201 | |
| 202 | rc = sqlite3_prepare_v2(pTab->db, |
| 203 | "SELECT 'sqlite_master' AS name, 1 AS rootpage, 'table' AS type" |
| 204 | " UNION ALL " |
| 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 | } |
| 213 | |
| 214 | *ppCursor = (sqlite3_vtab_cursor *)pCsr; |
| 215 | return SQLITE_OK; |
| 216 | } |
| 217 | |
| 218 | static void statClearPage(StatPage *p){ |
| 219 | int i; |
| 220 | for(i=0; i<p->nCell; i++){ |
| 221 | sqlite3_free(p->aCell[i].aOvfl); |
| 222 | } |
| 223 | sqlite3PagerUnref(p->pPg); |
| 224 | sqlite3_free(p->aCell); |
| 225 | sqlite3_free(p->zPath); |
| 226 | memset(p, 0, sizeof(StatPage)); |
| 227 | } |
| 228 | |
| 229 | static void statResetCsr(StatCursor *pCsr){ |
| 230 | int i; |
| 231 | sqlite3_reset(pCsr->pStmt); |
| 232 | for(i=0; i<ArraySize(pCsr->aPage); i++){ |
| 233 | statClearPage(&pCsr->aPage[i]); |
| 234 | } |
| 235 | pCsr->iPage = 0; |
| 236 | sqlite3_free(pCsr->zPath); |
| 237 | pCsr->zPath = 0; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | ** Close a statvfs cursor. |
| 242 | */ |
| 243 | static int statClose(sqlite3_vtab_cursor *pCursor){ |
| 244 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 245 | statResetCsr(pCsr); |
| 246 | sqlite3_finalize(pCsr->pStmt); |
| 247 | sqlite3_free(pCsr); |
| 248 | return SQLITE_OK; |
| 249 | } |
| 250 | |
| 251 | static void getLocalPayload( |
| 252 | int nUsable, /* Usable bytes per page */ |
| 253 | u8 flags, /* Page flags */ |
| 254 | int nTotal, /* Total record (payload) size */ |
| 255 | int *pnLocal /* OUT: Bytes stored locally */ |
| 256 | ){ |
| 257 | int nLocal; |
| 258 | int nMinLocal; |
| 259 | int nMaxLocal; |
| 260 | |
| 261 | if( flags==0x0D ){ /* Table leaf node */ |
| 262 | nMinLocal = (nUsable - 12) * 32 / 255 - 23; |
| 263 | nMaxLocal = nUsable - 35; |
| 264 | }else{ /* Index interior and leaf nodes */ |
| 265 | nMinLocal = (nUsable - 12) * 32 / 255 - 23; |
| 266 | nMaxLocal = (nUsable - 12) * 64 / 255 - 23; |
| 267 | } |
| 268 | |
| 269 | nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4); |
| 270 | if( nLocal>nMaxLocal ) nLocal = nMinLocal; |
| 271 | *pnLocal = nLocal; |
| 272 | } |
| 273 | |
| 274 | static int statDecodePage(Btree *pBt, StatPage *p){ |
| 275 | int nUnused; |
| 276 | int iOff; |
| 277 | int nHdr; |
| 278 | int isLeaf; |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 279 | int szPage; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 280 | |
| 281 | u8 *aData = sqlite3PagerGetData(p->pPg); |
| 282 | u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0]; |
| 283 | |
| 284 | p->flags = aHdr[0]; |
| 285 | p->nCell = get2byte(&aHdr[3]); |
| 286 | p->nMxPayload = 0; |
| 287 | |
| 288 | isLeaf = (p->flags==0x0A || p->flags==0x0D); |
| 289 | nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100; |
| 290 | |
| 291 | nUnused = get2byte(&aHdr[5]) - nHdr - 2*p->nCell; |
| 292 | nUnused += (int)aHdr[7]; |
| 293 | iOff = get2byte(&aHdr[1]); |
| 294 | while( iOff ){ |
| 295 | nUnused += get2byte(&aData[iOff+2]); |
| 296 | iOff = get2byte(&aData[iOff]); |
| 297 | } |
| 298 | p->nUnused = nUnused; |
| 299 | p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]); |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 300 | szPage = sqlite3BtreeGetPageSize(pBt); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 301 | |
| 302 | if( p->nCell ){ |
| 303 | int i; /* Used to iterate through cells */ |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 304 | int nUsable = szPage - sqlite3BtreeGetReserve(pBt); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 305 | |
| 306 | p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell)); |
| 307 | memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell)); |
| 308 | |
| 309 | for(i=0; i<p->nCell; i++){ |
| 310 | StatCell *pCell = &p->aCell[i]; |
| 311 | |
| 312 | iOff = get2byte(&aData[nHdr+i*2]); |
| 313 | if( !isLeaf ){ |
| 314 | pCell->iChildPg = sqlite3Get4byte(&aData[iOff]); |
| 315 | iOff += 4; |
| 316 | } |
| 317 | if( p->flags==0x05 ){ |
| 318 | /* A table interior node. nPayload==0. */ |
| 319 | }else{ |
| 320 | u32 nPayload; /* Bytes of payload total (local+overflow) */ |
| 321 | int nLocal; /* Bytes of payload stored locally */ |
| 322 | iOff += getVarint32(&aData[iOff], nPayload); |
| 323 | if( p->flags==0x0D ){ |
| 324 | u64 dummy; |
| 325 | iOff += sqlite3GetVarint(&aData[iOff], &dummy); |
| 326 | } |
| 327 | if( nPayload>p->nMxPayload ) p->nMxPayload = nPayload; |
| 328 | getLocalPayload(nUsable, p->flags, nPayload, &nLocal); |
| 329 | pCell->nLocal = nLocal; |
| 330 | assert( nPayload>=nLocal ); |
| 331 | assert( nLocal<=(nUsable-35) ); |
| 332 | if( nPayload>nLocal ){ |
| 333 | int j; |
| 334 | int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4); |
| 335 | pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4); |
| 336 | pCell->nOvfl = nOvfl; |
| 337 | pCell->aOvfl = sqlite3_malloc(sizeof(u32)*nOvfl); |
| 338 | pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]); |
| 339 | for(j=1; j<nOvfl; j++){ |
| 340 | int rc; |
| 341 | u32 iPrev = pCell->aOvfl[j-1]; |
| 342 | DbPage *pPg = 0; |
| 343 | rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg); |
| 344 | if( rc!=SQLITE_OK ){ |
| 345 | assert( pPg==0 ); |
| 346 | return rc; |
| 347 | } |
| 348 | pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg)); |
| 349 | sqlite3PagerUnref(pPg); |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | return SQLITE_OK; |
| 357 | } |
| 358 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 359 | /* |
| 360 | ** Move a statvfs cursor to the next entry in the file. |
| 361 | */ |
| 362 | static int statNext(sqlite3_vtab_cursor *pCursor){ |
| 363 | int rc; |
| 364 | int nPayload; |
| 365 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 366 | StatTable *pTab = (StatTable *)pCursor->pVtab; |
| 367 | Btree *pBt = pTab->db->aDb[0].pBt; |
| 368 | Pager *pPager = sqlite3BtreePager(pBt); |
| 369 | |
| 370 | sqlite3_free(pCsr->zPath); |
| 371 | pCsr->zPath = 0; |
| 372 | |
| 373 | if( pCsr->aPage[0].pPg==0 ){ |
| 374 | rc = sqlite3_step(pCsr->pStmt); |
| 375 | if( rc==SQLITE_ROW ){ |
dan | 763afe6 | 2010-08-03 06:42:39 +0000 | [diff] [blame] | 376 | int nPage; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 377 | u32 iRoot = sqlite3_column_int64(pCsr->pStmt, 1); |
dan | 763afe6 | 2010-08-03 06:42:39 +0000 | [diff] [blame] | 378 | sqlite3PagerPagecount(pPager, &nPage); |
| 379 | if( nPage==0 ){ |
| 380 | pCsr->isEof = 1; |
| 381 | return sqlite3_reset(pCsr->pStmt); |
| 382 | } |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 383 | rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg); |
| 384 | pCsr->aPage[0].iPgno = iRoot; |
| 385 | pCsr->aPage[0].iCell = 0; |
| 386 | pCsr->aPage[0].zPath = sqlite3_mprintf("/"); |
| 387 | pCsr->iPage = 0; |
| 388 | }else{ |
| 389 | pCsr->isEof = 1; |
| 390 | return sqlite3_reset(pCsr->pStmt); |
| 391 | } |
| 392 | }else{ |
| 393 | |
| 394 | /* Page p itself has already been visited. */ |
| 395 | StatPage *p = &pCsr->aPage[pCsr->iPage]; |
| 396 | |
| 397 | while( p->iCell<p->nCell ){ |
| 398 | StatCell *pCell = &p->aCell[p->iCell]; |
| 399 | if( pCell->iOvfl<pCell->nOvfl ){ |
| 400 | int nUsable = sqlite3BtreeGetPageSize(pBt)-sqlite3BtreeGetReserve(pBt); |
| 401 | pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); |
| 402 | pCsr->iPageno = pCell->aOvfl[pCell->iOvfl]; |
| 403 | pCsr->zPagetype = "overflow"; |
| 404 | pCsr->nCell = 0; |
| 405 | pCsr->nMxPayload = 0; |
| 406 | pCsr->zPath = sqlite3_mprintf( |
| 407 | "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl |
| 408 | ); |
| 409 | if( pCell->iOvfl<pCell->nOvfl-1 ){ |
| 410 | pCsr->nUnused = 0; |
| 411 | pCsr->nPayload = nUsable - 4; |
| 412 | }else{ |
| 413 | pCsr->nPayload = pCell->nLastOvfl; |
| 414 | pCsr->nUnused = nUsable - 4 - pCsr->nPayload; |
| 415 | } |
| 416 | pCell->iOvfl++; |
| 417 | return SQLITE_OK; |
| 418 | } |
| 419 | if( p->iRightChildPg ) break; |
| 420 | p->iCell++; |
| 421 | } |
| 422 | |
| 423 | while( !p->iRightChildPg || p->iCell>p->nCell ){ |
| 424 | statClearPage(p); |
| 425 | if( pCsr->iPage==0 ) return statNext(pCursor); |
| 426 | pCsr->iPage--; |
| 427 | p = &pCsr->aPage[pCsr->iPage]; |
| 428 | } |
| 429 | pCsr->iPage++; |
| 430 | assert( p==&pCsr->aPage[pCsr->iPage-1] ); |
| 431 | |
| 432 | if( p->iCell==p->nCell ){ |
| 433 | p[1].iPgno = p->iRightChildPg; |
| 434 | }else{ |
| 435 | p[1].iPgno = p->aCell[p->iCell].iChildPg; |
| 436 | } |
| 437 | rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg); |
| 438 | p[1].iCell = 0; |
| 439 | p[1].zPath = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell); |
| 440 | p->iCell++; |
| 441 | } |
| 442 | |
| 443 | |
| 444 | /* Populate the StatCursor fields with the values to be returned |
| 445 | ** by the xColumn() and xRowid() methods. |
| 446 | */ |
| 447 | if( rc==SQLITE_OK ){ |
| 448 | int i; |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 449 | sqlite3_file *fd; |
| 450 | sqlite3_int64 x[2]; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 451 | StatPage *p = &pCsr->aPage[pCsr->iPage]; |
| 452 | pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0); |
| 453 | pCsr->iPageno = p->iPgno; |
| 454 | |
| 455 | statDecodePage(pBt, p); |
| 456 | |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 457 | /* The default page size and offset */ |
| 458 | pCsr->szPage = sqlite3BtreeGetPageSize(pBt); |
| 459 | pCsr->iOffset = pCsr->szPage * (p->iPgno - 1); |
| 460 | |
| 461 | /* If connected to a ZIPVFS backend, override the page size and |
| 462 | ** offset with actual values obtained from ZIPVFS. |
| 463 | */ |
| 464 | fd = sqlite3PagerFile(pPager); |
| 465 | x[0] = p->iPgno; |
| 466 | if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){ |
| 467 | pCsr->iOffset = x[0]; |
| 468 | pCsr->szPage = x[1]; |
| 469 | } |
| 470 | |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 471 | switch( p->flags ){ |
| 472 | case 0x05: /* table internal */ |
| 473 | case 0x02: /* index internal */ |
| 474 | pCsr->zPagetype = "internal"; |
| 475 | break; |
| 476 | case 0x0D: /* table leaf */ |
| 477 | case 0x0A: /* index leaf */ |
| 478 | pCsr->zPagetype = "leaf"; |
| 479 | break; |
| 480 | default: |
| 481 | pCsr->zPagetype = "corrupted"; |
| 482 | break; |
| 483 | } |
| 484 | pCsr->nCell = p->nCell; |
| 485 | pCsr->nUnused = p->nUnused; |
| 486 | pCsr->nMxPayload = p->nMxPayload; |
| 487 | pCsr->zPath = sqlite3_mprintf("%s", p->zPath); |
| 488 | nPayload = 0; |
| 489 | for(i=0; i<p->nCell; i++){ |
| 490 | nPayload += p->aCell[i].nLocal; |
| 491 | } |
| 492 | pCsr->nPayload = nPayload; |
| 493 | } |
| 494 | |
| 495 | return rc; |
| 496 | } |
| 497 | |
| 498 | static int statEof(sqlite3_vtab_cursor *pCursor){ |
| 499 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 500 | return pCsr->isEof; |
| 501 | } |
| 502 | |
| 503 | static int statFilter( |
| 504 | sqlite3_vtab_cursor *pCursor, |
| 505 | int idxNum, const char *idxStr, |
| 506 | int argc, sqlite3_value **argv |
| 507 | ){ |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 508 | StatCursor *pCsr = (StatCursor *)pCursor; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 509 | |
drh | e5918c6 | 2010-08-14 12:42:45 +0000 | [diff] [blame] | 510 | statResetCsr(pCsr); |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 511 | return statNext(pCursor); |
| 512 | } |
| 513 | |
| 514 | static int statColumn( |
| 515 | sqlite3_vtab_cursor *pCursor, |
| 516 | sqlite3_context *ctx, |
| 517 | int i |
| 518 | ){ |
| 519 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 520 | switch( i ){ |
| 521 | case 0: /* name */ |
| 522 | sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_STATIC); |
| 523 | break; |
| 524 | case 1: /* path */ |
| 525 | sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT); |
| 526 | break; |
| 527 | case 2: /* pageno */ |
| 528 | sqlite3_result_int64(ctx, pCsr->iPageno); |
| 529 | break; |
| 530 | case 3: /* pagetype */ |
| 531 | sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC); |
| 532 | break; |
| 533 | case 4: /* ncell */ |
| 534 | sqlite3_result_int(ctx, pCsr->nCell); |
| 535 | break; |
| 536 | case 5: /* payload */ |
| 537 | sqlite3_result_int(ctx, pCsr->nPayload); |
| 538 | break; |
| 539 | case 6: /* unused */ |
| 540 | sqlite3_result_int(ctx, pCsr->nUnused); |
| 541 | break; |
| 542 | case 7: /* mx_payload */ |
| 543 | sqlite3_result_int(ctx, pCsr->nMxPayload); |
| 544 | break; |
drh | 4c9f129 | 2011-09-28 00:50:14 +0000 | [diff] [blame^] | 545 | case 8: /* pgoffset */ |
| 546 | sqlite3_result_int64(ctx, pCsr->iOffset); |
| 547 | break; |
| 548 | case 9: /* pgsize */ |
| 549 | sqlite3_result_int(ctx, pCsr->szPage); |
| 550 | break; |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 551 | } |
| 552 | return SQLITE_OK; |
| 553 | } |
| 554 | |
| 555 | static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){ |
| 556 | StatCursor *pCsr = (StatCursor *)pCursor; |
| 557 | *pRowid = pCsr->iPageno; |
| 558 | return SQLITE_OK; |
| 559 | } |
| 560 | |
| 561 | int sqlite3_dbstat_register(sqlite3 *db){ |
| 562 | static sqlite3_module dbstat_module = { |
| 563 | 0, /* iVersion */ |
| 564 | statConnect, /* xCreate */ |
| 565 | statConnect, /* xConnect */ |
| 566 | statBestIndex, /* xBestIndex */ |
| 567 | statDisconnect, /* xDisconnect */ |
| 568 | statDisconnect, /* xDestroy */ |
| 569 | statOpen, /* xOpen - open a cursor */ |
| 570 | statClose, /* xClose - close a cursor */ |
| 571 | statFilter, /* xFilter - configure scan constraints */ |
| 572 | statNext, /* xNext - advance a cursor */ |
| 573 | statEof, /* xEof - check for end of scan */ |
| 574 | statColumn, /* xColumn - read data */ |
| 575 | statRowid, /* xRowid - read data */ |
| 576 | 0, /* xUpdate */ |
| 577 | 0, /* xBegin */ |
| 578 | 0, /* xSync */ |
| 579 | 0, /* xCommit */ |
| 580 | 0, /* xRollback */ |
| 581 | 0, /* xFindMethod */ |
| 582 | 0, /* xRename */ |
| 583 | }; |
| 584 | sqlite3_create_module(db, "dbstat", &dbstat_module, 0); |
| 585 | return SQLITE_OK; |
| 586 | } |
| 587 | |
dan | 6d3eb82 | 2010-07-12 18:12:41 +0000 | [diff] [blame] | 588 | #endif |
| 589 | |
dan | 0ae479d | 2011-09-21 16:43:07 +0000 | [diff] [blame] | 590 | #if defined(SQLITE_TEST) || TCLSH==2 |
dan | 599e9d2 | 2010-07-12 08:39:37 +0000 | [diff] [blame] | 591 | #include <tcl.h> |
| 592 | |
| 593 | static int test_dbstat( |
| 594 | void *clientData, |
| 595 | Tcl_Interp *interp, |
| 596 | int objc, |
| 597 | Tcl_Obj *CONST objv[] |
| 598 | ){ |
| 599 | #ifdef SQLITE_OMIT_VIRTUALTABLE |
| 600 | Tcl_AppendResult(interp, "dbstat not available because of " |
| 601 | "SQLITE_OMIT_VIRTUALTABLE", (void*)0); |
| 602 | return TCL_ERROR; |
| 603 | #else |
| 604 | struct SqliteDb { sqlite3 *db; }; |
| 605 | char *zDb; |
| 606 | Tcl_CmdInfo cmdInfo; |
| 607 | |
| 608 | if( objc!=2 ){ |
| 609 | Tcl_WrongNumArgs(interp, 1, objv, "DB"); |
| 610 | return TCL_ERROR; |
| 611 | } |
| 612 | |
| 613 | zDb = Tcl_GetString(objv[1]); |
| 614 | if( Tcl_GetCommandInfo(interp, zDb, &cmdInfo) ){ |
| 615 | sqlite3* db = ((struct SqliteDb*)cmdInfo.objClientData)->db; |
| 616 | sqlite3_dbstat_register(db); |
| 617 | } |
| 618 | return TCL_OK; |
| 619 | #endif |
| 620 | } |
| 621 | |
| 622 | int SqlitetestStat_Init(Tcl_Interp *interp){ |
| 623 | Tcl_CreateObjCommand(interp, "register_dbstat_vtab", test_dbstat, 0, 0); |
| 624 | return TCL_OK; |
| 625 | } |
dan | 0ae479d | 2011-09-21 16:43:07 +0000 | [diff] [blame] | 626 | #endif /* if defined(SQLITE_TEST) || TCLSH==2 */ |