mem_files.txt (2511B)
1 MemFiles 2 3 An in-memory, editable view of the single file on disk. 4 5 The text is held in a `ropey::Rope` which is an in-memory file tree 6 that allows cheap inserts and deletes without copying the whole 7 buffer on every key stroke. 8 9 `MemFile` is the thing an editing session mutates; disk only ever 10 sees the results of a save. 11 12 pub struct MemFile { 13 /// The current text of the file, including any unsaved edits. 14 data: ropey::Rope, 15 16 /// The path on disk this buffer reads from and writes back to. 17 path: std::path::PathBuf, 18 19 /// Number of single character edits applied since the last save. 20 /// Reset to zero every time the buffer is flushed to disk. 21 ops_since_save: usize, 22 } 23 24 +--------+ 25 | user 1 |---------------+ 26 +--------+ | 27 | 28 +--------+ +---------------+ +--------------+ 29 | user 2 |-----------| MemFile |-----| file on disk | 30 +--------+ | (ropey::Rope) | +--------------+ 31 +---------------+ (saves every 5 ops) 32 | 33 +--------+ | 34 | user 3 |---------------+ 35 +--------+ 36 37 *This works exactly like how the virtual DOM in React works. 38 *ropey::Rope is a utf8 text rope. 39 40 41 ==================================================================== 42 43 The MemFileGuard owns everyfile an editing session has open in 44 memory at once. 45 46 pub struct MemFileGuard { 47 /// The live buffers, keyed by the path each one reads from 48 /// and writes back to. One entry per file currently open in 49 /// this session. 50 pub file_map: HashMap<std::path::PathBuf, MemFile> 51 } 52 53 ==================================================================== 54 55 How the actor gets spun up (the chain UP) 56 57 WebSocket/HTTP API endpoint (e.g. api/files/load.rs) 58 └─→ Allocator actor git_management/actors/allocator/actor.rs 59 └─→ register() allocator/processes/register.rs:48 60 └─→ git_files_actor_constructor() git_files/actor.rs:364 61 └─→ git_files_actor() (the Tokio task) actor.rs:42 62 └─→ MemFileGuard::default() actor.rs:91 63 64 When writing to MemFile: 65 1. Loads a file from disk into a new in-memory buffer MemFile 66 2. Insert it into the MemFileGuard 67 3. Match on transaction's operation type (Insert/Delete/Swap) 68 4. If its an insert, do insert_char or insert_text on the MemFile 69 5. Finally, open up the file from disk, write to its buffer, 70 and then flush it to write to the OS.