コード例 #1
0
void SqliteFinalize(TPerfTestMode aMode)
	{
	if(aMode == EPerfTestSqliteSqlMode)
		{
		(void)sqlite3_enable_shared_cache(0);
		sqlite3_soft_heap_limit(0);
		}
	sqlite3_shutdown();
	}
コード例 #2
0
/**
@SYMTestCaseID			SYSLIB-SQL-UT-3433
@SYMTestCaseDesc		Test for DEF104744 - RSqlStatement::Next() SQL Server crashes on ORDER BY clause.
						The test creates a database with a table with 30 integer columns, then inserts 100
						records. After that, sets the soft heap limit to be very low - 10K 
						(to get sqlite3_release_memory() called by SQLITE ), creates a statement object 
						and attempts to retrieve the inserted records in descending order.
@SYMTestPriority		High
@SYMTestActions			Test for DEF104744 - RSqlStatement::Next() SQL Server crashes on ORDER BY clause.
@SYMTestExpectedResults Test must not fail
@SYMDEF					DEF104744
*/
void DEF104744()
	{
	(void)TheFs.Delete(KTestDatabase);
	TheSqliteDb = NULL;
	TInt err = sqlite3_open((const char*)KTestDatabaseZ().Ptr(), &TheSqliteDb);
	TEST2(err, SQLITE_OK);
	
	_LIT8(KCreateTblSqlZ, "CREATE TABLE A1(F1 INTEGER,F2 INTEGER,F3 INTEGER,F4 INTEGER,F5 INTEGER,F6 INTEGER,F7 INTEGER,F8 INTEGER,F9 INTEGER,F10 INTEGER,F11 INTEGER,F12 INTEGER,F13 INTEGER,F14 INTEGER,F15 INTEGER,F16 INTEGER,F17 INTEGER,F18 INTEGER,F19 INTEGER,F20 INTEGER,F21 INTEGER,F22 INTEGER,F23 INTEGER,F24 INTEGER,F25 INTEGER,F26 INTEGER,F27 INTEGER,F28 INTEGER,F29 INTEGER,F30 INTEGER)\x0");
	err = sqlite3_exec(TheSqliteDb, (const char*)KCreateTblSqlZ().Ptr(), 0, 0, 0);
	TEST2(err, SQLITE_OK);

	//Insert a 100 records
	const TInt KTestRecCnt = 100;
	_LIT8(KInsertSqlZ, "INSERT INTO A1(F1,F2 ,F3 ,F4 ,F5 ,F6 ,F7 ,F8 ,F9 ,F10,F11,F12,F13,F14,F15,F16,F17,F18,F19,F20,F21,F22,F23,F24,F25,F26,F27,F28,F29,F30) VALUES(:Prm1,:Prm2,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296,4294967296)\x0");
	sqlite3_stmt* stmt1 = NULL;
	err = sqlite3_prepare_v2(TheSqliteDb, (const char*)(KInsertSqlZ().Ptr()), -1, &stmt1, NULL);
	TEST2(err, SQLITE_OK);
	
	_LIT8(KBeginSqlZ, "BEGIN\x0");
	err = sqlite3_exec(TheSqliteDb, (const char*)KBeginSqlZ().Ptr(), 0, 0, 0);
	TEST2(err, SQLITE_OK);
	
	for(TInt i=0;i<KTestRecCnt;++i)
		{ 
		err = sqlite3_bind_int(stmt1, 1, i);
		TEST2(err, SQLITE_OK);
		err = sqlite3_bind_int(stmt1, 2, i + 1);
		TEST2(err, SQLITE_OK);
		err = sqlite3_step(stmt1);
		TEST2(err, SQLITE_DONE);
		err = sqlite3_reset(stmt1);
		TEST2(err, SQLITE_OK);
		TInt cnt = sqlite3_changes(TheSqliteDb);
		TEST2(cnt, 1);
		}
		
	_LIT8(KCommitSqlZ, "COMMIT\x0");
	err = sqlite3_exec(TheSqliteDb, (const char*)KCommitSqlZ().Ptr(), 0, 0, 0);
	TEST2(err, SQLITE_OK);
	sqlite3_finalize(stmt1);

	sqlite3_soft_heap_limit(10 * 1024);//Setting very low soft heap limit - 10K

	// Get the inserted record data in descending order.
	sqlite3_stmt* stmt2 = NULL;
	_LIT8(KSelectSqlZ,"SELECT * FROM A1 ORDER BY F1 DESC");
	err = sqlite3_prepare_v2(TheSqliteDb, (const char*)(KSelectSqlZ().Ptr()), -1, &stmt2, NULL);
	TEST2(err, SQLITE_OK);
	err = sqlite3_step(stmt2);
	TEST2(err, SQLITE_ROW);
	sqlite3_finalize(stmt2);

	sqlite3_close(TheSqliteDb); 
	TheSqliteDb = NULL;
	(void)TheFs.Delete(KTestDatabase);
	}
