feat: async opseq queuing and transforming

This commit is contained in:
əlemi 2023-04-13 02:19:31 +02:00
parent 56db49746d
commit eeb72545c6
2 changed files with 51 additions and 11 deletions

View file

@ -66,6 +66,9 @@ impl CodempClient {
.map_err(|_| Status::invalid_argument("could not serialize opseq"))?, .map_err(|_| Status::invalid_argument("could not serialize opseq"))?,
}; };
let res = self.client.edit(req).await?.into_inner(); let res = self.client.edit(req).await?.into_inner();
if let Err(e) = factory.ack(op.clone()).await {
error!("could not ack op '{:?}' : {}", op, e);
}
Ok(res.accepted) Ok(res.accepted)
}, },
} }
@ -84,6 +87,9 @@ impl CodempClient {
.map_err(|_| Status::invalid_argument("could not serialize opseq"))?, .map_err(|_| Status::invalid_argument("could not serialize opseq"))?,
}; };
let res = self.client.edit(req).await?.into_inner(); let res = self.client.edit(req).await?.into_inner();
if let Err(e) = factory.ack(op.clone()).await {
error!("could not ack op '{:?}' : {}", op, e);
}
Ok(res.accepted) Ok(res.accepted)
}, },
} }
@ -102,6 +108,9 @@ impl CodempClient {
.map_err(|_| Status::invalid_argument("could not serialize opseq"))?, .map_err(|_| Status::invalid_argument("could not serialize opseq"))?,
}; };
let res = self.client.edit(req).await?.into_inner(); let res = self.client.edit(req).await?.into_inner();
if let Err(e) = factory.ack(op.clone()).await {
error!("could not ack op '{:?}' : {}", op, e);
}
Ok(res.accepted) Ok(res.accepted)
}, },
} }

View file

