From aab9beb3de90837ccd8fbf95fa8547837c8556d9 Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Fri, 29 Aug 2025 12:33:30 -0700 Subject: [PATCH] indexeddb: Add index schemas (#38891) Creates schemas to hold index information. These tables are created when the database is initialized. These tables are not updated however. Testing: WPT and unit Fixes: Partially #38100 --------- Signed-off-by: Ashwin Naren --- .../net/indexeddb/engines/sqlite/create.rs | 89 ++++++++++++------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/components/net/indexeddb/engines/sqlite/create.rs b/components/net/indexeddb/engines/sqlite/create.rs index 06a3a4f4045..44bf7f23e2c 100644 --- a/components/net/indexeddb/engines/sqlite/create.rs +++ b/components/net/indexeddb/engines/sqlite/create.rs @@ -2,58 +2,81 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ +// Adapted from: +// https://github.com/mozilla-firefox/firefox/blob/ee102e926521b3e460293b0aea6b54b1a03f6f74/dom/indexedDB/DBSchema.cpp#L78 + pub(crate) fn create_tables(conn: &rusqlite::Connection) -> Result<(), rusqlite::Error> { - conn.execute( - r#"create table database -( + const DATABASE: &str = r#" +create table database ( name varchar not null primary key, origin varchar not null, version bigint default 0 not null -);"#, - [], - )?; +) WITHOUT ROWID;"#; + conn.execute(DATABASE, [])?; - conn.execute( - r#"create table object_store -( + const OBJECT_STORE: &str = r#" +create table object_store ( id integer not null primary key autoincrement, name varchar not null unique, key_path varbinary_blob, auto_increment boolean default FALSE not null -);"#, - [], - )?; +);"#; + conn.execute(OBJECT_STORE, [])?; - conn.execute( - r#"create table object_data -( + const OBJECT_DATA: &str = r#" +create table object_data ( object_store_id integer not null references object_store, key blob not null, data blob not null, constraint "pk-object_data" primary key (object_store_id, key) -);"#, - [], - )?; +) WITHOUT ROWID;"#; + conn.execute(OBJECT_DATA, [])?; - conn.execute( - r#"create table object_store_index - ( - id integer not null - primary key autoincrement, - object_store_id integer not null - references object_store, - name varchar not null - unique, - key_path varbinary_blob not null, - unique_index boolean not null, - multi_entry_index boolean not null - );"#, - [], - )?; + const OBJECT_STORE_INDEX: &str = r#" +create table object_store_index ( + id integer not null + primary key autoincrement, + object_store_id integer not null + references object_store, + name varchar not null + unique, + key_path varbinary_blob not null, + unique_index boolean not null, + multi_entry_index boolean not null +);"#; + conn.execute(OBJECT_STORE_INDEX, [])?; + + const INDEX_DATA: &str = r#" +CREATE TABLE index_data ( + index_id INTEGER NOT NULL, + value BLOB NOT NULL, + object_data_key BLOB NOT NULL, + object_store_id INTEGER NOT NULL, + value_locale BLOB, + PRIMARY KEY (index_id, value, object_data_key) + FOREIGN KEY (index_id) REFERENCES object_store_index(id), + FOREIGN KEY (object_store_id, object_data_key) + REFERENCES object_data(object_store_id, key) +) WITHOUT ROWID;"#; + conn.execute(INDEX_DATA, [])?; + + const UNIQUE_INDEX_DATA: &str = r#" +CREATE TABLE unique_index_data ( + index_id INTEGER NOT NULL, + value BLOB NOT NULL, + object_store_id INTEGER NOT NULL, + object_data_key BLOB NOT NULL, + value_locale BLOB, + PRIMARY KEY (index_id, value), + FOREIGN KEY (index_id) REFERENCES object_store_index(id), + FOREIGN KEY (object_store_id, object_data_key) + REFERENCES object_data(object_store_id, key) +) WITHOUT ROWID;"#; + conn.execute(UNIQUE_INDEX_DATA, [])?; Ok(()) }