table-editor 0.5.1

A local HTTP server and browser bundle for editing a repository's JSONL tables
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
//! The resolved `Data/` directory every table reads and writes through.

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, MutexGuard};

use anyhow::{Result, anyhow, bail};
use serde::de::DeserializeOwned;

use crate::error::ApiError;
use crate::jsonl;

/// What one file held the first time this context looked: its text, or the
/// error that said it was not there.
type Cached = Result<String, String>;

/// The version a file that is not there has. No present file can take it,
/// since a hash is sixteen hex digits, so a write that states the version of a
/// file since deleted is refused rather than quietly recreating it.
const ABSENT: &str = "absent";

/// The `Data/` directory a request's tables live in. Sibling reads go through
/// it too, so a table that cross-checks against another reads it from the same
/// place the editor writes it.
///
/// Each file is read from disk once per context. A table whose `validate`,
/// `derive`, and `siblings` all consult the same sibling therefore see one
/// version of it, however the file changes underneath them, and pay for one
/// read rather than three. A context is built per request, so a later request
/// reads the file again; parsing still happens per call, since the rows are
/// handed out by value and the row type differs from caller to caller.
///
/// What was read is remembered under the file name as it was spelled, not the
/// path it resolves to, so two spellings of one file would be read twice and
/// could disagree. A table's file comes from [`crate::TableLogic::file`],
/// which is one `&'static str` and a bare name, so a table and everything
/// cross-checking against it name the file the same way by construction.
pub struct Context {
    data_dir: PathBuf,
    cache: Mutex<HashMap<String, Cached>>,
}

impl Context {
    /// A context rooted at an explicit directory.
    pub fn new(data_dir: impl Into<PathBuf>) -> Self {
        Self {
            data_dir: data_dir.into(),
            cache: Mutex::new(HashMap::new()),
        }
    }

    /// Walk up from the current directory to the nearest ancestor containing a
    /// `Data/` directory.
    pub fn find() -> Result<Self> {
        let start =
            std::env::current_dir().map_err(|e| anyhow!("could not get current directory: {e}"))?;
        let mut dir = start.as_path();
        loop {
            let candidate = dir.join("Data");
            if candidate.is_dir() {
                return Ok(Self::new(candidate));
            }
            match dir.parent() {
                Some(parent) => dir = parent,
                None => break,
            }
        }
        bail!(
            "could not find a Data/ directory at or above {}",
            start.display()
        )
    }

    pub fn data_dir(&self) -> &Path {
        &self.data_dir
    }

    /// Read a file the table needs. A missing file is a 500: the table cannot
    /// be served without it.
    pub fn read(&self, file: &str) -> Result<String, ApiError> {
        match self.cached(file)? {
            Ok(text) => Ok(text),
            Err(message) => Err(ApiError::server(format!(
                "could not read {file}: {message}"
            ))),
        }
    }

    /// Read a file the table can do without. A missing file is `None`; an
    /// unreadable one is still a 500.
    pub fn read_optional(&self, file: &str) -> Result<Option<String>, ApiError> {
        Ok(self.cached(file)?.ok())
    }

    /// This context's view of one file, reading the disk the first time it is
    /// asked. A file that is not there is remembered as absent; any other
    /// failure is reported without being remembered, so a read that failed for
    /// a reason that may pass is tried again.
    ///
    /// The lock is held across the read. That makes a second caller wait on a
    /// read already in flight rather than start one of its own, which is what
    /// keeps the promise that one context yields one version of a file even
    /// when it is shared between threads. Nothing under the lock reaches back
    /// into the context, so there is nothing here to deadlock against.
    fn cached(&self, file: &str) -> Result<Cached, ApiError> {
        let mut cache = self.cache();
        if let Some(cached) = cache.get(file) {
            return Ok(cached.clone());
        }

        let cached = match std::fs::read_to_string(self.data_dir.join(file)) {
            Ok(text) => Ok(text),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Err(e.to_string()),
            Err(e) => return Err(ApiError::server(format!("could not read {file}: {e}"))),
        };
        cache.insert(file.to_string(), cached.clone());
        Ok(cached)
    }

