Skip to content

Commit 78454da

Browse files
jeromegntantaman
authored andcommitted
wip: compare site_id if values are the same and option is turned on
1 parent 53ae92e commit 78454da

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

core/rs/core/src/c.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ pub struct crsql_ExtData {
6767
pub pSetSiteIdOrdinalStmt: *mut sqlite::stmt,
6868
pub pSelectSiteIdOrdinalStmt: *mut sqlite::stmt,
6969
pub pSelectClockTablesStmt: *mut sqlite::stmt,
70+
pub tieBreakSameColValue: bool,
7071
}
7172

7273
#[repr(C)]

core/rs/core/src/changes_vtab_write.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,21 @@ use crate::util::slab_rowid;
1919

2020
/**
2121
* did_cid_win does not take into account the causal length.
22-
* The expectation is that all cuasal length concerns have already been handle
22+
* The expectation is that all causal length concerns have already been handle
2323
* via:
2424
* - early return because insert_cl < local_cl
2525
* - automatic win because insert_cl > local_cl
26-
* - come here to did_cid_win iff insert_cl = local_cl
26+
* - come here to did_cid_win if insert_cl = local_cl
2727
*/
2828
fn did_cid_win(
2929
db: *mut sqlite3,
30+
ext_data: *mut crsql_ExtData,
3031
insert_tbl: &str,
3132
tbl_info: &TableInfo,
3233
unpacked_pks: &Vec<ColumnValue>,
3334
key: sqlite::int64,
3435
insert_val: *mut sqlite::value,
36+
insert_site_id: &[u8],
3537
col_name: &str,
3638
col_version: sqlite::int64,
3739
errmsg: *mut *mut c_char,
@@ -89,10 +91,15 @@ fn did_cid_win(
8991
match step_result {
9092
Ok(ResultCode::ROW) => {
9193
let local_value = col_val_stmt.column_value(0)?;
92-
let ret = crsql_compare_sqlite_values(insert_val, local_value);
94+
let mut ret = crsql_compare_sqlite_values(insert_val, local_value);
9395
reset_cached_stmt(col_val_stmt.stmt)?;
94-
// value won, take value
95-
// if values are the same (ret == 0) then we return false and do not take the update
96+
if ret == 0 && unsafe { (*ext_data).tieBreakSameColValue } {
97+
// values are the same (ret == 0) and the option to tie break on site_id is true
98+
ret = unsafe {
99+
let my_site_id = core::slice::from_raw_parts((*ext_data).siteId, 16);
100+
insert_site_id.cmp(my_site_id) as c_int
101+
};
102+
}
96103
return Ok(ret > 0);
97104
}
98105
_ => {
@@ -586,17 +593,19 @@ unsafe fn merge_insert(
586593
|| !row_exists_locally
587594
|| did_cid_win(
588595
db,
596+
(*tab).pExtData,
589597
insert_tbl,
590598
&tbl_info,
591599
&unpacked_pks,
592600
key,
593601
insert_val,
602+
insert_site_id,
594603
insert_col,
595604
insert_col_vrsn,
596605
errmsg,
597606
)?;
598607

599-
if does_cid_win == false {
608+
if !does_cid_win {
600609
// doesCidWin == 0? compared against our clocks, nothing wins. OK and
601610
// Done.
602611
return Ok(ResultCode::OK);

0 commit comments

Comments
 (0)