@@ -22,16 +22,11 @@ use errors::{
2222} ;
2323
2424use mentat_core:: {
25- Entid ,
2625 KnownEntid ,
2726} ;
2827use mentat_db:: {
29- USER0 ,
30- TX0 ,
31- } ;
32- use mentat_db:: db;
33- use mentat_db:: db:: {
34- PartitionMapping ,
28+ renumber,
29+ PartitionMap ,
3530} ;
3631
3732use entity_builder:: {
@@ -52,11 +47,7 @@ pub trait Syncable {
5247 fn sync ( & mut self , server_uri : & String , user_uuid : & String ) -> Result < ( ) > ;
5348}
5449
55- fn within_user_partition ( entid : Entid ) -> bool {
56- entid >= USER0 && entid < TX0
57- }
58-
59- fn fast_forward_local < ' a , ' c > ( in_progress : & mut InProgress < ' a , ' c > , txs : Vec < Tx > ) -> Result < ( ) > {
50+ fn fast_forward_local < ' a , ' c > ( in_progress : & mut InProgress < ' a , ' c > , txs : Vec < Tx > ) -> Result < Option < PartitionMap > > {
6051 let mut last_tx = None ;
6152
6253 // During fast-forwarding, we will insert datoms with known entids
@@ -69,21 +60,22 @@ fn fast_forward_local<'a, 'c>(in_progress: &mut InProgress<'a, 'c>, txs: Vec<Tx>
6960 // In absence of excision and implementation bugs, this should work
7061 // just as if we counted number of incoming entids and expanded by
7162 // that number instead.
72- let mut largest_entid_encountered = USER0 ;
63+ let mut last_encountered_partition_map = None ;
7364
7465 for tx in txs {
7566 let mut builder = TermBuilder :: new ( ) ;
7667
68+ last_encountered_partition_map = match tx. parts [ 0 ] . partitions . clone ( ) {
69+ Some ( parts) => Some ( parts) ,
70+ None => bail ! ( TolstoyError :: BadServerState ( "Missing partition map in incoming transaction" . to_string( ) ) )
71+ } ;
72+
7773 for part in tx. parts {
7874 if part. added {
7975 builder. add ( KnownEntid ( part. e ) , KnownEntid ( part. a ) , part. v ) ?;
8076 } else {
8177 builder. retract ( KnownEntid ( part. e ) , KnownEntid ( part. a ) , part. v ) ?;
8278 }
83- // Ignore datoms that fall outside of the user partition:
84- if within_user_partition ( part. e ) && part. e > largest_entid_encountered {
85- largest_entid_encountered = part. e ;
86- }
8779 }
8880
8981 let report = in_progress. transact_builder ( builder) ?;
@@ -96,15 +88,9 @@ fn fast_forward_local<'a, 'c>(in_progress: &mut InProgress<'a, 'c>, txs: Vec<Tx>
9688 if let Some ( ( entid, uuid) ) = last_tx {
9789 SyncMetadataClient :: set_remote_head ( & mut in_progress. transaction , & uuid) ?;
9890 TxMapper :: set_tx_uuid ( & mut in_progress. transaction , entid, & uuid) ?;
99-
100- // We only need to advance the user partition, since we're using KnownEntid and partition
101- // won't get auto-updated; shouldn't be a problem for tx partition, since we're relying on
102- // the builder to create a tx and advance the partition for us.
103- in_progress. partition_map . expand_up_to ( ":db.part/user" , largest_entid_encountered) ;
104- db:: update_partition_map ( & mut in_progress. transaction , & in_progress. partition_map ) ?;
10591 }
10692
107- Ok ( ( ) )
93+ Ok ( last_encountered_partition_map )
10894}
10995
11096impl Conn {
@@ -118,6 +104,7 @@ impl Conn {
118104 let mut in_progress = self . begin_transaction ( sqlite) ?;
119105
120106 let sync_result = Syncer :: flow ( & mut in_progress. transaction , server_uri, & uuid) ?;
107+ let mut incoming_partition = None ;
121108
122109 match sync_result {
123110 SyncResult :: EmptyServer => ( ) ,
@@ -126,7 +113,10 @@ impl Conn {
126113 SyncResult :: Merge => bail ! ( TolstoyError :: NotYetImplemented (
127114 format!( "Can't sync against diverged local." )
128115 ) ) ,
129- SyncResult :: LocalFastForward ( txs) => fast_forward_local ( & mut in_progress, txs) ?,
116+ SyncResult :: LocalFastForward ( txs) => {
117+ incoming_partition = fast_forward_local ( & mut in_progress, txs) ?;
118+ ( )
119+ } ,
130120 SyncResult :: BadServerState => bail ! ( TolstoyError :: NotYetImplemented (
131121 format!( "Bad server state." )
132122 ) ) ,
@@ -136,6 +126,16 @@ impl Conn {
136126 ) ) ,
137127 }
138128
129+ match incoming_partition {
130+ Some ( incoming) => {
131+ let root = SyncMetadataClient :: get_partitions ( & in_progress. transaction , true ) ?;
132+ let current = SyncMetadataClient :: get_partitions ( & in_progress. transaction , false ) ?;
133+ renumber ( & in_progress. transaction , & root, & current, & incoming) ?;
134+ ( )
135+ } ,
136+ None => ( )
137+ }
138+
139139 in_progress. commit ( )
140140 }
141141}
0 commit comments