Skip to content

Commit 74967a4

Browse files
committed
put pk on tableinfo instead
Signed-off-by: Somtochi Onyekwere <[email protected]>
1 parent 4533635 commit 74967a4

File tree

11 files changed

+99
-168
lines changed

11 files changed

+99
-168
lines changed

core/rs/core/src/c.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ pub struct crsql_ExtData {
7474
pub mergeEqualValues: ::core::ffi::c_int,
7575
pub timestamp: ::core::ffi::c_ulonglong,
7676
pub ordinalMap: *mut ::core::ffi::c_void,
77-
pub clCache: *mut ::core::ffi::c_void,
7877
}
7978

8079
#[repr(C)]
@@ -512,14 +511,4 @@ fn bindgen_test_layout_crsql_ExtData() {
512511
stringify!(ordinalMap)
513512
)
514513
);
515-
assert_eq!(
516-
unsafe { ::core::ptr::addr_of!((*ptr).clCache) as usize - ptr as usize },
517-
168usize,
518-
concat!(
519-
"Offset of field: ",
520-
stringify!(crsql_ExtData),
521-
"::",
522-
stringify!(clCache)
523-
)
524-
);
525514
}

core/rs/core/src/changes_vtab.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -587,18 +587,15 @@ pub extern "C" fn crsql_changes_rollback_to(vtab: *mut sqlite::vtab, _: c_int) -
587587
))
588588
};
589589