コード例 #3
0
void SqliteInitialize(TPerfTestMode aMode)
	{
	if(aMode == EPerfTestSqliteSqlMode)
		{
		const TInt KSqliteLookAsideCellSize = 128;
		const TInt KSqliteLookAsideCellCount = 512;
		int err;
		err = sqlite3_config(SQLITE_CONFIG_LOOKASIDE, KSqliteLookAsideCellSize, KSqliteLookAsideCellCount);
		TEST2(err, SQLITE_OK);
		sqlite3_soft_heap_limit(1024 * 1024);
		err = sqlite3_enable_shared_cache(1);
		TEST2(err, SQLITE_OK);
		}
	}
// Sets the global SQLite configuration.
// This must be called before any other SQLite functions are called.
static void sqliteInitialize() {
    // Enable multi-threaded mode.  In this mode, SQLite is safe to use by multiple
    // threads as long as no two threads use the same database connection at the same
    // time (which we guarantee in the SQLite database wrappers).
    sqlite3_config(SQLITE_CONFIG_MULTITHREAD);

    // Redirect SQLite log messages to the Android log.
    bool verboseLog = android_util_Log_isVerboseLogEnabled(SQLITE_LOG_TAG);
    sqlite3_config(SQLITE_CONFIG_LOG, &sqliteLogCallback, verboseLog ? (void*)1 : NULL);

    // The soft heap limit prevents the page cache allocations from growing
    // beyond the given limit, no matter what the max page cache sizes are
    // set to. The limit does not, as of 3.5.0, affect any other allocations.
    sqlite3_soft_heap_limit(SOFT_HEAP_LIMIT);

    // Initialize SQLite.
    sqlite3_initialize();
}
コード例 #5
0
/* public native void dbopen(String path, int flags, String locale); */
static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags)
{
    int err;
    sqlite3 * handle = NULL;
    sqlite3_stmt * statement = NULL;
    char const * path8 = env->GetStringUTFChars(pathString, NULL);
    int sqliteFlags;

    // register the logging func on sqlite. needs to be done BEFORE any sqlite3 func is called.
    registerLoggingFunc(path8);

    // convert our flags into the sqlite flags
    if (flags & CREATE_IF_NECESSARY) {
        sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
    } else if (flags & OPEN_READONLY) {
        sqliteFlags = SQLITE_OPEN_READONLY;
    } else {
        sqliteFlags = SQLITE_OPEN_READWRITE;
    }

    err = sqlite3_open_v2(path8, &handle, sqliteFlags, NULL);
    if (err != SQLITE_OK) {
        LOGE("sqlite3_open_v2(\"%s\", &handle, %d, NULL) failed\n", path8, sqliteFlags);
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    // The soft heap limit prevents the page cache allocations from growing
    // beyond the given limit, no matter what the max page cache sizes are
    // set to. The limit does not, as of 3.5.0, affect any other allocations.
    sqlite3_soft_heap_limit(sSqliteSoftHeapLimit);

    // Set the default busy handler to retry for 1000ms and then return SQLITE_BUSY
    err = sqlite3_busy_timeout(handle, 1000 /* ms */);
    if (err != SQLITE_OK) {
        LOGE("sqlite3_busy_timeout(handle, 1000) failed for \"%s\"\n", path8);
        throw_sqlite3_exception(env, handle);
        goto done;
    }

#ifdef DB_INTEGRITY_CHECK
    static const char* integritySql = "pragma integrity_check(1);";
    err = sqlite3_prepare_v2(handle, integritySql, -1, &statement, NULL);
    if (err != SQLITE_OK) {
        LOGE("sqlite_prepare_v2(handle, \"%s\") failed for \"%s\"\n", integritySql, path8);
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    // first is OK or error message
    err = sqlite3_step(statement);
    if (err != SQLITE_ROW) {
        LOGE("integrity check failed for \"%s\"\n", integritySql, path8);
        throw_sqlite3_exception(env, handle);
        goto done;
    } else {
        const char *text = (const char*)sqlite3_column_text(statement, 0);
        if (strcmp(text, "ok") != 0) {
            LOGE("integrity check failed for \"%s\": %s\n", integritySql, path8, text);
            jniThrowException(env, "android/database/sqlite/SQLiteDatabaseCorruptException", text);
            goto done;
        }
    }
#endif

    err = register_android_functions(handle, UTF16_STORAGE);
    if (err) {
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    LOGV("Opened '%s' - %p\n", path8, handle);
    env->SetIntField(object, offset_db_handle, (int) handle);
    handle = NULL;  // The caller owns the handle now.

done:
    // Release allocated resources
    if (path8 != NULL) env->ReleaseStringUTFChars(pathString, path8);
    if (statement != NULL) sqlite3_finalize(statement);
    if (handle != NULL) sqlite3_close(handle);
}
コード例 #6
0
int
main(int argc, char *argv[])
{
    int stcp = -1;
    int sudp = -1;

    int rc = -1;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s [port]\n", argv[0]);
        exit(1);
    }

    /* Limit memory usage by SQLite to a reasonable, relatively small level. */
#ifdef WWDEBUG
    printf("Current SQLite heap size limit is %ld.  Setting it to 8MB.\n", sqlite3_soft_heap_limit(-1));
#endif
    sqlite3_soft_heap_limit(8*1024*1024);
#ifdef WWDEBUG
    printf("New SQLite heap size limit is %ld.\n", sqlite3_soft_heap_limit(-1));
#endif

/* Include this only if we are *not* compiling a debug version */
#ifndef WWDEBUG
    /* Fork and background (basically) */
    pid_t pid = fork();
    if (pid != 0) {
        if (pid < 0) {
            perror("AGGREGATOR: Failed to fork");
            exit(1);
        }
        exit(0);
    }
#endif

    bzero(sock_data, sizeof(sock_data));

    // Get the database ready
    // Attempt to open database & check for failure
#ifdef WWDEBUG
    printf("Attempting to open database: %s\n", SQLITE_DB_FNAME);
#endif
    rc = sqlite3_open(SQLITE_DB_FNAME, &db);
    if (rc) {
        fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(1);
    } else {
        // Now check & create tables if required 
        createTable(db, 1);
        createTable(db, 2);
#ifdef WWDEBUG
        printf("Database ready for reading and writing...\n");
#endif
    }

    // Prepare to accept clients
    FD_ZERO(&rfds);
    FD_ZERO(&wfds);

    // Open TCP (SOCK_STREAM) & UDP (SOCK_DGRAM) sockets, bind to the port 
    // given and listen on TCP sock for connections
    if ((setup_sockets(atoi(argv[1]), &stcp, &sudp)) < 0) {
        perror("WWAGGREGATOR: Error setting up Sockets\n");
        exit(1);
    }
#ifdef WWDEBUG
    printf("Our listen sock # is - %d & UDP sock # is - %d \n", stcp, sudp);
#endif
    //printf("FD_SETSIZE - %d\n",FD_SETSIZE);

    //Add the created TCP & UDP sockets to the read set of file descriptors
    FD_SET(stcp, &rfds);
    FD_SET(sudp, &rfds);

    // Event loop
    while (1) {

        int n = 0;
        fd_set _rfds, _wfds;

        memcpy(&_rfds, &rfds, sizeof(fd_set));
        memcpy(&_wfds, &wfds, sizeof(fd_set));

        // Block until there's an event to handle
        // Select function call is made; return value 'n' gives the number of FDs ready to be serviced
        if (((n = select(FD_SETSIZE, &_rfds, &_wfds, NULL, 0)) < 0) && (errno != EINTR)) {
            perror("select");
            exit(1);
        }
        // Handle events
        for (int i = 0; (i < FD_SETSIZE) && n; i++) {

            if (FD_ISSET(i, &_rfds)) {
                // Handle our main mother, TCP listening socket differently
                if (i == stcp) {
                    if (accept_conn(stcp) < 0)
                        exit(1);
                    // Handle our UDP socket differently
                } else if (i == sudp) {
                    read_and_dump_data(sudp);
                } else {
#ifdef WWDEBUG
                    fprintf(stderr, "File descriptor %d is ready for reading .. call'g readHLR\n", i);
#endif
                    read_handler(i);
                }

                n--;
            }

            if (FD_ISSET(i, &_wfds)) {
#ifdef WWDEBUG
                fprintf(stderr, "File descriptor %d is ready for writing .. call'g writeHLR\n", i);
#endif
                write_handler(i);
                n--;
            }
        }
    }
}
void Connector::enableSoftHeapLimit(int limit)
{
	sqlite3_soft_heap_limit(limit);
}
コード例 #8
0
/* public native void dbopen(String path, int flags, String locale); */
static void dbopen(JNIEnv* env, jobject object, jstring pathString, jint flags)
{
    int err;
    sqlite3 * handle = NULL;
    sqlite3_stmt * statement = NULL;
    char const * path8 = env->GetStringUTFChars(pathString, NULL);
    int sqliteFlags;
    // Error code handling for SQLite exec
    char* zErrMsg = NULL;

    // register the logging func on sqlite. needs to be done BEFORE any sqlite3 func is called.
    registerLoggingFunc(path8);

    // convert our flags into the sqlite flags
    if (flags & CREATE_IF_NECESSARY) {
        sqliteFlags = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
    } else if (flags & OPEN_READONLY) {
        sqliteFlags = SQLITE_OPEN_READONLY;
    } else {
        sqliteFlags = SQLITE_OPEN_READWRITE;
    }

    err = sqlite3_open_v2(path8, &handle, sqliteFlags, NULL);
    if (err != SQLITE_OK) {
        LOGE("sqlite3_open_v2(\"%s\", &handle, %d, NULL) failed\n", path8, sqliteFlags);
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    // WAL is a new rollback method available in SQLite v3.7+. WAL speeds up writes to
    // SQLite databases. WAL cannot be used with Read Only databases or databases opened
    // in read only mode.

    // Check if DB can use WAL mode; Open in WAL mode for non-ReadOnly DBs
    if(!(flags & OPEN_READONLY) && (use_wal_mode(path8))) {
        // Configure databases to run in WAL mode.
        err = sqlite3_exec(handle,"PRAGMA journal_mode = WAL;",
                           NULL, NULL,&zErrMsg);
        if (SQLITE_OK != err) {
           LOGE("sqlite3_exec - Failed to set WAL mode for [%s] \n", path8);
           err = sqlite3_exec(handle,"PRAGMA journal_mode = DELETE;",
                           NULL, NULL,&zErrMsg);
           if(SQLITE_OK != err) {
               LOGE("sqlite3_exec - Failed to set DELETE mode for [%s] \n", path8);
               throw_sqlite3_exception(env, handle);
               goto done;
           }
        }
        else {
            LOGI("WAL succesfuly enabled for [%s] \n", path8);
            // Set autocheckpoint = 100 pages
            err = sqlite3_wal_autocheckpoint(handle,
                                             100);
            if (SQLITE_OK != err) {
               LOGE("sqlite3_exec to set WAL autocheckpoint failed\n");
               throw_sqlite3_exception(env, handle);
               goto done;
            } else if (use_wal_mode(path8) == 2) {
                /* Try to disable fsyncs. We don't care if it fails */
                sqlite3_exec(handle,"PRAGMA synchronous = OFF;",
                           NULL, NULL,&zErrMsg);
            }
        }
    }

    // The soft heap limit prevents the page cache allocations from growing
    // beyond the given limit, no matter what the max page cache sizes are
    // set to. The limit does not, as of 3.5.0, affect any other allocations.
    sqlite3_soft_heap_limit(SQLITE_SOFT_HEAP_LIMIT);

    // Set the default busy handler to retry for 1000ms and then return SQLITE_BUSY
    err = sqlite3_busy_timeout(handle, 1000 /* ms */);
    if (err != SQLITE_OK) {
        LOGE("sqlite3_busy_timeout(handle, 1000) failed for \"%s\"\n", path8);
        throw_sqlite3_exception(env, handle);
        goto done;
    }

#ifdef DB_INTEGRITY_CHECK
    static const char* integritySql = "pragma integrity_check(1);";
    err = sqlite3_prepare_v2(handle, integritySql, -1, &statement, NULL);
    if (err != SQLITE_OK) {
        LOGE("sqlite_prepare_v2(handle, \"%s\") failed for \"%s\"\n", integritySql, path8);
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    // first is OK or error message
    err = sqlite3_step(statement);
    if (err != SQLITE_ROW) {
        LOGE("integrity check failed for \"%s\"\n", integritySql, path8);
        throw_sqlite3_exception(env, handle);
        goto done;
    } else {
        const char *text = (const char*)sqlite3_column_text(statement, 0);
        if (strcmp(text, "ok") != 0) {
            LOGE("integrity check failed for \"%s\": %s\n", integritySql, path8, text);
            jniThrowException(env, "android/database/sqlite/SQLiteDatabaseCorruptException", text);
            goto done;
        }
    }
#endif

    err = register_android_functions(handle, UTF16_STORAGE);
    if (err) {
        throw_sqlite3_exception(env, handle);
        goto done;
    }

    LOGV("Opened '%s' - %p\n", path8, handle);
    env->SetIntField(object, offset_db_handle, (int) handle);
    handle = NULL;  // The caller owns the handle now.

done:
    // Release allocated resources
    if (path8 != NULL) env->ReleaseStringUTFChars(pathString, path8);
    if (statement != NULL) sqlite3_finalize(statement);
    if (handle != NULL) sqlite3_close(handle);
}