bug 413589: enable fts3 (full text index) sqlite module; r=sdwilsh, a=damons

This commit is contained in:
myk@mozilla.org 2008-01-29 15:34:19 -08:00
parent 2a0e04351d
commit 1f26f9ec43
8 changed files with 40822 additions and 60 deletions

View File

@ -86,7 +86,9 @@ CSRCS = \
# -DSQLITE_SECURE_DELETE=1 will cause SQLITE to 0-fill delete data so we
# don't have to vacuum to make sure the data is not visible in the file.
DEFINES = -DSQLITE_SECURE_DELETE=1 -DTHREADSAFE=1
# -DSQLITE_ENABLE_FTS3=1 enables the full-text index module.
# -DSQLITE_CORE=1 statically links that module into the SQLite library.
DEFINES = -DSQLITE_SECURE_DELETE=1 -DTHREADSAFE=1 -DSQLITE_CORE=1 -DSQLITE_ENABLE_FTS3=1
ifeq ($(OS_ARCH),OS2)
ifdef MOZ_OS2_HIGH_MEMORY

File diff suppressed because it is too large Load Diff

View File

@ -48,7 +48,7 @@ interface nsIFile;
*
* This is the only way to open a database connection.
*/
[scriptable, uuid(a4a0cad9-e0da-4379-bee4-2feef3dddc7e)]
[scriptable, uuid(336d2741-8438-449d-8746-8c37c62a2ccb)]
interface mozIStorageService : nsISupports {
/**
* Get a connection to a named special database storage.
@ -82,16 +82,48 @@ interface mozIStorageService : nsISupports {
* The connection object returned by this function is not threadsafe. You must
* use it only from the thread you created it from.
*
* If your database contains virtual tables (f.e. for full-text indexes), you
* must open it with openUnsharedDatabase, as those tables are incompatible
* with a shared cache. If you attempt to use this method to open a database
* containing virtual tables, it will think the database is corrupted and
* throw NS_ERROR_FILE_CORRUPTED.
*
* @param aDatabaseFile a nsIFile of the database to open.
*
* @returns a mozIStorageConnection for the requested
* database file.
* @returns a mozIStorageConnection for the requested database file.
*
* @throws NS_ERROR_FAILURE if any operation fails while opening
* the database.
* @throws NS_ERROR_FAILURE if any operation fails while opening the database.
*/
mozIStorageConnection openDatabase(in nsIFile aDatabaseFile);
/**
* Open a connection to the specified file that doesn't share a sqlite cache.
*
* Each connection uses its own sqlite cache, which is inefficient, so you
* should use openDatabase instead of this method unless you need a feature
* of SQLite that is incompatible with a shared cache, like virtual table
* and full text indexing support.
*
* ==========
* DANGER
* ==========
*
* If you have more than one connection to a file, you MUST use the EXACT
* SAME NAME for the file each time, including case. The sqlite code uses
* a simple string compare to see if there is already a connection. Opening
* a connection to "Foo.sqlite" and "foo.sqlite" will CORRUPT YOUR DATABASE.
*
* The connection object returned by this function is not threadsafe. You must
* use it only from the thread you created it from.
*
* @param aDatabaseFile a nsIFile of the database to open.
*
* @returns a mozIStorageConnection for the requested database file.
*
* @throws NS_ERROR_FAILURE if any operation fails while opening the database.
*/
mozIStorageConnection openUnsharedDatabase(in nsIFile aDatabaseFile);
};
%{C++

View File

@ -141,3 +141,28 @@ mozStorageService::OpenDatabase(nsIFile *aDatabaseFile, mozIStorageConnection **
NS_ADDREF(*_retval = msc);
return NS_OK;
}
/* mozIStorageConnection openUnsharedDatabase(in nsIFile aDatabaseFile); */
NS_IMETHODIMP
mozStorageService::OpenUnsharedDatabase(nsIFile *aDatabaseFile, mozIStorageConnection **_retval)
{
nsresult rv;
mozStorageConnection *msc = new mozStorageConnection(this);
if (!msc)
return NS_ERROR_OUT_OF_MEMORY;
// Initialize the connection, temporarily turning off shared caches so the
// new connection gets its own cache. Database connections are assigned
// caches when they are opened, and they retain those caches for their
// lifetimes, unaffected by changes to the shared caches setting, so we can
// disable shared caches temporarily while we initialize the new connection
// without affecting the caches currently in use by other connections.
sqlite3_enable_shared_cache(0);
rv = msc->Initialize (aDatabaseFile);
sqlite3_enable_shared_cache(1);
NS_ENSURE_SUCCESS(rv, rv);
NS_ADDREF(*_retval = msc);
return NS_OK;
}

View File

@ -72,10 +72,22 @@ function getService()
}
var gDBConn = null;
function getOpenedDatabase()
/**
* Get a connection to the test database. Creates and caches the connection
* if necessary, otherwise reuses the existing cached connection.
*
* @param unshared {boolean}
* whether or not to open a connection to the database that doesn't share
* its cache; if true, we use mozIStorageService::openUnsharedDatabase
* to create the connection; otherwise we use openDatabase.
*/
function getOpenedDatabase(unshared)
{
if (!gDBConn) {
gDBConn = getService().openDatabase(getTestDB());
gDBConn = getService()
[unshared ? "openUnsharedDatabase" : "openDatabase"]
(getTestDB());
}
return gDBConn;
}

