Skip to content

Commit

Permalink
sql: refactor struct Schema
Browse files Browse the repository at this point in the history
Refactor struct Schema - remove all unnecessary fields, save
only actually used fields like tblHash, trigHash, fkeyHash,
schema_cookie.

For #3119
  • Loading branch information
TheAviat0r committed Feb 27, 2018
1 parent 62d440a commit 3a93ef4
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 78 deletions.
8 changes: 3 additions & 5 deletions src/box/sql/build.c
Expand Up @@ -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);
}

/*
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -2226,7 +2226,6 @@ sqlite3ViewGetColumnNames(Parse * pParse, Table * pTable)
} else {
nErr++;
}
pTable->pSchema->schemaFlags |= DB_UnresetViews;
#endif /* SQLITE_OMIT_VIEW */
return nErr;
}
Expand All @@ -2249,7 +2248,6 @@ sqliteViewResetAll(sqlite3 * db)
pTab->nCol = 0;
}
}
DbClearProperty(db, DB_UnresetViews);
}
#else
#define sqliteViewResetAll(A,B)
Expand Down
13 changes: 6 additions & 7 deletions src/box/sql/callback.c
Expand Up @@ -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;
Expand All @@ -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. */
Expand Down
2 changes: 1 addition & 1 deletion src/box/sql/main.c
Expand Up @@ -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;
Expand Down
17 changes: 5 additions & 12 deletions src/box/sql/prepare.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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);
Expand Down
31 changes: 2 additions & 29 deletions src/box/sql/sqliteInt.h
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 3 additions & 15 deletions src/box/sql/vdbe.c
Expand Up @@ -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.
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
11 changes: 2 additions & 9 deletions src/box/sql/vdbesort.c
Expand Up @@ -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);
Expand Down

0 comments on commit 3a93ef4

Please sign in to comment.