use super::*;
#[allow(unsafe_code)] fn spilled(connection: &Connection) -> i32 {
let (mut current, mut highest) = (0, 0);
let code = unsafe {
rusqlite::ffi::sqlite3_db_status(
connection.handle(),
rusqlite::ffi::SQLITE_DBSTATUS_CACHE_SPILL,
&raw mut current,
&raw mut highest,
0,
)
};
assert_eq!(code, rusqlite::ffi::SQLITE_OK);
current
}
fn cache_size(connection: &Connection) -> i64 {
connection
.query_row("PRAGMA cache_size", [], |row| row.get(0))
.unwrap()
}
fn large(prefix: &str, control: &StorageReadControl) -> PreparedRecordCommit {
let value = vec![0x5a_u8; 1024];
let keys = (0..4096)
.map(|record| format!("{prefix}/{record:08}").into_bytes())
.collect::<Vec<_>>();
let writes = keys
.iter()
.map(|key| RecordWrite {
key,
expected: None,
value: Some(&value),
})
.collect::<Vec<_>>();
PreparedRecordCommit::new(&writes, control).unwrap()
}
#[test]
fn a_transaction_of_the_same_size_spills_an_ordinary_cache() {
let directory = tempfile::tempdir().unwrap();
let raw = Connection::open(directory.path().join("raw.db")).unwrap();
raw.pragma_update(None, "journal_mode", "WAL").unwrap();
raw.execute_batch(
"CREATE TABLE records (key BLOB PRIMARY KEY, value BLOB) WITHOUT ROWID; BEGIN",
)
.unwrap();
let value = vec![0x5a_u8; 1024];
for record in 0..4096 {
raw.execute(
"INSERT INTO records VALUES (?1, ?2)",
params![format!("large/{record:08}").into_bytes(), value],
)
.unwrap();
}
assert!(spilled(&raw) > 0);
raw.execute_batch("COMMIT").unwrap();
}
#[test]
fn a_large_commit_keeps_its_pages_and_returns_to_the_ordinary_cache() {
let directory = tempfile::tempdir().unwrap();
let connection = ManagedConnection::open(&directory.path().join("cache.db")).unwrap();
let store = SQLiteRecordStore::new(&connection).unwrap();
let control = control();
let status = || {
connection
.with(|connection| Ok((spilled(connection), cache_size(connection))))
.unwrap()
};
let (before, ordinary) = status();
let id = store.allocate_transaction(&control).unwrap();
store
.commit(id, &large("first", &control), &control)
.unwrap();
assert_eq!(status(), (before, ordinary));
let snapshot = store.snapshot(&control).unwrap();
assert_eq!(
snapshot
.get(b"first/00004095", &control)
.unwrap()
.and_then(|record| record.value().map(|value| value.len())),
Some(1024)
);
let id = store.allocate_transaction(&control).unwrap();
assert!(matches!(
store.commit(id, &large("first", &control), &control),
Err(CommitFailure::Rejected(VersionError::WriteConflict { .. }))
));
assert_eq!(status(), (before, ordinary));
let id = store.allocate_transaction(&control).unwrap();
store
.commit(id, &prepared(b"small", b"live", &control), &control)
.unwrap();
assert_eq!(status(), (before, ordinary));
}