delta.rs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. use crate::errors::{ErrorBuilder, OTError, OTErrorCode};
  2. use crate::core::delta::{DeltaIterator, MAX_IV_LEN};
  3. use crate::core::flowy_str::OTString;
  4. use crate::core::interval::Interval;
  5. use crate::core::operation::{Attributes, Operation, OperationTransform, PhantomAttributes};
  6. use bytes::Bytes;
  7. use serde::de::DeserializeOwned;
  8. use std::{
  9. cmp::{min, Ordering},
  10. fmt,
  11. iter::FromIterator,
  12. str,
  13. str::FromStr,
  14. };
  15. pub type PlainTextDelta = Delta<PhantomAttributes>;
  16. /// A [Delta] contains list of operations that consists of 'Retain', 'Delete' and 'Insert' operation.
  17. /// Check out the [Operation] for more details. It describes the document as a sequence of
  18. /// operations.
  19. ///
  20. /// If the [T] supports 'serde', that will enable delta to serialize to JSON or deserialize from
  21. /// a JSON string.
  22. ///
  23. #[derive(Clone, Debug, PartialEq, Eq)]
  24. pub struct Delta<T: Attributes> {
  25. pub ops: Vec<Operation<T>>,
  26. /// 'Delete' and 'Retain' operation will update the [utf16_base_len]
  27. /// Transforming the other delta, it requires the utf16_base_len must be equal.
  28. pub utf16_base_len: usize,
  29. /// Represents the current len of the delta.
  30. /// 'Insert' and 'Retain' operation will update the [utf16_target_len]
  31. pub utf16_target_len: usize,
  32. }
  33. impl<T> Default for Delta<T>
  34. where
  35. T: Attributes,
  36. {
  37. fn default() -> Self {
  38. Self {
  39. ops: Vec::new(),
  40. utf16_base_len: 0,
  41. utf16_target_len: 0,
  42. }
  43. }
  44. }
  45. impl<T> fmt::Display for Delta<T>
  46. where
  47. T: Attributes,
  48. {
  49. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  50. // f.write_str(&serde_json::to_string(self).unwrap_or("".to_owned()))?;
  51. f.write_str("[ ")?;
  52. for op in &self.ops {
  53. f.write_fmt(format_args!("{} ", op))?;
  54. }
  55. f.write_str("]")?;
  56. Ok(())
  57. }
  58. }
  59. impl<T> FromIterator<Operation<T>> for Delta<T>
  60. where
  61. T: Attributes,
  62. {
  63. fn from_iter<I: IntoIterator<Item = Operation<T>>>(ops: I) -> Self {
  64. let mut operations = Delta::default();
  65. for op in ops {
  66. operations.add(op);
  67. }
  68. operations
  69. }
  70. }
  71. impl<T> Delta<T>
  72. where
  73. T: Attributes,
  74. {
  75. pub fn new() -> Self {
  76. Self::default()
  77. }
  78. #[inline]
  79. pub fn with_capacity(capacity: usize) -> Self {
  80. Self {
  81. ops: Vec::with_capacity(capacity),
  82. utf16_base_len: 0,
  83. utf16_target_len: 0,
  84. }
  85. }
  86. /// Adding an operation. It will be added in sequence.
  87. pub fn add(&mut self, op: Operation<T>) {
  88. match op {
  89. Operation::Delete(i) => self.delete(i),
  90. Operation::Insert(i) => self.insert(&i.s, i.attributes),
  91. Operation::Retain(r) => self.retain(r.n, r.attributes),
  92. }
  93. }
  94. /// Creating a [Delete] operation with len [n]
  95. pub fn delete(&mut self, n: usize) {
  96. if n == 0 {
  97. return;
  98. }
  99. self.utf16_base_len += n as usize;
  100. if let Some(Operation::Delete(n_last)) = self.ops.last_mut() {
  101. *n_last += n;
  102. } else {
  103. self.ops.push(Operation::delete(n));
  104. }
  105. }
  106. /// Creating a [Insert] operation with string, [s].
  107. pub fn insert(&mut self, s: &str, attributes: T) {
  108. let s: OTString = s.into();
  109. if s.is_empty() {
  110. return;
  111. }
  112. self.utf16_target_len += s.utf16_len();
  113. let new_last = match self.ops.as_mut_slice() {
  114. [.., Operation::<T>::Insert(insert)] => {
  115. //
  116. insert.merge_or_new_op(&s, attributes)
  117. }
  118. [.., Operation::<T>::Insert(pre_insert), Operation::Delete(_)] => {
  119. //
  120. pre_insert.merge_or_new_op(&s, attributes)
  121. }
  122. [.., op_last @ Operation::<T>::Delete(_)] => {
  123. let new_last = op_last.clone();
  124. *op_last = Operation::<T>::insert_with_attributes(&s, attributes);
  125. Some(new_last)
  126. }
  127. _ => Some(Operation::<T>::insert_with_attributes(&s, attributes)),
  128. };
  129. match new_last {
  130. None => {}
  131. Some(new_last) => self.ops.push(new_last),
  132. }
  133. }
  134. /// Creating a [Retain] operation with len, [n].
  135. pub fn retain(&mut self, n: usize, attributes: T) {
  136. if n == 0 {
  137. return;
  138. }
  139. self.utf16_base_len += n as usize;
  140. self.utf16_target_len += n as usize;
  141. if let Some(Operation::<T>::Retain(retain)) = self.ops.last_mut() {
  142. if let Some(new_op) = retain.merge_or_new(n, attributes) {
  143. self.ops.push(new_op);
  144. }
  145. } else {
  146. self.ops.push(Operation::<T>::retain_with_attributes(n, attributes));
  147. }
  148. }
  149. /// Return the a new string described by this delta. The new string will contains the input string.
  150. /// The length of the [applied_str] must be equal to the the [utf16_base_len].
  151. ///
  152. /// # Arguments
  153. ///
  154. /// * `applied_str`: A string represents the utf16_base_len content. it will be consumed by the [retain]
  155. /// or [delete] operations.
  156. ///
  157. ///
  158. /// # Examples
  159. ///
  160. /// ```
  161. /// use lib_ot::core::PlainTextDeltaBuilder;
  162. /// let s = "hello";
  163. /// let delta_a = PlainTextDeltaBuilder::new().insert(s).build();
  164. /// let delta_b = PlainTextDeltaBuilder::new()
  165. /// .retain(s.len())
  166. /// .insert(", AppFlowy")
  167. /// .build();
  168. ///
  169. /// let after_a = delta_a.content_str().unwrap();
  170. /// let after_b = delta_b.apply(&after_a).unwrap();
  171. /// assert_eq!("hello, AppFlowy", &after_b);
  172. /// ```
  173. pub fn apply(&self, applied_str: &str) -> Result<String, OTError> {
  174. let applied_str: OTString = applied_str.into();
  175. if applied_str.utf16_len() != self.utf16_base_len {
  176. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength)
  177. .msg(format!(
  178. "Expected: {}, but received: {}",
  179. self.utf16_base_len,
  180. applied_str.utf16_len()
  181. ))
  182. .build());
  183. }
  184. let mut new_s = String::new();
  185. let code_point_iter = &mut applied_str.utf16_iter();
  186. for op in &self.ops {
  187. match &op {
  188. Operation::Retain(retain) => {
  189. for c in code_point_iter.take(retain.n as usize) {
  190. new_s.push_str(str::from_utf8(c.0).unwrap_or(""));
  191. }
  192. }
  193. Operation::Delete(delete) => {
  194. for _ in 0..*delete {
  195. code_point_iter.next();
  196. }
  197. }
  198. Operation::Insert(insert) => {
  199. new_s += &insert.s;
  200. }
  201. }
  202. }
  203. Ok(new_s)
  204. }
  205. /// Computes the inverse [Delta]. The inverse of an operation is the
  206. /// operation that reverts the effects of the operation
  207. /// # Arguments
  208. ///
  209. /// * `inverted_s`: A string represents the utf16_base_len content. The len of [inverted_s]
  210. /// must equal to the [utf16_base_len], it will be consumed by the [retain] or [delete] operations.
  211. ///
  212. /// If the delta's operations just contain a insert operation. The inverted_s must be empty string.
  213. ///
  214. /// # Examples
  215. ///
  216. /// ```
  217. /// use lib_ot::core::PlainTextDeltaBuilder;
  218. /// let s = "hello world";
  219. /// let delta = PlainTextDeltaBuilder::new().insert(s).build();
  220. /// let invert_delta = delta.invert_str(s);
  221. /// assert_eq!(delta.utf16_base_len, invert_delta.utf16_target_len);
  222. /// assert_eq!(delta.utf16_target_len, invert_delta.utf16_base_len);
  223. ///
  224. /// assert_eq!(invert_delta.apply(s).unwrap(), "")
  225. ///
  226. /// ```
  227. ///
  228. pub fn invert_str(&self, inverted_s: &str) -> Self {
  229. let mut inverted = Delta::default();
  230. let inverted_s: OTString = inverted_s.into();
  231. let code_point_iter = &mut inverted_s.utf16_iter();
  232. for op in &self.ops {
  233. match &op {
  234. Operation::Retain(retain) => {
  235. inverted.retain(retain.n, T::default());
  236. for _ in 0..retain.n {
  237. code_point_iter.next();
  238. }
  239. }
  240. Operation::Insert(insert) => {
  241. inverted.delete(insert.utf16_size());
  242. }
  243. Operation::Delete(delete) => {
  244. let bytes = code_point_iter
  245. .take(*delete as usize)
  246. .into_iter()
  247. .flat_map(|a| str::from_utf8(a.0).ok())
  248. .collect::<String>();
  249. inverted.insert(&bytes, op.get_attributes());
  250. }
  251. }
  252. }
  253. inverted
  254. }
  255. /// Return true if the delta doesn't contain any [Insert] or [Delete] operations.
  256. pub fn is_noop(&self) -> bool {
  257. matches!(self.ops.as_slice(), [] | [Operation::Retain(_)])
  258. }
  259. pub fn is_empty(&self) -> bool {
  260. self.ops.is_empty()
  261. }
  262. pub fn extend(&mut self, other: Self) {
  263. other.ops.into_iter().for_each(|op| self.add(op));
  264. }
  265. }
  266. impl<T> OperationTransform for Delta<T>
  267. where
  268. T: Attributes,
  269. {
  270. fn compose(&self, other: &Self) -> Result<Self, OTError>
  271. where
  272. Self: Sized,
  273. {
  274. let mut new_delta = Delta::default();
  275. let mut iter = DeltaIterator::new(self);
  276. let mut other_iter = DeltaIterator::new(other);
  277. while iter.has_next() || other_iter.has_next() {
  278. if other_iter.is_next_insert() {
  279. new_delta.add(other_iter.next_op().unwrap());
  280. continue;
  281. }
  282. if iter.is_next_delete() {
  283. new_delta.add(iter.next_op().unwrap());
  284. continue;
  285. }
  286. let length = min(
  287. iter.next_op_len().unwrap_or(MAX_IV_LEN),
  288. other_iter.next_op_len().unwrap_or(MAX_IV_LEN),
  289. );
  290. let op = iter
  291. .next_op_with_len(length)
  292. .unwrap_or_else(|| Operation::retain(length));
  293. let other_op = other_iter
  294. .next_op_with_len(length)
  295. .unwrap_or_else(|| Operation::retain(length));
  296. // debug_assert_eq!(op.len(), other_op.len(), "Composing delta failed,");
  297. match (&op, &other_op) {
  298. (Operation::Retain(retain), Operation::Retain(other_retain)) => {
  299. let composed_attrs = retain.attributes.compose(&other_retain.attributes)?;
  300. new_delta.add(Operation::retain_with_attributes(retain.n, composed_attrs))
  301. }
  302. (Operation::Insert(insert), Operation::Retain(other_retain)) => {
  303. let mut composed_attrs = insert.attributes.compose(&other_retain.attributes)?;
  304. composed_attrs.remove_empty();
  305. new_delta.add(Operation::insert_with_attributes(op.get_data(), composed_attrs))
  306. }
  307. (Operation::Retain(_), Operation::Delete(_)) => {
  308. new_delta.add(other_op);
  309. }
  310. (a, b) => {
  311. debug_assert!(a.is_insert());
  312. debug_assert!(b.is_delete());
  313. continue;
  314. }
  315. }
  316. }
  317. Ok(new_delta)
  318. }
  319. fn transform(&self, other: &Self) -> Result<(Self, Self), OTError>
  320. where
  321. Self: Sized,
  322. {
  323. if self.utf16_base_len != other.utf16_base_len {
  324. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength)
  325. .msg(format!(
  326. "cur base length: {}, other base length: {}",
  327. self.utf16_base_len, other.utf16_base_len
  328. ))
  329. .build());
  330. }
  331. let mut a_prime = Delta::default();
  332. let mut b_prime = Delta::default();
  333. let mut ops1 = self.ops.iter().cloned();
  334. let mut ops2 = other.ops.iter().cloned();
  335. let mut next_op1 = ops1.next();
  336. let mut next_op2 = ops2.next();
  337. loop {
  338. match (&next_op1, &next_op2) {
  339. (None, None) => break,
  340. (Some(Operation::Insert(insert)), _) => {
  341. // let composed_attrs = transform_attributes(&next_op1, &next_op2, true);
  342. a_prime.insert(&insert.s, insert.attributes.clone());
  343. b_prime.retain(insert.utf16_size(), insert.attributes.clone());
  344. next_op1 = ops1.next();
  345. }
  346. (_, Some(Operation::Insert(o_insert))) => {
  347. let composed_attrs = transform_op_attribute(&next_op1, &next_op2)?;
  348. a_prime.retain(o_insert.utf16_size(), composed_attrs.clone());
  349. b_prime.insert(&o_insert.s, composed_attrs);
  350. next_op2 = ops2.next();
  351. }
  352. (None, _) => {
  353. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  354. }
  355. (_, None) => {
  356. return Err(ErrorBuilder::new(OTErrorCode::IncompatibleLength).build());
  357. }
  358. (Some(Operation::Retain(retain)), Some(Operation::Retain(o_retain))) => {
  359. let composed_attrs = transform_op_attribute(&next_op1, &next_op2)?;
  360. match retain.cmp(o_retain) {
  361. Ordering::Less => {
  362. a_prime.retain(retain.n, composed_attrs.clone());
  363. b_prime.retain(retain.n, composed_attrs.clone());
  364. next_op2 = Some(Operation::retain(o_retain.n - retain.n));
  365. next_op1 = ops1.next();
  366. }
  367. Ordering::Equal => {
  368. a_prime.retain(retain.n, composed_attrs.clone());
  369. b_prime.retain(retain.n, composed_attrs.clone());
  370. next_op1 = ops1.next();
  371. next_op2 = ops2.next();
  372. }
  373. Ordering::Greater => {
  374. a_prime.retain(o_retain.n, composed_attrs.clone());
  375. b_prime.retain(o_retain.n, composed_attrs.clone());
  376. next_op1 = Some(Operation::retain(retain.n - o_retain.n));
  377. next_op2 = ops2.next();
  378. }
  379. };
  380. }
  381. (Some(Operation::Delete(i)), Some(Operation::Delete(j))) => match i.cmp(j) {
  382. Ordering::Less => {
  383. next_op2 = Some(Operation::delete(*j - *i));
  384. next_op1 = ops1.next();
  385. }
  386. Ordering::Equal => {
  387. next_op1 = ops1.next();
  388. next_op2 = ops2.next();
  389. }
  390. Ordering::Greater => {
  391. next_op1 = Some(Operation::delete(*i - *j));
  392. next_op2 = ops2.next();
  393. }
  394. },
  395. (Some(Operation::Delete(i)), Some(Operation::Retain(o_retain))) => {
  396. match i.cmp(o_retain) {
  397. Ordering::Less => {
  398. a_prime.delete(*i);
  399. next_op2 = Some(Operation::retain(o_retain.n - *i));
  400. next_op1 = ops1.next();
  401. }
  402. Ordering::Equal => {
  403. a_prime.delete(*i);
  404. next_op1 = ops1.next();
  405. next_op2 = ops2.next();
  406. }
  407. Ordering::Greater => {
  408. a_prime.delete(o_retain.n);
  409. next_op1 = Some(Operation::delete(*i - o_retain.n));
  410. next_op2 = ops2.next();
  411. }
  412. };
  413. }
  414. (Some(Operation::Retain(retain)), Some(Operation::Delete(j))) => {
  415. match retain.cmp(j) {
  416. Ordering::Less => {
  417. b_prime.delete(retain.n);
  418. next_op2 = Some(Operation::delete(*j - retain.n));
  419. next_op1 = ops1.next();
  420. }
  421. Ordering::Equal => {
  422. b_prime.delete(retain.n);
  423. next_op1 = ops1.next();
  424. next_op2 = ops2.next();
  425. }
  426. Ordering::Greater => {
  427. b_prime.delete(*j);
  428. next_op1 = Some(Operation::retain(retain.n - *j));
  429. next_op2 = ops2.next();
  430. }
  431. };
  432. }
  433. }
  434. }
  435. Ok((a_prime, b_prime))
  436. }
  437. fn invert(&self, other: &Self) -> Self {
  438. let mut inverted = Delta::default();
  439. let mut index = 0;
  440. for op in &self.ops {
  441. let len: usize = op.len() as usize;
  442. match op {
  443. Operation::Delete(n) => {
  444. invert_other(&mut inverted, other, op, index, index + *n);
  445. index += len;
  446. }
  447. Operation::Retain(_) => {
  448. match op.has_attribute() {
  449. true => invert_other(&mut inverted, other, op, index, index + len),
  450. false => {
  451. // tracing::trace!("invert retain: {} by retain {} {}", op, len,
  452. // op.get_attributes());
  453. inverted.retain(len as usize, op.get_attributes())
  454. }
  455. }
  456. index += len;
  457. }
  458. Operation::Insert(_) => {
  459. // tracing::trace!("invert insert: {} by delete {}", op, len);
  460. inverted.delete(len as usize);
  461. }
  462. }
  463. }
  464. inverted
  465. }
  466. }
  467. /// Removes trailing retain operation with empty attributes, if present.
  468. pub fn trim<T>(delta: &mut Delta<T>)
  469. where
  470. T: Attributes,
  471. {
  472. if let Some(last) = delta.ops.last() {
  473. if last.is_retain() && last.is_plain() {
  474. delta.ops.pop();
  475. }
  476. }
  477. }
  478. fn invert_other<T: Attributes>(
  479. base: &mut Delta<T>,
  480. other: &Delta<T>,
  481. operation: &Operation<T>,
  482. start: usize,
  483. end: usize,
  484. ) {
  485. tracing::trace!("invert op: {} [{}:{}]", operation, start, end);
  486. let other_ops = DeltaIterator::from_interval(other, Interval::new(start, end)).ops();
  487. other_ops.into_iter().for_each(|other_op| match operation {
  488. Operation::Delete(_n) => {
  489. // tracing::trace!("invert delete: {} by add {}", n, other_op);
  490. base.add(other_op);
  491. }
  492. Operation::Retain(_retain) => {
  493. tracing::trace!(
  494. "invert attributes: {:?}, {:?}",
  495. operation.get_attributes(),
  496. other_op.get_attributes()
  497. );
  498. let inverted_attrs = operation.get_attributes().invert(&other_op.get_attributes());
  499. base.retain(other_op.len(), inverted_attrs);
  500. }
  501. Operation::Insert(_) => {
  502. log::error!("Impossible to here. Insert operation should be treated as delete")
  503. }
  504. });
  505. }
  506. fn transform_op_attribute<T: Attributes>(
  507. left: &Option<Operation<T>>,
  508. right: &Option<Operation<T>>,
  509. ) -> Result<T, OTError> {
  510. if left.is_none() {
  511. if right.is_none() {
  512. return Ok(T::default());
  513. }
  514. return Ok(right.as_ref().unwrap().get_attributes());
  515. }
  516. let left = left.as_ref().unwrap().get_attributes();
  517. let right = right.as_ref().unwrap().get_attributes();
  518. // TODO: replace with anyhow and thiserror.
  519. Ok(left.transform(&right)?.0)
  520. }
  521. impl<T> Delta<T>
  522. where
  523. T: Attributes + DeserializeOwned,
  524. {
  525. /// # Examples
  526. ///
  527. /// ```
  528. /// use lib_ot::core::DeltaBuilder;
  529. /// use lib_ot::rich_text::{RichTextDelta};
  530. /// let json = r#"[
  531. /// {"retain":7,"attributes":{"bold":null}}
  532. /// ]"#;
  533. /// let delta = RichTextDelta::from_json_str(json).unwrap();
  534. /// assert_eq!(delta.to_json_str(), r#"[{"retain":7,"attributes":{"bold":""}}]"#);
  535. /// ```
  536. pub fn from_json_str(json: &str) -> Result<Self, OTError> {
  537. let delta = serde_json::from_str(json).map_err(|e| {
  538. tracing::trace!("Deserialize failed: {:?}", e);
  539. tracing::trace!("{:?}", json);
  540. e
  541. })?;
  542. Ok(delta)
  543. }
  544. pub fn from_bytes<B: AsRef<[u8]>>(bytes: B) -> Result<Self, OTError> {
  545. let json = str::from_utf8(bytes.as_ref())?.to_owned();
  546. let val = Self::from_json_str(&json)?;
  547. Ok(val)
  548. }
  549. }
  550. impl<T> Delta<T>
  551. where
  552. T: Attributes + serde::Serialize,
  553. {
  554. pub fn to_json_str(&self) -> String {
  555. serde_json::to_string(self).unwrap_or_else(|_| "".to_owned())
  556. }
  557. pub fn content_str(&self) -> Result<String, OTError> {
  558. self.apply("")
  559. }
  560. pub fn to_json_bytes(&self) -> Bytes {
  561. let json = self.to_json_str();
  562. Bytes::from(json.into_bytes())
  563. }
  564. }
  565. impl<T> FromStr for Delta<T>
  566. where
  567. T: Attributes,
  568. {
  569. type Err = ();
  570. fn from_str(s: &str) -> Result<Delta<T>, Self::Err> {
  571. let mut delta = Delta::with_capacity(1);
  572. delta.add(Operation::Insert(s.into()));
  573. Ok(delta)
  574. }
  575. }
  576. impl<T> std::convert::TryFrom<Vec<u8>> for Delta<T>
  577. where
  578. T: Attributes + DeserializeOwned,
  579. {
  580. type Error = OTError;
  581. fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
  582. Delta::from_bytes(bytes)
  583. }
  584. }
  585. impl<T> std::convert::TryFrom<Bytes> for Delta<T>
  586. where
  587. T: Attributes + DeserializeOwned,
  588. {
  589. type Error = OTError;
  590. fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
  591. Delta::from_bytes(&bytes)
  592. }
  593. }