|
| 1 | +// Copyright (c) 2023 Yuki Kishimoto |
| 2 | +// Distributed under the MIT software license |
| 3 | + |
| 4 | +use std::ops::Deref; |
| 5 | +use std::sync::Arc; |
| 6 | + |
| 7 | +use parking_lot::RwLock; |
| 8 | + |
| 9 | +mod error; |
| 10 | +mod bytes; |
| 11 | + |
| 12 | +use self::error::Result; |
| 13 | +pub use self::bytes::Bytes; |
| 14 | +pub use self::error::NegentropyError; |
| 15 | + |
| 16 | +pub struct ReconcileWithIds { |
| 17 | + pub have_ids: Vec<Arc<Bytes>>, |
| 18 | + pub need_ids: Vec<Arc<Bytes>>, |
| 19 | + pub output: Option<Arc<Bytes>>, |
| 20 | +} |
| 21 | + |
| 22 | +pub struct Negentropy { |
| 23 | + inner: RwLock<negentropy::Negentropy>, |
| 24 | +} |
| 25 | + |
| 26 | +impl Negentropy { |
| 27 | + pub fn new(id_size: u8, frame_size_limit: Option<u64>) -> Result<Self> { |
| 28 | + Ok(Self { |
| 29 | + inner: RwLock::new(negentropy::Negentropy::new(id_size as usize, frame_size_limit)?) |
| 30 | + }) |
| 31 | + } |
| 32 | + |
| 33 | + pub fn id_size(&self) -> u64 { |
| 34 | + self.inner.read().id_size() as u64 |
| 35 | + } |
| 36 | + |
| 37 | + /// Check if current instance it's an initiator |
| 38 | + pub fn is_initiator(&self) -> bool { |
| 39 | + self.inner.read().is_initiator() |
| 40 | + } |
| 41 | + |
| 42 | + /// Check if sealed |
| 43 | + pub fn is_sealed(&self) -> bool { |
| 44 | + self.inner.read().is_sealed() |
| 45 | + } |
| 46 | + |
| 47 | + /// Check if need to continue |
| 48 | + pub fn continuation_needed(&self) -> bool { |
| 49 | + self.inner.read().continuation_needed() |
| 50 | + } |
| 51 | + |
| 52 | + pub fn add_item(&self, created_at: u64, id: Arc<Bytes>) -> Result<()> { |
| 53 | + let mut negentropy = self.inner.write(); |
| 54 | + Ok(negentropy.add_item(created_at, id.as_ref().deref().clone())?) |
| 55 | + } |
| 56 | + |
| 57 | + pub fn seal(&self) -> Result<()> { |
| 58 | + let mut negentropy = self.inner.write(); |
| 59 | + Ok(negentropy.seal()?) |
| 60 | + } |
| 61 | + |
| 62 | + /// Initiate reconciliation set |
| 63 | + pub fn initiate(&self) -> Result<Arc<Bytes>> { |
| 64 | + let mut negentropy = self.inner.write(); |
| 65 | + Ok(Arc::new(negentropy.initiate()?.into())) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn reconcile(&self, query: Arc<Bytes>) -> Result<Arc<Bytes>> { |
| 69 | + let mut negentropy = self.inner.write(); |
| 70 | + Ok(Arc::new(negentropy.reconcile(query.as_ref().deref())?.into())) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn reconcile_with_ids( |
| 74 | + &self, |
| 75 | + query: Arc<Bytes>, |
| 76 | + ) -> Result<ReconcileWithIds> { |
| 77 | + let mut negentropy = self.inner.write(); |
| 78 | + let mut have_ids: Vec<negentropy::Bytes> = Vec::new(); |
| 79 | + let mut need_ids: Vec<negentropy::Bytes> = Vec::new(); |
| 80 | + let output: Option<negentropy::Bytes> = negentropy.reconcile_with_ids(query.as_ref().deref(), &mut have_ids, &mut need_ids)?; |
| 81 | + Ok(ReconcileWithIds { |
| 82 | + have_ids: have_ids.into_iter().map(|id| Arc::new(id.into())).collect(), |
| 83 | + need_ids: need_ids.into_iter().map(|id| Arc::new(id.into())).collect(), |
| 84 | + output: output.map(|o| Arc::new(o.into())) |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | + |
| 90 | +// UDL |
| 91 | +uniffi::include_scaffolding!("negentropy"); |
0 commit comments