    /// The version of one file as this context sees it: a hash of the bytes it
    /// read, or [`ABSENT`] where the file is not there.
    ///
    /// It is what a client states back when it writes rows it read, so that a
    /// write cannot go over a change made after the read. Within one request
    /// this answers for the same bytes the rows were parsed from, since a
    /// context reads a file once and a write replaces what it read.
    ///
    /// The version is the file's contents rather than its timestamp, because a
    /// timestamp says a file was touched where what matters is whether it now
    /// holds something else: a sync that writes the same bytes back, or a tool
    /// that rewrites a file unchanged, moves the timestamp and changes nothing
    /// a client is holding.
    pub fn version(&self, file: &str) -> Result<String, ApiError> {
        Ok(match self.cached(file)? {
            Ok(text) => hash(text.as_bytes()),
            Err(_) => ABSENT.to_string(),
        })
    }

    fn cache(&self) -> MutexGuard<'_, HashMap<String, Cached>> {
        // A panic under the lock would poison it, and a poisoned cache is
        // still a usable one: the map is taken back rather than propagated.
        self.cache.lock().unwrap_or_else(|e| e.into_inner())
    }

    /// Replace a table file with new contents.
    ///
    /// The text goes to a sibling temporary file first and is renamed over the
    /// target, so an interrupted write leaves the old table intact rather than
    /// a truncated one. The temporary file shares the directory, so the rename
    /// stays within one volume.
    ///
    /// What was written becomes this context's view of the file, so a read
    /// after a write sees the new text rather than whatever was read before.
    pub fn write(&self, file: &str, text: &str) -> Result<(), ApiError> {
        let target = self.data_dir.join(file);
        let temporary = self
            .data_dir
            .join(format!(".{file}.{}.tmp", std::process::id()));

        // A write that fails partway leaves the file in a state this context
        // has no view of, so forget what it knew either way.
        self.cache().remove(file);

        std::fs::write(&temporary, text)
            .map_err(|e| ApiError::server(format!("could not write {file}: {e}")))?;

        if let Err(e) = std::fs::rename(&temporary, &target) {
            let _ = std::fs::remove_file(&temporary);
            return Err(ApiError::server(format!("could not replace {file}: {e}")));
        }

        self.cache().insert(file.to_string(), Ok(text.to_string()));
        Ok(())
    }

    /// Read and parse a file the table needs.
    pub fn rows<T: DeserializeOwned>(&self, file: &str) -> Result<Vec<T>, ApiError> {
        let text = self.read(file)?;
        jsonl::parse(&text).map_err(|e| ApiError::from_parse(file, &e))
    }

    /// Read and parse a sibling table. A missing file yields no rows, so the
    /// cross-checks that consult it are skipped rather than failing; a
    /// present-but-unparseable file is a 500.
    pub fn optional_rows<T: DeserializeOwned>(&self, file: &str) -> Result<Vec<T>, ApiError> {
        match self.read_optional(file)? {
            Some(text) => jsonl::parse(&text).map_err(|e| ApiError::from_parse(file, &e)),
            None => Ok(Vec::new()),
        }
    }
}

/// FNV-1a over some bytes, as sixteen hex digits.
///
/// A version only has to say whether two readings of a file found the same
/// thing, so this is a hash and not a signature: nothing is kept out by it,
/// and anything that can write a table can state whatever version it likes.
/// FNV-1a is a fixed algorithm in a few lines and costs no dependency, which
/// is what the job wants. The standard library's `DefaultHasher` computes
/// something deliberately unspecified that may differ between builds, so a
/// page that loaded from one build of a server would have its next save
/// refused by the next.
fn hash(bytes: &[u8]) -> String {
    let mut value: u64 = 0xcbf2_9ce4_8422_2325;
    for byte in bytes {
        value ^= u64::from(*byte);
        value = value.wrapping_mul(0x0000_0100_0000_01b3);
    }
    format!("{value:016x}")
}

#[cfg(test)]
mod tests {
    use serde::Deserialize;

    use super::*;
    use crate::fixture;

    #[derive(Debug, Deserialize)]
    struct Row {
        name: String,
    }

