Commit graph

52 commits

Author SHA1 Message Date
Zixuan Chen
f99bfd8e21
Refactor rm unused code (#328)
* chore: init

* fix: fuzz config

* refactor: rm unused code
2024-04-22 21:20:00 +08:00
Zixuan Chen
a32b75ad3b
fix(js): allow convert from undefined to LoroValue (#323) 2024-04-18 09:47:00 +08:00
Zixuan Chen
01b45598d8
refactor: parse uint8array to binary (#320)
and refine Awareness type in ts
2024-04-17 15:09:03 +08:00
Zixuan Chen
454b4088a6
chore(rs): bump versions of rust crates 2024-04-09 16:23:48 +08:00
Zixuan Chen
81ae2abacc
Cursors (#290)
This PR introduces support for retrieving and querying cursors.

## Motivation

Using "index" to denote cursor positions can be unstable, as positions may shift with document edits. To reliably represent a position or range within a document, it is more effective to leverage the unique ID of each item/character in a List CRDT or Text CRDT.

## Updating Cursors

Loro optimizes State metadata by not storing the IDs of deleted elements. This approach, while efficient, complicates tracking cursor positions since they rely on these IDs for precise locations within the document. The solution recalculates position by replaying relevant history to update stable positions accurately. To minimize the performance impact of history replay, the system updates cursor info to reference only the IDs of currently present elements, thereby reducing the need for replay.

Each position has a "Side" information, indicating the actual cursor position is on the left, right, or directly in the center of the target ID.

Note: In JavaScript, the offset returned when querying a Stable Position is based on the UTF-16 index.

# Example

```ts
const loro = new Loro();
const list = loro.getList("list");
list.insert(0, "a");
const pos0 = list.getStablePos(0);
list.insert(1, "b");
{
  const ans = loro.queryStablePos(pos0!);
  expect(ans.offset).toEqual(0);
  expect(ans.side).toEqual(0);
  expect(ans.update).toBeUndefined();
}
list.insert(0, "c");
{
  const ans = loro.queryStablePos(pos0!);
  expect(ans.offset).toEqual(1);
  expect(ans.side).toEqual(0);
  expect(ans.update).toBeUndefined();
}
list.delete(1, 1);
{
  const ans = loro.queryStablePos(pos0!);
  expect(ans.offset).toEqual(1);
  expect(ans.side).toEqual(-1);
  expect(ans.update).toBeDefined();
}
```
2024-04-09 16:01:37 +08:00
Leon Zhao
51890ff8d8
fix: decode iter return result by updating columnar to 0.3.4 (#309) 2024-04-01 17:29:07 +08:00
Zixuan Chen
3c124f454b
feat: access value/container by path (#308)
* feat: access elem by path

* fix: parse tree node index by str

* fix: reuse treeid try_from

* chore: rename methods on lib.rs

---------

Co-authored-by: Leon Zhao <leeeon233@gmail.com>
2024-04-01 17:25:12 +08:00
Zixuan Chen
9ecc0a90b1
refactor!: Add prelim support, making creating sub container easier (#300)
This PR includes a BREAKING CHANGE.

It enables you to create containers before attaching them to the document, making the API more intuitive and straightforward.

A container can be either attached to a document or detached. When it's detached, its history/state does not persist. You can attach a container to a document by inserting it into an attached container. Once a container is attached, its state, along with all of its descendants's states, will be recreated in the document. After attaching, the container and its descendants, will each have their corresponding "attached" version of themselves?

When a detached container x is attached to a document, you can use x.getAttached() to obtain the corresponding attached container.
2024-03-30 11:38:24 +08:00
Zixuan Chen
9a673acfde
feat: add getOrCreate on map (#296) 2024-03-27 17:52:53 +08:00
Zixuan Chen
c8b8694908
refactor: extract common methods on handlers (#295)
Co-authored-by: Leon <leeeon233@gmail.com>
2024-03-25 11:58:51 +08:00
Zixuan Chen
7e07e4dad4
perf: reduce mem usage in fugue span (#283) 2024-03-04 12:02:49 +08:00
Zixuan Chen
08847d6639
refactor: rename client_id in idspan to peer (#287)
* refactor: rename client_id in idspan to peer

* fix: type err
2024-03-02 19:10:33 +08:00
Zixuan Chen
71addcb6de
fix: fix a few legacy bugs (#286) 2024-03-02 13:42:50 +08:00
Zixuan Chen
06e3a5420d
refactor: reduce tracker mem usage by using nonmax id (#282) 2024-02-29 22:55:57 +08:00
Zixuan Chen
b8eb57f4a5
Refactor ID (#274)
* refactor: add idlp and add lamport info to snapshot enc

* fix: fix warnings

* fix: idlp err due to incorrect merge

* fix: comments

* test: fix fuzz
2024-02-27 23:36:17 +08:00
Zixuan Chen
403eb18fa5
feat: expose timestamp related configures (#270)
* feat: expose timestamp related configures

* fix(wasm): set change merge interval api
2024-02-22 20:22:04 +08:00
Zixuan Chen
bd57eb52b1
refactor: replace i32 with i64 (#269) 2024-02-18 17:27:33 +08:00
Zixuan Chen
f648b353ad
chore: upgrade rust crates 2024-02-16 11:30:56 +08:00
Zixuan Chen
57287fa6d8
chore: add pkg info 2024-02-16 11:25:12 +08:00
Leon Zhao
dcbdd55195
feat: remove deleted set in tree state and optimize api (#259)
Co-authored-by: Zixuan Chen <me@zxch3n.com>
2024-01-30 09:54:54 +08:00
Zixuan Chen
0bcc3bd56d
chore: upgrade wasm-bindgen to 0.2.90 (#262) 2024-01-29 22:40:33 +08:00
Zixuan Chen
9e57ccbc00
Fix avoid rich text apply diff err when time travel (#256)
* fix: avoid enter invalid richtext state

* fix: only include the style when the doc contains both style start and style end

* fix: iter_range err in richtext state

* fix: richtext state iter range

* fix: iter range err

* fix: iter range

* chore: rm log

* fix: iter range

* fix: get affected range

* fix: return err if given checkout target is invalid
2024-01-21 19:51:27 +08:00
Zixuan Chen
b5aa622554
fix: how to find best insert pos for richtext & expand type reverse behavior (#250)
* chore: bump loro-crdt version

* fix: find best insert pos

* fix: style anchors' ExpandType's reverse behavior
2024-01-19 22:19:56 +08:00
Zixuan Chen
1295ac6d61
(wasm) Extract VersionVector class and fix inconsistent PeerID repr (#249)
* refactor(wasm): extract VersionVector class and fix inconsistent PeerID in wasm

* fix: example type err

* fix: binding err

* fix: peer id repr should be consistent

* test: update tests
2024-01-18 13:28:28 +08:00
Zixuan Chen
b4701a4de6
refactor: use rich text style config (#246)
* refactor: use rich text style config

* chore: rm log

* feat: support config text style in wasm

* feat: overlapped styles

* chore: add warning style key cannot contain ':'

* test: refine test case for richtext

* test: refine test
2024-01-17 22:55:46 +08:00
Zixuan Chen
00f158c0df
Update loro_value! macro and add binary support (#231)
* docs: update loro_value! doc

* fix: type errors
2024-01-04 22:58:21 +08:00
Zixuan Chen
a6be7d2ea6
refactor: make InternalString an internal struct (#233) 2024-01-02 20:11:22 +08:00
Zixuan Chen
bc27a47531
feat: stabilizing encoding (#219)
This PR implements a new encode schema that is more extendible and more compact. It’s also simpler and takes less binary size and maintaining effort. It is inspired by the [Automerge Encoding Format](https://automerge.org/automerge-binary-format-spec/).

The main motivation is the extensibility. When we integrate a new CRDT algorithm, we don’t want to make a breaking change to the encoding or keep multiple versions of the encoding schema in the code, as it will make our WASM size much larger. We need a stable and extendible encoding schema for our v1.0 version.

This PR also exposes the ops that compose the current container state. For example, now you can make a query about which operation a certain character quickly. This behavior is required in the new snapshot encoding, so it’s included in this PR.

# Encoding Schema

## Header

The header has 22 bytes.

- (0-4 bytes) Magic Bytes: The encoding starts with `loro` as magic bytes.
- (4-20 bytes) Checksum: MD5 checksum of the encoded data, including the header starting from 20th bytes. The checksum is encoded as a 16-byte array. The `checksum` and `magic bytes` fields are trimmed when calculating the checksum.
- (20-21 bytes) Encoding Method (2 bytes, big endian): Multiple encoding methods are available for a specific encoding version.

## Encode Mode: Updates

In this approach, only ops, specifically their historical record, are encoded, while document states are excluded.

Like Automerge's format, we employ columnar encoding for operations and changes.

Previously, operations were ordered by their Operation ID (OpId) before columnar encoding. However, sorting operations based on their respective containers initially enhance compression potential.

## Encode Mode: Snapshot

This mode simultaneously captures document state and historical data. Upon importing a snapshot into a new document, initialization occurs directly from the snapshot, bypassing the need for CRDT-based recalculations.

Unlike previous snapshot encoding methods, the current binary output in snapshot mode is compatible with the updates mode. This enhances the efficiency of importing snapshots into non-empty documents, where initialization via snapshot is infeasible. 

Additionally, when feasible, we leverage the sequence of operations to construct state snapshots. In CRDTs, deducing the specific ops constituting the current container state is feasible. These ops are tagged in relation to the container, facilitating direct state reconstruction from them. This approach, pioneered by Automerge, significantly improves compression efficiency.
2024-01-02 17:03:24 +08:00
Leon zhao
442f2561a1
Remove prelim feature (#215)
* chore: remove prelim temporarily
2023-12-13 10:25:00 +08:00
Zixuan Chen
cd8be80a6f
docs: refine rust api doc 2023-11-28 23:20:57 +08:00
Zixuan Chen
564dde7703
chore: publish mvp rust api 2023-11-28 21:29:11 +08:00
Zixuan Chen
6ef1e12d71
chore: rm zerovec 2023-11-28 21:01:01 +08:00
Zixuan Chen
f799da9abb
fix: avoid i32 overflow 2023-11-16 22:25:37 +08:00
Zixuan Chen
1ff1505933
feat: get sub container directly when getting value (#175) 2023-11-16 21:46:57 +08:00
leeeon233
c4b753dfd8 chore: add license 2023-11-12 23:23:12 +08:00
Leon zhao
acafc76aff
Feat: diff calc bring back & tree new event and value (#149)
* feat: new tree state

* fix: emit meta event

* fix: semantic tree event

* fix: diff calc bring_back

* chore: clear comments

* fix: merge

* fix: tree snapshot

* fix: filter empty bring back

* feat: tree add external diff

* fix: imported changes were not mergeable (#147)

* fix: imported changes were not mergeable
now the small encoding size is supported in example

* fix: stupid err in richtext checkout

* fix: rle oplog encode err
- support pending changes
- start counters were wrong

* fix: utf16 query err (#151)

* fix: tree movable node lamport

* fix: merge

* perf: bring back

* doc: add deep value meta doc

* refactor: bring back only when record diff

---------

Co-authored-by: Zixuan Chen <remch183@outlook.com>
2023-11-05 15:53:33 +08:00
Zixuan Chen
da16b8a99d
refactor: use better repr for container id (#144) 2023-11-02 17:13:08 +08:00
Zixuan Chen
a40b5c6e4a
feat: support richtext in wasm & mark text with arbitrary value (#142)
- Support mark text with custom value [LORO-299] Allow users to mark text with custom value #139
- Expose richtext in wasm
2023-11-02 14:20:34 +08:00
Zixuan Chen
c1d732b57c
Fix event hint error (#129)
* fix: event hint err

* fix: delete event hint
2023-10-31 10:00:54 +08:00
Zixuan Chen
45490f3370
Perf: reduce mem usage when transaction is large (#128)
* perf: reduce mem usage when txn is large

* test: add diagnose info for id_to_cursor

* perf: make id span easier to merge
2023-10-30 21:56:33 +08:00
Zixuan Chen
8293347334
Feat: autocommit transaction (#127)
* feat: auto commit

* fix: make recursive single thread event work again
2023-10-30 18:32:36 +08:00
Leon zhao
e01e98411c
feat: movable tree support (#120)
* feat: tree state

* feat: tree value

* feat: tree handler

* fix: tree diff

* test: fuzz tree

* feat: tree snapshot

* fix: tree default value

* fix: test new node

* fix: tree diff

* fix: tree unresolved value

* fix: tree fuzz

* fix: tree fuzz move

* fix: sort by tree id

* fix: tree diff sorted by lamport

* fix: sort roots before tree converted to string

* fix: rebase main

* fix: tree fuzz

* fix: delete undo

* fix: tree to json children sorted

* fix: diff calculate

* fix: diff cycle move

* fix: tree old parent cache

* feat: cache

* fix: local op add tree cache

* fix: don't add same tree move to cache

* fix: need update cache

* feat: new cache

* bench: add checkout bench

* chore: clean

* fix: apply node uncheck

* perf: lamport bound

* fix: calc old parent

* feat: tree wasm

* fix: change tree diff

* fix: tree diff retreat

* fix: tree diff should not apply when add node

* feat: new tree loro value

* chore: typo

* fix: tree deep value

* fix: snapshot tree index -1

* fix: decode tree snapshot use state

* fix: release state lock when emit event

* fix: tree node meta container

* fix: need set map container when covert to local tree op

* fix: tree value add deleted

* fix: more then one op in a change

* fix: tree fuzz deleted equal

* fix: tree calc min lamport

* feat: tree encoding v2

* doc: movable tree

* fix: test tree meta

* test: remove import bytes check

* refactor: diff of text and map

* refactor: del span

* perf: tree state use deleted cache

* fix: some details

* fix: loro js tree create

* feat: add un exist tree node

* bench:  tree depth

* fix: check out should emit event

* refactor: event

* fix: fuzz err

* fix: pass all tests

* fix: fuzz err

* fix: list child cache err

* chore: rm debug code

* fix: encode enhanced err

* fix: encode enchanced

* fix: fix several richtext issue

* fix: richtext anchor err

* chore: rm debug code

* fix: richtext fuzz err

* feat: speedup text snapshot decode

* perf: optimize snapshot encoding

* perf: speed up decode & insert

* fix: fugue span merge err

* perf: speedup delete & id cursor map

* fix: fugue merge err

* chore: update utils

* fix: fix merge

* fix: return err apply op

* fix: fix merge

* fix: get map container as tree meta
2023-10-30 11:13:52 +08:00
Zixuan Chen
d942e3d7a2
Feat: Peritext-like rich text support (#123)
* feat: richtext wip

* feat: add insert to style range map wip

* feat: richtext state

* fix: fix style state inserting and style map

* fix: tiny vec merge err

* fix: comment err

* refactor: use new generic-btree & refine impl

* feat: fugue tracker

* feat: tracker

* feat: tracker

* fix: fix a few err in impl

* feat: init richtext content state

* feat: refactor arena

* feat: extract anchor_type info out of style flag

* refactor: state apply op more efficiently
we can now reuse the repr in state and op

* fix: new clippy errors

* refactor: use state chunk as delta item

* refactor: use two op to insert style start and style end

* feat: diff calc

* feat: handler

* fix: tracker checkout err

* fix: pass basic richtext handler tests

* fix: pass handler basic marking tests

* fix: pass all peritext criteria

* feat: snapshot encoding for richtext init

* refactor: replace Text with Richtext

* refacotr: rm text code

* fix: richtext checkout err

* refactor: diff of text and map

* refactor: del span

* refactor: event

* fix: fuzz err

* fix: pass all tests

* fix: fuzz err

* fix: list child cache err

* chore: rm debug code

* fix: encode enhanced err

* fix: encode enchanced

* fix: fix several richtext issue

* fix: richtext anchor err

* chore: rm debug code

* fix: richtext fuzz err

* feat: speedup text snapshot decode

* perf: optimize snapshot encoding

* perf: speed up decode & insert

* fix: fugue span merge err

* perf: speedup delete & id cursor map

* fix: fugue merge err

* chore: update utils

* perf: speedup text insert / del

* fix: cursor cache

* perf: reduce conversion by introducing InsertText

* perf: speed up by refined cursor cache

* chore: update gbtree dep

* refactor(wasm): use quill delta format

* chore: fix warnings
2023-10-29 14:02:13 +08:00
leeeon233
331dc6c994 feat: LoroValue Binary 2023-09-12 15:57:06 +08:00
Zixuan Chen
f208744ec1
feat: encode/decode v2 2023-08-29 15:13:52 +08:00
Zixuan Chen
5ee860b74e chore: use serde 1 2023-08-04 11:09:32 +08:00
Zixuan Chen
b5c325b490 feat: event (buggy) 2023-07-22 19:02:22 +08:00
Zixuan Chen
16ec59ddee fix: fix a few recursive_refactored bug 2023-07-17 19:09:18 +08:00
Zixuan Chen
e993f1b155 feat: basic snapshot encoding 2023-07-17 12:27:11 +08:00
Zixuan Chen
6983a2b00c refactor: mov loro value to loro_common 2023-07-15 00:47:47 +08:00