590-
let mut cl_cache = unsafe {
590+
let mut table_infos = unsafe {
591591
mem::ManuallyDrop::new(Box::from_raw(
592-
(*(*tab).pExtData).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
592+
(*(*tab).pExtData).tableInfos as *mut Vec<TableInfo>,
593593
))
594594
};
595-
596-
for (_, map) in cl_cache.iter_mut() {
597-
if !map.is_empty() {
598-
map.clear();
599-
}
595+
for tbl_info in table_infos.iter_mut() {
596+
tbl_info.clear_cl_cache();
600597
}
601-
598+
602599
ordinals.clear();
603600
ResultCode::OK as c_int
604601
}

core/rs/core/src/changes_vtab_write.rs

Lines changed: 36 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use alloc::boxed::Box;
2-
use alloc::collections::BTreeMap;
32
use alloc::ffi::CString;
43
use alloc::format;
5-
use alloc::string::String;
64
use alloc::vec::Vec;
75
use core::ffi::{c_char, c_int};
86
use core::mem;
@@ -382,36 +380,45 @@ pub unsafe extern "C" fn crsql_merge_insert(
382380

383381
fn get_local_cl(
384382
db: *mut sqlite::sqlite3,
385-
tbl_info: &TableInfo,
383+
tbl_info: &mut TableInfo,
386384
key: sqlite::int64,
387385
) -> Result<sqlite::int64, ResultCode> {
388-
let local_cl_stmt_ref = tbl_info.get_local_cl_stmt(db)?;
389-
let local_cl_stmt = local_cl_stmt_ref.as_ref().ok_or(ResultCode::ERROR)?;
390-
391-
let rc = local_cl_stmt
392-
.bind_int64(1, key)
393-
.and_then(|_| local_cl_stmt.bind_int64(2, key));
394-
if let Err(rc) = rc {
395-
reset_cached_stmt(local_cl_stmt.stmt)?;
396-
return Err(rc);
386+
if let Some(cl) = tbl_info.get_cl(key) {
387+
return Ok(*cl);
397388
}
398389

399-
let step_result = local_cl_stmt.step();
400-
match step_result {
401-
Ok(ResultCode::ROW) => {
402-
let ret = local_cl_stmt.column_int64(0);
403-
reset_cached_stmt(local_cl_stmt.stmt)?;
404-
Ok(ret)
405-
}
406-
Ok(ResultCode::DONE) => {
390+
let cl = {
391+
let local_cl_stmt_ref = tbl_info.get_local_cl_stmt(db)?;
392+
let local_cl_stmt = local_cl_stmt_ref.as_ref().ok_or(ResultCode::ERROR)?;
393+
394+
let rc = local_cl_stmt
395+
.bind_int64(1, key)
396+
.and_then(|_| local_cl_stmt.bind_int64(2, key));
397+
if let Err(rc) = rc {
407398
reset_cached_stmt(local_cl_stmt.stmt)?;
408-
Ok(0)
399+
return Err(rc);
409400
}
410-
Ok(rc) | Err(rc) => {
411-
reset_cached_stmt(local_cl_stmt.stmt)?;
412-
Err(rc)
401+
402+
let step_result = local_cl_stmt.step();
403+
match step_result {
404+
Ok(ResultCode::ROW) => {
405+
let ret = local_cl_stmt.column_int64(0);
406+
reset_cached_stmt(local_cl_stmt.stmt)?;
407+
ret
408+
}
409+
Ok(ResultCode::DONE) => {
410+
reset_cached_stmt(local_cl_stmt.stmt)?;
411+
0
412+
}
413+
Ok(rc) | Err(rc) => {
414+
reset_cached_stmt(local_cl_stmt.stmt)?;
415+
return Err(rc);
416+
}
413417
}
414-
}
418+
};
419+
420+
tbl_info.set_cl(key, cl);
421+
Ok(cl)
415422
}
416423

417424
unsafe fn merge_insert(
@@ -465,14 +472,10 @@ unsafe fn merge_insert(
465472

466473
let insert_site_id = insert_site_id.blob();
467474

468-
let tbl_infos = mem::ManuallyDrop::new(Box::from_raw(
475+
let mut tbl_infos = mem::ManuallyDrop::new(Box::from_raw(
469476
(*(*tab).pExtData).tableInfos as *mut Vec<TableInfo>,
470477
));
471478

472-
let mut cl_cache = mem::ManuallyDrop::new(Box::from_raw(
473-
(*(*tab).pExtData).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
474-
));
475-
476479
// TODO: will this work given `insert_tbl` is null termed?
477480
let tbl_info_index = tbl_infos.iter().position(|x| x.tbl_name == insert_tbl);
478481

@@ -487,14 +490,14 @@ unsafe fn merge_insert(
487490
// TODO: technically safe since we checked `is_none` but this should be more idiomatic
488491
let tbl_info_index = tbl_info_index.unwrap();
489492

490-
let tbl_info = &tbl_infos[tbl_info_index];
493+
let tbl_info = &mut tbl_infos[tbl_info_index];
491494
let unpacked_pks = unpack_columns(insert_pks.blob())?;
492495

493496
// Get or create key as the first thing we do.
494497
// We'll need the key for all later operations.
495498
let key = tbl_info.get_or_create_key(db, &unpacked_pks)?;
496499

497-
let local_cl = get_pk_cl(db, &mut cl_cache, &tbl_info, key)?;
500+
let local_cl = get_local_cl(db, tbl_info, key)?;
498501

499502
// We can ignore all updates from older causal lengths.
500503
// They won't win at anything.
@@ -720,37 +723,9 @@ unsafe fn merge_insert(
720723

721724
// a bigger cl always wins
722725
if insert_cl > local_cl {
723-
match cl_cache.get_mut(&tbl_info.tbl_name) {
724-
Some(x) => {
725-
x.insert(key, insert_cl);
726-
}
727-
None => {
728-
let mut new_map = BTreeMap::new();
729-
new_map.insert(key, insert_cl);
730-
cl_cache.insert(tbl_info.tbl_name.clone(), new_map);
731-
}
732-
}
726+
tbl_info.set_cl(key, insert_cl);
733727
}
734728
}
735729

736730
res
737731
}
738-
739-
unsafe fn get_pk_cl(
740-
db: *mut sqlite3,
741-
cl_cache: &mut BTreeMap<String, BTreeMap<i64, i64>>,
742-
tbl_info: &TableInfo,
743-
key: sqlite::int64,
744-
) -> Result<sqlite::int64, ResultCode> {
745-
match cl_cache.get(&tbl_info.tbl_name).and_then(|x| x.get(&key)) {
746-
Some(cl) => Ok(*cl),
747-
None => {
748-
let cl = get_local_cl(db, tbl_info, key)?;
749-
cl_cache
750-
.entry(tbl_info.tbl_name.clone())
751-
.or_default()
752-
.insert(key, cl);
753-
Ok(cl)
754-
}
755-
}
756-
}

core/rs/core/src/commit.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use alloc::{boxed::Box, collections::BTreeMap, string::String, vec::Vec};
1+
use alloc::{boxed::Box, collections::BTreeMap, vec::Vec};
22
use core::{
33
ffi::{c_int, c_void},
44
mem,
@@ -8,6 +8,7 @@ use core::{
88
use sqlite_nostd::ResultCode;
99

1010
use crate::c::crsql_ExtData;
11+
use crate::tableinfo::TableInfo;
1112

1213
#[no_mangle]
1314
pub unsafe extern "C" fn crsql_commit_hook(user_data: *mut c_void) -> c_int {
@@ -38,16 +39,12 @@ pub unsafe fn commit_or_rollback_reset(ext_data: *mut crsql_ExtData) {
3839
Box::from_raw((*ext_data).ordinalMap as *mut BTreeMap<Vec<u8>, i64>),
3940
);
4041

41-
let mut cl_cache = unsafe {
42-
mem::ManuallyDrop::new(Box::from_raw(
43-
(*ext_data).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
44-
))
42+
let mut table_infos = unsafe {
43+
mem::ManuallyDrop::new(Box::from_raw((*ext_data).tableInfos as *mut Vec<TableInfo>))
4544
};
45+
ordinals.clear();
4646

47-
for (_, map) in cl_cache.iter_mut() {
48-
if !map.is_empty() {
49-
map.clear();
50-
}
47+
for tbl_info in table_infos.iter_mut() {
48+
tbl_info.clear_cl_cache();
5149
}
52-
ordinals.clear();
5350
}

core/rs/core/src/db_version.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,6 @@ pub extern "C" fn crsql_drop_ordinal_map(ext_data: *mut crsql_ExtData) {
211211
}
212212
}
213213

214-
#[no_mangle]
215-
pub extern "C" fn crsql_init_cl_cache(ext_data: *mut crsql_ExtData) {
216-
let map: BTreeMap<String, BTreeMap<i64, i64>> = BTreeMap::new();
217-
unsafe { (*ext_data).clCache = Box::into_raw(Box::new(map)) as *mut c_void }
218-
}
219-
220-
#[no_mangle]
221-
pub extern "C" fn crsql_drop_cl_cache(ext_data: *mut crsql_ExtData) {
222-
unsafe {
223-
drop(Box::from_raw(
224-
(*ext_data).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
225-
));
226-
}
227-
}
228-
229214
pub fn insert_db_version(
230215
ext_data: *mut crsql_ExtData,
231216
insert_site_id: &[u8],

core/rs/core/src/lib.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ mod unpack_columns_vtab;
4949
mod util;
5050

5151
use alloc::format;
52-
use alloc::string::String;
5352
use alloc::string::ToString;
5453
use alloc::{borrow::Cow, boxed::Box, collections::BTreeMap, vec::Vec};
5554
use core::ffi::c_char;
@@ -74,7 +73,9 @@ use local_writes::after_update::x_crsql_after_update;
7473
use sqlite::{Destructor, ResultCode};
7574
use sqlite_nostd as sqlite;
7675
use sqlite_nostd::{Connection, Context, Value};
77-
use tableinfo::{crsql_ensure_table_infos_are_up_to_date, is_table_compatible, pull_table_info};
76+
use tableinfo::{
77+
crsql_ensure_table_infos_are_up_to_date, is_table_compatible, pull_table_info, TableInfo,
78+
};
7879
use teardown::*;
7980
use triggers::create_triggers;
8081

@@ -1008,16 +1009,16 @@ unsafe extern "C" fn x_crsql_cache_pk_cl(
10081009
let table_name = args[0].text();
10091010
let pk_key = args[1].int64();
10101011

1011-
let cl_cache = mem::ManuallyDrop::new(Box::from_raw(
1012-
(*ext_data).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
1013-
));
1012+
let table_infos =
1013+
mem::ManuallyDrop::new(Box::from_raw((*ext_data).tableInfos as *mut Vec<TableInfo>));
1014+
let table_info = table_infos.iter().find(|t| t.tbl_name == table_name);
10141015

1015-
let table_map = cl_cache.get(table_name);
1016-
let cl = table_map
1017-
.and_then(|x| x.get(&pk_key))
1018-
.cloned()
1019-
.unwrap_or(-1);
1020-
sqlite::result_int64(ctx, cl);
1016+
if let Some(table_info) = table_info {
1017+
let cl = table_info.get_cl(pk_key).cloned().unwrap_or(-1);
1018+
sqlite::result_int64(ctx, cl);
1019+
} else {
1020+
ctx.result_error("table not found");
1021+
}
10211022
}
10221023

10231024
/**

core/rs/core/src/local_writes/after_delete.rs

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use alloc::string::String;
22
use alloc::string::ToString;
3-
use alloc::{boxed::Box, collections::BTreeMap};
43
use core::ffi::c_int;
5-
use core::mem;
64
use sqlite::sqlite3;
75
use sqlite::value;
86
use sqlite::Context;
@@ -40,7 +38,7 @@ pub unsafe extern "C" fn x_crsql_after_delete(
4038
fn after_delete(
4139
db: *mut sqlite3,
4240
ext_data: *mut crsql_ExtData,
43-
tbl_info: &TableInfo,
41+
tbl_info: &mut TableInfo,
4442
pks_old: &[*mut value],
4543
) -> Result<ResultCode, String> {
4644
let ts = unsafe { (*ext_data).timestamp.to_string() };
@@ -52,28 +50,22 @@ fn after_delete(
5250

5351
let cl = mark_locally_deleted(db, tbl_info, key, db_version, seq, &ts)?;
5452

55-
// now actually delete the row metadata
56-
let drop_clocks_stmt_ref = tbl_info
57-
.get_merge_delete_drop_clocks_stmt(db)
58-
.map_err(|_e| "failed to get mark_locally_deleted_stmt")?;
59-
let drop_clocks_stmt = drop_clocks_stmt_ref
60-
.as_ref()
61-
.ok_or("Failed to deref sentinel stmt")?;
53+
{
54+
// now actually delete the row metadata
55+
let drop_clocks_stmt_ref = tbl_info
56+
.get_merge_delete_drop_clocks_stmt(db)
57+
.map_err(|_e| "failed to get mark_locally_deleted_stmt")?;
58+
let drop_clocks_stmt = drop_clocks_stmt_ref
59+
.as_ref()
60+
.ok_or("Failed to deref sentinel stmt")?;
6261

63-
drop_clocks_stmt
64-
.bind_int64(1, key)
65-
.map_err(|_e| "failed to bind pks to drop_clocks_stmt")?;
66-
super::step_trigger_stmt(drop_clocks_stmt)?;
62+
drop_clocks_stmt
63+
.bind_int64(1, key)
64+
.map_err(|_e| "failed to bind pks to drop_clocks_stmt")?;
65+
super::step_trigger_stmt(drop_clocks_stmt)?;
66+
}
6767

68-
let mut cl_cache = unsafe {
69-
mem::ManuallyDrop::new(Box::from_raw(
70-
(*ext_data).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
71-
))
72-
};
73-
cl_cache
74-
.entry(tbl_info.tbl_name.clone())
75-
.or_default()
76-
.insert(key, cl);
68+
tbl_info.set_cl(key, cl);
7769

7870
Ok(ResultCode::OK)
7971
}

core/rs/core/src/local_writes/after_insert.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
use alloc::boxed::Box;
2-
use alloc::collections::BTreeMap;
31
use alloc::string::String;
42
use alloc::string::ToString;
53
use core::ffi::c_int;
6-
use core::mem;
74
use sqlite::sqlite3;
85
use sqlite::value;
96
use sqlite::Context;
@@ -40,7 +37,7 @@ pub unsafe extern "C" fn x_crsql_after_insert(
4037
fn after_insert(
4138
db: *mut sqlite3,
4239
ext_data: *mut crsql_ExtData,
43-
tbl_info: &TableInfo,
40+
tbl_info: &mut TableInfo,
4441
pks_new: &[*mut value],
4542
) -> Result<ResultCode, String> {
4643
let ts = unsafe { (*ext_data).timestamp.to_string() };
@@ -57,16 +54,7 @@ fn after_insert(
5754
// update the create record since it already exists.
5855
let seq = bump_seq(ext_data);
5956
let col_version = update_create_record(db, tbl_info, key_new, db_version, seq, &ts)?;
60-
61-
let mut cl_cache = unsafe {
62-
mem::ManuallyDrop::new(Box::from_raw(
63-
(*ext_data).clCache as *mut BTreeMap<String, BTreeMap<i64, i64>>,
64-
))
65-
};
66-
cl_cache
67-
.entry(tbl_info.tbl_name.clone())
68-
.or_default()
69-
.insert(key_new, col_version);
57+
tbl_info.set_cl(key_new, col_version);
7058
}
7159

7260
super::mark_locally_inserted(db, ext_data, tbl_info, key_new, db_version, &ts)?;

0 commit comments

Comments
 (0)