Saving, Appending and Recovery
About 1334 words
UInk has two ways to save. A full save rewrites the whole file; an incremental write only appends complete blocks to the end of the file, which is fast, and a crash loses at most the last unfinished block. Incremental writes come in two kinds: appending content blocks to the last Canvas, and appending a complete new version of any page to the end (page append).
Levels
- Core readers: crash recovery (FILE-16, FILE-17) and "for the same page, the last version to appear wins" (FILE-8).
- Writers: incremental write and page append are both optional; an app may do only full saves.
- Full saveSAVE-1, SAVE-2Write a temporary file first, verify it, then replace the original file as a whole. The new file is compact, and the statistics in the Header are recomputed.Whole file replaced
- Headerrecomputed
- P1
- P2′
- P3
- Append to the last CanvasSAVE-6–SAVE-8Each time the pen lifts, that stroke is appended to the end of the file as a complete block; it belongs to the last Canvas (page 3). Not a single earlier byte changes.
- Headerunchanged
- P1
- P2
- P3 Canvas
- Ink
- Inkappended
- Page appendSAVE-10, SAVE-15After page 2 changes, its complete new version P2′ is appended to the end. The old P2 stays in the file but is no longer shown, and is removed at the next compaction.
- Headerunchanged
- P1
- P2superseded
- P3
- P2′appended
P1, P2 and P3 each stand for one page: a Canvas and the content blocks that follow it.
Full save
- SAVE-1 A full save must first write a temporary file, verify it, and then replace the target file as a whole; it must not read and write the original file at the same time.
- SAVE-2 A full save outputs a compact file: each Canvas unique key appears only once, undone content is removed,
contentIdandundoIdare renumbered, and the statistics in the Header are recalculated. - SAVE-3 Canvases are recommended to be ordered by Workspace registration order,
pageIndex, Device registration order andlayerIndex, which makes streaming reads and manual inspection easier. - SAVE-4 Content that can still be restored by undo must be kept: old intervals hidden by Clear (Inkeys extension) and originals hidden by
renderOnlyWhenLatest(Full). UInk does not save redo. - SAVE-5 When a resource package exists, commit the resource package first according to the resource package commit order, and atomically replace the main file last.
Appending to the last Canvas
Used for real-time protection while writing: every time the pen is lifted, append that stroke to the end of the file.
- SAVE-6 Only complete blocks may be appended. An unfinished stroke must not be written, and part of a Map must not be written to be completed later.
- SAVE-7 Only the last Canvas in the file may receive further content blocks.
- SAVE-8 The
contentIdof a new block continues from the last number in this Canvas;undoIdmust not be smaller than that of the previous block. When appending a Clear, use a new, largerundoId. - SAVE-9 Erase Ink, Shape, Media and Clear can all be appended this way. An ordinary eraser that cuts existing strokes cannot be saved this way (INK-16). To save the result, append a new version of the entire page or perform a full save.
Page append
Used for multi-page files such as whiteboards and PowerPoint: after a page changes, append a complete new version of that page to the end of the file, without rewriting the other pages.
- Header
- P1
pageIndex 0 - P2
pageIndex 1 - P3
pageIndex 2
1. Write three pagesAfter a full save, the file holds P1, P2 and P3 in order, each a Canvas followed by its content blocks.
- SAVE-10 What is appended is a complete Canvas with all its content blocks. When a reader meets the same Canvas unique key, the last one to appear wins (FILE-8).
- SAVE-11 When a page has several Canvases on several Devices or several layers, each Canvas is replaced separately; only the changed ones need to be appended.
- SAVE-12 The
contentIdandundoIdof the new Canvas version both start from0. - SAVE-13 A new page is appended the same way: a Canvas with a new
pageGuid. Once it is the last Canvas, content blocks can continue to be appended according to SAVE-7. - SAVE-14 Changes to page metadata and
viewport, and page reordering, can all be expressed by appending new versions. After reordering,pageIndexmay temporarily contain duplicates or gaps: a reader sorts pages bypageIndex, breaks ties by the position of each page's last occurrence in the object stream, and then renumbers the pages for the current display. - SAVE-15 Replaced old versions remain in the file and take up space. A writer should compact the file at a suitable time.
Compacting
- SAVE-16 Compacting is simply a full save: keep only the last version of each Canvas unique key and output a compact file.
- SAVE-17 When to compact is up to the app, for example when replaced data exceeds a certain proportion, when the app is idle, or when the file is closed. Before exporting for other apps, the file must be compacted first (COMPAT-5).
When a full save is required
SAVE-18 The following cannot be expressed by appending and require a full save:
- adding, deleting or modifying Device and Workspace registration entries (the Header Extension is at the start of the file and cannot be appended);
- deleting pages (UInk defines no deletion marker);
- after recovering from corruption in the middle (SAVE-24);
- any situation in which the appended result cannot be guaranteed to match the current picture.
After modifying content on an old page, or after undo or redo, you may append a new version of the whole page or do a full save; you must not append only the changed parts to "patch" the old version.
SAVE-19 When adding a new Workspace, it must be registered and at least one of its Canvases written in the same full save.
The Header is a snapshot
- SAVE-20 Incremental writes must not modify the Header.
deviceNum,workspaceNum,pageNumandtimein the Header keep their values from the most recent full save. - SAVE-21 A reader recalculates the page count and other state from the objects it actually reads; a mismatch between the Header statistics and reality is not an error, and the Header statistics must not be used to pre-allocate memory.
Crash recovery
When reading (FILE-16, FILE-17):
- The file ends exactly at an object boundary: finish normally.
- The file ends in the middle of the last object: discard it and keep all objects before it.
- An object in the middle, or bytes between objects, cannot be decoded: keep the objects before it, stop reading, and do not try to find the next object.
- A block that decodes completely but has invalid fields: skip it according to that block's rules and continue reading.
Reading a file during crash recovery does not automatically modify the source file. Before appending again:
- SAVE-22 Determine the safe append position: the start of the incomplete last block; or the start of the decoding failure in the middle; or the start of the first of the consecutive invalid blocks at the end.
- SAVE-23 Truncate the file to the safe append position, then check the last valid Canvas: if it has no content, new content starts from
contentId = 0andundoId = 0; if it has content, continue its numbering. - SAVE-24 If invalid blocks are located before valid blocks (corruption in the middle of the file), a full save is required; the writer must not truncate the file at the point of corruption and then append directly.
Recommended practice
| Scenario | Recommendation |
|---|---|
| While writing | Append to the last Canvas after each pen lift |
| Turning a whiteboard page, moving to another slide | Page-append the page being left |
| Closing the file, explicit save | Decide whether to compact based on how much replaced data there is |
| Undo, modifying an old page | Append a new version of the whole page, or do a full save |
| Deleting pages, modifying the registry | Full save |
| Exporting for other apps | Compact before exporting; use compatible export if needed |