|
| 1 | +// Copyright 2016 Mozilla |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may not use |
| 4 | +// this file except in compliance with the License. You may obtain a copy of the |
| 5 | +// License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +// Unless required by applicable law or agreed to in writing, software distributed |
| 7 | +// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 8 | +// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 9 | +// specific language governing permissions and limitations under the License. |
| 10 | + |
| 11 | +use uuid::Uuid; |
| 12 | + |
| 13 | +use conn::Store; |
| 14 | +use errors::{ |
| 15 | + Result, |
| 16 | + ErrorKind, |
| 17 | +}; |
| 18 | + |
| 19 | +use mentat_core::{ |
| 20 | + Entid, |
| 21 | + KnownEntid, |
| 22 | +}; |
| 23 | +use mentat_db as db; |
| 24 | + |
| 25 | +use entity_builder::BuildTerms; |
| 26 | + |
| 27 | +use mentat_tolstoy::{ |
| 28 | + Syncer, |
| 29 | + SyncMetadataClient, |
| 30 | + TxMapper, |
| 31 | +}; |
| 32 | +use mentat_tolstoy::syncer::{ |
| 33 | + Tx, |
| 34 | + SyncResult, |
| 35 | +}; |
| 36 | +use mentat_tolstoy::metadata::HeadTrackable; |
| 37 | + |
| 38 | +pub trait Syncable { |
| 39 | + fn sync(&mut self, server_uri: &String, user_uuid: &String) -> Result<()>; |
| 40 | + fn fast_forward_local(&mut self, txs: Vec<Tx>) -> Result<()>; |
| 41 | +} |
| 42 | + |
| 43 | +fn within_user_partition(entid: Entid) -> bool { |
| 44 | + entid >= db::USER0 && entid < db::TX0 |
| 45 | +} |
| 46 | + |
| 47 | +impl Syncable for Store { |
| 48 | + fn fast_forward_local(&mut self, txs: Vec<Tx>) -> Result<()> { |
| 49 | + let mut last_tx_entid = None; |
| 50 | + let mut last_tx_uuid = None; |
| 51 | + |
| 52 | + // During fast-forwarding, we will insert datoms with known entids |
| 53 | + // which, by definition, fall outside of our user partition. |
| 54 | + // Once we've done with insertion, we need to ensure that user |
| 55 | + // partition's next allocation will not overlap with just-inserted datoms. |
| 56 | + // To allow for "holes" in the user partition (due to data excision), |
| 57 | + // we track the highest incoming entid we saw, and expand our |
| 58 | + // local partition to match. |
| 59 | + // In absence of excision and implementation bugs, this should work |
| 60 | + // just as if we counted number of incoming entids and expanded by |
| 61 | + // that number instead. |
| 62 | + let mut largest_endid_encountered = db::USER0; |
| 63 | + |
| 64 | + for tx in txs { |
| 65 | + let in_progress = self.begin_transaction()?; |
| 66 | + let mut builder = in_progress.builder(); |
| 67 | + for part in tx.parts { |
| 68 | + if part.added { |
| 69 | + builder.add(KnownEntid(part.e), KnownEntid(part.a), part.v.clone())?; |
| 70 | + } else { |
| 71 | + builder.retract(KnownEntid(part.e), KnownEntid(part.a), part.v.clone())?; |
| 72 | + } |
| 73 | + // Ignore datoms that fall outside of the user partition: |
| 74 | + if within_user_partition(part.e) && part.e > largest_endid_encountered { |
| 75 | + largest_endid_encountered = part.e; |
| 76 | + } |
| 77 | + } |
| 78 | + let report = builder.commit()?; |
| 79 | + last_tx_entid = Some(report.tx_id); |
| 80 | + last_tx_uuid = Some(tx.tx.clone()); |
| 81 | + } |
| 82 | + |
| 83 | + // We've just transacted a new tx, and generated a new tx entid. |
| 84 | + // Map it to the corresponding incoming tx uuid, advance our |
| 85 | + // "locally known remote head". |
| 86 | + if let Some(uuid) = last_tx_uuid { |
| 87 | + if let Some(entid) = last_tx_entid { |
| 88 | + { |
| 89 | + let mut db_tx = self.sqlite.transaction()?; |
| 90 | + SyncMetadataClient::set_remote_head(&mut db_tx, &uuid)?; |
| 91 | + TxMapper::set_tx_uuid(&mut db_tx, entid, &uuid)?; |
| 92 | + db_tx.commit()?; |
| 93 | + } |
| 94 | + |
| 95 | + // only need to advance the user partition, since we're using KnownEntid and partition won't |
| 96 | + // get auto-updated; shouldn't be a problem for tx partition, since we're relying on the builder |
| 97 | + // to create a tx and advance the partition for us. |
| 98 | + self.fast_forward_user_partition(largest_endid_encountered)?; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + Ok(()) |
| 103 | + } |
| 104 | + |
| 105 | + fn sync(&mut self, server_uri: &String, user_uuid: &String) -> Result<()> { |
| 106 | + let uuid = Uuid::parse_str(&user_uuid)?; |
| 107 | + |
| 108 | + let sync_result; |
| 109 | + { |
| 110 | + let mut db_tx = self.sqlite.transaction()?; |
| 111 | + sync_result = Syncer::flow(&mut db_tx, server_uri, &uuid)?; |
| 112 | + |
| 113 | + // TODO this should be done _after_ all of the operations below conclude! |
| 114 | + // Commits any changes Syncer made (schema, updated heads, tu mappings during an upload, etc) |
| 115 | + db_tx.commit()?; |
| 116 | + } |
| 117 | + |
| 118 | + // TODO These operations need to borrow self as mutable; but we already borrow it for db_tx above, |
| 119 | + // and so for now we split up sync into multiple db transactions /o\ |
| 120 | + // Fixing this likely involves either implementing flow on InProgress, or changing flow to |
| 121 | + // take an InProgress instead of a raw sql transaction. |
| 122 | + |
| 123 | + match sync_result { |
| 124 | + SyncResult::EmptyServer => Ok(()), |
| 125 | + SyncResult::NoChanges => Ok(()), |
| 126 | + SyncResult::ServerFastForward => Ok(()), |
| 127 | + SyncResult::Merge => bail!(ErrorKind::NotYetImplemented( |
| 128 | + format!("Can't sync against diverged local.") |
| 129 | + )), |
| 130 | + SyncResult::LocalFastForward(txs) => self.fast_forward_local(txs) |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments