* refactor: make container states can get their ids
* test: add utils to test consistency between the current states and diff calc
* test: add state diff calc consistency check
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.
* feat: use resolved diff as event
* feat: wasm ValueOrContainer event
* fix: cargo fix
* fix: avoid state clone
* chore: add resolve event bench
* test: add handler in event test
* refactor: use resolved as external diff
* chore: cargo fix
* fix: typescript loro value type
* fix: use Arc::new_cyclic
* refactor: bring back sub container
* chore: rm logs
* fix: richtext delta event
* fix(quill): move apply delta to editor part
* fix: consistency issue with Quill
- Quill assumes there is always a \n at the end of the line
- Quill assumes \n does not merge with other delta item
- Quill assumes there is no inline format inside {insert: '\n'} delta
item
* 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
* 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
* feat: checkout to frontiers
* feat: record timestamp
* fix: use unicode len by default for text
now "你好" has length of 2 instead of 6
* chore: rm dbg!
* feat: utf16 event for wasm (draft, buggy)
* fix: work around the issue of unknown text content
* fix: utf16 err
* fix: compose utf16 meta
* docs: add comments