From 3a93ef4bdb80cf6ac47117c7f17ce410175430df Mon Sep 17 00:00:00 2001 From: Bulat Niatshin Date: Wed, 14 Feb 2018 19:44:48 +0300 Subject: [PATCH] sql: refactor struct Schema Refactor struct Schema - remove all unnecessary fields, save only actually used fields like tblHash, trigHash, fkeyHash, schema_cookie. For #3119 --- src/box/sql/build.c | 8 +++----- src/box/sql/callback.c | 13 ++++++------- src/box/sql/main.c | 2 +- src/box/sql/prepare.c | 17 +++++------------ src/box/sql/sqliteInt.h | 31 ++----------------------------- src/box/sql/vdbe.c | 18 +++--------------- src/box/sql/vdbesort.c | 11 ++--------- 7 files changed, 22 insertions(+), 78 deletions(-) diff --git a/src/box/sql/build.c b/src/box/sql/build.c index eb2a1855a731..a32e67ddddd2 100644 --- a/src/box/sql/build.c +++ b/src/box/sql/build.c @@ -382,7 +382,7 @@ sqlite3ResetOneSchema(sqlite3 * db) { /* Case 1: Reset the single schema of the database */ assert(db->pSchema != 0); - sqlite3SchemaClear(db->pSchema); + sqlite3SchemaClear(db); } /* @@ -394,7 +394,7 @@ sqlite3ResetAllSchemasOfConnection(sqlite3 * db) { struct session *user_session = current_session(); if (db->pSchema) { - sqlite3SchemaClear(db->pSchema); + sqlite3SchemaClear(db); } user_session->sql_flags &= ~SQLITE_InternChanges; } @@ -1205,7 +1205,7 @@ sqlite3ChangeCookie(Parse * pParse) sqlite3 *db = pParse->db; Vdbe *v = pParse->pVdbe; sqlite3VdbeAddOp3(v, OP_SetCookie, 0, 0, - db->mdb.pSchema->schema_cookie + 1); + db->pSchema->schema_cookie + 1); } /* @@ -2226,7 +2226,6 @@ sqlite3ViewGetColumnNames(Parse * pParse, Table * pTable) } else { nErr++; } - pTable->pSchema->schemaFlags |= DB_UnresetViews; #endif /* SQLITE_OMIT_VIEW */ return nErr; } @@ -2249,7 +2248,6 @@ sqliteViewResetAll(sqlite3 * db) pTab->nCol = 0; } } - DbClearProperty(db, DB_UnresetViews); } #else #define sqliteViewResetAll(A,B) diff --git a/src/box/sql/callback.c b/src/box/sql/callback.c index 5f2ddeaeed9f..3ccd059ae56d 100644 --- a/src/box/sql/callback.c +++ b/src/box/sql/callback.c @@ -387,12 +387,14 @@ sqlite3FindFunction(sqlite3 * db, /* An open database */ * The Schema.cache_size variable is not cleared. */ void -sqlite3SchemaClear(void *p) +sqlite3SchemaClear(sqlite3 * db) { + assert(db->pSchema); + Hash temp1; Hash temp2; HashElem *pElem; - Schema *pSchema = (Schema *) p; + Schema *pSchema = db->pSchema; temp1 = pSchema->tblHash; temp2 = pSchema->trigHash; @@ -410,11 +412,8 @@ sqlite3SchemaClear(void *p) } sqlite3HashClear(&temp1); sqlite3HashClear(&pSchema->fkeyHash); - pSchema->pSeqTab = 0; - if (pSchema->schemaFlags & DB_SchemaLoaded) { - pSchema->iGeneration++; - pSchema->schemaFlags &= ~DB_SchemaLoaded; - } + + db->pSchema = NULL; } /* Create a brand new schema. */ diff --git a/src/box/sql/main.c b/src/box/sql/main.c index a9149d63c52a..7cc88d1722fd 100644 --- a/src/box/sql/main.c +++ b/src/box/sql/main.c @@ -2373,7 +2373,7 @@ openDatabase(const char *zFilename, /* Database filename UTF-8 encoded */ goto opendb_out; } - db->pSchema = sqlite3SchemaCreate(db); + db->pSchema = 0; db->magic = SQLITE_MAGIC_OPEN; if (db->mallocFailed) { goto opendb_out; diff --git a/src/box/sql/prepare.c b/src/box/sql/prepare.c index 5d340ee987b1..2889f0f5effe 100644 --- a/src/box/sql/prepare.c +++ b/src/box/sql/prepare.c @@ -87,7 +87,6 @@ sqlite3InitCallback(void *pInit, int argc, char **argv, char **NotUsed) assert(argc == 3); UNUSED_PARAMETER2(NotUsed, argc); assert(sqlite3_mutex_held(db->mutex)); - DbClearProperty(db, DB_Empty); if (db->mallocFailed) { corruptSchema(pData, argv[0], 0); return 1; @@ -185,11 +184,7 @@ sqlite3InitDatabase(sqlite3 * db) /* Tarantool: schema_cookie is not used so far, but * might be used in future. Set it to dummy value. */ - pDb->pSchema->schema_cookie = 0; - - if (pDb->pSchema->cache_size == 0) { - pDb->pSchema->cache_size = SQLITE_DEFAULT_CACHE_SIZE; - } + db->pSchema->schema_cookie = 0; /* Read the schema information out of the schema tables */ @@ -217,9 +212,6 @@ sqlite3InitDatabase(sqlite3 * db) rc = SQLITE_NOMEM_BKPT; sqlite3ResetAllSchemasOfConnection(db); } - if (rc == SQLITE_OK) { - DbSetProperty(db, DB_SchemaLoaded); - } error_out: if (rc == SQLITE_NOMEM || rc == SQLITE_IOERR_NOMEM) { @@ -231,8 +223,8 @@ sqlite3InitDatabase(sqlite3 * db) /* * Initialize all database files - the main database file * Return a success code. - * After a database is initialized, the DB_SchemaLoaded - * bit is set in the flags field of the Db structure. + * After a database is initialized, the pSchema field in database + * structure will be not NULL. */ int sqlite3Init(sqlite3 * db) @@ -245,7 +237,8 @@ sqlite3Init(sqlite3 * db) assert(db->init.busy == 0); rc = SQLITE_OK; db->init.busy = 1; - if (!DbHasProperty(db, DB_SchemaLoaded)) { + if (!db->pSchema) { + db->pSchema = sqlite3SchemaCreate(db); rc = sqlite3InitDatabase(db); if (rc) { sqlite3ResetOneSchema(db); diff --git a/src/box/sql/sqliteInt.h b/src/box/sql/sqliteInt.h index c51564dbcbfd..a25116a3f5a9 100644 --- a/src/box/sql/sqliteInt.h +++ b/src/box/sql/sqliteInt.h @@ -892,39 +892,12 @@ typedef int VList; * An instance of the following structure stores a database schema. */ struct Schema { - int schema_cookie; /* Database schema version number for this file */ - int iGeneration; /* Generation counter. Incremented with each change */ + int schema_cookie; /* Database schema version number for this file */ Hash tblHash; /* All tables indexed by name */ Hash trigHash; /* All triggers indexed by name */ Hash fkeyHash; /* All foreign keys by referenced table name */ - Table *pSeqTab; /* The sqlite_sequence table used by AUTOINCREMENT */ - u8 enc; /* Text encoding used by this database */ - u16 schemaFlags; /* Flags associated with this schema */ - int cache_size; /* Number of pages to use in the cache */ }; -/* - * These macros can be used to test, set, or clear bits in the - * Db.pSchema->flags field. - */ -#define DbHasProperty(D,P) (((D)->pSchema->schemaFlags&(P))==(P)) -#define DbHasAnyProperty(D,P) (((D)->pSchema->schemaFlags&(P))!=0) -#define DbSetProperty(D,P) (D)->pSchema->schemaFlags|=(P) - -/* - * Allowed values for the DB.pSchema->flags field. - * - * The DB_SchemaLoaded flag is set after the database schema has been - * read into internal hash tables. - * - * DB_UnresetViews means that one or more views have column names that - * have been filled out. If the schema changes, these column names might - * changes and so the view will need to be reset. - */ -#define DB_SchemaLoaded 0x0001 /* The schema has been loaded */ -#define DB_UnresetViews 0x0002 /* Some views have defined column names */ -#define DB_Empty 0x0004 /* The file is empty (length 0 bytes) */ - /* * The number of different kinds of things that can be limited * using the sqlite3_limit() interface. @@ -3466,7 +3439,7 @@ void sqlite3DeleteIndexSamples(sqlite3 *, Index *); void sqlite3DefaultRowEst(Index *); void sqlite3RegisterLikeFunctions(sqlite3 *, int); int sqlite3IsLikeFunction(sqlite3 *, Expr *, int *, char *); -void sqlite3SchemaClear(void *); +void sqlite3SchemaClear(sqlite3 *); Schema *sqlite3SchemaCreate(sqlite3 *); int sqlite3SchemaToIndex(sqlite3 * db, Schema *); KeyInfo *sqlite3KeyInfoAlloc(sqlite3 *, int, int); diff --git a/src/box/sql/vdbe.c b/src/box/sql/vdbe.c index 8bcb20430c3a..5dc66b147d91 100644 --- a/src/box/sql/vdbe.c +++ b/src/box/sql/vdbe.c @@ -3016,7 +3016,7 @@ case OP_AutoCommit: { break; } -/* Opcode: Transaction P1 P2 P3 P4 P5 +/* Opcode: Transaction P1 P2 * * * * * Begin a transaction on database P1 if a transaction is not already * active. @@ -3039,16 +3039,6 @@ case OP_AutoCommit: { * entire transaction. If no error is encountered, the statement transaction * will automatically commit when the VDBE halts. * - * If P5!=0 then this opcode also checks the schema cookie against P3 - * and the schema generation counter against P4. - * The cookie changes its value whenever the database schema changes. - * This operation is used to detect when that the cookie has changed - * and that the current process needs to reread the schema. If the schema - * cookie in P3 differs from the schema cookie in the database header or - * if the schema generation counter in P4 differs from the current - * generation counter, then an SQLITE_SCHEMA error is raised and execution - * halts. The sqlite3_step() wrapper function might then reprepare the - * statement and rerun it from the beginning. */ case OP_Transaction: { assert(p->bIsReader); @@ -3069,7 +3059,6 @@ case OP_Transaction: { } goto abort_due_to_error; } - assert(pOp->p5==0 || pOp->p4type==P4_INT32); if (rc) goto abort_due_to_error; break; @@ -3128,7 +3117,6 @@ case OP_SetCookie: { assert(p->readOnly==0); /* See note about index shifting on OP_ReadCookie */ /* When the schema cookie changes, record the new cookie internally */ - pDb->pSchema->schema_cookie = pOp->p3; user_session->sql_flags |= SQLITE_InternChanges; if (pOp->p1==1) { /* Invalidate all prepared statements whenever the TEMP database @@ -4755,7 +4743,7 @@ case OP_ParseSchema2: { char *argv[4] = {NULL, NULL, NULL, NULL}; - assert(DbHasProperty(db, DB_SchemaLoaded)); + assert(db->pSchema); initData.db = db; initData.pzErrMsg = &p->zErrMsg; @@ -4809,7 +4797,7 @@ case OP_ParseSchema3: { Mem *pRec; char zPgnoBuf[16]; char *argv[4] = {NULL, zPgnoBuf, NULL, NULL}; - assert(DbHasProperty(db, DB_SchemaLoaded)); + assert(db->pSchema); initData.db = db; initData.pzErrMsg = &p->zErrMsg; diff --git a/src/box/sql/vdbesort.c b/src/box/sql/vdbesort.c index aa35de228d09..080f2460b4bd 100644 --- a/src/box/sql/vdbesort.c +++ b/src/box/sql/vdbesort.c @@ -913,15 +913,8 @@ sqlite3VdbeSorterInit(sqlite3 * db, /* Database connection (for malloc()) */ u32 szPma = sqlite3GlobalConfig.szPma; pSorter->mnPmaSize = szPma * pgsz; - mxCache = db->mdb.pSchema->cache_size; - if (mxCache < 0) { - /* A negative cache-size value C indicates that the cache is abs(C) - * KiB in size. - */ - mxCache = mxCache * -1024; - } else { - mxCache = mxCache * pgsz; - } + mxCache = SQLITE_DEFAULT_CACHE_SIZE; + mxCache = mxCache * -1024; mxCache = MIN(mxCache, SQLITE_MAX_PMASZ); pSorter->mxPmaSize = MAX(pSorter->mnPmaSize, (int)mxCache);