mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 16:03:10 +00:00
fix(indexeddb): preserve UTF-16 schema names through SQLite
Carry store and index names as IndexedDbName code units through WebIDL DOMString16 conversion, transaction scopes/admission, metadata, queued operations, cursors and DOMStringList. Store labels as ordered UTF-16BE BLOBs in SQLite schema v3; migrate known v1/v2 tables transactionally while preserving stable ids, records, indexes, generators and logical usage. Reuse the database-name codec and existing SQL command FIFO. Add exact surrogate/replacement/NUL/astral name operations in Window, iframe and Worker, plus native disk restart and malformed-label migration rollback/retry tests. This prerequisite supplies the lossless name model needed by the separate index/store rename setters.
This commit is contained in:
@@ -157,8 +157,16 @@ fn partition_shutdown_retires_retained_sql_handles_before_releasing_the_profile_
|
||||
let kept = database(&reopened, b"shutdown-owner");
|
||||
let metadata = kept.committed().unwrap();
|
||||
assert_eq!(metadata.version, 1);
|
||||
assert!(metadata.stores.contains_key("kept"));
|
||||
assert!(!metadata.stores.contains_key("discarded"));
|
||||
assert!(
|
||||
metadata
|
||||
.stores
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("kept"))
|
||||
);
|
||||
assert!(
|
||||
!metadata
|
||||
.stores
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("discarded"))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
globalThis.schemaNameChecks = [];
|
||||
async function schemaNameProbe(databaseName) {
|
||||
const checks = globalThis.schemaNameChecks;
|
||||
const check = (label, pass, actual = '') => checks.push({label, pass: !!pass, actual: String(actual)});
|
||||
const equal = (label, actual, expected) => check(label, JSON.stringify(actual) === JSON.stringify(expected), JSON.stringify(actual));
|
||||
const request = r => new Promise((resolve, reject) => {
|
||||
r.onsuccess = () => resolve(r.result);
|
||||
r.onerror = event => { event.preventDefault(); reject(r.error); };
|
||||
});
|
||||
const done = tx => new Promise((resolve, reject) => {
|
||||
tx.oncomplete = resolve;
|
||||
tx.onabort = () => reject(tx.error || new Error('unexpected abort'));
|
||||
});
|
||||
const names = ['', String.fromCharCode(0), String.fromCharCode(0xd800), String.fromCharCode(0xd801),
|
||||
String.fromCharCode(0xdc00), String.fromCharCode(0xfffd), String.fromCharCode(0xdc00, 0xd800),
|
||||
String.fromCodePoint(0x1f600), String.fromCharCode(0xe000), '__proto__'];
|
||||
const open = (version, setup) => new Promise((resolve, reject) => {
|
||||
const r = indexedDB.open(databaseName, version);
|
||||
let setupError;
|
||||
r.onupgradeneeded = () => { try { setup?.(r.result, r.transaction); } catch (error) { setupError = error; r.transaction.abort(); } };
|
||||
r.onsuccess = () => resolve(r.result);
|
||||
r.onerror = event => { event.preventDefault(); reject(setupError || r.error); };
|
||||
});
|
||||
let db;
|
||||
try {
|
||||
db = await open(1, (database, tx) => {
|
||||
for (const [ordinal, raw] of names.entries()) {
|
||||
const store = database.createObjectStore(raw);
|
||||
equal('created exact store name', store.name, raw);
|
||||
for (const indexName of names) {
|
||||
const index = store.createIndex(indexName, 'label');
|
||||
equal('created exact index name', index.name, indexName);
|
||||
}
|
||||
equal('index list uses code-unit ordering', Array.from(store.indexNames), [...names].sort());
|
||||
store.add({label: ordinal, value: ordinal}, 1);
|
||||
}
|
||||
equal('database list uses code-unit ordering', Array.from(database.objectStoreNames), [...names].sort());
|
||||
equal('upgrade scope uses code-unit ordering', Array.from(tx.objectStoreNames), [...names].sort());
|
||||
});
|
||||
let tx = db.transaction([...names].reverse().concat(names));
|
||||
let complete = done(tx);
|
||||
equal('regular scope sorts and deduplicates exact names', Array.from(tx.objectStoreNames), [...names].sort());
|
||||
const reads = [];
|
||||
for (const [ordinal, raw] of names.entries()) {
|
||||
const store = tx.objectStore(raw);
|
||||
equal('lookup exact store name', store.name, raw);
|
||||
check('store list contains exact name', db.objectStoreNames.contains(raw));
|
||||
for (const indexName of names) {
|
||||
const index = store.index(indexName);
|
||||
equal('lookup exact index name', index.name, indexName);
|
||||
check('index list contains exact name', store.indexNames.contains(indexName));
|
||||
reads.push(request(index.get(ordinal)).then(value => equal('index keeps independent record', value, {label: ordinal, value: ordinal})));
|
||||
}
|
||||
reads.push(request(store.get(1)).then(value => equal('store keeps independent record', value, {label: ordinal, value: ordinal})));
|
||||
}
|
||||
await Promise.all(reads); await complete;
|
||||
db.close();
|
||||
const abort = await open(2, (database, upgrade) => {
|
||||
const raw = names[2];
|
||||
const store = upgrade.objectStore(names[3]);
|
||||
store.deleteIndex(raw);
|
||||
check('delete index removes only exact surrogate', !store.indexNames.contains(raw) && store.indexNames.contains(names[3]) && store.indexNames.contains(names[5]));
|
||||
database.deleteObjectStore(raw);
|
||||
check('delete store removes only exact surrogate', !database.objectStoreNames.contains(raw) && database.objectStoreNames.contains(names[3]) && database.objectStoreNames.contains(names[5]));
|
||||
upgrade.abort();
|
||||
equal('abort restores exact store labels', Array.from(database.objectStoreNames), [...names].sort());
|
||||
equal('abort restores exact index labels', Array.from(store.indexNames), [...names].sort());
|
||||
}).then(() => null, error => error);
|
||||
equal('upgrade abort preserves prior names and version', abort?.name, 'AbortError');
|
||||
db = await open(1);
|
||||
tx = db.transaction(names);
|
||||
complete = done(tx);
|
||||
equal('reopen keeps exact schema labels', Array.from(db.objectStoreNames), [...names].sort());
|
||||
await Promise.all(names.map((raw, ordinal) => request(tx.objectStore(raw).get(1))
|
||||
.then(value => equal('reopen retains independent records', value, {label: ordinal, value: ordinal}))));
|
||||
await complete;
|
||||
return {state: checks.every(c => c.pass) ? 'pass' : 'fail', checks};
|
||||
} finally { db?.close(); await request(indexedDB.deleteDatabase(databaseName)); }
|
||||
}
|
||||
@@ -522,3 +522,23 @@ async fn indexed_db_database_names_preserve_utf16_identity_across_realms_and_que
|
||||
server.shutdown().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn indexed_db_schema_names_keep_utf16_identity_through_sqlite_operations() -> Result<()> {
|
||||
let server = FixtureServer::spawn().await?;
|
||||
let browser = Browser::new(AppConfig::default())?;
|
||||
let fixture = include_str!("fixtures/indexeddb-schema-names.js");
|
||||
for target in ["window", "child", "worker"] {
|
||||
let source = format!(
|
||||
"{fixture}\nschemaNameProbe('schema-names-{target}').then(finish, error => finish({{state:'error',error:String(error),checks:schemaNameChecks}}));"
|
||||
);
|
||||
let result = run_probe(&browser, &server, target, &source).await?;
|
||||
assert_eq!(result["state"], "pass", "{target}: {result}");
|
||||
assert!(
|
||||
result["checks"].as_array().unwrap().len() > 400,
|
||||
"{target}: {result}"
|
||||
);
|
||||
}
|
||||
server.shutdown().await;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ fn main() -> Result<(), Box<dyn Error>> {
|
||||
quota: None,
|
||||
},
|
||||
)?;
|
||||
let stores = [String::from("store")];
|
||||
let stores = [moli_indexeddb::IndexedDbName::from("store")];
|
||||
let transaction = client.allocate_transaction_handle()?;
|
||||
let lease = connection.queue_transaction_start(
|
||||
&stores,
|
||||
|
||||
@@ -54,6 +54,17 @@ impl fmt::Display for IndexedDbName {
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<str> for IndexedDbName {
|
||||
fn eq(&self, other: &str) -> bool {
|
||||
self.0.iter().copied().eq(other.encode_utf16())
|
||||
}
|
||||
}
|
||||
impl PartialEq<&str> for IndexedDbName {
|
||||
fn eq(&self, other: &&str) -> bool {
|
||||
self == *other
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
CREATE TABLE object_stores (
|
||||
store_id INTEGER PRIMARY KEY CHECK (store_id > 0),
|
||||
name BLOB NOT NULL CHECK (length(name) % 2 = 0) UNIQUE,
|
||||
key_path BLOB,
|
||||
auto_increment INTEGER NOT NULL CHECK (auto_increment IN (0, 1)),
|
||||
generator INTEGER NOT NULL CHECK (generator BETWEEN 0 AND 9007199254740992)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE indexes (
|
||||
index_id INTEGER PRIMARY KEY CHECK (index_id > 0),
|
||||
store_id INTEGER NOT NULL CHECK (store_id > 0),
|
||||
name BLOB NOT NULL CHECK (length(name) % 2 = 0),
|
||||
key_path BLOB NOT NULL,
|
||||
is_unique INTEGER NOT NULL CHECK (is_unique IN (0, 1)),
|
||||
multi_entry INTEGER NOT NULL CHECK (multi_entry IN (0, 1)),
|
||||
UNIQUE (store_id, name)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
@@ -28,7 +28,7 @@ use crate::{
|
||||
pub use query::{Query, QuerySource, Seek};
|
||||
pub use records::RecordWrite;
|
||||
|
||||
pub const SCHEMA_VERSION: u32 = 2;
|
||||
pub const SCHEMA_VERSION: u32 = 3;
|
||||
pub const CHUNK_BYTES: usize = 256 * 1024;
|
||||
const REQUEST_SAVEPOINT: &str = "SAVEPOINT idb_request";
|
||||
|
||||
@@ -75,10 +75,10 @@ pub struct IndexMetadata {
|
||||
pub struct StoreMetadata {
|
||||
pub id: i64,
|
||||
pub options: ObjectStoreOptions,
|
||||
pub indexes: BTreeMap<String, IndexMetadata>,
|
||||
pub indexes: BTreeMap<crate::IndexedDbName, IndexMetadata>,
|
||||
}
|
||||
|
||||
pub type StoreDefinitions = BTreeMap<String, StoreMetadata>;
|
||||
pub type StoreDefinitions = BTreeMap<crate::IndexedDbName, StoreMetadata>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct DatabaseMetadata {
|
||||
@@ -101,7 +101,7 @@ impl Eq for DatabaseMetadata {}
|
||||
struct ActiveTransaction {
|
||||
handle: TransactionHandle,
|
||||
mode: TransactionMode,
|
||||
stores: BTreeSet<String>,
|
||||
stores: BTreeSet<crate::IndexedDbName>,
|
||||
pending: Option<PendingWrite>,
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ pub struct PreparedWrite {
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct IndexKeys {
|
||||
pub name: String,
|
||||
pub name: crate::IndexedDbName,
|
||||
pub keys: Vec<Key>,
|
||||
}
|
||||
|
||||
@@ -212,8 +212,8 @@ impl Database {
|
||||
}
|
||||
drop(rows);
|
||||
drop(statement);
|
||||
if version == 1 {
|
||||
names::migrate_legacy(&mut connection, &identity.name)?;
|
||||
if version < SCHEMA_VERSION {
|
||||
names::migrate_legacy(&mut connection, &identity.name, version)?;
|
||||
}
|
||||
let committed = schema::read_metadata(&connection)?;
|
||||
Ok(Self {
|
||||
@@ -266,7 +266,7 @@ impl Database {
|
||||
&mut self,
|
||||
handle: TransactionHandle,
|
||||
mode: TransactionMode,
|
||||
stores: BTreeSet<String>,
|
||||
stores: BTreeSet<crate::IndexedDbName>,
|
||||
upgrade_version: Option<u64>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
if self.active.is_none() && !self.connection.is_autocommit() {
|
||||
@@ -473,7 +473,11 @@ impl Database {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn store_id(&self, handle: TransactionHandle, name: &str) -> Result<i64, IndexedDbError> {
|
||||
fn store_id(
|
||||
&self,
|
||||
handle: TransactionHandle,
|
||||
name: &crate::IndexedDbName,
|
||||
) -> Result<i64, IndexedDbError> {
|
||||
let transaction = self.transaction(handle)?;
|
||||
if transaction.mode != TransactionMode::VersionChange && !transaction.stores.contains(name)
|
||||
{
|
||||
@@ -484,7 +488,7 @@ impl Database {
|
||||
self.connection
|
||||
.query_row(
|
||||
"SELECT store_id FROM object_stores WHERE name=?1",
|
||||
[name],
|
||||
[names::encode(name)?],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.optional()?
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
//! Database names use UTF-16BE BLOBs in schema v2. Store/index names retain
|
||||
//! their schema-v1 representation until their independent name migrations.
|
||||
//! Names use ordered UTF-16BE BLOBs. Migration preserves legacy v1/v2 identities.
|
||||
use super::*;
|
||||
use crate::IndexedDbName;
|
||||
|
||||
pub(super) fn schema_version(connection: &Connection) -> Result<u32, IndexedDbError> {
|
||||
let version: i64 = connection.pragma_query_value(None, "user_version", |row| row.get(0))?;
|
||||
match version {
|
||||
1 | 2 => Ok(version as u32),
|
||||
1..=3 => Ok(version as u32),
|
||||
_ => Err(corruption("unsupported IndexedDB SQL schema version")),
|
||||
}
|
||||
}
|
||||
@@ -57,7 +56,7 @@ pub(super) fn read_name(
|
||||
units.extend(text.encode_utf16());
|
||||
Ok(IndexedDbName::from_utf16(units))
|
||||
}
|
||||
(2, rusqlite::types::ValueRef::Blob(bytes)) => decode(bytes),
|
||||
(2 | 3, rusqlite::types::ValueRef::Blob(bytes)) => decode(bytes),
|
||||
_ => Err(corruption("invalid IndexedDB database name column")),
|
||||
}
|
||||
}
|
||||
@@ -77,17 +76,74 @@ pub(super) fn copy(name: &IndexedDbName) -> Result<IndexedDbName, IndexedDbError
|
||||
pub(super) fn migrate_legacy(
|
||||
connection: &mut Connection,
|
||||
name: &IndexedDbName,
|
||||
version: u32,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let name = encode(name)?;
|
||||
let transaction =
|
||||
connection.transaction_with_behavior(rusqlite::TransactionBehavior::Immediate)?;
|
||||
transaction.execute_batch("ALTER TABLE database_metadata RENAME TO database_metadata_v1")?;
|
||||
transaction.execute_batch(include_str!("migrations/v2_metadata.sql"))?;
|
||||
transaction.execute(
|
||||
if version == 1 {
|
||||
transaction
|
||||
.execute_batch("ALTER TABLE database_metadata RENAME TO database_metadata_v1")?;
|
||||
transaction.execute_batch(include_str!("migrations/v2_metadata.sql"))?;
|
||||
transaction.execute(
|
||||
"INSERT INTO database_metadata SELECT singleton, ?1, generation, version, state, key_format, key_path_format, value_format, usage, next_store_id, next_index_id, next_record_id, next_object_id FROM database_metadata_v1",
|
||||
[name],
|
||||
)?;
|
||||
transaction.execute_batch("DROP TABLE database_metadata_v1; PRAGMA user_version=2")?;
|
||||
transaction.execute_batch("DROP TABLE database_metadata_v1")?;
|
||||
}
|
||||
migrate_schema_names(&transaction)?;
|
||||
transaction.pragma_update(None, "user_version", 3)?;
|
||||
transaction.commit()?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Only labels move to the lossless representation. Stable row identifiers,
|
||||
/// record/index contents and generator state remain in their original tables.
|
||||
fn migrate_schema_names(transaction: &rusqlite::Transaction<'_>) -> Result<(), IndexedDbError> {
|
||||
transaction.execute_batch(
|
||||
"ALTER TABLE object_stores RENAME TO object_stores_v2;
|
||||
ALTER TABLE indexes RENAME TO indexes_v2;",
|
||||
)?;
|
||||
transaction.execute_batch(include_str!("migrations/v3_schema_names.sql"))?;
|
||||
{
|
||||
let mut statement = transaction.prepare(
|
||||
"SELECT store_id, name, key_path, auto_increment, generator FROM object_stores_v2",
|
||||
)?;
|
||||
let mut rows = statement.query([])?;
|
||||
while let Some(row) = rows.next()? {
|
||||
let name = read_name(row, 1, 1)?;
|
||||
transaction.execute(
|
||||
"INSERT INTO object_stores VALUES (?1, ?2, ?3, ?4, ?5)",
|
||||
rusqlite::params![
|
||||
row.get::<_, i64>(0)?,
|
||||
encode(&name)?,
|
||||
row.get::<_, Option<Vec<u8>>>(2)?,
|
||||
row.get::<_, i64>(3)?,
|
||||
row.get::<_, i64>(4)?
|
||||
],
|
||||
)?;
|
||||
}
|
||||
}
|
||||
{
|
||||
let mut statement = transaction.prepare(
|
||||
"SELECT index_id, store_id, name, key_path, is_unique, multi_entry FROM indexes_v2",
|
||||
)?;
|
||||
let mut rows = statement.query([])?;
|
||||
while let Some(row) = rows.next()? {
|
||||
let name = read_name(row, 2, 1)?;
|
||||
transaction.execute(
|
||||
"INSERT INTO indexes VALUES (?1, ?2, ?3, ?4, ?5, ?6)",
|
||||
rusqlite::params![
|
||||
row.get::<_, i64>(0)?,
|
||||
row.get::<_, i64>(1)?,
|
||||
encode(&name)?,
|
||||
row.get::<_, Vec<u8>>(3)?,
|
||||
row.get::<_, i64>(4)?,
|
||||
row.get::<_, i64>(5)?
|
||||
],
|
||||
)?;
|
||||
}
|
||||
}
|
||||
transaction.execute_batch("DROP TABLE indexes_v2; DROP TABLE object_stores_v2")?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ use crate::{CursorDirection, KeyRange, RecordSnapshot};
|
||||
pub enum QuerySource {
|
||||
#[default]
|
||||
ObjectStore,
|
||||
Index(String),
|
||||
Index(crate::IndexedDbName),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -115,7 +115,7 @@ impl Database {
|
||||
fn selection(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: &crate::IndexedDbName,
|
||||
source: &QuerySource,
|
||||
) -> Result<Selection, IndexedDbError> {
|
||||
self.ready(transaction)?;
|
||||
@@ -133,7 +133,7 @@ impl Database {
|
||||
.connection
|
||||
.query_row(
|
||||
"SELECT index_id, is_unique FROM indexes WHERE store_id=?1 AND name=?2",
|
||||
rusqlite::params![store, name],
|
||||
rusqlite::params![store, names::encode(name)?],
|
||||
|row| Ok((row.get(0)?, row.get(1)?)),
|
||||
)
|
||||
.optional()?
|
||||
@@ -151,7 +151,7 @@ impl Database {
|
||||
fn compile_query(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: &crate::IndexedDbName,
|
||||
query: &Query,
|
||||
) -> Result<Selection, IndexedDbError> {
|
||||
let mut selection = self.selection(transaction, store_name, &query.source)?;
|
||||
@@ -199,10 +199,12 @@ impl Database {
|
||||
pub fn query(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
query: &Query,
|
||||
) -> Result<Vec<RecordSnapshot>, IndexedDbError> {
|
||||
let selection = self.compile_query(transaction, store_name, query)?;
|
||||
let store_name = store_name.into();
|
||||
|
||||
let selection = self.compile_query(transaction, &store_name, query)?;
|
||||
let mut statement = self.connection.prepare_cached(&selection.sql)?;
|
||||
let mut rows = statement.query(params_from_iter(selection.parameters.iter()))?;
|
||||
let mut output: Vec<RecordSnapshot> = Vec::new();
|
||||
@@ -258,11 +260,13 @@ impl Database {
|
||||
pub fn query_count(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
source: &QuerySource,
|
||||
range: &KeyRange,
|
||||
) -> Result<u64, IndexedDbError> {
|
||||
let mut selection = self.selection(transaction, store_name, source)?;
|
||||
let store_name = store_name.into();
|
||||
|
||||
let mut selection = self.selection(transaction, &store_name, source)?;
|
||||
selection.range(range)?;
|
||||
let count: i64 = self.connection.query_row(
|
||||
&format!("SELECT count(*){}", selection.sql),
|
||||
@@ -275,11 +279,13 @@ impl Database {
|
||||
pub fn delete_range(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
range: &KeyRange,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.writable(transaction)?;
|
||||
let mut selection = self.selection(transaction, store_name, &QuerySource::ObjectStore)?;
|
||||
let mut selection = self.selection(transaction, &store_name, &QuerySource::ObjectStore)?;
|
||||
selection.range(range)?;
|
||||
self.request(transaction, |owner| {
|
||||
owner.remove_selected_records(&selection.sql, &selection.parameters)
|
||||
|
||||
@@ -275,7 +275,7 @@ fn seek_queries_use_btree_search_without_a_temp_sort() {
|
||||
}),
|
||||
..query(direction)
|
||||
};
|
||||
let compiled = owner.compile_query(TX, "a", &options).unwrap();
|
||||
let compiled = owner.compile_query(TX, &"a".into(), &options).unwrap();
|
||||
let mut statement = owner
|
||||
.connection
|
||||
.prepare(&format!("EXPLAIN QUERY PLAN {}", compiled.sql))
|
||||
|
||||
@@ -18,11 +18,13 @@ impl Database {
|
||||
pub fn write(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
write: RecordWrite,
|
||||
) -> Result<Key, IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.writable(transaction)?;
|
||||
let store_id = self.store_id(transaction, store_name)?;
|
||||
let store_id = self.store_id(transaction, &store_name)?;
|
||||
self.request(transaction, |request| {
|
||||
let key = request.prepare_key(store_id, write.key, write.add_only)?;
|
||||
request.write_record(store_id, key, write.value, write.indexes, write.quota)
|
||||
@@ -32,12 +34,14 @@ impl Database {
|
||||
pub fn prepare_write(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
key: Option<Key>,
|
||||
add_only: bool,
|
||||
) -> Result<PreparedWrite, IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.writable(transaction)?;
|
||||
let store_id = self.store_id(transaction, store_name)?;
|
||||
let store_id = self.store_id(transaction, &store_name)?;
|
||||
let id = self.next_request;
|
||||
self.next_request = id
|
||||
.checked_add(1)
|
||||
@@ -259,13 +263,17 @@ impl Database {
|
||||
fn index_definitions(
|
||||
&self,
|
||||
store: i64,
|
||||
) -> Result<BTreeMap<String, (i64, bool, bool)>, IndexedDbError> {
|
||||
) -> Result<BTreeMap<crate::IndexedDbName, (i64, bool, bool)>, IndexedDbError> {
|
||||
let mut statement = self.connection.prepare_cached("SELECT name, index_id, is_unique, multi_entry FROM indexes WHERE store_id=?1 ORDER BY name")?;
|
||||
Ok(statement
|
||||
.query_map([store], |row| {
|
||||
Ok((row.get(0)?, (row.get(1)?, row.get(2)?, row.get(3)?)))
|
||||
})?
|
||||
.collect::<Result<_, _>>()?)
|
||||
let mut rows = statement.query([store])?;
|
||||
let mut definitions = BTreeMap::new();
|
||||
while let Some(row) = rows.next()? {
|
||||
definitions.insert(
|
||||
names::read_name(row, 0, SCHEMA_VERSION)?,
|
||||
(row.get(1)?, row.get(2)?, row.get(3)?),
|
||||
);
|
||||
}
|
||||
Ok(definitions)
|
||||
}
|
||||
|
||||
fn insert_index_keys(
|
||||
@@ -307,20 +315,23 @@ impl Database {
|
||||
pub fn backfill_index_record(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
index_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
index_name: impl Into<crate::IndexedDbName>,
|
||||
primary_key: &Key,
|
||||
keys: Vec<Key>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
let index_name = index_name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
self.request(transaction, |owner| {
|
||||
let (index, unique, multi_entry) = owner
|
||||
.index_definitions(store)?
|
||||
.remove(index_name)
|
||||
.remove(&index_name)
|
||||
.ok_or_else(|| {
|
||||
IndexedDbError::NotFound(format!("index `{index_name}` was not found"))
|
||||
})?;
|
||||
IndexedDbError::NotFound(format!("index `{index_name}` was not found"))
|
||||
})?;
|
||||
let primary = primary_key.encode_ordered()?;
|
||||
let record: i64 = owner.connection.query_row(
|
||||
"SELECT record_id FROM records WHERE store_id=?1 AND key_sort=?2",
|
||||
@@ -338,11 +349,13 @@ impl Database {
|
||||
pub fn get(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
key: &Key,
|
||||
) -> Result<Option<IndexedDbValue>, IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.ready(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
let record: Option<(i64, i64)> = self
|
||||
.connection
|
||||
.query_row(
|
||||
@@ -364,11 +377,13 @@ impl Database {
|
||||
pub fn get_key(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
key: &Key,
|
||||
) -> Result<Option<Key>, IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.ready(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
let value: Option<Vec<u8>> = self
|
||||
.connection
|
||||
.query_row(
|
||||
@@ -383,10 +398,12 @@ impl Database {
|
||||
pub fn count(
|
||||
&self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
) -> Result<u64, IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.ready(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
let count: i64 = self.connection.query_row(
|
||||
"SELECT count(*) FROM records WHERE store_id=?1",
|
||||
[store],
|
||||
@@ -398,11 +415,13 @@ impl Database {
|
||||
pub fn delete(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
key: &Key,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.writable(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
let sort = key.encode_ordered()?;
|
||||
self.request(transaction, |owner| {
|
||||
let old: Option<(i64, i64)> = owner
|
||||
@@ -427,10 +446,12 @@ impl Database {
|
||||
pub fn clear(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
|
||||
self.writable(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
self.request(transaction, |owner| owner.remove_store_records(store))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ pub(super) fn read_metadata(
|
||||
validate_object_store_options(options.key_path.as_ref(), options.auto_increment)
|
||||
.map_err(|_| corruption("invalid object store options"))?;
|
||||
stores.insert(
|
||||
owned_text(text(row, 1)?)?,
|
||||
names::read_name(row, 1, SCHEMA_VERSION)?,
|
||||
StoreMetadata {
|
||||
id,
|
||||
options,
|
||||
@@ -61,7 +61,7 @@ pub(super) fn read_metadata(
|
||||
while let Some(row) = rows.next()? {
|
||||
let id: i64 = row.get(0)?;
|
||||
let store = stores
|
||||
.get_mut(text(row, 1)?)
|
||||
.get_mut(&names::read_name(row, 1, SCHEMA_VERSION)?)
|
||||
.ok_or_else(|| corruption("index refers to an unknown object store"))?;
|
||||
if id <= 0 {
|
||||
return Err(corruption("invalid index ID"));
|
||||
@@ -77,9 +77,10 @@ pub(super) fn read_metadata(
|
||||
};
|
||||
validate_index_options(&options.key_path, options.multi_entry)
|
||||
.map_err(|_| corruption("invalid index options"))?;
|
||||
store
|
||||
.indexes
|
||||
.insert(owned_text(text(row, 2)?)?, IndexMetadata { id, options });
|
||||
store.indexes.insert(
|
||||
names::read_name(row, 2, SCHEMA_VERSION)?,
|
||||
IndexMetadata { id, options },
|
||||
);
|
||||
}
|
||||
if version == 0 && !stores.is_empty() {
|
||||
return Err(corruption("unpublished IndexedDB contains object stores"));
|
||||
@@ -110,21 +111,12 @@ fn text<'a>(row: &'a rusqlite::Row<'_>, column: usize) -> Result<&'a str, Indexe
|
||||
.map_err(|_| corruption("invalid schema text column"))
|
||||
}
|
||||
|
||||
fn owned_text(value: &str) -> Result<String, IndexedDbError> {
|
||||
let mut output = String::new();
|
||||
output
|
||||
.try_reserve_exact(value.len())
|
||||
.map_err(|_| IndexedDbError::Resource("Unable to allocate IndexedDB schema text".into()))?;
|
||||
output.push_str(value);
|
||||
Ok(output)
|
||||
pub(super) fn store_usage(name: &crate::IndexedDbName, options: &ObjectStoreOptions) -> u64 {
|
||||
name.usage_bytes() + 9 + options.key_path.as_ref().map_or(0, key_path_usage_bytes)
|
||||
}
|
||||
|
||||
pub(super) fn store_usage(name: &str, options: &ObjectStoreOptions) -> u64 {
|
||||
name.len() as u64 + 9 + options.key_path.as_ref().map_or(0, key_path_usage_bytes)
|
||||
}
|
||||
|
||||
pub(super) fn index_usage(name: &str, options: &IndexOptions) -> u64 {
|
||||
name.len() as u64 + 2 + key_path_usage_bytes(&options.key_path)
|
||||
pub(super) fn index_usage(name: &crate::IndexedDbName, options: &IndexOptions) -> u64 {
|
||||
name.usage_bytes() + 2 + key_path_usage_bytes(&options.key_path)
|
||||
}
|
||||
|
||||
impl Database {
|
||||
@@ -143,9 +135,11 @@ impl Database {
|
||||
pub fn create_object_store(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
name: &str,
|
||||
name: impl Into<crate::IndexedDbName>,
|
||||
options: ObjectStoreOptions,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let name = name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
if options
|
||||
.key_path
|
||||
@@ -156,7 +150,7 @@ impl Database {
|
||||
}
|
||||
let exists: bool = self.connection.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM object_stores WHERE name=?1)",
|
||||
[name],
|
||||
[names::encode(&name)?],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
if exists {
|
||||
@@ -180,23 +174,25 @@ impl Database {
|
||||
.transpose()?;
|
||||
owner.connection.execute(
|
||||
"INSERT INTO object_stores VALUES(?1, ?2, ?3, ?4, 0)",
|
||||
rusqlite::params![id, name, key_path, options.auto_increment],
|
||||
rusqlite::params![id, names::encode(&name)?, key_path, options.auto_increment],
|
||||
)?;
|
||||
owner.adjust_usage(i128::from(store_usage(name, &options)))
|
||||
owner.adjust_usage(i128::from(store_usage(&name, &options)))
|
||||
})
|
||||
}
|
||||
|
||||
pub fn delete_object_store(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
name: &str,
|
||||
name: impl Into<crate::IndexedDbName>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let name = name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
let store = self.store_id(transaction, name)?;
|
||||
let store = self.store_id(transaction, &name)?;
|
||||
self.request(transaction, |owner| {
|
||||
let metadata = read_metadata(&owner.connection)?;
|
||||
let metadata = metadata.stores.get(name).ok_or_else(|| corruption("missing store metadata"))?;
|
||||
let definitions = store_usage(name, &metadata.options) + metadata.indexes.iter().map(|(name, index)| index_usage(name, &index.options)).sum::<u64>();
|
||||
let metadata = metadata.stores.get(&name).ok_or_else(|| corruption("missing store metadata"))?;
|
||||
let definitions = store_usage(&name, &metadata.options) + metadata.indexes.iter().map(|(name, index)| index_usage(name, &index.options)).sum::<u64>();
|
||||
owner.remove_store_records(store)?;
|
||||
owner.connection.execute("DELETE FROM index_entries WHERE index_id IN (SELECT index_id FROM indexes WHERE store_id=?1)", [store])?;
|
||||
owner.connection.execute("DELETE FROM indexes WHERE store_id=?1", [store])?;
|
||||
@@ -210,15 +206,18 @@ impl Database {
|
||||
pub fn create_index(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
name: impl Into<crate::IndexedDbName>,
|
||||
options: IndexOptions,
|
||||
) -> Result<IndexMetadata, IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
let name = name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
let exists: bool = self.connection.query_row(
|
||||
"SELECT EXISTS(SELECT 1 FROM indexes WHERE store_id=?1 AND name=?2)",
|
||||
rusqlite::params![store, name],
|
||||
rusqlite::params![store, names::encode(&name)?],
|
||||
|row| row.get(0),
|
||||
)?;
|
||||
if exists {
|
||||
@@ -241,13 +240,13 @@ impl Database {
|
||||
rusqlite::params![
|
||||
id,
|
||||
store,
|
||||
name,
|
||||
names::encode(&name)?,
|
||||
options.key_path.encode_value()?,
|
||||
options.unique,
|
||||
options.multi_entry
|
||||
],
|
||||
)?;
|
||||
owner.adjust_usage(i128::from(index_usage(name, &options)))?;
|
||||
owner.adjust_usage(i128::from(index_usage(&name, &options)))?;
|
||||
Ok(IndexMetadata { id, options })
|
||||
})
|
||||
}
|
||||
@@ -255,17 +254,20 @@ impl Database {
|
||||
pub fn delete_index(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
name: impl Into<crate::IndexedDbName>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
let name = name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
self.request(transaction, |owner| {
|
||||
let metadata = read_metadata(&owner.connection)?;
|
||||
let index = metadata
|
||||
.stores
|
||||
.get(store_name)
|
||||
.and_then(|store| store.indexes.get(name))
|
||||
.get(&store_name)
|
||||
.and_then(|store| store.indexes.get(&name))
|
||||
.ok_or_else(|| IndexedDbError::NotFound(format!("index `{name}` was not found")))?;
|
||||
owner
|
||||
.connection
|
||||
@@ -274,7 +276,7 @@ impl Database {
|
||||
"DELETE FROM indexes WHERE store_id=?1 AND index_id=?2",
|
||||
[store, index.id],
|
||||
)?;
|
||||
owner.adjust_usage(-i128::from(index_usage(name, &index.options)))
|
||||
owner.adjust_usage(-i128::from(index_usage(&name, &index.options)))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
PRAGMA user_version = 2;
|
||||
PRAGMA user_version = 3;
|
||||
|
||||
CREATE TABLE database_info (
|
||||
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
||||
@@ -29,7 +29,7 @@ CREATE TABLE database_metadata (
|
||||
|
||||
CREATE TABLE object_stores (
|
||||
store_id INTEGER PRIMARY KEY CHECK (store_id > 0),
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
name BLOB NOT NULL CHECK (length(name) % 2 = 0) UNIQUE,
|
||||
key_path BLOB,
|
||||
auto_increment INTEGER NOT NULL CHECK (auto_increment IN (0, 1)),
|
||||
generator INTEGER NOT NULL CHECK (generator BETWEEN 0 AND 9007199254740992)
|
||||
@@ -38,7 +38,7 @@ CREATE TABLE object_stores (
|
||||
CREATE TABLE indexes (
|
||||
index_id INTEGER PRIMARY KEY CHECK (index_id > 0),
|
||||
store_id INTEGER NOT NULL CHECK (store_id > 0),
|
||||
name TEXT NOT NULL,
|
||||
name BLOB NOT NULL CHECK (length(name) % 2 = 0),
|
||||
key_path BLOB NOT NULL,
|
||||
is_unique INTEGER NOT NULL CHECK (is_unique IN (0, 1)),
|
||||
multi_entry INTEGER NOT NULL CHECK (multi_entry IN (0, 1)),
|
||||
|
||||
@@ -6,20 +6,23 @@ impl Database {
|
||||
pub fn rename_object_store(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
name: &str,
|
||||
new_name: &str,
|
||||
name: impl Into<crate::IndexedDbName>,
|
||||
new_name: impl Into<crate::IndexedDbName>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let name = name.into();
|
||||
let new_name = new_name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
let store = self.store_id(transaction, name)?;
|
||||
let store = self.store_id(transaction, &name)?;
|
||||
if name == new_name {
|
||||
return Ok(());
|
||||
}
|
||||
self.request(transaction, |owner| {
|
||||
owner.connection.execute(
|
||||
"UPDATE object_stores SET name=?1 WHERE store_id=?2",
|
||||
rusqlite::params![new_name, store],
|
||||
rusqlite::params![names::encode(&new_name)?, store],
|
||||
)?;
|
||||
owner.adjust_usage(i128::from(new_name.len() as u64) - i128::from(name.len() as u64))
|
||||
owner.adjust_usage(i128::from(new_name.usage_bytes()) - i128::from(name.usage_bytes()))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -27,17 +30,21 @@ impl Database {
|
||||
pub fn rename_index(
|
||||
&mut self,
|
||||
transaction: TransactionHandle,
|
||||
store_name: &str,
|
||||
name: &str,
|
||||
new_name: &str,
|
||||
store_name: impl Into<crate::IndexedDbName>,
|
||||
name: impl Into<crate::IndexedDbName>,
|
||||
new_name: impl Into<crate::IndexedDbName>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let store_name = store_name.into();
|
||||
let name = name.into();
|
||||
let new_name = new_name.into();
|
||||
|
||||
self.versionchange(transaction)?;
|
||||
let store = self.store_id(transaction, store_name)?;
|
||||
let store = self.store_id(transaction, &store_name)?;
|
||||
let index: i64 = self
|
||||
.connection
|
||||
.query_row(
|
||||
"SELECT index_id FROM indexes WHERE store_id=?1 AND name=?2",
|
||||
rusqlite::params![store, name],
|
||||
rusqlite::params![store, names::encode(&name)?],
|
||||
|row| row.get(0),
|
||||
)
|
||||
.optional()?
|
||||
@@ -48,9 +55,9 @@ impl Database {
|
||||
self.request(transaction, |owner| {
|
||||
owner.connection.execute(
|
||||
"UPDATE indexes SET name=?1 WHERE index_id=?2 AND store_id=?3",
|
||||
rusqlite::params![new_name, index, store],
|
||||
rusqlite::params![names::encode(&new_name)?, index, store],
|
||||
)?;
|
||||
owner.adjust_usage(i128::from(new_name.len() as u64) - i128::from(name.len() as u64))
|
||||
owner.adjust_usage(i128::from(new_name.usage_bytes()) - i128::from(name.usage_bytes()))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -452,7 +452,7 @@ impl DatabaseClient {
|
||||
pub fn queue_transaction_start(
|
||||
&self,
|
||||
connection: crate::DatabaseHandle,
|
||||
stores: &[String],
|
||||
stores: &[crate::IndexedDbName],
|
||||
mode: TransactionMode,
|
||||
wake: crate::TransactionRequestWake,
|
||||
) -> Result<crate::TransactionRequestLease, IndexedDbError> {
|
||||
@@ -463,7 +463,7 @@ impl DatabaseClient {
|
||||
fn queue_transaction_start_locked(
|
||||
&self,
|
||||
connection: crate::DatabaseHandle,
|
||||
stores: &[String],
|
||||
stores: &[crate::IndexedDbName],
|
||||
mode: TransactionMode,
|
||||
wake: crate::TransactionRequestWake,
|
||||
) -> Result<crate::TransactionRequestLease, IndexedDbError> {
|
||||
|
||||
@@ -8,50 +8,50 @@ pub enum Command {
|
||||
Begin {
|
||||
transaction: TransactionHandle,
|
||||
mode: TransactionMode,
|
||||
stores: BTreeSet<String>,
|
||||
stores: BTreeSet<crate::IndexedDbName>,
|
||||
version: Option<u64>,
|
||||
admission: Option<TransactionRequestHandle>,
|
||||
},
|
||||
CreateStore {
|
||||
transaction: TransactionHandle,
|
||||
name: String,
|
||||
name: crate::IndexedDbName,
|
||||
options: ObjectStoreOptions,
|
||||
},
|
||||
DeleteStore {
|
||||
transaction: TransactionHandle,
|
||||
name: String,
|
||||
name: crate::IndexedDbName,
|
||||
},
|
||||
RenameStore {
|
||||
transaction: TransactionHandle,
|
||||
name: String,
|
||||
new_name: String,
|
||||
name: crate::IndexedDbName,
|
||||
new_name: crate::IndexedDbName,
|
||||
},
|
||||
CreateIndex {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
name: String,
|
||||
store: crate::IndexedDbName,
|
||||
name: crate::IndexedDbName,
|
||||
options: IndexOptions,
|
||||
},
|
||||
DeleteIndex {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
name: String,
|
||||
store: crate::IndexedDbName,
|
||||
name: crate::IndexedDbName,
|
||||
},
|
||||
RenameIndex {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
name: String,
|
||||
new_name: String,
|
||||
store: crate::IndexedDbName,
|
||||
name: crate::IndexedDbName,
|
||||
new_name: crate::IndexedDbName,
|
||||
},
|
||||
/// Only an auto-generated numeric inline key needs a renderer round trip.
|
||||
PrepareWrite {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: crate::IndexedDbName,
|
||||
add_only: bool,
|
||||
},
|
||||
Write {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: crate::IndexedDbName,
|
||||
key: Option<Key>,
|
||||
value: IndexedDbValue,
|
||||
indexes: Vec<IndexKeys>,
|
||||
@@ -71,25 +71,25 @@ pub enum Command {
|
||||
},
|
||||
BackfillIndex {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
name: String,
|
||||
store: crate::IndexedDbName,
|
||||
name: crate::IndexedDbName,
|
||||
primary_key: Key,
|
||||
keys: Vec<Key>,
|
||||
},
|
||||
Query {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: crate::IndexedDbName,
|
||||
query: Query,
|
||||
},
|
||||
Count {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: crate::IndexedDbName,
|
||||
source: QuerySource,
|
||||
range: crate::KeyRange,
|
||||
},
|
||||
Delete {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: crate::IndexedDbName,
|
||||
range: crate::KeyRange,
|
||||
},
|
||||
Commit {
|
||||
|
||||
@@ -258,7 +258,7 @@ impl DatabaseConnection {
|
||||
|
||||
pub fn queue_transaction_start(
|
||||
&self,
|
||||
stores: &[String],
|
||||
stores: &[crate::IndexedDbName],
|
||||
mode: TransactionMode,
|
||||
wake: crate::TransactionRequestWake,
|
||||
) -> Result<crate::TransactionRequestLease, IndexedDbError> {
|
||||
|
||||
@@ -874,6 +874,54 @@ fn catalog_recovers_legacy_text_names_in_the_existing_generation_directory() {
|
||||
transaction
|
||||
.execute_batch("DROP TABLE database_metadata_v2; PRAGMA user_version=1")
|
||||
.unwrap();
|
||||
// Convert the fixture's label tables back too: v1/v2 use TEXT, while
|
||||
// the freshly seeded service now uses schema-v3 UTF-16 BLOBs.
|
||||
transaction.execute_batch("ALTER TABLE object_stores RENAME TO object_stores_v3; ALTER TABLE indexes RENAME TO indexes_v3").unwrap();
|
||||
let labels = schema
|
||||
.split_once("CREATE TABLE object_stores")
|
||||
.unwrap()
|
||||
.1
|
||||
.split_once("CREATE TABLE records")
|
||||
.unwrap()
|
||||
.0;
|
||||
transaction
|
||||
.execute_batch(&format!("CREATE TABLE object_stores{labels}"))
|
||||
.unwrap();
|
||||
for (table, id_column, columns) in [
|
||||
(
|
||||
"object_stores",
|
||||
"store_id",
|
||||
"store_id,?1,key_path,auto_increment,generator",
|
||||
),
|
||||
(
|
||||
"indexes",
|
||||
"index_id",
|
||||
"index_id,store_id,?1,key_path,is_unique,multi_entry",
|
||||
),
|
||||
] {
|
||||
let mut statement = transaction
|
||||
.prepare(&format!("SELECT {id_column},name FROM {table}_v3"))
|
||||
.unwrap();
|
||||
let mut rows = statement.query([]).unwrap();
|
||||
while let Some(row) = rows.next().unwrap() {
|
||||
let id: i64 = row.get(0).unwrap();
|
||||
let raw: Vec<u8> = row.get(1).unwrap();
|
||||
let name = names::decode(&raw).unwrap();
|
||||
let name =
|
||||
String::from_utf16(name.as_utf16()).expect("legacy fixture names are Unicode");
|
||||
transaction
|
||||
.execute(
|
||||
&format!(
|
||||
"INSERT INTO {table} SELECT {columns} FROM {table}_v3 WHERE {id_column}=?2"
|
||||
),
|
||||
rusqlite::params![name, id],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
transaction
|
||||
.execute_batch("DROP TABLE indexes_v3; DROP TABLE object_stores_v3")
|
||||
.unwrap();
|
||||
transaction.commit().unwrap();
|
||||
drop(connection);
|
||||
let mut service = Service::on_disk(root, config()).unwrap();
|
||||
@@ -891,7 +939,7 @@ fn catalog_recovers_legacy_text_names_in_the_existing_generation_directory() {
|
||||
connection
|
||||
.pragma_query_value(None, "user_version", |row| row.get::<_, u32>(0))
|
||||
.unwrap(),
|
||||
2
|
||||
SCHEMA_VERSION
|
||||
);
|
||||
assert_eq!(
|
||||
connection
|
||||
|
||||
@@ -199,7 +199,8 @@ fn catalog_recovers_after_forced_process_exit() {
|
||||
metadata
|
||||
.stores
|
||||
.keys()
|
||||
.map(String::as_str)
|
||||
.map(|name| String::from_utf16(name.as_utf16())
|
||||
.expect("fixture names are Unicode"))
|
||||
.collect::<Vec<_>>(),
|
||||
["a", "b"]
|
||||
);
|
||||
|
||||
@@ -90,12 +90,22 @@ fn failed_schema_read_before_commit_rolls_back_and_allows_a_later_upgrade() {
|
||||
if fail {
|
||||
assert!(matches!(result.result, Err(IndexedDbError::Io(_))));
|
||||
assert!(Arc::ptr_eq(&original, &client.committed().unwrap()));
|
||||
assert!(!client.committed().unwrap().stores.contains_key("new"));
|
||||
assert!(
|
||||
!client
|
||||
.committed()
|
||||
.unwrap()
|
||||
.stores
|
||||
.contains_key(&crate::IndexedDbName::from("new"))
|
||||
);
|
||||
} else {
|
||||
result.result.unwrap();
|
||||
let committed = client.committed().unwrap();
|
||||
assert_eq!(committed.version, 2);
|
||||
assert!(committed.stores.contains_key("new"));
|
||||
assert!(
|
||||
committed
|
||||
.stores
|
||||
.contains_key(&crate::IndexedDbName::from("new"))
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,8 +215,10 @@ fn renamed_schema_and_data_survive_disk_restart_and_a_later_aborted_rename() {
|
||||
)
|
||||
.unwrap();
|
||||
let before = client.committed().unwrap();
|
||||
let store_id = before.stores["a"].id;
|
||||
let index_id = before.stores["a"].indexes["tag"].id;
|
||||
let store_id = before.stores[&crate::IndexedDbName::from("a")].id;
|
||||
let index_id = before.stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("tag")]
|
||||
.id;
|
||||
begin_upgrade(&client, 3);
|
||||
execute(
|
||||
&client,
|
||||
@@ -246,9 +248,14 @@ fn renamed_schema_and_data_survive_disk_restart_and_a_later_aborted_rename() {
|
||||
)
|
||||
.unwrap();
|
||||
let after = client.committed().unwrap();
|
||||
assert_eq!(after.stores["renamed\0😀"].id, store_id);
|
||||
assert_eq!(
|
||||
after.stores["renamed\0😀"].indexes["renamed\0索引"].id,
|
||||
after.stores[&crate::IndexedDbName::from("renamed\0😀")].id,
|
||||
store_id
|
||||
);
|
||||
assert_eq!(
|
||||
after.stores[&crate::IndexedDbName::from("renamed\0😀")].indexes
|
||||
[&crate::IndexedDbName::from("renamed\0索引")]
|
||||
.id,
|
||||
index_id
|
||||
);
|
||||
let generation = client.identity().generation;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
mod database_names;
|
||||
mod schema_names;
|
||||
mod schema_rename;
|
||||
mod storage_contracts;
|
||||
use super::*;
|
||||
@@ -431,13 +432,19 @@ fn explicit_key_advancement_and_generator_boundary_rollback_are_atomic() {
|
||||
put(&mut database, None, b"one", vec![], None).unwrap(),
|
||||
Key::from(1)
|
||||
);
|
||||
database
|
||||
.connection
|
||||
.execute(
|
||||
"UPDATE object_stores SET generator=?1 WHERE name='a'",
|
||||
[(MAX_AUTO_INCREMENT_KEY - 1) as i64],
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
database
|
||||
.connection
|
||||
.execute(
|
||||
"UPDATE object_stores SET generator=?1 WHERE name=?2",
|
||||
rusqlite::params![
|
||||
(MAX_AUTO_INCREMENT_KEY - 1) as i64,
|
||||
names::encode(&"a".into()).unwrap()
|
||||
],
|
||||
)
|
||||
.unwrap(),
|
||||
1
|
||||
);
|
||||
let request = database.prepare_write(TX, "a", None, false).unwrap();
|
||||
assert_eq!(request.key, Key::from(MAX_AUTO_INCREMENT_KEY as i64));
|
||||
database.cancel_write(TX, request.id).unwrap();
|
||||
|
||||
@@ -46,7 +46,7 @@ fn legacy_database(directory: &Directory) -> DatabaseIdentity {
|
||||
auto_increment: true,
|
||||
..Default::default()
|
||||
};
|
||||
let usage = identity.name.usage_bytes() + 8 + schema::store_usage("a", &options) + 12;
|
||||
let usage = identity.name.usage_bytes() + 8 + schema::store_usage(&"a".into(), &options) + 12;
|
||||
transaction.execute(
|
||||
"INSERT INTO database_metadata(singleton,name,generation,version,state,key_format,key_path_format,value_format,usage,next_store_id,next_record_id) VALUES(1,?1,?2,1,'live',1,1,1,?3,4,8)",
|
||||
rusqlite::params![String::from_utf16(identity.name.as_utf16()).unwrap(), identity.generation.as_slice(), usage as i64],
|
||||
@@ -120,8 +120,14 @@ fn legacy_name_migration_preserves_records_ids_generator_and_following_commits()
|
||||
.unwrap();
|
||||
let mut database = Database::open(connection, identity.clone()).unwrap();
|
||||
assert_eq!(database.committed().name, identity.name);
|
||||
assert_eq!(names::schema_version(&database.connection).unwrap(), 2);
|
||||
assert_eq!(database.committed().stores["a"].id, 3);
|
||||
assert_eq!(
|
||||
names::schema_version(&database.connection).unwrap(),
|
||||
SCHEMA_VERSION
|
||||
);
|
||||
assert_eq!(
|
||||
database.committed().stores[&crate::IndexedDbName::from("a")].id,
|
||||
3
|
||||
);
|
||||
begin(&mut database, TransactionMode::ReadWrite);
|
||||
assert_eq!(
|
||||
database
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
PRAGMA user_version = 2;
|
||||
|
||||
CREATE TABLE database_info (
|
||||
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
||||
backend TEXT NOT NULL CHECK (backend = 'indexeddb'),
|
||||
owner_key BLOB NOT NULL CHECK (length(owner_key) > 0),
|
||||
identity_format INTEGER NOT NULL CHECK (identity_format = 1)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE database_metadata (
|
||||
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
||||
name BLOB NOT NULL CHECK (length(name) % 2 = 0),
|
||||
generation BLOB NOT NULL CHECK (length(generation) = 16),
|
||||
version INTEGER NOT NULL CHECK (version >= 0),
|
||||
state TEXT NOT NULL CHECK (state IN ('creating', 'live')),
|
||||
key_format INTEGER NOT NULL CHECK (key_format = 1),
|
||||
key_path_format INTEGER NOT NULL CHECK (key_path_format = 1),
|
||||
value_format INTEGER NOT NULL CHECK (value_format = 1),
|
||||
usage INTEGER NOT NULL CHECK (usage >= 0),
|
||||
next_store_id INTEGER NOT NULL DEFAULT 1 CHECK (next_store_id > 0),
|
||||
next_index_id INTEGER NOT NULL DEFAULT 1 CHECK (next_index_id > 0),
|
||||
next_record_id INTEGER NOT NULL DEFAULT 1 CHECK (next_record_id > 0),
|
||||
next_object_id INTEGER NOT NULL DEFAULT 1 CHECK (next_object_id > 0),
|
||||
CHECK (
|
||||
(state = 'creating' AND version = 0)
|
||||
OR (state = 'live' AND version > 0)
|
||||
)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE object_stores (
|
||||
store_id INTEGER PRIMARY KEY CHECK (store_id > 0),
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
key_path BLOB,
|
||||
auto_increment INTEGER NOT NULL CHECK (auto_increment IN (0, 1)),
|
||||
generator INTEGER NOT NULL CHECK (generator BETWEEN 0 AND 9007199254740992)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE indexes (
|
||||
index_id INTEGER PRIMARY KEY CHECK (index_id > 0),
|
||||
store_id INTEGER NOT NULL CHECK (store_id > 0),
|
||||
name TEXT NOT NULL,
|
||||
key_path BLOB NOT NULL,
|
||||
is_unique INTEGER NOT NULL CHECK (is_unique IN (0, 1)),
|
||||
multi_entry INTEGER NOT NULL CHECK (multi_entry IN (0, 1)),
|
||||
UNIQUE (store_id, name)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE records (
|
||||
record_id INTEGER PRIMARY KEY CHECK (record_id > 0),
|
||||
store_id INTEGER NOT NULL CHECK (store_id > 0),
|
||||
key_sort BLOB NOT NULL CHECK (length(key_sort) > 0),
|
||||
key_value BLOB NOT NULL CHECK (length(key_value) > 0),
|
||||
wire_size INTEGER NOT NULL CHECK (wire_size >= 0),
|
||||
usage INTEGER NOT NULL CHECK (usage >= 0),
|
||||
UNIQUE (store_id, key_sort)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE record_chunks (
|
||||
record_id INTEGER NOT NULL CHECK (record_id > 0),
|
||||
chunk_no INTEGER NOT NULL CHECK (chunk_no >= 0),
|
||||
bytes BLOB NOT NULL CHECK (length(bytes) BETWEEN 1 AND 262144),
|
||||
PRIMARY KEY (record_id, chunk_no)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE index_entries (
|
||||
index_id INTEGER NOT NULL CHECK (index_id > 0),
|
||||
index_key_sort BLOB NOT NULL CHECK (length(index_key_sort) > 0),
|
||||
index_key_value BLOB NOT NULL CHECK (length(index_key_value) > 0),
|
||||
primary_key_sort BLOB NOT NULL CHECK (length(primary_key_sort) > 0),
|
||||
record_id INTEGER NOT NULL CHECK (record_id > 0),
|
||||
is_unique INTEGER NOT NULL CHECK (is_unique IN (0, 1)),
|
||||
PRIMARY KEY (index_id, index_key_sort, primary_key_sort)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE UNIQUE INDEX index_entries_unique
|
||||
ON index_entries(index_id, index_key_sort) WHERE is_unique = 1;
|
||||
CREATE INDEX index_entries_record ON index_entries(record_id);
|
||||
|
||||
CREATE TABLE external_objects (
|
||||
object_id INTEGER PRIMARY KEY CHECK (object_id > 0),
|
||||
kind TEXT NOT NULL CHECK (kind IN ('blob', 'file', 'file-system-handle')),
|
||||
size INTEGER NOT NULL CHECK (size >= 0),
|
||||
mime_type TEXT,
|
||||
file_name TEXT,
|
||||
last_modified_f64 BLOB CHECK (
|
||||
last_modified_f64 IS NULL OR length(last_modified_f64) = 8
|
||||
),
|
||||
handle_kind TEXT CHECK (handle_kind IN ('file', 'directory')),
|
||||
handle_bucket_kind TEXT CHECK (handle_bucket_kind IN ('default', 'named')),
|
||||
handle_bucket_id INTEGER CHECK (handle_bucket_id IS NULL OR handle_bucket_id > 0),
|
||||
CHECK (
|
||||
(kind = 'blob' AND mime_type IS NOT NULL
|
||||
AND file_name IS NULL AND last_modified_f64 IS NULL
|
||||
AND handle_kind IS NULL AND handle_bucket_kind IS NULL AND handle_bucket_id IS NULL)
|
||||
OR
|
||||
(kind = 'file' AND mime_type IS NOT NULL
|
||||
AND file_name IS NOT NULL AND last_modified_f64 IS NOT NULL
|
||||
AND handle_kind IS NULL AND handle_bucket_kind IS NULL AND handle_bucket_id IS NULL)
|
||||
OR
|
||||
(kind = 'file-system-handle' AND mime_type IS NULL
|
||||
AND file_name IS NULL AND last_modified_f64 IS NULL AND size = 0
|
||||
AND handle_kind IS NOT NULL AND handle_bucket_kind IS NOT NULL
|
||||
AND ((handle_bucket_kind = 'default' AND handle_bucket_id IS NULL)
|
||||
OR (handle_bucket_kind = 'named' AND handle_bucket_id IS NOT NULL)))
|
||||
)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE record_objects (
|
||||
record_id INTEGER NOT NULL CHECK (record_id > 0),
|
||||
ordinal INTEGER NOT NULL CHECK (ordinal >= 0),
|
||||
object_id INTEGER NOT NULL CHECK (object_id > 0),
|
||||
PRIMARY KEY (record_id, ordinal)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE INDEX record_objects_object ON record_objects(object_id);
|
||||
|
||||
CREATE TABLE blob_chunks (
|
||||
object_id INTEGER NOT NULL CHECK (object_id > 0),
|
||||
chunk_no INTEGER NOT NULL CHECK (chunk_no >= 0),
|
||||
bytes BLOB NOT NULL CHECK (length(bytes) BETWEEN 1 AND 262144),
|
||||
PRIMARY KEY (object_id, chunk_no)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
|
||||
CREATE TABLE handle_components (
|
||||
object_id INTEGER NOT NULL CHECK (object_id > 0),
|
||||
ordinal INTEGER NOT NULL CHECK (ordinal >= 0),
|
||||
name TEXT NOT NULL,
|
||||
PRIMARY KEY (object_id, ordinal)
|
||||
) STRICT, WITHOUT ROWID;
|
||||
@@ -0,0 +1,273 @@
|
||||
use super::*;
|
||||
use crate::IndexedDbName;
|
||||
|
||||
struct Directory(std::path::PathBuf);
|
||||
impl Directory {
|
||||
fn new() -> Self {
|
||||
let mut token = [0; 16];
|
||||
moli_crypto::fill_secure_random(&mut token).unwrap();
|
||||
let directory = Self(std::env::temp_dir().join(format!(
|
||||
"moli-idb-schema-names-{}",
|
||||
moli_crypto::sha256_hex(token)
|
||||
)));
|
||||
std::fs::create_dir(&directory.0).unwrap();
|
||||
directory
|
||||
}
|
||||
fn path(&self) -> std::path::PathBuf {
|
||||
self.0.join("database.sqlite")
|
||||
}
|
||||
}
|
||||
impl Drop for Directory {
|
||||
fn drop(&mut self) {
|
||||
let _ = std::fs::remove_dir_all(&self.0);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn lossless_store_and_index_names_preserve_separate_records_across_disk_restart() {
|
||||
let directory = Directory::new();
|
||||
let names = [
|
||||
vec![],
|
||||
vec![0],
|
||||
vec![0xd800],
|
||||
vec![0xd801],
|
||||
vec![0xdc00],
|
||||
vec![0xfffd],
|
||||
vec![0xdc00, 0xd800],
|
||||
vec![0xd83d, 0xde00],
|
||||
vec![0xe000],
|
||||
]
|
||||
.into_iter()
|
||||
.map(IndexedDbName::from_utf16)
|
||||
.collect::<Vec<_>>();
|
||||
let connection = moli_sqlite::open_disk(
|
||||
&directory.path(),
|
||||
moli_sqlite::OpenMode::Create,
|
||||
Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut database = Database::create(connection, identity()).unwrap();
|
||||
database
|
||||
.begin(TX, TransactionMode::VersionChange, BTreeSet::new(), Some(1))
|
||||
.unwrap();
|
||||
for (ordinal, name) in names.iter().enumerate() {
|
||||
database
|
||||
.create_object_store(
|
||||
TX,
|
||||
name,
|
||||
ObjectStoreOptions {
|
||||
auto_increment: true,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
for index in &names {
|
||||
database
|
||||
.create_index(
|
||||
TX,
|
||||
name,
|
||||
index,
|
||||
IndexOptions {
|
||||
key_path: KeyPath::String("label".into()),
|
||||
unique: true,
|
||||
multi_entry: false,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
database
|
||||
.write(
|
||||
TX,
|
||||
name,
|
||||
RecordWrite {
|
||||
key: None,
|
||||
value: vec![ordinal as u8].into(),
|
||||
indexes: names
|
||||
.iter()
|
||||
.map(|name| IndexKeys {
|
||||
name: name.clone(),
|
||||
keys: vec![(ordinal as i64).into()],
|
||||
})
|
||||
.collect(),
|
||||
add_only: true,
|
||||
quota: None,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
let original = database.commit(TX).unwrap();
|
||||
database.close().unwrap();
|
||||
let connection = moli_sqlite::open_disk(
|
||||
&directory.path(),
|
||||
moli_sqlite::OpenMode::Existing,
|
||||
Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut database = Database::open(connection, identity()).unwrap();
|
||||
assert_eq!(database.committed(), &original);
|
||||
database
|
||||
.begin(
|
||||
TX,
|
||||
TransactionMode::ReadOnly,
|
||||
names.iter().cloned().collect(),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
for (ordinal, name) in names.iter().enumerate() {
|
||||
for index in &names {
|
||||
let entries = database
|
||||
.query(
|
||||
TX,
|
||||
name,
|
||||
&Query {
|
||||
source: QuerySource::Index(index.clone()),
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(entries.len(), 1);
|
||||
assert_eq!(entries[0].key, (ordinal as i64).into());
|
||||
assert_eq!(
|
||||
entries[0].value.as_ref().unwrap().wire_bytes(),
|
||||
&[ordinal as u8]
|
||||
);
|
||||
}
|
||||
}
|
||||
database.commit(TX).unwrap();
|
||||
database
|
||||
.begin(TX, TransactionMode::VersionChange, BTreeSet::new(), Some(2))
|
||||
.unwrap();
|
||||
for (ordinal, name) in names.iter().enumerate() {
|
||||
let temporary = format!("temporary-{ordinal}");
|
||||
database
|
||||
.rename_object_store(TX, name, temporary.as_str())
|
||||
.unwrap();
|
||||
database
|
||||
.rename_index(TX, temporary.as_str(), name, "temporary-index")
|
||||
.unwrap();
|
||||
}
|
||||
database.abort(TX).unwrap();
|
||||
assert_eq!(database.committed(), &original);
|
||||
database.close().unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn v2_name_migration_rolls_back_all_ddl_on_a_malformed_label_and_can_retry() {
|
||||
let directory = Directory::new();
|
||||
let connection = moli_sqlite::open_disk(
|
||||
&directory.path(),
|
||||
moli_sqlite::OpenMode::Create,
|
||||
Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
connection
|
||||
.execute_batch(include_str!("fixtures/schema-v2.sql"))
|
||||
.unwrap();
|
||||
let identity = identity();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO database_info VALUES (1,'indexeddb',?1,1)",
|
||||
[&identity.owner_key],
|
||||
)
|
||||
.unwrap();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO database_metadata VALUES (1,?1,?2,1,'live',1,1,1,128,4,10,1,1)",
|
||||
rusqlite::params![
|
||||
names::encode(&identity.name).unwrap(),
|
||||
identity.generation.as_slice()
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
connection
|
||||
.execute("INSERT INTO object_stores VALUES (3,'legacy',NULL,1,7)", [])
|
||||
.unwrap();
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO indexes VALUES (9,3,CAST(X'FF' AS TEXT),?1,0,0)",
|
||||
[KeyPath::String("label".into()).encode_value().unwrap()],
|
||||
)
|
||||
.unwrap();
|
||||
connection.close().unwrap();
|
||||
let connection = moli_sqlite::open_disk(
|
||||
&directory.path(),
|
||||
moli_sqlite::OpenMode::Existing,
|
||||
Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(matches!(
|
||||
Database::open(connection, identity.clone()),
|
||||
Err(IndexedDbError::Corruption(_))
|
||||
));
|
||||
let connection = moli_sqlite::open_disk(
|
||||
&directory.path(),
|
||||
moli_sqlite::OpenMode::Existing,
|
||||
Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(names::schema_version(&connection).unwrap(), 2);
|
||||
assert_eq!(
|
||||
connection
|
||||
.query_row("SELECT typeof(name) FROM object_stores", [], |row| row
|
||||
.get::<_, String>(
|
||||
0
|
||||
))
|
||||
.unwrap(),
|
||||
"text"
|
||||
);
|
||||
assert_eq!(connection.query_row("SELECT count(*) FROM sqlite_schema WHERE name IN ('object_stores_v2','indexes_v2')", [], |row| row.get::<_,i64>(0)).unwrap(), 0);
|
||||
connection
|
||||
.execute("UPDATE indexes SET name='valid'", [])
|
||||
.unwrap();
|
||||
connection.close().unwrap();
|
||||
let connection = moli_sqlite::open_disk(
|
||||
&directory.path(),
|
||||
moli_sqlite::OpenMode::Existing,
|
||||
Default::default(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut database = Database::open(connection, identity).unwrap();
|
||||
assert_eq!(
|
||||
names::schema_version(&database.connection).unwrap(),
|
||||
SCHEMA_VERSION
|
||||
);
|
||||
assert_eq!(
|
||||
database.committed().stores[&IndexedDbName::from("legacy")].id,
|
||||
3
|
||||
);
|
||||
assert_eq!(
|
||||
database.committed().stores[&IndexedDbName::from("legacy")].indexes
|
||||
[&IndexedDbName::from("valid")]
|
||||
.id,
|
||||
9
|
||||
);
|
||||
database
|
||||
.begin(
|
||||
TX,
|
||||
TransactionMode::ReadWrite,
|
||||
BTreeSet::from(["legacy".into()]),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
database
|
||||
.write(
|
||||
TX,
|
||||
"legacy",
|
||||
RecordWrite {
|
||||
key: None,
|
||||
value: b"kept".to_vec().into(),
|
||||
indexes: vec![IndexKeys {
|
||||
name: "valid".into(),
|
||||
keys: vec![1.into()]
|
||||
}],
|
||||
add_only: true,
|
||||
quota: None,
|
||||
}
|
||||
)
|
||||
.unwrap(),
|
||||
8.into()
|
||||
);
|
||||
database.commit(TX).unwrap();
|
||||
database.close().unwrap();
|
||||
}
|
||||
@@ -44,8 +44,10 @@ fn upgrade(database: &mut Database) {
|
||||
fn schema_rename_keeps_ids_records_index_entries_and_key_generator() {
|
||||
let mut database = seeded();
|
||||
let before = database.committed().clone();
|
||||
let store_id = before.stores["a"].id;
|
||||
let index_id = before.stores["a"].indexes["by-tag"].id;
|
||||
let store_id = before.stores[&crate::IndexedDbName::from("a")].id;
|
||||
let index_id = before.stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("by-tag")]
|
||||
.id;
|
||||
upgrade(&mut database);
|
||||
database.rename_object_store(TX, "a", "renamed").unwrap();
|
||||
database
|
||||
@@ -57,9 +59,21 @@ fn schema_rename_keeps_ids_records_index_entries_and_key_generator() {
|
||||
"pending names are not published"
|
||||
);
|
||||
let candidate = database.candidate_metadata(TX).unwrap();
|
||||
assert!(!candidate.stores.contains_key("a"));
|
||||
assert_eq!(candidate.stores["renamed"].id, store_id);
|
||||
assert_eq!(candidate.stores["renamed"].indexes["new-tag"].id, index_id);
|
||||
assert!(
|
||||
!candidate
|
||||
.stores
|
||||
.contains_key(&crate::IndexedDbName::from("a"))
|
||||
);
|
||||
assert_eq!(
|
||||
candidate.stores[&crate::IndexedDbName::from("renamed")].id,
|
||||
store_id
|
||||
);
|
||||
assert_eq!(
|
||||
candidate.stores[&crate::IndexedDbName::from("renamed")].indexes
|
||||
[&crate::IndexedDbName::from("new-tag")]
|
||||
.id,
|
||||
index_id
|
||||
);
|
||||
assert_eq!(candidate.usage, before.usage + 7);
|
||||
let records = database
|
||||
.query(
|
||||
@@ -95,9 +109,14 @@ fn schema_rename_keeps_ids_records_index_entries_and_key_generator() {
|
||||
2.into()
|
||||
);
|
||||
database.commit(TX).unwrap();
|
||||
assert_eq!(database.committed().stores["renamed"].id, store_id);
|
||||
assert_eq!(
|
||||
database.committed().stores["renamed"].indexes["new-tag"].id,
|
||||
database.committed().stores[&crate::IndexedDbName::from("renamed")].id,
|
||||
store_id
|
||||
);
|
||||
assert_eq!(
|
||||
database.committed().stores[&crate::IndexedDbName::from("renamed")].indexes
|
||||
[&crate::IndexedDbName::from("new-tag")]
|
||||
.id,
|
||||
index_id
|
||||
);
|
||||
}
|
||||
@@ -106,8 +125,10 @@ fn schema_rename_keeps_ids_records_index_entries_and_key_generator() {
|
||||
fn schema_rename_constraint_errors_leave_the_upgrade_usable() {
|
||||
let mut database = seeded();
|
||||
let before = database.committed().clone();
|
||||
let store_id = before.stores["a"].id;
|
||||
let index_id = before.stores["a"].indexes["by-tag"].id;
|
||||
let store_id = before.stores[&crate::IndexedDbName::from("a")].id;
|
||||
let index_id = before.stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("by-tag")]
|
||||
.id;
|
||||
upgrade(&mut database);
|
||||
let sibling = database
|
||||
.create_index(
|
||||
@@ -138,9 +159,22 @@ fn schema_rename_constraint_errors_leave_the_upgrade_usable() {
|
||||
Err(IndexedDbError::NotFound(_))
|
||||
));
|
||||
let metadata = database.candidate_metadata(TX).unwrap();
|
||||
assert_eq!(metadata.stores["a"].id, store_id);
|
||||
assert_eq!(metadata.stores["a"].indexes["by-tag"].id, index_id);
|
||||
assert_eq!(metadata.stores["a"].indexes["other"].id, sibling.id);
|
||||
assert_eq!(
|
||||
metadata.stores[&crate::IndexedDbName::from("a")].id,
|
||||
store_id
|
||||
);
|
||||
assert_eq!(
|
||||
metadata.stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("by-tag")]
|
||||
.id,
|
||||
index_id
|
||||
);
|
||||
assert_eq!(
|
||||
metadata.stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("other")]
|
||||
.id,
|
||||
sibling.id
|
||||
);
|
||||
// Empty names are valid, and a same-name assignment leaves accounting unchanged.
|
||||
database.rename_object_store(TX, "a", "").unwrap();
|
||||
database.rename_index(TX, "", "by-tag", "").unwrap();
|
||||
@@ -149,8 +183,16 @@ fn schema_rename_constraint_errors_leave_the_upgrade_usable() {
|
||||
database.rename_index(TX, "", "", "").unwrap();
|
||||
assert_eq!(database.candidate_metadata(TX).unwrap().usage, usage);
|
||||
database.commit(TX).unwrap();
|
||||
assert_eq!(database.committed().stores[""].id, store_id);
|
||||
assert_eq!(database.committed().stores[""].indexes[""].id, index_id);
|
||||
assert_eq!(
|
||||
database.committed().stores[&crate::IndexedDbName::from("")].id,
|
||||
store_id
|
||||
);
|
||||
assert_eq!(
|
||||
database.committed().stores[&crate::IndexedDbName::from("")].indexes
|
||||
[&crate::IndexedDbName::from("")]
|
||||
.id,
|
||||
index_id
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -197,14 +239,14 @@ fn schema_rename_abort_and_commit_quota_failure_restore_the_original_names() {
|
||||
#[test]
|
||||
fn schema_rename_and_reusing_the_old_name_preserve_separate_storage_identities() {
|
||||
let mut database = seeded();
|
||||
let original = database.committed().stores["a"].id;
|
||||
let original = database.committed().stores[&crate::IndexedDbName::from("a")].id;
|
||||
upgrade(&mut database);
|
||||
database.rename_object_store(TX, "a", "kept").unwrap();
|
||||
database
|
||||
.create_object_store(TX, "a", ObjectStoreOptions::default())
|
||||
.unwrap();
|
||||
assert_ne!(
|
||||
database.candidate_metadata(TX).unwrap().stores["a"].id,
|
||||
database.candidate_metadata(TX).unwrap().stores[&crate::IndexedDbName::from("a")].id,
|
||||
original
|
||||
);
|
||||
assert_eq!(database.count(TX, "a").unwrap(), 0);
|
||||
@@ -219,7 +261,10 @@ fn schema_rename_and_reusing_the_old_name_preserve_separate_storage_identities()
|
||||
b"kept"
|
||||
);
|
||||
database.commit(TX).unwrap();
|
||||
assert_eq!(database.committed().stores["kept"].id, original);
|
||||
assert_eq!(
|
||||
database.committed().stores[&crate::IndexedDbName::from("kept")].id,
|
||||
original
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -250,7 +295,9 @@ fn schema_rename_requires_an_active_versionchange_transaction() {
|
||||
#[test]
|
||||
fn renaming_an_index_and_reusing_its_old_name_keeps_entries_separate() {
|
||||
let mut database = seeded();
|
||||
let original = database.committed().stores["a"].indexes["by-tag"].id;
|
||||
let original = database.committed().stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("by-tag")]
|
||||
.id;
|
||||
upgrade(&mut database);
|
||||
database.rename_index(TX, "a", "by-tag", "kept").unwrap();
|
||||
let replacement = database
|
||||
@@ -306,7 +353,9 @@ fn renaming_an_index_and_reusing_its_old_name_keeps_entries_separate() {
|
||||
);
|
||||
database.commit(TX).unwrap();
|
||||
assert_eq!(
|
||||
database.committed().stores["a"].indexes["kept"].id,
|
||||
database.committed().stores[&crate::IndexedDbName::from("a")].indexes
|
||||
[&crate::IndexedDbName::from("kept")]
|
||||
.id,
|
||||
original
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,9 +60,9 @@ fn schema_uses_eleven_strict_without_rowid_tables_and_explicit_ids() {
|
||||
i64::from(crate::sqlite::SCHEMA_VERSION)
|
||||
);
|
||||
for statement in [
|
||||
"INSERT INTO object_stores(name, auto_increment, generator) VALUES ('store', 0, 0)",
|
||||
"INSERT INTO object_stores(name, auto_increment, generator) VALUES (X'00730074006f00720065', 0, 0)",
|
||||
"INSERT INTO indexes(store_id, name, key_path, is_unique, multi_entry)
|
||||
VALUES (1, 'index', X'00', 0, 0)",
|
||||
VALUES (1, X'0069006e006400650078', X'00', 0, 0)",
|
||||
"INSERT INTO records(store_id, key_sort, key_value, wire_size, usage)
|
||||
VALUES (1, X'10', X'10', 0, 0)",
|
||||
"INSERT INTO external_objects(kind, size, mime_type) VALUES ('blob', 0, '')",
|
||||
@@ -87,7 +87,7 @@ fn schema_preserves_inclusive_generator_boundary_and_metadata_state() {
|
||||
for generator in [0_i64, 9_007_199_254_740_991, 9_007_199_254_740_992] {
|
||||
connection
|
||||
.execute(
|
||||
"INSERT INTO object_stores VALUES (1, 'store', NULL, 1, ?1)
|
||||
"INSERT INTO object_stores VALUES (1, X'00730074006f00720065', NULL, 1, ?1)
|
||||
ON CONFLICT(store_id) DO UPDATE SET generator = excluded.generator",
|
||||
[generator],
|
||||
)
|
||||
|
||||
@@ -36,7 +36,7 @@ struct QueueState {
|
||||
}
|
||||
|
||||
struct TransactionScope {
|
||||
stores: BTreeSet<String>,
|
||||
stores: BTreeSet<crate::IndexedDbName>,
|
||||
mode: TransactionMode,
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ impl TransactionRequestQueues {
|
||||
self: &Arc<Self>,
|
||||
key: (String, String),
|
||||
database: DatabaseHandle,
|
||||
stores: BTreeSet<String>,
|
||||
stores: BTreeSet<crate::IndexedDbName>,
|
||||
mode: TransactionMode,
|
||||
wake: TransactionRequestWake,
|
||||
) -> TransactionRequestLease {
|
||||
@@ -247,7 +247,7 @@ impl TransactionRequestHandle {
|
||||
pub fn database(&self) -> DatabaseHandle {
|
||||
self.database
|
||||
}
|
||||
pub fn store_names(&self) -> Vec<String> {
|
||||
pub fn store_names(&self) -> Vec<crate::IndexedDbName> {
|
||||
self.request.scope.stores.iter().cloned().collect()
|
||||
}
|
||||
pub fn mode(&self) -> TransactionMode {
|
||||
|
||||
@@ -63,7 +63,7 @@ pub struct IndexOptions {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct IndexInfo {
|
||||
pub name: String,
|
||||
pub name: crate::IndexedDbName,
|
||||
pub key_path: KeyPath,
|
||||
pub unique: bool,
|
||||
pub multi_entry: bool,
|
||||
@@ -79,10 +79,10 @@ pub enum TransactionMode {
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ObjectStoreInfo {
|
||||
pub name: String,
|
||||
pub name: crate::IndexedDbName,
|
||||
pub key_path: Option<KeyPath>,
|
||||
pub auto_increment: bool,
|
||||
pub index_names: Vec<String>,
|
||||
pub index_names: Vec<crate::IndexedDbName>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
|
||||
@@ -16,9 +16,9 @@ use super::{
|
||||
register_indexed_db_wrapper_with_owner, remove_indexed_db_database_index_metadata,
|
||||
remove_indexed_db_database_store_metadata, replace_indexed_db_database_metadata,
|
||||
replace_indexed_db_runtime_array, set_indexed_db_database_index_metadata,
|
||||
set_indexed_db_database_store_metadata, set_indexed_db_internal_object_property,
|
||||
set_indexed_db_object_store_metadata, set_indexed_db_request_surface_value,
|
||||
set_indexed_db_slot_value, v8_string, v8str,
|
||||
set_indexed_db_database_store_metadata, set_indexed_db_internal_name_property,
|
||||
set_indexed_db_internal_object_property, set_indexed_db_object_store_metadata,
|
||||
set_indexed_db_request_surface_value, set_indexed_db_slot_value, v8_string, v8str,
|
||||
};
|
||||
use std::collections::BTreeSet;
|
||||
use v8::{ValueDeserializerHelper, ValueSerializerHelper};
|
||||
|
||||
+17
-11
@@ -40,16 +40,23 @@ struct DomStringListItemArgs {
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "DOMStringList.contains")]
|
||||
struct DomStringListContainsArgs {
|
||||
#[webidl(required, name = "string")]
|
||||
expected: String,
|
||||
#[webidl(required, name = "string", converter = "raw")]
|
||||
expected: webidl::DomString16,
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn new_idb_dom_string_list<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
values: &[String],
|
||||
values: &[moli_indexeddb::IndexedDbName],
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
let values =
|
||||
crate::util::serialize_v8_array(scope, values).unwrap_or_else(|| v8::Array::new(scope, 0));
|
||||
let elements = values
|
||||
.iter()
|
||||
.map(|name| {
|
||||
crate::util::v8_string_from_utf16_units(scope, name.as_utf16())
|
||||
.expect("IndexedDB name fits in a V8 string")
|
||||
.into()
|
||||
})
|
||||
.collect::<Vec<v8::Local<v8::Value>>>();
|
||||
let values = v8::Array::new_with_elements(scope, &elements);
|
||||
let template = v8::ObjectTemplate::new(scope);
|
||||
template.set_indexed_property_handler(
|
||||
v8::IndexedPropertyHandlerConfiguration::new()
|
||||
@@ -71,10 +78,10 @@ pub(in crate::context_bootstrap::indexed_db) fn new_idb_dom_string_list<'s>(
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn new_idb_name_list<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
names: &[String],
|
||||
names: &[moli_indexeddb::IndexedDbName],
|
||||
) -> v8::Local<'s, v8::Object> {
|
||||
let mut names = names.to_vec();
|
||||
names.sort_by(|left, right| left.encode_utf16().cmp(right.encode_utf16()));
|
||||
names.sort();
|
||||
new_idb_dom_string_list(scope, &names)
|
||||
}
|
||||
|
||||
@@ -136,10 +143,9 @@ fn dom_string_list_contains_callback<'s>(
|
||||
let Some(value) = values.get_index(scope, index) else {
|
||||
continue;
|
||||
};
|
||||
if value
|
||||
.to_string(scope)
|
||||
.is_some_and(|value| value.to_rust_string_lossy(scope) == parsed.expected)
|
||||
{
|
||||
if value.to_string(scope).is_some_and(|value| {
|
||||
crate::util::v8_string_to_u16_string(scope, value).as_slice() == parsed.expected.0
|
||||
}) {
|
||||
rv.set_bool(true);
|
||||
return;
|
||||
}
|
||||
|
||||
+1
-1
@@ -61,7 +61,7 @@ pub(in crate::context_bootstrap::indexed_db) fn create_transaction_object<'s>(
|
||||
db: v8::Local<'s, v8::Object>,
|
||||
handle: Option<TransactionHandle>,
|
||||
mode: TransactionMode,
|
||||
store_names: &[String],
|
||||
store_names: &[moli_indexeddb::IndexedDbName],
|
||||
) -> Option<v8::Local<'s, v8::Object>> {
|
||||
let mode_label: &'static str = mode.into();
|
||||
let object_store_names = new_idb_name_list(scope, store_names);
|
||||
|
||||
@@ -32,7 +32,7 @@ pub(in crate::context_bootstrap::indexed_db) fn set_database_store_metadata<'s>(
|
||||
pub(in crate::context_bootstrap::indexed_db) fn remove_database_store_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
remove_indexed_db_database_store_metadata(scope, database, store_name)?;
|
||||
sync_indexed_db_store_handles(scope, database, store_name);
|
||||
|
||||
+20
-11
@@ -8,8 +8,8 @@ use moli_webapi_declare::WebApiObject;
|
||||
data_properties,
|
||||
enumerable
|
||||
)]
|
||||
struct IndexDescriptorDeclaration<'scope, 'value> {
|
||||
name: &'value str,
|
||||
struct IndexDescriptorDeclaration<'scope> {
|
||||
name: v8::Local<'scope, v8::String>,
|
||||
key_path: v8::Local<'scope, v8::Value>,
|
||||
unique: bool,
|
||||
multi_entry: bool,
|
||||
@@ -22,8 +22,8 @@ struct IndexDescriptorDeclaration<'scope, 'value> {
|
||||
data_properties,
|
||||
enumerable
|
||||
)]
|
||||
struct ObjectStoreDescriptorDeclaration<'scope, 'value> {
|
||||
name: &'value str,
|
||||
struct ObjectStoreDescriptorDeclaration<'scope> {
|
||||
name: v8::Local<'scope, v8::String>,
|
||||
key_path: v8::Local<'scope, v8::Value>,
|
||||
auto_increment: bool,
|
||||
}
|
||||
@@ -34,9 +34,14 @@ pub(in crate::context_bootstrap::indexed_db) fn create_index_descriptor_object<'
|
||||
) -> Option<v8::Local<'s, v8::Object>> {
|
||||
let key_path = key_path_to_js_value(scope, &info.key_path)?;
|
||||
let descriptor = new_null_prototype_object(scope);
|
||||
IndexDescriptorDeclaration::new(&info.name, key_path, info.unique, info.multi_entry)
|
||||
.initialize(scope, descriptor)
|
||||
.ok()?;
|
||||
IndexDescriptorDeclaration::new(
|
||||
crate::util::v8_string_from_utf16_units(scope, info.name.as_utf16())?,
|
||||
key_path,
|
||||
info.unique,
|
||||
info.multi_entry,
|
||||
)
|
||||
.initialize(scope, descriptor)
|
||||
.ok()?;
|
||||
Some(descriptor)
|
||||
}
|
||||
|
||||
@@ -50,9 +55,13 @@ pub(in crate::context_bootstrap::indexed_db) fn create_object_store_descriptor_o
|
||||
None => v8::null(scope).into(),
|
||||
};
|
||||
let descriptor = new_null_prototype_object(scope);
|
||||
ObjectStoreDescriptorDeclaration::new(&info.name, key_path, info.auto_increment)
|
||||
.initialize(scope, descriptor)
|
||||
.ok()?;
|
||||
ObjectStoreDescriptorDeclaration::new(
|
||||
crate::util::v8_string_from_utf16_units(scope, info.name.as_utf16())?,
|
||||
key_path,
|
||||
info.auto_increment,
|
||||
)
|
||||
.initialize(scope, descriptor)
|
||||
.ok()?;
|
||||
let index_names_list = new_idb_name_list(scope, metadata.index_names());
|
||||
set_indexed_db_internal_object_property(
|
||||
scope,
|
||||
@@ -66,7 +75,7 @@ pub(in crate::context_bootstrap::indexed_db) fn create_object_store_descriptor_o
|
||||
continue;
|
||||
};
|
||||
let descriptor_value = create_index_descriptor_object(scope, index)?;
|
||||
set_indexed_db_internal_object_property(
|
||||
set_indexed_db_internal_name_property(
|
||||
scope,
|
||||
index_map,
|
||||
&index.name,
|
||||
|
||||
+4
-4
@@ -9,8 +9,8 @@ use moli_webapi_declare::WebApiObject;
|
||||
data_properties,
|
||||
enumerable
|
||||
)]
|
||||
struct IdbObjectStoreSurfaceDeclaration<'scope, 'value> {
|
||||
name: &'value str,
|
||||
struct IdbObjectStoreSurfaceDeclaration<'scope> {
|
||||
name: v8::Local<'scope, v8::String>,
|
||||
key_path: v8::Local<'scope, v8::Value>,
|
||||
auto_increment: bool,
|
||||
index_names: v8::Local<'scope, v8::Object>,
|
||||
@@ -44,7 +44,7 @@ pub(in crate::context_bootstrap::indexed_db) fn sync_store_surface_fields<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
store: v8::Local<'s, v8::Object>,
|
||||
info: &ObjectStoreInfo,
|
||||
names: &[String],
|
||||
names: &[moli_indexeddb::IndexedDbName],
|
||||
) -> Option<()> {
|
||||
let key_path_value = match &info.key_path {
|
||||
Some(value) => key_path_to_js_value(scope, value)?,
|
||||
@@ -52,7 +52,7 @@ pub(in crate::context_bootstrap::indexed_db) fn sync_store_surface_fields<'s>(
|
||||
};
|
||||
let index_names = new_idb_name_list(scope, names);
|
||||
IdbObjectStoreSurfaceDeclaration::new(
|
||||
&info.name,
|
||||
crate::util::v8_string_from_utf16_units(scope, info.name.as_utf16())?,
|
||||
key_path_value,
|
||||
info.auto_increment,
|
||||
index_names,
|
||||
|
||||
@@ -6,7 +6,7 @@ use crate::context_bootstrap::indexed_db::{
|
||||
pub(in crate::context_bootstrap::indexed_db) fn index_info_from_store_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
store: v8::Local<'s, v8::Object>,
|
||||
index_name: &str,
|
||||
index_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Option<std::rc::Rc<IndexInfo>> {
|
||||
indexed_db_object_store_metadata(scope, store)?.shared_index(index_name)
|
||||
}
|
||||
@@ -14,7 +14,7 @@ pub(in crate::context_bootstrap::indexed_db) fn index_info_from_store_metadata<'
|
||||
pub(in crate::context_bootstrap::indexed_db) fn set_database_index_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
info: &IndexInfo,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
set_indexed_db_database_index_metadata(scope, database, store_name, info)?;
|
||||
@@ -25,8 +25,8 @@ pub(in crate::context_bootstrap::indexed_db) fn set_database_index_metadata<'s>(
|
||||
pub(in crate::context_bootstrap::indexed_db) fn remove_database_index_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
index_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
index_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
remove_indexed_db_database_index_metadata(scope, database, store_name, index_name)?;
|
||||
mark_indexed_db_index_handles_deleted(scope, database, store_name, index_name);
|
||||
|
||||
@@ -27,7 +27,7 @@ pub(in crate::context_bootstrap::indexed_db) fn refresh_database_surface<'s>(
|
||||
pub(in crate::context_bootstrap::indexed_db) fn object_store_info_from_database_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Option<ObjectStoreInfo> {
|
||||
indexed_db_database_store_metadata(scope, database, store_name)
|
||||
.map(|metadata| metadata.info().clone())
|
||||
|
||||
@@ -10,8 +10,8 @@ use moli_webapi_declare::WebApiObject;
|
||||
data_properties,
|
||||
enumerable
|
||||
)]
|
||||
struct IdbIndexObjectDeclaration<'scope, 'value> {
|
||||
name: &'value str,
|
||||
struct IdbIndexObjectDeclaration<'scope> {
|
||||
name: v8::Local<'scope, v8::String>,
|
||||
key_path: v8::Local<'scope, v8::Value>,
|
||||
unique: bool,
|
||||
multi_entry: bool,
|
||||
@@ -45,7 +45,7 @@ pub(in crate::context_bootstrap::indexed_db) fn create_failed_index_object<'s>(
|
||||
) -> Option<v8::Local<'s, v8::Object>> {
|
||||
let key_path_value = key_path_to_js_value(scope, &info.key_path)?;
|
||||
IdbIndexObjectDeclaration::new(
|
||||
&info.name,
|
||||
crate::util::v8_string_from_utf16_units(scope, info.name.as_utf16())?,
|
||||
key_path_value,
|
||||
info.unique,
|
||||
info.multi_entry,
|
||||
|
||||
@@ -133,7 +133,7 @@ pub(in crate::context_bootstrap::indexed_db) fn cursor_source_is_index<'s>(
|
||||
pub(in crate::context_bootstrap::indexed_db) struct CursorMutation<'s> {
|
||||
pub(super) transaction: v8::Local<'s, v8::Object>,
|
||||
pub(super) store: v8::Local<'s, v8::Object>,
|
||||
pub(super) store_name: String,
|
||||
pub(super) store_name: moli_indexeddb::IndexedDbName,
|
||||
pub(super) primary_key: Key,
|
||||
}
|
||||
|
||||
|
||||
@@ -11,8 +11,8 @@ use moli_indexeddb::{ObjectStoreOptionsValidationError, validate_object_store_op
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "IDBDatabase.createObjectStore")]
|
||||
struct IdbDatabaseCreateObjectStoreArgs {
|
||||
#[webidl(required)]
|
||||
name: String,
|
||||
#[webidl(required, converter = "raw")]
|
||||
name: webidl::DomString16,
|
||||
#[webidl(index = 1, dictionary)]
|
||||
options: IdbObjectStoreParameters,
|
||||
}
|
||||
@@ -34,7 +34,7 @@ pub(in crate::context_bootstrap::indexed_db) fn idb_database_create_object_store
|
||||
let Some(parsed) = webidl::parse_args::<IdbDatabaseCreateObjectStoreArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let name = parsed.name;
|
||||
let name = moli_indexeddb::IndexedDbName::from_utf16(parsed.name.0);
|
||||
let key_path = parsed.options.key_path;
|
||||
let auto_increment = parsed.options.auto_increment;
|
||||
let database = args.this();
|
||||
|
||||
@@ -10,8 +10,8 @@ use moli_indexeddb::sqlite::service::Command;
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "IDBDatabase.deleteObjectStore")]
|
||||
struct IdbDatabaseDeleteObjectStoreArgs {
|
||||
#[webidl(required)]
|
||||
name: String,
|
||||
#[webidl(required, converter = "raw")]
|
||||
name: webidl::DomString16,
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn idb_database_delete_object_store_callback<'s>(
|
||||
@@ -22,7 +22,7 @@ pub(in crate::context_bootstrap::indexed_db) fn idb_database_delete_object_store
|
||||
let Some(parsed) = webidl::parse_args::<IdbDatabaseDeleteObjectStoreArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let name = parsed.name;
|
||||
let name = moli_indexeddb::IndexedDbName::from_utf16(parsed.name.0);
|
||||
let database = args.this();
|
||||
let Some(transaction) = version_change_transaction(scope, database) else {
|
||||
return;
|
||||
|
||||
@@ -17,7 +17,7 @@ use moli_indexeddb::sqlite::{
|
||||
|
||||
struct Backfill {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: moli_indexeddb::IndexedDbName,
|
||||
index: IndexInfo,
|
||||
}
|
||||
|
||||
@@ -182,7 +182,7 @@ pub(in crate::context_bootstrap::indexed_db) fn schedule_schema_operation<'s>(
|
||||
transaction: v8::Local<'s, v8::Object>,
|
||||
client: DatabaseClient,
|
||||
command: Command,
|
||||
backfill: Option<(String, IndexInfo)>,
|
||||
backfill: Option<(moli_indexeddb::IndexedDbName, IndexInfo)>,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
let handle = crate::context_bootstrap::indexed_db::transaction_handle_from_value(
|
||||
scope,
|
||||
|
||||
@@ -124,7 +124,7 @@ fn create_regular_transaction<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
handle: DatabaseHandle,
|
||||
store_names: &[String],
|
||||
store_names: &[moli_indexeddb::IndexedDbName],
|
||||
mode: TransactionMode,
|
||||
) -> Result<Option<v8::Local<'s, v8::Object>>, IndexedDbError> {
|
||||
// Objects and task queues belong to the connection's realm even when the
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::webidl;
|
||||
|
||||
pub(super) struct TransactionStoreNames(pub(super) Vec<String>);
|
||||
pub(super) struct TransactionStoreNames(pub(super) Vec<moli_indexeddb::IndexedDbName>);
|
||||
|
||||
impl<'s> webidl::WebIdlConverter<'s> for TransactionStoreNames {
|
||||
type Options = ();
|
||||
@@ -11,15 +11,21 @@ impl<'s> webidl::WebIdlConverter<'s> for TransactionStoreNames {
|
||||
context: webidl::Context,
|
||||
_options: &Self::Options,
|
||||
) -> Result<Self, webidl::WebIdlError> {
|
||||
if let Some(names) = webidl::convert_optional_sequence::<webidl::DomString>(
|
||||
if let Some(names) = webidl::convert_optional_sequence::<webidl::DomString16>(
|
||||
scope,
|
||||
value,
|
||||
context,
|
||||
&webidl::StringOptions::default(),
|
||||
)? {
|
||||
return Ok(Self(names.0.into_iter().map(Into::into).collect()));
|
||||
return Ok(Self(
|
||||
names
|
||||
.0
|
||||
.into_iter()
|
||||
.map(|name| moli_indexeddb::IndexedDbName::from_utf16(name.0))
|
||||
.collect(),
|
||||
));
|
||||
}
|
||||
webidl::convert::<webidl::DomString>(scope, value, context)
|
||||
.map(|value| Self(vec![value.into()]))
|
||||
webidl::convert::<webidl::DomString16>(scope, value, context)
|
||||
.map(|value| Self(vec![moli_indexeddb::IndexedDbName::from_utf16(value.0)]))
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -4,8 +4,8 @@ use crate::webidl;
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "IDBTransaction.objectStore")]
|
||||
struct IdbTransactionObjectStoreArgs {
|
||||
#[webidl(required)]
|
||||
name: String,
|
||||
#[webidl(required, converter = "raw")]
|
||||
name: webidl::DomString16,
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn idb_transaction_object_store_callback<'s>(
|
||||
@@ -30,7 +30,7 @@ pub(in crate::context_bootstrap::indexed_db) fn idb_transaction_object_store_cal
|
||||
scope.throw_exception(error);
|
||||
return;
|
||||
}
|
||||
let name = parsed.name;
|
||||
let name = moli_indexeddb::IndexedDbName::from_utf16(parsed.name.0);
|
||||
let Some(database) =
|
||||
crate::context_bootstrap::indexed_db::indexed_db_transaction_database(scope, transaction)
|
||||
else {
|
||||
|
||||
@@ -357,3 +357,15 @@ pub(crate) fn flush_indexed_db_transaction_starts(scope: &mut v8::PinScope<'_, '
|
||||
enqueue_ready_transaction_starts(scope);
|
||||
}
|
||||
pub(crate) use self::typed_state::retire_indexed_db_context;
|
||||
|
||||
fn set_indexed_db_internal_name_property(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
object: v8::Local<'_, v8::Object>,
|
||||
key: &IndexedDbName,
|
||||
value: v8::Local<'_, v8::Value>,
|
||||
) {
|
||||
let Some(key) = crate::util::v8_string_from_utf16_units(scope, key.as_utf16()) else {
|
||||
return;
|
||||
};
|
||||
let _ = object.define_own_property(scope, key.into(), value, v8::PropertyAttribute::NONE);
|
||||
}
|
||||
|
||||
@@ -6,7 +6,11 @@ use crate::context_bootstrap::indexed_db::{
|
||||
pub(in crate::context_bootstrap::indexed_db) fn index_operation_state<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
index: v8::Local<'s, v8::Object>,
|
||||
) -> Option<(v8::Local<'s, v8::Object>, String, IndexInfo)> {
|
||||
) -> Option<(
|
||||
v8::Local<'s, v8::Object>,
|
||||
moli_indexeddb::IndexedDbName,
|
||||
IndexInfo,
|
||||
)> {
|
||||
if indexed_db_index_is_deleted(scope, index) {
|
||||
let error = dom_exception_value(scope, "The index has been deleted.", "InvalidStateError");
|
||||
scope.throw_exception(error);
|
||||
|
||||
@@ -42,7 +42,7 @@ pub(in crate::context_bootstrap::indexed_db) fn create_store_request<'s>(
|
||||
pub(in crate::context_bootstrap::indexed_db) fn object_store_operation_state<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
store: v8::Local<'s, v8::Object>,
|
||||
) -> Option<(v8::Local<'s, v8::Object>, String)> {
|
||||
) -> Option<(v8::Local<'s, v8::Object>, moli_indexeddb::IndexedDbName)> {
|
||||
let transaction = object_store_active_transaction(scope, store)?;
|
||||
let name = indexed_db_object_store_name(scope, store)?;
|
||||
Some((transaction, name))
|
||||
|
||||
+1
-1
@@ -10,7 +10,7 @@ pub(in crate::context_bootstrap::indexed_db) fn object_store_versionchange_commo
|
||||
v8::Local<'s, v8::Object>,
|
||||
v8::Local<'s, v8::Object>,
|
||||
TransactionHandle,
|
||||
String,
|
||||
moli_indexeddb::IndexedDbName,
|
||||
),
|
||||
IndexedDbError,
|
||||
> {
|
||||
|
||||
+3
-3
@@ -10,8 +10,8 @@ use moli_indexeddb::{IndexOptionsValidationError, validate_index_options};
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "IDBObjectStore.createIndex")]
|
||||
struct IdbObjectStoreCreateIndexArgs {
|
||||
#[webidl(required)]
|
||||
index_name: String,
|
||||
#[webidl(required, converter = "raw")]
|
||||
index_name: webidl::DomString16,
|
||||
#[webidl(name = "keyPath", with = parse_create_index_key_path_arg)]
|
||||
key_path: KeyPath,
|
||||
#[webidl(index = 2, dictionary)]
|
||||
@@ -35,7 +35,7 @@ pub(in crate::context_bootstrap::indexed_db) fn idb_object_store_create_index_ca
|
||||
let Some(parsed) = webidl::parse_args::<IdbObjectStoreCreateIndexArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let index_name = parsed.index_name;
|
||||
let index_name = moli_indexeddb::IndexedDbName::from_utf16(parsed.index_name.0);
|
||||
let key_path = parsed.key_path;
|
||||
let unique = parsed.options.unique;
|
||||
let multi_entry = parsed.options.multi_entry;
|
||||
|
||||
+3
-3
@@ -9,8 +9,8 @@ use moli_indexeddb::sqlite::service::Command;
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "IDBObjectStore.deleteIndex")]
|
||||
struct IdbObjectStoreDeleteIndexArgs {
|
||||
#[webidl(required)]
|
||||
index_name: String,
|
||||
#[webidl(required, converter = "raw")]
|
||||
index_name: webidl::DomString16,
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn idb_object_store_delete_index_callback<'s>(
|
||||
@@ -21,7 +21,7 @@ pub(in crate::context_bootstrap::indexed_db) fn idb_object_store_delete_index_ca
|
||||
let Some(parsed) = webidl::parse_args::<IdbObjectStoreDeleteIndexArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let index_name = parsed.index_name;
|
||||
let index_name = moli_indexeddb::IndexedDbName::from_utf16(parsed.index_name.0);
|
||||
let store = args.this();
|
||||
let (transaction, database, handle, store_name) =
|
||||
match object_store_versionchange_common(scope, store) {
|
||||
|
||||
+3
-3
@@ -8,8 +8,8 @@ use crate::webidl;
|
||||
#[derive(webidl::WebIdlArgs)]
|
||||
#[webidl(prefix = "IDBObjectStore.index")]
|
||||
struct IdbObjectStoreIndexArgs {
|
||||
#[webidl(required)]
|
||||
index_name: String,
|
||||
#[webidl(required, converter = "raw")]
|
||||
index_name: webidl::DomString16,
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn idb_object_store_index_callback<'s>(
|
||||
@@ -20,7 +20,7 @@ pub(in crate::context_bootstrap::indexed_db) fn idb_object_store_index_callback<
|
||||
let Some(parsed) = webidl::parse_args::<IdbObjectStoreIndexArgs>(scope, &args) else {
|
||||
return;
|
||||
};
|
||||
let index_name = parsed.index_name;
|
||||
let index_name = moli_indexeddb::IndexedDbName::from_utf16(parsed.index_name.0);
|
||||
let store = args.this();
|
||||
let finished = indexed_db_object_store_transaction(scope, store).is_none_or(|transaction| {
|
||||
object_bool_property(scope, transaction, INDEXED_DB_TRANSACTION_FINISHED_SLOT)
|
||||
|
||||
@@ -5,7 +5,7 @@ pub(in crate::context_bootstrap::indexed_db) fn submit_cursor_open_operation<'s>
|
||||
transaction: v8::Local<'s, v8::Object>,
|
||||
source: v8::Local<'s, v8::Object>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
operation: IndexedDbCursorOpenOperation,
|
||||
) {
|
||||
schedule_transaction_operation(
|
||||
|
||||
+2
-2
@@ -4,7 +4,7 @@ use crate::context_bootstrap::indexed_db::indexed_db_typed_storage_scope;
|
||||
pub(super) struct TransactionOperation<'s> {
|
||||
pub(super) source: v8::Local<'s, v8::Object>,
|
||||
pub(super) handle: TransactionHandle,
|
||||
pub(super) store_name: String,
|
||||
pub(super) store_name: moli_indexeddb::IndexedDbName,
|
||||
pub(super) kind: IndexedDbTransactionOperation,
|
||||
}
|
||||
|
||||
@@ -104,7 +104,7 @@ pub(in crate::context_bootstrap::indexed_db) fn schedule_transaction_operation<'
|
||||
transaction: v8::Local<'s, v8::Object>,
|
||||
source: v8::Local<'s, v8::Object>,
|
||||
request: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
input: IndexedDbTransactionOperation,
|
||||
) {
|
||||
let transaction_context = transaction
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ use moli_indexeddb::sqlite::{IndexKeys, PreparedWrite, service::Completion};
|
||||
|
||||
struct Write {
|
||||
transaction: TransactionHandle,
|
||||
store: String,
|
||||
store: moli_indexeddb::IndexedDbName,
|
||||
prepared: PreparedObjectStoreWrite,
|
||||
metadata: IndexedDbObjectStoreMetadata,
|
||||
add_only: bool,
|
||||
|
||||
@@ -441,15 +441,15 @@ struct IndexedDbObjectStoreLifecycleState {
|
||||
wrapper: v8::Weak<v8::Object>,
|
||||
transaction: v8::Weak<v8::Object>,
|
||||
database: v8::Weak<v8::Object>,
|
||||
name_override: Option<String>,
|
||||
name_override: Option<moli_indexeddb::IndexedDbName>,
|
||||
metadata: IndexedDbObjectStoreMetadata,
|
||||
deleted: bool,
|
||||
}
|
||||
|
||||
impl IndexedDbObjectStoreLifecycleState {
|
||||
fn name(&self) -> &str {
|
||||
fn name(&self) -> &moli_indexeddb::IndexedDbName {
|
||||
self.name_override
|
||||
.as_deref()
|
||||
.as_ref()
|
||||
.unwrap_or(&self.metadata.info.name)
|
||||
}
|
||||
|
||||
@@ -1769,7 +1769,7 @@ pub(super) fn replace_indexed_db_database_metadata<'s>(
|
||||
pub(super) fn indexed_db_database_store_names(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
database: v8::Local<'_, v8::Object>,
|
||||
) -> Vec<String> {
|
||||
) -> Vec<moli_indexeddb::IndexedDbName> {
|
||||
let mirror = indexed_db_database_mirror(scope, database).expect("database mirror");
|
||||
mirror
|
||||
.borrow()
|
||||
@@ -1824,7 +1824,7 @@ pub(super) fn promote_indexed_db_sql_schema<'s>(
|
||||
pub(super) fn indexed_db_database_store_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Option<IndexedDbObjectStoreMetadata> {
|
||||
let mirror = indexed_db_database_mirror(scope, database)?;
|
||||
mirror.borrow().metadata.as_ref()?.get(store_name).cloned()
|
||||
@@ -1850,7 +1850,7 @@ pub(super) fn set_indexed_db_database_store_metadata<'s>(
|
||||
pub(super) fn remove_indexed_db_database_store_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
indexed_db_database_sql_client(scope, database).ok_or_else(missing_mirror)?;
|
||||
let mirror = indexed_db_database_mirror(scope, database).ok_or_else(missing_mirror)?;
|
||||
@@ -1869,7 +1869,7 @@ pub(super) fn remove_indexed_db_database_store_metadata<'s>(
|
||||
pub(super) fn set_indexed_db_database_index_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
info: &IndexInfo,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
indexed_db_database_sql_client(scope, database).ok_or_else(missing_mirror)?;
|
||||
@@ -1890,8 +1890,8 @@ pub(super) fn set_indexed_db_database_index_metadata<'s>(
|
||||
pub(super) fn remove_indexed_db_database_index_metadata<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
index_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
index_name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Result<(), IndexedDbError> {
|
||||
indexed_db_database_sql_client(scope, database).ok_or_else(missing_mirror)?;
|
||||
let mirror = indexed_db_database_mirror(scope, database).ok_or_else(missing_mirror)?;
|
||||
@@ -1940,7 +1940,7 @@ pub(super) fn indexed_db_object_store_database<'s>(
|
||||
pub(super) fn indexed_db_object_store_name(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
store: v8::Local<'_, v8::Object>,
|
||||
) -> Option<String> {
|
||||
) -> Option<moli_indexeddb::IndexedDbName> {
|
||||
Some(
|
||||
object_store_native_state(scope, store)?
|
||||
.borrow()
|
||||
@@ -2276,12 +2276,12 @@ fn set_indexed_db_typed_transaction_slot_value(
|
||||
|
||||
fn create_database_metadata_object_from_typed<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
metadata: &BTreeMap<String, IndexedDbObjectStoreMetadata>,
|
||||
metadata: &BTreeMap<moli_indexeddb::IndexedDbName, IndexedDbObjectStoreMetadata>,
|
||||
) -> Option<v8::Local<'s, v8::Object>> {
|
||||
let object = new_null_prototype_object(scope);
|
||||
for (store_name, store) in metadata {
|
||||
let descriptor = create_object_store_descriptor_from_typed(scope, store)?;
|
||||
set_indexed_db_internal_object_property(scope, object, store_name, descriptor.into());
|
||||
set_indexed_db_internal_name_property(scope, object, store_name, descriptor.into());
|
||||
}
|
||||
Some(object)
|
||||
}
|
||||
@@ -2366,7 +2366,10 @@ fn indexed_db_typed_object_store_slot_value<'s>(
|
||||
key: &str,
|
||||
) -> Option<v8::Local<'s, v8::Value>> {
|
||||
match key {
|
||||
INDEXED_DB_OBJECT_STORE_NAME_SLOT => v8_string(scope, store.name()).map(Into::into),
|
||||
INDEXED_DB_OBJECT_STORE_NAME_SLOT => {
|
||||
v8::String::new_from_two_byte(scope, store.name().as_utf16(), v8::NewStringType::Normal)
|
||||
.map(Into::into)
|
||||
}
|
||||
INDEXED_DB_OBJECT_STORE_METADATA_SLOT => {
|
||||
create_object_store_descriptor_from_typed(scope, &store.metadata).map(Into::into)
|
||||
}
|
||||
@@ -2385,7 +2388,11 @@ fn set_indexed_db_typed_object_store_slot_value(
|
||||
store.name_override = Some(
|
||||
value
|
||||
.to_string(scope)
|
||||
.map(|value| value.to_rust_string_lossy(scope))
|
||||
.map(|value| {
|
||||
moli_indexeddb::IndexedDbName::from_utf16(
|
||||
crate::util::v8_string_to_u16_string(scope, value).into_vec(),
|
||||
)
|
||||
})
|
||||
.unwrap_or_default(),
|
||||
);
|
||||
true
|
||||
@@ -2776,13 +2783,13 @@ mod tests {
|
||||
#[test]
|
||||
fn object_store_metadata_tracks_index_lifecycle() {
|
||||
let mut metadata = IndexedDbObjectStoreMetadata::from_info(&ObjectStoreInfo {
|
||||
name: "posts".to_owned(),
|
||||
name: "posts".into(),
|
||||
key_path: None,
|
||||
auto_increment: false,
|
||||
index_names: Vec::new(),
|
||||
});
|
||||
let index = IndexInfo {
|
||||
name: "by-tag".to_owned(),
|
||||
name: "by-tag".into(),
|
||||
key_path: KeyPath::String("tag".to_owned()),
|
||||
unique: true,
|
||||
multi_entry: false,
|
||||
@@ -2791,16 +2798,16 @@ mod tests {
|
||||
metadata = metadata.with_index(&index);
|
||||
|
||||
assert_eq!(metadata.info().index_names, ["by-tag"]);
|
||||
assert_eq!(metadata.index("by-tag"), Some(&index));
|
||||
assert_eq!(metadata.index(&"by-tag".into()), Some(&index));
|
||||
assert_eq!(
|
||||
metadata.indexes_in_name_order(),
|
||||
std::slice::from_ref(&index)
|
||||
);
|
||||
|
||||
metadata = metadata.without_index("by-tag");
|
||||
metadata = metadata.without_index(&"by-tag".into());
|
||||
|
||||
assert!(metadata.info().index_names.is_empty());
|
||||
assert!(metadata.index("by-tag").is_none());
|
||||
assert!(metadata.index(&"by-tag".into()).is_none());
|
||||
assert!(metadata.indexes_in_name_order().is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use super::*;
|
||||
use moli_indexeddb::sqlite::Query;
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) struct SqlCursorQuery {
|
||||
pub(in crate::context_bootstrap::indexed_db) store: String,
|
||||
pub(in crate::context_bootstrap::indexed_db) store: moli_indexeddb::IndexedDbName,
|
||||
pub(in crate::context_bootstrap::indexed_db) query: Query,
|
||||
}
|
||||
|
||||
|
||||
@@ -571,7 +571,7 @@ fn materialize<'s>(
|
||||
.take()
|
||||
.expect("armed upgrade rollback");
|
||||
let mut store_names = indexed_db_database_store_names(scope, database);
|
||||
store_names.sort_unstable_by(|a, b| a.encode_utf16().cmp(b.encode_utf16()));
|
||||
store_names.sort_unstable();
|
||||
let transaction = create_transaction_object(
|
||||
scope,
|
||||
database,
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::*;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
pub(super) type StoreMap = BTreeMap<String, IndexedDbObjectStoreMetadata>;
|
||||
pub(super) type StoreMap = BTreeMap<moli_indexeddb::IndexedDbName, IndexedDbObjectStoreMetadata>;
|
||||
|
||||
// Editing a candidate keeps every previously shared snapshot immutable.
|
||||
pub(super) fn edit_shared<T: Clone>(value: &Rc<T>, edit: impl FnOnce(&mut T)) -> Rc<T> {
|
||||
@@ -14,14 +14,14 @@ pub(super) fn edit_shared<T: Clone>(value: &Rc<T>, edit: impl FnOnce(&mut T)) ->
|
||||
pub(in crate::context_bootstrap::indexed_db) struct IndexedDbObjectStoreMetadata {
|
||||
pub(super) data: Rc<IndexedDbObjectStoreMetadataData>,
|
||||
pub(super) created_in_upgrade: std::cell::Cell<bool>,
|
||||
pub(super) created_indexes: RefCell<Option<Rc<BTreeSet<String>>>>,
|
||||
pub(super) created_indexes: RefCell<Option<Rc<BTreeSet<moli_indexeddb::IndexedDbName>>>>,
|
||||
pub(super) visible_indexes: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub(in crate::context_bootstrap::indexed_db) struct IndexedDbObjectStoreMetadataData {
|
||||
pub(super) info: ObjectStoreInfo,
|
||||
pub(super) indexes: BTreeMap<String, Rc<IndexInfo>>,
|
||||
pub(super) indexes: BTreeMap<moli_indexeddb::IndexedDbName, Rc<IndexInfo>>,
|
||||
}
|
||||
|
||||
impl std::ops::Deref for IndexedDbObjectStoreMetadata {
|
||||
@@ -50,7 +50,7 @@ impl IndexedDbObjectStoreMetadata {
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap::indexed_db) fn from_sql(
|
||||
name: &str,
|
||||
name: &moli_indexeddb::IndexedDbName,
|
||||
store: &moli_indexeddb::sqlite::StoreMetadata,
|
||||
) -> Self {
|
||||
let indexes = store
|
||||
@@ -69,7 +69,7 @@ impl IndexedDbObjectStoreMetadata {
|
||||
})
|
||||
.collect();
|
||||
let mut names = store.indexes.keys().cloned().collect::<Vec<_>>();
|
||||
names.sort_unstable_by(|a, b| a.encode_utf16().cmp(b.encode_utf16()));
|
||||
names.sort_unstable();
|
||||
Self::from_data(Rc::new(IndexedDbObjectStoreMetadataData {
|
||||
info: ObjectStoreInfo {
|
||||
name: name.into(),
|
||||
@@ -84,21 +84,26 @@ impl IndexedDbObjectStoreMetadata {
|
||||
pub(in crate::context_bootstrap::indexed_db) fn info(&self) -> &ObjectStoreInfo {
|
||||
&self.info
|
||||
}
|
||||
pub(in crate::context_bootstrap::indexed_db) fn index_names(&self) -> &[String] {
|
||||
pub(in crate::context_bootstrap::indexed_db) fn index_names(
|
||||
&self,
|
||||
) -> &[moli_indexeddb::IndexedDbName] {
|
||||
if self.visible_indexes {
|
||||
&self.info.index_names
|
||||
} else {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
pub(in crate::context_bootstrap::indexed_db) fn index(&self, name: &str) -> Option<&IndexInfo> {
|
||||
pub(in crate::context_bootstrap::indexed_db) fn index(
|
||||
&self,
|
||||
name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Option<&IndexInfo> {
|
||||
self.visible_indexes
|
||||
.then(|| self.indexes.get(name).map(|info| &**info))
|
||||
.flatten()
|
||||
}
|
||||
pub(in crate::context_bootstrap::indexed_db) fn shared_index(
|
||||
&self,
|
||||
name: &str,
|
||||
name: &moli_indexeddb::IndexedDbName,
|
||||
) -> Option<Rc<IndexInfo>> {
|
||||
self.visible_indexes
|
||||
.then(|| self.indexes.get(name).cloned())
|
||||
@@ -111,7 +116,7 @@ impl IndexedDbObjectStoreMetadata {
|
||||
.filter_map(|name| self.index(name).cloned())
|
||||
.collect()
|
||||
}
|
||||
pub(super) fn index_created_in_upgrade(&self, name: &str) -> bool {
|
||||
pub(super) fn index_created_in_upgrade(&self, name: &moli_indexeddb::IndexedDbName) -> bool {
|
||||
self.created_in_upgrade.get()
|
||||
|| self
|
||||
.created_indexes
|
||||
@@ -140,7 +145,7 @@ impl IndexedDbObjectStoreMetadata {
|
||||
..self.clone()
|
||||
}
|
||||
}
|
||||
pub(super) fn without_index(&self, name: &str) -> Self {
|
||||
pub(super) fn without_index(&self, name: &moli_indexeddb::IndexedDbName) -> Self {
|
||||
let data = edit_shared(&self.data, |data| {
|
||||
data.info.index_names.retain(|candidate| candidate != name);
|
||||
data.indexes.remove(name);
|
||||
|
||||
@@ -145,7 +145,7 @@ impl Backend {
|
||||
}
|
||||
|
||||
fn begin(&self, transaction: TransactionHandle) {
|
||||
let stores = vec!["records".to_owned()];
|
||||
let stores = vec![moli_indexeddb::IndexedDbName::from("records")];
|
||||
let admission = self
|
||||
.connection
|
||||
.queue_transaction_start(&stores, TransactionMode::ReadWrite, Arc::new(|| {}))
|
||||
@@ -339,8 +339,9 @@ fn request<'s>(scope: &mut v8::PinScope<'s, '_>, backend: &Backend) -> v8::Local
|
||||
transaction,
|
||||
database,
|
||||
IndexedDbObjectStoreMetadata::from_sql(
|
||||
"records",
|
||||
&backend.client.committed().unwrap().stores["records"],
|
||||
&"records".into(),
|
||||
&backend.client.committed().unwrap().stores
|
||||
[&moli_indexeddb::IndexedDbName::from("records")],
|
||||
),
|
||||
);
|
||||
request_for_store(scope, transaction, store)
|
||||
@@ -365,8 +366,9 @@ fn write_store<'s>(
|
||||
transaction,
|
||||
database,
|
||||
IndexedDbObjectStoreMetadata::from_sql(
|
||||
"records",
|
||||
&backend.client.committed().unwrap().stores["records"],
|
||||
&"records".into(),
|
||||
&backend.client.committed().unwrap().stores
|
||||
[&moli_indexeddb::IndexedDbName::from("records")],
|
||||
),
|
||||
);
|
||||
(transaction, store)
|
||||
@@ -743,7 +745,7 @@ fn schedule<'s>(
|
||||
.unwrap()
|
||||
.to_object(scope)
|
||||
.unwrap();
|
||||
schedule_transaction_operation(scope, transaction, source, request, "records", kind);
|
||||
schedule_transaction_operation(scope, transaction, source, request, &"records".into(), kind);
|
||||
}
|
||||
|
||||
fn result<'s>(
|
||||
|
||||
@@ -237,7 +237,7 @@ pub(in crate::context_bootstrap::indexed_db) fn restore_indexed_db_upgrade_metad
|
||||
pub(in crate::context_bootstrap::indexed_db) fn sync_indexed_db_store_handles<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
database: v8::Local<'s, v8::Object>,
|
||||
store_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
) {
|
||||
let metadata = indexed_db_database_store_metadata(scope, database, store_name);
|
||||
let mut stores = Vec::new();
|
||||
@@ -276,8 +276,8 @@ pub(in crate::context_bootstrap::indexed_db) fn sync_indexed_db_store_handles<'s
|
||||
pub(in crate::context_bootstrap::indexed_db) fn mark_indexed_db_index_handles_deleted(
|
||||
scope: &mut v8::PinScope<'_, '_>,
|
||||
database: v8::Local<'_, v8::Object>,
|
||||
store_name: &str,
|
||||
index_name: &str,
|
||||
store_name: &moli_indexeddb::IndexedDbName,
|
||||
index_name: &moli_indexeddb::IndexedDbName,
|
||||
) {
|
||||
for table in indexed_db_handle_tables(scope, database) {
|
||||
let indexes = table
|
||||
@@ -287,7 +287,7 @@ pub(in crate::context_bootstrap::indexed_db) fn mark_indexed_db_index_handles_de
|
||||
.filter_map(|(id, index)| {
|
||||
let index = index.upgrade()?;
|
||||
let index = index.borrow();
|
||||
if index.info.name != index_name {
|
||||
if &index.info.name != index_name {
|
||||
return None;
|
||||
}
|
||||
let store = index.object_store.to_local(scope)?;
|
||||
@@ -296,7 +296,7 @@ pub(in crate::context_bootstrap::indexed_db) fn mark_indexed_db_index_handles_de
|
||||
.collect::<Vec<_>>();
|
||||
for (id, store) in indexes {
|
||||
if !indexed_db_object_store_is_deleted(scope, store)
|
||||
&& indexed_db_object_store_name(scope, store).as_deref() == Some(store_name)
|
||||
&& indexed_db_object_store_name(scope, store).as_ref() == Some(store_name)
|
||||
&& indexed_db_object_store_database(scope, store) == Some(database)
|
||||
&& let Some(index) = table
|
||||
.borrow()
|
||||
|
||||
@@ -538,9 +538,22 @@ async fn idb_sqlite_schema_creates_deletes_and_mirrors_before_native_completion(
|
||||
);
|
||||
let metadata = backend.client.committed().unwrap();
|
||||
assert_eq!(metadata.version, 2);
|
||||
assert!(metadata.stores["records"].indexes.is_empty());
|
||||
assert_eq!(metadata.stores["new"].indexes.len(), 2);
|
||||
assert!(!metadata.stores.contains_key("temporary"));
|
||||
assert!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.is_empty()
|
||||
);
|
||||
assert_eq!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("new")]
|
||||
.indexes
|
||||
.len(),
|
||||
2
|
||||
);
|
||||
assert!(
|
||||
!metadata
|
||||
.stores
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("temporary"))
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
@@ -759,6 +772,14 @@ async fn idb_sqlite_schema_inactive_upgrade_cannot_queue_late_schema_changes() {
|
||||
r#"["complete"]"#
|
||||
);
|
||||
let metadata = backend.client.committed().unwrap();
|
||||
assert!(!metadata.stores.contains_key("late"));
|
||||
assert!(metadata.stores["records"].indexes.contains_key("created"));
|
||||
assert!(
|
||||
!metadata
|
||||
.stores
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("late"))
|
||||
);
|
||||
assert!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("created"))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -52,11 +52,23 @@ async fn idb_sqlite_schema_commands_commit_after_synchronous_metadata_update() {
|
||||
let metadata = backend.client.committed().unwrap();
|
||||
assert_eq!(metadata.version, 2);
|
||||
if operation.contains("createObjectStore") {
|
||||
assert!(metadata.stores.contains_key("added"));
|
||||
assert!(
|
||||
metadata
|
||||
.stores
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("added"))
|
||||
);
|
||||
} else if operation.contains("createIndex") {
|
||||
assert!(metadata.stores["records"].indexes.contains_key("added"));
|
||||
assert!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("added"))
|
||||
);
|
||||
} else if operation.contains("deleteIndex") {
|
||||
assert!(metadata.stores["records"].indexes.is_empty());
|
||||
assert!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.is_empty()
|
||||
);
|
||||
} else {
|
||||
assert!(metadata.stores.is_empty());
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ async fn idb_sqlite_small_write_shares_large_schema() {
|
||||
backend.execute(Command::CreateIndex {
|
||||
transaction,
|
||||
store: "records".into(),
|
||||
name: format!("unused{index}"),
|
||||
name: format!("unused{index}").into(),
|
||||
options: IndexOptions {
|
||||
key_path: KeyPath::String(format!("missing{index}{}", "x".repeat(8192))),
|
||||
unique: false,
|
||||
@@ -49,7 +49,7 @@ async fn idb_sqlite_small_write_shares_large_schema() {
|
||||
r#"["key:1","complete"]"#
|
||||
);
|
||||
assert_eq!(
|
||||
backend.client.committed().unwrap().stores["records"]
|
||||
backend.client.committed().unwrap().stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.len(),
|
||||
11
|
||||
|
||||
@@ -29,7 +29,12 @@ async fn idb_sqlite_abort_from_write_listener_restores_schema_records_and_genera
|
||||
assert_eq!(vm.eval("String(db.version)").unwrap(), "1");
|
||||
let metadata = backend.client.committed().unwrap();
|
||||
assert_eq!(metadata.version, 1);
|
||||
assert_eq!(metadata.stores["records"].indexes.len(), 1);
|
||||
assert_eq!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
vm.eval(
|
||||
r#"
|
||||
globalThis.saved = -1;
|
||||
@@ -116,8 +121,17 @@ async fn idb_sqlite_same_task_schema_abort_cancels_pending_writes_in_fifo_order(
|
||||
);
|
||||
let metadata = backend.client.committed().unwrap();
|
||||
assert_eq!(metadata.version, 1);
|
||||
assert_eq!(metadata.stores["records"].indexes.len(), 1);
|
||||
assert!(metadata.stores["records"].indexes.contains_key("unique"));
|
||||
assert_eq!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.len(),
|
||||
1
|
||||
);
|
||||
assert!(
|
||||
metadata.stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("unique"))
|
||||
);
|
||||
vm.eval("globalThis.savedCount = -1; db.transaction('records').objectStore('records').count().onsuccess = event => savedCount = event.target.result").unwrap();
|
||||
backend.drain(&mut vm, &loader).await;
|
||||
assert_eq!(vm.eval("String(savedCount)").unwrap(), "0");
|
||||
|
||||
@@ -162,7 +162,7 @@ async fn idb_sqlite_deleted_index_definition_is_released_at_last_wrapper_gc() {
|
||||
"InvalidStateError"
|
||||
);
|
||||
assert_eq!(
|
||||
backend.client.committed().unwrap().stores["records"]
|
||||
backend.client.committed().unwrap().stores[&moli_indexeddb::IndexedDbName::from("records")]
|
||||
.indexes
|
||||
.len(),
|
||||
1
|
||||
|
||||
@@ -240,7 +240,11 @@ fn long_sql_paths_survive_restart_and_expired_bucket_cleanup() {
|
||||
.unwrap();
|
||||
assert_eq!(databases.len(), 1);
|
||||
assert_eq!(databases[0].version, 1);
|
||||
assert!(databases[0].stores.contains_key("kept"));
|
||||
assert!(
|
||||
databases[0]
|
||||
.stores
|
||||
.contains_key(&moli_indexeddb::IndexedDbName::from("kept"))
|
||||
);
|
||||
fixture.expire(&old);
|
||||
let replacement = fixture.open("long-path");
|
||||
assert_ne!(replacement.bucket_id(), old.bucket_id());
|
||||
|
||||
Reference in New Issue
Block a user