View File

@ -0,0 +1,119 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Myk Melez <myk@mozilla.org> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This file tests support for the fts3 (full-text index) module.
// Example statements in these tests are taken from the Full Text Index page
// on the SQLite wiki: http://www.sqlite.org/cvstrac/wiki?p=FullTextIndex
function test_table_creation()
{
var msc = getOpenedDatabase(true);
msc.executeSimpleSQL(
"CREATE VIRTUAL TABLE recipe USING fts3(name, ingredients)");
do_check_true(msc.tableExists("recipe"));
}
function test_insertion()
{
var msc = getOpenedDatabase(true);
msc.executeSimpleSQL("INSERT INTO recipe (name, ingredients) VALUES " +
"('broccoli stew', 'broccoli peppers cheese tomatoes')");
msc.executeSimpleSQL("INSERT INTO recipe (name, ingredients) VALUES " +
"('pumpkin stew', 'pumpkin onions garlic celery')");
msc.executeSimpleSQL("INSERT INTO recipe (name, ingredients) VALUES " +
"('broccoli pie', 'broccoli cheese onions flour')");
msc.executeSimpleSQL("INSERT INTO recipe (name, ingredients) VALUES " +
"('pumpkin pie', 'pumpkin sugar flour butter')");
var stmt = msc.createStatement("SELECT COUNT(*) FROM recipe");
stmt.executeStep();
do_check_eq(stmt.getInt32(0), 4);
stmt.reset();
stmt.finalize();
}
function test_selection()
{
var msc = getOpenedDatabase(true);
var stmt = msc.createStatement(
"SELECT rowid, name, ingredients FROM recipe WHERE name MATCH 'pie'");
do_check_true(stmt.executeStep());
do_check_eq(stmt.getInt32(0), 3);
do_check_eq(stmt.getString(1), "broccoli pie");
do_check_eq(stmt.getString(2), "broccoli cheese onions flour");
do_check_true(stmt.executeStep());
do_check_eq(stmt.getInt32(0), 4);
do_check_eq(stmt.getString(1), "pumpkin pie");
do_check_eq(stmt.getString(2), "pumpkin sugar flour butter");
do_check_false(stmt.executeStep());
stmt.reset();
stmt.finalize();
}
var tests = [test_table_creation, test_insertion, test_selection];
function run_test()
{
// It's extra important to start from scratch, since these tests won't work
// with an existing shared cache connection, so we do it even though the last
// test probably did it already.
cleanup();
try {
for (var i = 0; i < tests.length; i++) {
tests[i]();
}
}
// It's extra important to clean up afterwards, since later tests that use
// a shared cache connection will not be able to read the database we create,
// so we do this in a finally block to ensure it happens even if some of our
// tests fail.
finally {
cleanup();
}
}

View File

@ -35,7 +35,8 @@
*
* ***** END LICENSE BLOCK ***** */
// This file tests the functions of mozIStorageService
// This file tests the functions of mozIStorageService except for
// openUnsharedDatabase, which is tested by test_storage_service_unshared.js.
function test_openSpecialDatabase_invalid_arg()
{

View File

@ -0,0 +1,68 @@
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Storage Test Code.
*
* The Initial Developer of the Original Code is
* Mozilla Corporation.
* Portions created by the Initial Developer are Copyright (C) 2007
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Shawn Wilsher <me@shawnwilsher.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
// This file tests the openUnsharedDatabase function of mozIStorageService.
function test_openUnsharedDatabase_file_DNE()
{
// the file should be created after calling
var db = getTestDB();
do_check_false(db.exists());
getService().openUnsharedDatabase(db);
do_check_true(db.exists());
}
function test_openUnsharedDatabase_file_exists()
{
// it should already exist from our last test
var db = getTestDB();
do_check_true(db.exists());
getService().openUnsharedDatabase(db);
do_check_true(db.exists());
}
var tests = [test_openUnsharedDatabase_file_DNE,
test_openUnsharedDatabase_file_exists];
function run_test()
{
for (var i = 0; i < tests.length; i++)
tests[i]();
cleanup();
}