Skip to content

Commit

Permalink
Make log_index and timestamp public
Browse files Browse the repository at this point in the history
  • Loading branch information
dkellner committed Aug 7, 2021
1 parent e58b75d commit 4f66a72
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
22 changes: 3 additions & 19 deletions src/internal.rs
Expand Up @@ -27,7 +27,9 @@ impl<A: Author, T> Chronofold<A, T> {
if let Some((_, idx)) = self
.iter_log_indices_causal_range(reference..)
.filter(|(_, i)| self.references.get(i) == Some(reference))
.filter(|(c, i)| matches!(c, Change::Delete) || self.timestamp(i).unwrap() > id)
.filter(|(c, i)| {
matches!(c, Change::Delete) || self.timestamp(*i).unwrap() > id
})
.last()
{
self.iter_subtree(idx).last()
Expand All @@ -43,24 +45,6 @@ impl<A: Author, T> Chronofold<A, T> {
}
}

pub(crate) fn log_index(&self, timestamp: &Timestamp<A>) -> Option<LogIndex> {
for i in (timestamp.0).0..self.log.len() {
if self.timestamp(&LogIndex(i)).unwrap() == *timestamp {
return Some(LogIndex(i));
}
}
None
}

pub(crate) fn timestamp(&self, index: &LogIndex) -> Option<Timestamp<A>> {
if let (Some(shift), Some(author)) = (self.index_shifts.get(index), self.authors.get(index))
{
Some(Timestamp(index - shift, *author))
} else {
None
}
}

pub(crate) fn apply_change(
&mut self,
id: Timestamp<A>,
Expand Down
4 changes: 2 additions & 2 deletions src/iter.rs
Expand Up @@ -182,11 +182,11 @@ where
let idx = LogIndex(self.idx_iter.next()?);
let id = self
.cfold
.timestamp(&idx)
.timestamp(idx)
.expect("timestamps of already applied ops have to exist");
let reference = self.cfold.references.get(&idx).map(|r| {
self.cfold
.timestamp(&r)
.timestamp(r)
.expect("references of already applied ops have to exist")
});
let payload = match &self.cfold.log[idx.0] {
Expand Down
19 changes: 19 additions & 0 deletions src/lib.rs
Expand Up @@ -196,6 +196,25 @@ impl<A: Author, T> Chronofold<A, T> {
Session::new(author, self)
}

pub fn log_index(&self, timestamp: &Timestamp<A>) -> Option<LogIndex> {
for i in (timestamp.0).0..self.log.len() {
if self.timestamp(LogIndex(i)).unwrap() == *timestamp {
return Some(LogIndex(i));
}
}
None
}

pub fn timestamp(&self, index: LogIndex) -> Option<Timestamp<A>> {
if let (Some(shift), Some(author)) =
(self.index_shifts.get(&index), self.authors.get(&index))
{
Some(Timestamp(&index - shift, *author))
} else {
None
}
}

/// Applies an op to the chronofold.
pub fn apply<V>(&mut self, op: Op<A, V>) -> Result<(), ChronofoldError<A, V>>
where
Expand Down

0 comments on commit 4f66a72

Please sign in to comment.