    #[test]
    fn read_of_a_missing_file_is_a_server_error() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        assert_eq!(ctx.read("Absent.jsonl").unwrap_err().status, 500);
    }

    #[test]
    fn read_optional_of_a_missing_file_is_none() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        assert!(ctx.read_optional("Absent.jsonl").unwrap().is_none());
    }

    #[test]
    fn optional_rows_of_a_missing_file_is_empty() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        let rows: Vec<Row> = ctx.optional_rows("Absent.jsonl").unwrap();
        assert!(rows.is_empty());
    }

    #[test]
    fn rows_round_trip_through_write() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        ctx.write("Rows.jsonl", "{\"name\":\"a\"}\n").unwrap();
        // Through a context of its own, so the rows come off the disk rather
        // than out of the writer's cache.
        let rows: Vec<Row> = dir.context().rows("Rows.jsonl").unwrap();
        assert_eq!(rows[0].name, "a");
    }

    #[test]
    fn write_replaces_the_target_and_leaves_no_temporary_behind() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());

        ctx.write("Rows.jsonl", "{\"name\":\"a\"}\n").unwrap();
        ctx.write("Rows.jsonl", "{\"name\":\"b\"}\n").unwrap();

        assert_eq!(dir.read("Rows.jsonl"), "{\"name\":\"b\"}\n");
        let left_over: Vec<_> = std::fs::read_dir(dir.path())
            .unwrap()
            .map(|entry| entry.unwrap().file_name())
            .filter(|name| name.to_string_lossy() != "Rows.jsonl")
            .collect();
        assert!(left_over.is_empty(), "stray files: {left_over:?}");
    }

    #[test]
    fn a_failed_write_leaves_the_stored_table_alone() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        ctx.write("Rows.jsonl", "{\"name\":\"a\"}\n").unwrap();

        // A directory in the target's place cannot be renamed over.
        std::fs::create_dir(dir.path().join("Blocked.jsonl")).unwrap();
        assert_eq!(ctx.write("Blocked.jsonl", "x\n").unwrap_err().status, 500);

        assert_eq!(dir.read("Rows.jsonl"), "{\"name\":\"a\"}\n");
        assert!(dir.path().join("Blocked.jsonl").is_dir());
    }

    #[test]
    fn a_file_is_read_from_disk_once_per_context() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        dir.write("Rows.jsonl", "{\"name\":\"a\"}");

        assert_eq!(ctx.read("Rows.jsonl").unwrap(), "{\"name\":\"a\"}\n");

        // A second reader of the same file within one request sees what the
        // first read, whatever has happened to the file since.
        dir.write("Rows.jsonl", "{\"name\":\"b\"}");
        assert_eq!(ctx.read("Rows.jsonl").unwrap(), "{\"name\":\"a\"}\n");
        let rows: Vec<Row> = ctx.rows("Rows.jsonl").unwrap();
        assert_eq!(rows[0].name, "a");
    }

    #[test]
    fn a_later_context_reads_the_file_again() {
        let dir = fixture::temp_dir();
        dir.write("Rows.jsonl", "{\"name\":\"a\"}");
        assert_eq!(
            dir.context().read("Rows.jsonl").unwrap(),
            "{\"name\":\"a\"}\n"
        );

        dir.write("Rows.jsonl", "{\"name\":\"b\"}");
        assert_eq!(
            dir.context().read("Rows.jsonl").unwrap(),
            "{\"name\":\"b\"}\n"
        );
    }

    #[test]
    fn a_file_that_was_absent_stays_absent_within_one_context() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        assert!(ctx.read_optional("Rows.jsonl").unwrap().is_none());

        dir.write("Rows.jsonl", "{\"name\":\"a\"}");
        assert!(ctx.read_optional("Rows.jsonl").unwrap().is_none());
        assert_eq!(ctx.read("Rows.jsonl").unwrap_err().status, 500);
    }

    #[test]
    fn a_write_replaces_what_this_context_has_read() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());

        ctx.write("Rows.jsonl", "{\"name\":\"a\"}\n").unwrap();
        assert_eq!(ctx.read("Rows.jsonl").unwrap(), "{\"name\":\"a\"}\n");

        ctx.write("Rows.jsonl", "{\"name\":\"b\"}\n").unwrap();
        assert_eq!(ctx.read("Rows.jsonl").unwrap(), "{\"name\":\"b\"}\n");
        let rows: Vec<Row> = ctx.rows("Rows.jsonl").unwrap();
        assert_eq!(rows[0].name, "b");
    }

    #[test]
    fn a_write_to_a_file_read_as_absent_makes_it_present() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());

        assert!(ctx.read_optional("Rows.jsonl").unwrap().is_none());
        ctx.write("Rows.jsonl", "{\"name\":\"a\"}\n").unwrap();
        assert_eq!(
            ctx.read_optional("Rows.jsonl").unwrap().as_deref(),
            Some("{\"name\":\"a\"}\n")
        );
    }

    #[test]
    fn a_version_follows_the_contents_and_not_the_timestamp() {
        let dir = fixture::temp_dir();
        dir.write("Rows.jsonl", "{\"name\":\"a\"}");
        let first = dir.context().version("Rows.jsonl").unwrap();

        // The same bytes written again: a sync or a tool that rewrites a file
        // unchanged has changed nothing anybody is holding.
        dir.write("Rows.jsonl", "{\"name\":\"a\"}");
        assert_eq!(dir.context().version("Rows.jsonl").unwrap(), first);

        dir.write("Rows.jsonl", "{\"name\":\"b\"}");
        assert_ne!(dir.context().version("Rows.jsonl").unwrap(), first);
    }

    #[test]
    fn a_version_is_the_published_fnv_1a_of_the_bytes() {
        // The test vectors for FNV-1a 64. The algorithm has to compute the
        // same value in every build, or a page that read a table from one
        // build of the server would have its next save refused by the next, so
        // it is pinned by value and not only by shape.
        assert_eq!(hash(b""), "cbf29ce484222325");
        assert_eq!(hash(b"a"), "af63dc4c8601ec8c");
    }

    #[test]
    fn a_version_is_of_the_bytes_as_stored_rather_than_of_tidied_text() {
        let dir = fixture::temp_dir();
        let write = |file: &str, bytes: &[u8]| {
            std::fs::write(dir.path().join(file), bytes).unwrap();
            dir.context().version(file).unwrap()
        };

        // The same rows stored three ways. A checkout with CRLF line endings,
        // or a file some editor has left a byte order mark on, holds different
        // bytes and has a version of its own, which is what keeps the version
        // a client states comparable with the file it read.
        let lf = write("Lf.jsonl", b"a\nb\n");
        let crlf = write("Crlf.jsonl", b"a\r\nb\r\n");
        let marked = write("Marked.jsonl", "\u{feff}a\nb\n".as_bytes());

        assert_ne!(lf, crlf);
        assert_ne!(lf, marked);
        assert_ne!(crlf, marked);
    }

    #[test]
    fn a_version_is_sixteen_hex_digits() {
        let dir = fixture::temp_dir();
        dir.write("Rows.jsonl", "{\"name\":\"a\"}");
        let version = dir.context().version("Rows.jsonl").unwrap();
        assert_eq!(version.len(), 16, "{version}");
        assert!(version.chars().all(|c| c.is_ascii_hexdigit()), "{version}");
    }

    #[test]
    fn a_missing_file_has_a_version_no_present_file_can_take() {
        let dir = fixture::temp_dir();
        assert_eq!(dir.context().version("Rows.jsonl").unwrap(), ABSENT);

        dir.write("Rows.jsonl", "");
        assert_ne!(dir.context().version("Rows.jsonl").unwrap(), ABSENT);
    }

    #[test]
    fn a_version_answers_for_the_text_this_context_read() {
        let dir = fixture::temp_dir();
        dir.write("Rows.jsonl", "{\"name\":\"a\"}");
        let ctx = dir.context();
        let read = ctx.version("Rows.jsonl").unwrap();

        // The file is rewritten under the request. This context still answers
        // for what it handed out, so the rows it served and the version it
        // served them with still describe one thing.
        dir.write("Rows.jsonl", "{\"name\":\"b\"}");
        assert_eq!(ctx.version("Rows.jsonl").unwrap(), read);

        // A write through the context makes the version the written text's,
        // which is what the disk now holds.
        ctx.write("Rows.jsonl", "{\"name\":\"c\"}\n").unwrap();
        assert_eq!(
            ctx.version("Rows.jsonl").unwrap(),
            dir.context().version("Rows.jsonl").unwrap()
        );
    }

    #[test]
    fn a_context_can_be_shared_between_threads() {
        fn assert_send_sync<T: Send + Sync>(_: &T) {}
        assert_send_sync(&Context::new("Data"));
    }

    #[test]
    fn unparseable_rows_name_the_file() {
        let dir = fixture::temp_dir();
        let ctx = Context::new(dir.path());
        ctx.write("Rows.jsonl", "not json\n").unwrap();
        let err = ctx.rows::<Row>("Rows.jsonl").unwrap_err();
        assert_eq!(err.status, 500);
        assert!(err.message.starts_with("Rows.jsonl line 1:"));
    }
}