@ -1,16 +1,28 @@
use std::collections::VecDeque;
use operational_transform::{OperationSeq, OTError}; use operational_transform::{OperationSeq, OTError};
use similar::TextDiff; use similar::TextDiff;
use tokio::sync::{mpsc, watch, oneshot}; use tokio::sync::{mpsc, watch, oneshot};
use tracing::error; use tracing::{error, warn};
#[derive(Clone)] #[derive(Clone)]
pub struct OperationFactory { pub struct OperationFactory {
content: String, content: String,
queue: VecDeque<OperationSeq>,
} }
impl OperationFactory { impl OperationFactory {
pub fn new(init: Option<String>) -> Self { pub fn new(init: Option<String>) -> Self {
OperationFactory { content: init.unwrap_or(String::new()) } OperationFactory {
content: init.unwrap_or(String::new()),
queue: VecDeque::new(),
}
}
fn apply(&mut self, op: OperationSeq) -> Result<OperationSeq, OTError> {
self.content = op.apply(&self.content)?;
self.queue.push_back(op.clone());
Ok(op)
} }
// TODO remove the need for this // TODO remove the need for this
@ -25,8 +37,7 @@ impl OperationFactory {
pub fn replace(&mut self, txt: &str) -> Result<OperationSeq, OTError> { pub fn replace(&mut self, txt: &str) -> Result<OperationSeq, OTError> {
let mut out = OperationSeq::default(); let mut out = OperationSeq::default();
if self.content == txt { // TODO throw and error rather than wasting everyone's resources if self.content == txt { // TODO throw and error rather than wasting everyone's resources
out.retain(txt.len() as u64); return Err(OTError); // nothing to do
return Ok(out); // nothing to do
} }
let diff = TextDiff::from_chars(self.content.as_str(), txt); let diff = TextDiff::from_chars(self.content.as_str(), txt);
@ -49,8 +60,7 @@ impl OperationFactory {
out.retain(pos); out.retain(pos);
out.insert(txt); out.insert(txt);
out.retain(total - pos); out.retain(total - pos);
self.content = out.apply(&self.content)?; Ok(self.apply(out)?)
Ok(out)
} }
pub fn delete(&mut self, pos: u64, count: u64) -> Result<OperationSeq, OTError> { pub fn delete(&mut self, pos: u64, count: u64) -> Result<OperationSeq, OTError> {
@ -59,8 +69,7 @@ impl OperationFactory {
out.retain(pos - count); out.retain(pos - count);
out.delete(count); out.delete(count);
out.retain(len - pos); out.retain(len - pos);
self.content = out.apply(&self.content)?; Ok(self.apply(out)?)
Ok(out)
} }
pub fn cancel(&mut self, pos: u64, count: u64) -> Result<OperationSeq, OTError> { pub fn cancel(&mut self, pos: u64, count: u64) -> Result<OperationSeq, OTError> {
@ -69,11 +78,25 @@ impl OperationFactory {
out.retain(pos); out.retain(pos);
out.delete(count); out.delete(count);
out.retain(len - (pos+count)); out.retain(len - (pos+count));
self.content = out.apply(&self.content)?; Ok(self.apply(out)?)
Ok(out)
} }
pub fn process(&mut self, op: OperationSeq) -> Result<String, OTError> { pub fn ack(&mut self, op: OperationSeq) -> Result<(), OTError> { // TODO use a different error?
// TODO is manually iterating from behind worth the manual search boilerplate?
for (i, o) in self.queue.iter().enumerate().rev() {
if o == &op {
self.queue.remove(i);
return Ok(());
}
}
warn!("could not ack op {:?} from {:?}", op, self.queue);
Err(OTError)
}
pub fn process(&mut self, mut op: OperationSeq) -> Result<String, OTError> {
for o in self.queue.iter_mut() {
(op, *o) = op.transform(o)?;
}
self.content = op.apply(&self.content)?; self.content = op.apply(&self.content)?;
Ok(self.content.clone()) Ok(self.content.clone())
} }
@ -140,6 +163,12 @@ impl AsyncFactory {
self.ops.send(OpMsg::Process(opseq, tx)).await.map_err(|_| OTError)?; self.ops.send(OpMsg::Process(opseq, tx)).await.map_err(|_| OTError)?;
rx.await.map_err(|_| OTError)? rx.await.map_err(|_| OTError)?
} }
pub async fn ack(&self, opseq: OperationSeq) -> Result<(), OTError> {
let (tx, rx) = oneshot::channel();
self.ops.send(OpMsg::Ack(opseq, tx)).await.map_err(|_| OTError)?;
rx.await.map_err(|_| OTError)?
}
} }
@ -147,6 +176,7 @@ impl AsyncFactory {
enum OpMsg { enum OpMsg {
Exec(OpWrapper, oneshot::Sender<Result<OperationSeq, OTError>>), Exec(OpWrapper, oneshot::Sender<Result<OperationSeq, OTError>>),
Process(OperationSeq, oneshot::Sender<Result<String, OTError>>), Process(OperationSeq, oneshot::Sender<Result<String, OTError>>),
Ack(OperationSeq, oneshot::Sender<Result<(), OTError>>),
} }
#[derive(Debug)] #[derive(Debug)]
@ -175,6 +205,7 @@ impl AsyncFactoryWorker {
match msg { match msg {
OpMsg::Exec(op, tx) => tx.send(self.exec(op)).unwrap_or(()), OpMsg::Exec(op, tx) => tx.send(self.exec(op)).unwrap_or(()),
OpMsg::Process(opseq, tx) => tx.send(self.factory.process(opseq)).unwrap_or(()), OpMsg::Process(opseq, tx) => tx.send(self.factory.process(opseq)).unwrap_or(()),
OpMsg::Ack(opseq, tx) => tx.send(self.factory.ack(opseq)).unwrap_or(()),
} }
if let Err(e) = self.content.send(self.factory.content()) { if let Err(e) = self.content.send(self.factory.content()) {
error!("error updating content: {}", e); error!("error updating content: {}", e);