Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / sqlite / sqlite-src-3080704 / src / test_stat.c
blob615df3d80fe155847516cf00b5a5c0f0e129ea25
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.
11 ******************************************************************************
13 ** This file contains an implementation of the "dbstat" virtual table.
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.
21 #ifndef SQLITE_AMALGAMATION
22 # include "sqliteInt.h"
23 #endif
25 #ifndef SQLITE_OMIT_VIRTUALTABLE
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 '/'.
34 ** 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 ** 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
38 ** '/001', and so on, each sibling page identified by a 3-digit hex
39 ** value. The children of the 451st left-most sibling have paths such
40 ** as '/1c2/000/, '/1c2/001/' etc.
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:
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
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:
56 ** '/1c2/000/' // Left-most child of 451st child of root
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 */" \
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 */" \
70 ");"
73 typedef struct StatTable StatTable;
74 typedef struct StatCursor StatCursor;
75 typedef struct StatPage StatPage;
76 typedef struct StatCell StatCell;
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[] */
87 struct StatPage {
88 u32 iPgno;
89 DbPage *pPg;
90 int iCell;
92 char *zPath; /* Path to this page */
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 */
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 */
108 StatPage aPage[32];
109 int iPage; /* Current entry in aPage[] */
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 */
120 i64 iOffset; /* Value of 'pgOffset' column */
121 int szPage; /* Value of 'pgSize' column */
124 struct StatTable {
125 sqlite3_vtab base;
126 sqlite3 *db;
129 #ifndef get2byte
130 # define get2byte(x) ((x)[0]<<8 | (x)[1])
131 #endif
134 ** Connect to or create a statvfs virtual table.
136 static int statConnect(
137 sqlite3 *db,
138 void *pAux,
139 int argc, const char *const*argv,
140 sqlite3_vtab **ppVtab,
141 char **pzErr
143 StatTable *pTab;
145 pTab = (StatTable *)sqlite3_malloc(sizeof(StatTable));
146 memset(pTab, 0, sizeof(StatTable));
147 pTab->db = db;
149 sqlite3_declare_vtab(db, VTAB_SCHEMA);
150 *ppVtab = &pTab->base;
151 return SQLITE_OK;
155 ** Disconnect from or destroy a statvfs virtual table.
157 static int statDisconnect(sqlite3_vtab *pVtab){
158 sqlite3_free(pVtab);
159 return SQLITE_OK;
163 ** There is no "best-index". This virtual table always does a linear
164 ** scan of the binary VFS log file.
166 static int statBestIndex(sqlite3_vtab *tab, sqlite3_index_info *pIdxInfo){
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.
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
183 pIdxInfo->orderByConsumed = 1;
186 pIdxInfo->estimatedCost = 10.0;
187 return SQLITE_OK;
191 ** Open a new statvfs cursor.
193 static int statOpen(sqlite3_vtab *pVTab, sqlite3_vtab_cursor **ppCursor){
194 StatTable *pTab = (StatTable *)pVTab;
195 StatCursor *pCsr;
196 int rc;
198 pCsr = (StatCursor *)sqlite3_malloc(sizeof(StatCursor));
199 memset(pCsr, 0, sizeof(StatCursor));
200 pCsr->base.pVtab = pVTab;
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
209 if( rc!=SQLITE_OK ){
210 sqlite3_free(pCsr);
211 return rc;
214 *ppCursor = (sqlite3_vtab_cursor *)pCsr;
215 return SQLITE_OK;
218 static void statClearPage(StatPage *p){
219 int i;
220 for(i=0; i<p->nCell; i++){
221 sqlite3_free(p->aCell[i].aOvfl);
223 sqlite3PagerUnref(p->pPg);
224 sqlite3_free(p->aCell);
225 sqlite3_free(p->zPath);
226 memset(p, 0, sizeof(StatPage));
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]);
235 pCsr->iPage = 0;
236 sqlite3_free(pCsr->zPath);
237 pCsr->zPath = 0;
241 ** Close a statvfs cursor.
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;
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 */
257 int nLocal;
258 int nMinLocal;
259 int nMaxLocal;
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;
269 nLocal = nMinLocal + (nTotal - nMinLocal) % (nUsable - 4);
270 if( nLocal>nMaxLocal ) nLocal = nMinLocal;
271 *pnLocal = nLocal;
274 static int statDecodePage(Btree *pBt, StatPage *p){
275 int nUnused;
276 int iOff;
277 int nHdr;
278 int isLeaf;
279 int szPage;
281 u8 *aData = sqlite3PagerGetData(p->pPg);
282 u8 *aHdr = &aData[p->iPgno==1 ? 100 : 0];
284 p->flags = aHdr[0];
285 p->nCell = get2byte(&aHdr[3]);
286 p->nMxPayload = 0;
288 isLeaf = (p->flags==0x0A || p->flags==0x0D);
289 nHdr = 12 - isLeaf*4 + (p->iPgno==1)*100;
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]);
298 p->nUnused = nUnused;
299 p->iRightChildPg = isLeaf ? 0 : sqlite3Get4byte(&aHdr[8]);
300 szPage = sqlite3BtreeGetPageSize(pBt);
302 if( p->nCell ){
303 int i; /* Used to iterate through cells */
304 int nUsable = szPage - sqlite3BtreeGetReserve(pBt);
306 p->aCell = sqlite3_malloc((p->nCell+1) * sizeof(StatCell));
307 memset(p->aCell, 0, (p->nCell+1) * sizeof(StatCell));
309 for(i=0; i<p->nCell; i++){
310 StatCell *pCell = &p->aCell[i];
312 iOff = get2byte(&aData[nHdr+i*2]);
313 if( !isLeaf ){
314 pCell->iChildPg = sqlite3Get4byte(&aData[iOff]);
315 iOff += 4;
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);
327 if( nPayload>(u32)p->nMxPayload ) p->nMxPayload = nPayload;
328 getLocalPayload(nUsable, p->flags, nPayload, &nLocal);
329 pCell->nLocal = nLocal;
330 assert( nLocal>=0 );
331 assert( nPayload>=(u32)nLocal );
332 assert( nLocal<=(nUsable-35) );
333 if( nPayload>(u32)nLocal ){
334 int j;
335 int nOvfl = ((nPayload - nLocal) + nUsable-4 - 1) / (nUsable - 4);
336 pCell->nLastOvfl = (nPayload-nLocal) - (nOvfl-1) * (nUsable-4);
337 pCell->nOvfl = nOvfl;
338 pCell->aOvfl = sqlite3_malloc(sizeof(u32)*nOvfl);
339 pCell->aOvfl[0] = sqlite3Get4byte(&aData[iOff+nLocal]);
340 for(j=1; j<nOvfl; j++){
341 int rc;
342 u32 iPrev = pCell->aOvfl[j-1];
343 DbPage *pPg = 0;
344 rc = sqlite3PagerGet(sqlite3BtreePager(pBt), iPrev, &pPg);
345 if( rc!=SQLITE_OK ){
346 assert( pPg==0 );
347 return rc;
349 pCell->aOvfl[j] = sqlite3Get4byte(sqlite3PagerGetData(pPg));
350 sqlite3PagerUnref(pPg);
357 return SQLITE_OK;
361 ** Populate the pCsr->iOffset and pCsr->szPage member variables. Based on
362 ** the current value of pCsr->iPageno.
364 static void statSizeAndOffset(StatCursor *pCsr){
365 StatTable *pTab = (StatTable *)((sqlite3_vtab_cursor *)pCsr)->pVtab;
366 Btree *pBt = pTab->db->aDb[0].pBt;
367 Pager *pPager = sqlite3BtreePager(pBt);
368 sqlite3_file *fd;
369 sqlite3_int64 x[2];
371 /* The default page size and offset */
372 pCsr->szPage = sqlite3BtreeGetPageSize(pBt);
373 pCsr->iOffset = (i64)pCsr->szPage * (pCsr->iPageno - 1);
375 /* If connected to a ZIPVFS backend, override the page size and
376 ** offset with actual values obtained from ZIPVFS.
378 fd = sqlite3PagerFile(pPager);
379 x[0] = pCsr->iPageno;
380 if( sqlite3OsFileControl(fd, 230440, &x)==SQLITE_OK ){
381 pCsr->iOffset = x[0];
382 pCsr->szPage = (int)x[1];
387 ** Move a statvfs cursor to the next entry in the file.
389 static int statNext(sqlite3_vtab_cursor *pCursor){
390 int rc;
391 int nPayload;
392 StatCursor *pCsr = (StatCursor *)pCursor;
393 StatTable *pTab = (StatTable *)pCursor->pVtab;
394 Btree *pBt = pTab->db->aDb[0].pBt;
395 Pager *pPager = sqlite3BtreePager(pBt);
397 sqlite3_free(pCsr->zPath);
398 pCsr->zPath = 0;
400 statNextRestart:
401 if( pCsr->aPage[0].pPg==0 ){
402 rc = sqlite3_step(pCsr->pStmt);
403 if( rc==SQLITE_ROW ){
404 int nPage;
405 u32 iRoot = (u32)sqlite3_column_int64(pCsr->pStmt, 1);
406 sqlite3PagerPagecount(pPager, &nPage);
407 if( nPage==0 ){
408 pCsr->isEof = 1;
409 return sqlite3_reset(pCsr->pStmt);
411 rc = sqlite3PagerGet(pPager, iRoot, &pCsr->aPage[0].pPg);
412 pCsr->aPage[0].iPgno = iRoot;
413 pCsr->aPage[0].iCell = 0;
414 pCsr->aPage[0].zPath = sqlite3_mprintf("/");
415 pCsr->iPage = 0;
416 }else{
417 pCsr->isEof = 1;
418 return sqlite3_reset(pCsr->pStmt);
420 }else{
422 /* Page p itself has already been visited. */
423 StatPage *p = &pCsr->aPage[pCsr->iPage];
425 while( p->iCell<p->nCell ){
426 StatCell *pCell = &p->aCell[p->iCell];
427 if( pCell->iOvfl<pCell->nOvfl ){
428 int nUsable = sqlite3BtreeGetPageSize(pBt)-sqlite3BtreeGetReserve(pBt);
429 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
430 pCsr->iPageno = pCell->aOvfl[pCell->iOvfl];
431 pCsr->zPagetype = "overflow";
432 pCsr->nCell = 0;
433 pCsr->nMxPayload = 0;
434 pCsr->zPath = sqlite3_mprintf(
435 "%s%.3x+%.6x", p->zPath, p->iCell, pCell->iOvfl
437 if( pCell->iOvfl<pCell->nOvfl-1 ){
438 pCsr->nUnused = 0;
439 pCsr->nPayload = nUsable - 4;
440 }else{
441 pCsr->nPayload = pCell->nLastOvfl;
442 pCsr->nUnused = nUsable - 4 - pCsr->nPayload;
444 pCell->iOvfl++;
445 statSizeAndOffset(pCsr);
446 return SQLITE_OK;
448 if( p->iRightChildPg ) break;
449 p->iCell++;
452 if( !p->iRightChildPg || p->iCell>p->nCell ){
453 statClearPage(p);
454 if( pCsr->iPage==0 ) return statNext(pCursor);
455 pCsr->iPage--;
456 goto statNextRestart; /* Tail recursion */
458 pCsr->iPage++;
459 assert( p==&pCsr->aPage[pCsr->iPage-1] );
461 if( p->iCell==p->nCell ){
462 p[1].iPgno = p->iRightChildPg;
463 }else{
464 p[1].iPgno = p->aCell[p->iCell].iChildPg;
466 rc = sqlite3PagerGet(pPager, p[1].iPgno, &p[1].pPg);
467 p[1].iCell = 0;
468 p[1].zPath = sqlite3_mprintf("%s%.3x/", p->zPath, p->iCell);
469 p->iCell++;
473 /* Populate the StatCursor fields with the values to be returned
474 ** by the xColumn() and xRowid() methods.
476 if( rc==SQLITE_OK ){
477 int i;
478 StatPage *p = &pCsr->aPage[pCsr->iPage];
479 pCsr->zName = (char *)sqlite3_column_text(pCsr->pStmt, 0);
480 pCsr->iPageno = p->iPgno;
482 statDecodePage(pBt, p);
483 statSizeAndOffset(pCsr);
485 switch( p->flags ){
486 case 0x05: /* table internal */
487 case 0x02: /* index internal */
488 pCsr->zPagetype = "internal";
489 break;
490 case 0x0D: /* table leaf */
491 case 0x0A: /* index leaf */
492 pCsr->zPagetype = "leaf";
493 break;
494 default:
495 pCsr->zPagetype = "corrupted";
496 break;
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;
506 pCsr->nPayload = nPayload;
509 return rc;
512 static int statEof(sqlite3_vtab_cursor *pCursor){
513 StatCursor *pCsr = (StatCursor *)pCursor;
514 return pCsr->isEof;
517 static int statFilter(
518 sqlite3_vtab_cursor *pCursor,
519 int idxNum, const char *idxStr,
520 int argc, sqlite3_value **argv
522 StatCursor *pCsr = (StatCursor *)pCursor;
524 statResetCsr(pCsr);
525 return statNext(pCursor);
528 static int statColumn(
529 sqlite3_vtab_cursor *pCursor,
530 sqlite3_context *ctx,
531 int i
533 StatCursor *pCsr = (StatCursor *)pCursor;
534 switch( i ){
535 case 0: /* name */
536 sqlite3_result_text(ctx, pCsr->zName, -1, SQLITE_STATIC);
537 break;
538 case 1: /* path */
539 sqlite3_result_text(ctx, pCsr->zPath, -1, SQLITE_TRANSIENT);
540 break;
541 case 2: /* pageno */
542 sqlite3_result_int64(ctx, pCsr->iPageno);
543 break;
544 case 3: /* pagetype */
545 sqlite3_result_text(ctx, pCsr->zPagetype, -1, SQLITE_STATIC);
546 break;
547 case 4: /* ncell */
548 sqlite3_result_int(ctx, pCsr->nCell);
549 break;
550 case 5: /* payload */
551 sqlite3_result_int(ctx, pCsr->nPayload);
552 break;
553 case 6: /* unused */
554 sqlite3_result_int(ctx, pCsr->nUnused);
555 break;
556 case 7: /* mx_payload */
557 sqlite3_result_int(ctx, pCsr->nMxPayload);
558 break;
559 case 8: /* pgoffset */
560 sqlite3_result_int64(ctx, pCsr->iOffset);
561 break;
562 case 9: /* pgsize */
563 sqlite3_result_int(ctx, pCsr->szPage);
564 break;
566 return SQLITE_OK;
569 static int statRowid(sqlite3_vtab_cursor *pCursor, sqlite_int64 *pRowid){
570 StatCursor *pCsr = (StatCursor *)pCursor;
571 *pRowid = pCsr->iPageno;
572 return SQLITE_OK;
575 int sqlite3_dbstat_register(sqlite3 *db){
576 static sqlite3_module dbstat_module = {
577 0, /* iVersion */
578 statConnect, /* xCreate */
579 statConnect, /* xConnect */
580 statBestIndex, /* xBestIndex */
581 statDisconnect, /* xDisconnect */
582 statDisconnect, /* xDestroy */
583 statOpen, /* xOpen - open a cursor */
584 statClose, /* xClose - close a cursor */
585 statFilter, /* xFilter - configure scan constraints */
586 statNext, /* xNext - advance a cursor */
587 statEof, /* xEof - check for end of scan */
588 statColumn, /* xColumn - read data */
589 statRowid, /* xRowid - read data */
590 0, /* xUpdate */
591 0, /* xBegin */
592 0, /* xSync */
593 0, /* xCommit */
594 0, /* xRollback */
595 0, /* xFindMethod */
596 0, /* xRename */
598 sqlite3_create_module(db, "dbstat", &dbstat_module, 0);
599 return SQLITE_OK;
602 #endif
604 #if defined(SQLITE_TEST) || TCLSH==2
605 #include <tcl.h>
607 static int test_dbstat(
608 void *clientData,
609 Tcl_Interp *interp,
610 int objc,
611 Tcl_Obj *CONST objv[]
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;
622 if( objc!=2 ){
623 Tcl_WrongNumArgs(interp, 1, objv, "DB");
624 return TCL_ERROR;
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);
632 return TCL_OK;
633 #endif
636 int SqlitetestStat_Init(Tcl_Interp *interp){
637 Tcl_CreateObjCommand(interp, "register_dbstat_vtab", test_dbstat, 0, 0);
638 return TCL_OK;
640 #endif /* if defined(SQLITE_TEST) || TCLSH==2 */