delta.rs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. use crate::{
  2. core::{operation::*, DeltaIter, FlowyStr, Interval, OperationTransformable, MAX_IV_LEN},
  3. errors::{ErrorBuilder, OTError, OTErrorCode},
  4. };
  5. use bytes::Bytes;
  6. use serde::de::DeserializeOwned;
  7. use std::{
  8. cmp::{min, Ordering},
  9. fmt,
  10. iter::FromIterator,
  11. str,
  12. str::FromStr,
  13. };
  14. // TODO: optimize the memory usage with Arc_mut or Cow
  15. #[derive(Clone, Debug, PartialEq, Eq)]
  16. pub struct Delta<T: Attributes> {
  17. pub ops: Vec<Operation<T>>,
  18. pub base_len: usize,
  19. pub target_len: usize,
  20. }
  21. impl<T> Default for Delta<T>
  22. where
  23. T: Attributes,
  24. {
  25. fn default() -> Self {
  26. Self {
  27. ops: Vec::new(),
  28. base_len: 0,
  29. target_len: 0,
  30. }
  31. }
  32. }
  33. impl<T> fmt::Display for Delta<T>
  34. where
  35. T: Attributes,
  36. {
  37. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  38. // f.write_str(&serde_json::to_string(self).unwrap_or("".to_owned()))?;
  39. f.write_str("[ ")?;
  40. for op in &self.ops {
  41. f.write_fmt(format_args!("{} ", op))?;
  42. }
  43. f.write_str("]")?;
  44. Ok(())
  45. }
  46. }
  47. impl<T> FromIterator<Operation<T>> for Delta<T>
  48. where
  49. T: Attributes,
  50. {
  51. fn from_iter<I: IntoIterator<Item = Operation<T>>>(ops: I) -> Self {
  52. let mut operations = Delta::default();
  53. for op in ops {
  54. operations.add(op);
  55. }
  56. operations
  57. }
  58. }
  59. impl<T> Delta<T>
  60. where
  61. T: Attributes,
  62. {
  63. pub fn new() -> Self { Self::default() }
  64. #[inline]
  65. pub fn with_capacity(capacity: usize) -> Self {
  66. Self {
  67. ops: Vec::with_capacity(capacity),
  68. base_len: 0,
  69. target_len: 0,
  70. }
  71. }
  72. pub fn add(&mut self, op: Operation<T>) {
  73. match op {
  74. Operation::Delete(i) => self.delete(i),
  75. Operation::Insert(i) => self.insert(&i.s, i.attributes),
  76. Operation::Retain(r) => self.retain(r.n, r.attributes),
  77. }
  78. }
  79. pub fn delete(&mut self, n: usize) {
  80. if n == 0 {
  81. return;
  82. }
  83. self.base_len += n as usize;
  84. if let Some(Operation::Delete(n_last)) = self.ops.last_mut() {
  85. *n_last += n;
  86. } else {
  87. self.ops.push(OpBuilder::delete(n).build());
  88. }
  89. }
  90. pub fn insert(&mut self, s: &str, attributes: T) {
  91. let s: FlowyStr = s.into();
  92. if s.is_empty() {
  93. return;
  94. }
  95. self.target_len += s.count_utf16_code_units();
  96. let new_last = match self.ops.as_mut_slice() {
  97. [.., Operation::<T>::Insert(insert)] => {
  98. //
  99. insert.merge_or_new_op(&s, attributes)
  100. },
  101. [.., Operation::<T>::Insert(pre_insert), Operation::Delete(_)] => {
  102. //
  103. pre_insert.merge_or_new_op(&s, attributes)
  104. },
  105. [.., op_last @ Operation::<T>::Delete(_)] => {
  106. let new_last = op_last.clone();
  107. *op_last = OpBuilder::<T>::insert(&s).attributes(attributes).build();
  108. Some(new_last)
  109. },
  110. _ => Some(OpBuilder::<T>::insert(&s).attributes(attributes).build()),
  111. };
  112. match new_last {
  113. None => {},
  114. Some(new_last) => self.ops.push(new_last),
  115. }
  116. }
  117. pub fn retain(&mut self, n: usize, attributes: T) {
  118. if n == 0 {
  119. return;
  120. }
  121. self.base_len += n as usize;
  122. self.target_len += n as usize;
  123. if let Some(Operation::<T>::Retain(retain)) = self.ops.last_mut() {
  124. if let Some(new_op) = retain.merge_or_new(n, attributes) {
  125. self.ops.push(new_op);
  126. }
  127. } else {
  128. self.ops.push(OpBuilder::<T>::retain(n).attributes(attributes).build());
  129. }
  130. }
  131. /// Applies an operation to a string, returning a new string.
  132. pub fn apply(&self, s: &str) -> Result<String, OTError> {
  133. let s: FlowyStr = s.into();
  134. if s.count_utf16_code_units() != self.base_len {
  135. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  136. }
  137. let mut new_s = String::new();
  138. let code_point_iter = &mut s.code_point_iter();
  139. for op in &self.ops {
  140. match &op {
  141. Operation::Retain(retain) => {
  142. for c in code_point_iter.take(retain.n as usize) {
  143. new_s.push_str(str::from_utf8(c.0).unwrap_or(""));
  144. }
  145. },
  146. Operation::Delete(delete) => {
  147. for _ in 0..*delete {
  148. code_point_iter.next();
  149. }
  150. },
  151. Operation::Insert(insert) => {
  152. new_s += &insert.s;
  153. },
  154. }
  155. }
  156. Ok(new_s)
  157. }
  158. /// Computes the inverse of an operation. The inverse of an operation is the
  159. /// operation that reverts the effects of the operation
  160. pub fn invert_str(&self, s: &str) -> Self {
  161. let mut inverted = Delta::default();
  162. let chars = &mut s.chars();
  163. for op in &self.ops {
  164. match &op {
  165. Operation::Retain(retain) => {
  166. inverted.retain(retain.n, T::default());
  167. // TODO: use advance_by instead, but it's unstable now
  168. // chars.advance_by(retain.num)
  169. for _ in 0..retain.n {
  170. chars.next();
  171. }
  172. },
  173. Operation::Insert(insert) => {
  174. inverted.delete(insert.count_of_utf16_code_units());
  175. },
  176. Operation::Delete(delete) => {
  177. inverted.insert(&chars.take(*delete as usize).collect::<String>(), op.get_attributes());
  178. },
  179. }
  180. }
  181. inverted
  182. }
  183. /// Checks if this operation has no effect.
  184. #[inline]
  185. pub fn is_noop(&self) -> bool { matches!(self.ops.as_slice(), [] | [Operation::Retain(_)]) }
  186. pub fn is_empty(&self) -> bool { self.ops.is_empty() }
  187. pub fn extend(&mut self, other: Self) { other.ops.into_iter().for_each(|op| self.add(op)); }
  188. }
  189. impl<T> OperationTransformable for Delta<T>
  190. where
  191. T: Attributes,
  192. {
  193. fn compose(&self, other: &Self) -> Result<Self, OTError>
  194. where
  195. Self: Sized,
  196. {
  197. let mut new_delta = Delta::default();
  198. let mut iter = DeltaIter::new(self);
  199. let mut other_iter = DeltaIter::new(other);
  200. while iter.has_next() || other_iter.has_next() {
  201. if other_iter.is_next_insert() {
  202. new_delta.add(other_iter.next_op().unwrap());
  203. continue;
  204. }
  205. if iter.is_next_delete() {
  206. new_delta.add(iter.next_op().unwrap());
  207. continue;
  208. }
  209. let length = min(
  210. iter.next_op_len().unwrap_or(MAX_IV_LEN),
  211. other_iter.next_op_len().unwrap_or(MAX_IV_LEN),
  212. );
  213. let op = iter
  214. .next_op_with_len(length)
  215. .unwrap_or_else(|| OpBuilder::retain(length).build());
  216. let other_op = other_iter
  217. .next_op_with_len(length)
  218. .unwrap_or_else(|| OpBuilder::retain(length).build());
  219. // debug_assert_eq!(op.len(), other_op.len(), "Composing delta failed,");
  220. match (&op, &other_op) {
  221. (Operation::Retain(retain), Operation::Retain(other_retain)) => {
  222. let composed_attrs = retain.attributes.compose(&other_retain.attributes)?;
  223. new_delta.add(OpBuilder::retain(retain.n).attributes(composed_attrs).build())
  224. },
  225. (Operation::Insert(insert), Operation::Retain(other_retain)) => {
  226. let mut composed_attrs = insert.attributes.compose(&other_retain.attributes)?;
  227. composed_attrs.remove_empty();
  228. new_delta.add(OpBuilder::insert(op.get_data()).attributes(composed_attrs).build())
  229. },
  230. (Operation::Retain(_), Operation::Delete(_)) => {
  231. new_delta.add(other_op);
  232. },
  233. (a, b) => {
  234. debug_assert_eq!(a.is_insert(), true);
  235. debug_assert_eq!(b.is_delete(), true);
  236. continue;
  237. },
  238. }
  239. }
  240. Ok(new_delta)
  241. }
  242. fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
  243. where
  244. Self: Sized,
  245. {
  246. if self.base_len != other.base_len {
  247. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength)
  248. .msg(format!(
  249. "cur base length: {}, other base length: {}",
  250. self.base_len, other.base_len
  251. ))
  252. .build());
  253. }
  254. let mut a_prime = Delta::default();
  255. let mut b_prime = Delta::default();
  256. let mut ops1 = self.ops.iter().cloned();
  257. let mut ops2 = other.ops.iter().cloned();
  258. let mut next_op1 = ops1.next();
  259. let mut next_op2 = ops2.next();
  260. loop {
  261. match (&next_op1, &next_op2) {
  262. (None, None) => break,
  263. (Some(Operation::Insert(insert)), _) => {
  264. // let composed_attrs = transform_attributes(&next_op1, &next_op2, true);
  265. a_prime.insert(&insert.s, insert.attributes.clone());
  266. b_prime.retain(insert.count_of_utf16_code_units(), insert.attributes.clone());
  267. next_op1 = ops1.next();
  268. },
  269. (_, Some(Operation::Insert(o_insert))) => {
  270. let composed_attrs = transform_op_attribute(&next_op1, &next_op2)?;
  271. a_prime.retain(o_insert.count_of_utf16_code_units(), composed_attrs.clone());
  272. b_prime.insert(&o_insert.s, composed_attrs);
  273. next_op2 = ops2.next();
  274. },
  275. (None, _) => {
  276. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  277. },
  278. (_, None) => {
  279. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  280. },
  281. (Some(Operation::Retain(retain)), Some(Operation::Retain(o_retain))) => {
  282. let composed_attrs = transform_op_attribute(&next_op1, &next_op2)?;
  283. match retain.cmp(&o_retain) {
  284. Ordering::Less => {
  285. a_prime.retain(retain.n, composed_attrs.clone());
  286. b_prime.retain(retain.n, composed_attrs.clone());
  287. next_op2 = Some(OpBuilder::retain(o_retain.n - retain.n).build());
  288. next_op1 = ops1.next();
  289. },
  290. Ordering::Equal => {
  291. a_prime.retain(retain.n, composed_attrs.clone());
  292. b_prime.retain(retain.n, composed_attrs.clone());
  293. next_op1 = ops1.next();
  294. next_op2 = ops2.next();
  295. },
  296. Ordering::Greater => {
  297. a_prime.retain(o_retain.n, composed_attrs.clone());
  298. b_prime.retain(o_retain.n, composed_attrs.clone());
  299. next_op1 = Some(OpBuilder::retain(retain.n - o_retain.n).build());
  300. next_op2 = ops2.next();
  301. },
  302. };
  303. },
  304. (Some(Operation::Delete(i)), Some(Operation::Delete(j))) => match i.cmp(&j) {
  305. Ordering::Less => {
  306. next_op2 = Some(OpBuilder::delete(*j - *i).build());
  307. next_op1 = ops1.next();
  308. },
  309. Ordering::Equal => {
  310. next_op1 = ops1.next();
  311. next_op2 = ops2.next();
  312. },
  313. Ordering::Greater => {
  314. next_op1 = Some(OpBuilder::delete(*i - *j).build());
  315. next_op2 = ops2.next();
  316. },
  317. },
  318. (Some(Operation::Delete(i)), Some(Operation::Retain(o_retain))) => {
  319. match i.cmp(&o_retain) {
  320. Ordering::Less => {
  321. a_prime.delete(*i);
  322. next_op2 = Some(OpBuilder::retain(o_retain.n - *i).build());
  323. next_op1 = ops1.next();
  324. },
  325. Ordering::Equal => {
  326. a_prime.delete(*i);
  327. next_op1 = ops1.next();
  328. next_op2 = ops2.next();
  329. },
  330. Ordering::Greater => {
  331. a_prime.delete(o_retain.n);
  332. next_op1 = Some(OpBuilder::delete(*i - o_retain.n).build());
  333. next_op2 = ops2.next();
  334. },
  335. };
  336. },
  337. (Some(Operation::Retain(retain)), Some(Operation::Delete(j))) => {
  338. match retain.cmp(&j) {
  339. Ordering::Less => {
  340. b_prime.delete(retain.n);
  341. next_op2 = Some(OpBuilder::delete(*j - retain.n).build());
  342. next_op1 = ops1.next();
  343. },
  344. Ordering::Equal => {
  345. b_prime.delete(retain.n);
  346. next_op1 = ops1.next();
  347. next_op2 = ops2.next();
  348. },
  349. Ordering::Greater => {
  350. b_prime.delete(*j);
  351. next_op1 = Some(OpBuilder::retain(retain.n - *j).build());
  352. next_op2 = ops2.next();
  353. },
  354. };
  355. },
  356. }
  357. }
  358. Ok((a_prime, b_prime))
  359. }
  360. fn invert(&self, other: &Self) -> Self {
  361. let mut inverted = Delta::default();
  362. if other.is_empty() {
  363. return inverted;
  364. }
  365. tracing::trace!("🌜Calculate invert delta");
  366. tracing::trace!("current: {}", self);
  367. tracing::trace!("other: {}", other);
  368. let mut index = 0;
  369. for op in &self.ops {
  370. let len: usize = op.len() as usize;
  371. match op {
  372. Operation::Delete(n) => {
  373. invert_from_other(&mut inverted, other, op, index, index + *n);
  374. index += len;
  375. },
  376. Operation::Retain(_) => {
  377. match op.has_attribute() {
  378. true => invert_from_other(&mut inverted, other, op, index, index + len),
  379. false => {
  380. tracing::trace!("invert retain: {} by retain {} {}", op, len, op.get_attributes());
  381. inverted.retain(len as usize, op.get_attributes())
  382. },
  383. }
  384. index += len;
  385. },
  386. Operation::Insert(_) => {
  387. tracing::trace!("invert insert: {} by delete {}", op, len);
  388. inverted.delete(len as usize);
  389. },
  390. }
  391. }
  392. tracing::trace!("🌛invert result: {}", inverted);
  393. inverted
  394. }
  395. }
  396. /// Removes trailing retain operation with empty attributes, if present.
  397. pub fn trim<T>(delta: &mut Delta<T>)
  398. where
  399. T: Attributes,
  400. {
  401. if let Some(last) = delta.ops.last() {
  402. if last.is_retain() && last.is_plain() {
  403. delta.ops.pop();
  404. }
  405. }
  406. }
  407. fn invert_from_other<T: Attributes>(
  408. base: &mut Delta<T>,
  409. other: &Delta<T>,
  410. operation: &Operation<T>,
  411. start: usize,
  412. end: usize,
  413. ) {
  414. tracing::trace!("invert op: {} [{}:{}]", operation, start, end);
  415. let other_ops = DeltaIter::from_interval(other, Interval::new(start, end)).ops();
  416. other_ops.into_iter().for_each(|other_op| match operation {
  417. Operation::Delete(n) => {
  418. tracing::trace!("invert delete: {} by add {}", n, other_op);
  419. base.add(other_op);
  420. },
  421. Operation::Retain(_retain) => {
  422. tracing::trace!(
  423. "invert attributes: {:?}, {:?}",
  424. operation.get_attributes(),
  425. other_op.get_attributes()
  426. );
  427. let inverted_attrs = operation.get_attributes().invert(&other_op.get_attributes());
  428. base.retain(other_op.len(), inverted_attrs);
  429. },
  430. Operation::Insert(_) => {
  431. log::error!("Impossible to here. Insert operation should be treated as delete")
  432. },
  433. });
  434. }
  435. fn transform_op_attribute<T: Attributes>(
  436. left: &Option<Operation<T>>,
  437. right: &Option<Operation<T>>,
  438. ) -> Result<T, OTError> {
  439. if left.is_none() {
  440. if right.is_none() {
  441. return Ok(T::default());
  442. }
  443. return Ok(right.as_ref().unwrap().get_attributes());
  444. }
  445. let left = left.as_ref().unwrap().get_attributes();
  446. let right = right.as_ref().unwrap().get_attributes();
  447. // TODO: replace with anyhow and thiserror.
  448. Ok(left.transform(&right)?.0)
  449. }
  450. impl<T> Delta<T>
  451. where
  452. T: Attributes + DeserializeOwned,
  453. {
  454. pub fn from_json(json: &str) -> Result<Self, OTError> {
  455. let delta = serde_json::from_str(json).map_err(|e| {
  456. tracing::trace!("Deserialize failed: {:?}", e);
  457. tracing::trace!("{:?}", json);
  458. e
  459. })?;
  460. Ok(delta)
  461. }
  462. pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, OTError> {
  463. let json = str::from_utf8(bytes.as_ref())?.to_owned();
  464. let val = Self::from_json(&json)?;
  465. Ok(val)
  466. }
  467. }
  468. impl<T> Delta<T>
  469. where
  470. T: Attributes + serde::Serialize,
  471. {
  472. pub fn to_json(&self) -> String { serde_json::to_string(self).unwrap_or_else(|_| "".to_owned()) }
  473. pub fn to_bytes(&self) -> Bytes {
  474. let json = self.to_json();
  475. Bytes::from(json.into_bytes())
  476. }
  477. }
  478. impl<T> FromStr for Delta<T>
  479. where
  480. T: Attributes,
  481. {
  482. type Err = ();
  483. fn from_str(s: &str) -> Result<Delta<T>, Self::Err> {
  484. let mut delta = Delta::with_capacity(1);
  485. delta.add(Operation::Insert(s.into()));
  486. Ok(delta)
  487. }
  488. }
  489. impl<T> std::convert::TryFrom<Vec<u8>> for Delta<T>
  490. where
  491. T: Attributes + DeserializeOwned,
  492. {
  493. type Error = OTError;
  494. fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> { Delta::from_bytes(bytes) }
  495. }
  496. impl<T> std::convert::TryFrom<Bytes> for Delta<T>
  497. where
  498. T: Attributes + DeserializeOwned,
  499. {
  500. type Error = OTError;
  501. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> { Delta::from_bytes(&bytes) }
  502. }