@@ -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 */
2828fn 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