TSVZ 3.39__tar.gz → 4.3__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
tsvz-4.3/PKG-INFO ADDED
@@ -0,0 +1,360 @@
1
+ Metadata-Version: 2.4
2
+ Name: TSVZ
3
+ Version: 4.3
4
+ Summary: Key-value store kept in a TSV / CSV / NSV / PSV file, with tsvz-spec-v1 support
5
+ Home-page: https://github.com/yufei-pan/TSVZ
6
+ Author: Yufei Pan
7
+ Author-email: pan@zopyr.us
8
+ License: GPLv3+
9
+ Classifier: Programming Language :: Python :: 3
10
+ Classifier: Programming Language :: Python :: 3.6
11
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
12
+ Classifier: Operating System :: OS Independent
13
+ Requires-Python: >=3.6
14
+ Description-Content-Type: text/markdown
15
+ Provides-Extra: completion
16
+ Requires-Dist: argcomplete; extra == "completion"
17
+ Dynamic: author
18
+ Dynamic: author-email
19
+ Dynamic: classifier
20
+ Dynamic: description
21
+ Dynamic: description-content-type
22
+ Dynamic: home-page
23
+ Dynamic: license
24
+ Dynamic: provides-extra
25
+ Dynamic: requires-python
26
+ Dynamic: summary
27
+
28
+ # TSVZ
29
+
30
+ **[tsvz.org](https://tsvz.org)**: the format, its specification and its implementations.
31
+
32
+ TSVZ is a small, dependency-free library and CLI that keeps an **ordered
33
+ key-value store in a delimiter-separated text file**. The first column of each
34
+ row is the key; the file behaves like an ordered dictionary that is persisted to
35
+ disk as you change it.
36
+
37
+ **Version 4.3**, Python **3.6+**, one self-contained module (`TSVZ.py`): drop it
38
+ next to your code and `import TSVZ`, or install it.
39
+
40
+ ```bash
41
+ pip install tsvz # from PyPI
42
+ pip install -e . # editable, from this directory
43
+ ```
44
+
45
+ ## Two file dialects, one API
46
+
47
+ | Extension | Rules |
48
+ |---|---|
49
+ | `.tsv` `.csv` `.nsv` `.psv` (and any other) | The TSVZ **3.39** format, unchanged. A 3.39 host can read every file 4.x writes. |
50
+ | `.tsvz` `.csvz` `.nsvz` `.psvz` | The **[tsvz-spec-v1](tsvz-spec-v1.md)** append-only key-value log. |
51
+
52
+ The delimiter follows the extension: tab, comma, NUL, pipe. A trailing
53
+ compression suffix (`.gz`, `.bz2`, `.xz`, `.zst`) is allowed on both; `.zst`
54
+ needs Python 3.14.
55
+
56
+ `TSVZed`, `TSVZedLite`, `readTabularFile`, `appendTabularFile`,
57
+ `appendLinesTabularFile`, `clearTabularFile`, `scrubTabularFile` (and the
58
+ `*TSV` aliases) work on both dialects. `t.dialect` tells you which one a store
59
+ uses.
60
+
61
+ ## Quick start
62
+
63
+ ```python
64
+ import TSVZ
65
+
66
+ t = TSVZ.TSVZed('people.tsvz', header='id\tname\tscore')
67
+ t['alice'] = ['alice', 'Alice', '10']
68
+ t['bob'] = 'bob\tBob\t20'
69
+ t['alice'] = ['alice', 'Alice', '11'] # last write wins
70
+ del t['bob'] # appends a tombstone
71
+ print(t['alice']) # ['alice', 'Alice', '11']
72
+ t.close()
73
+ ```
74
+
75
+ ```bash
76
+ tsvz set people.tsvz carol Carol 5 # add or update a row (append is the same)
77
+ tsvz get people.tsvz carol # one row; exit 3 if carol is missing
78
+ tsvz read people.tsvz # every row: a table on a terminal, records in a pipe
79
+ tsvz delete people.tsvz carol # tombstone
80
+ tsvz read people.tsvz | tsvz set copy.tsvz - # copy every row
81
+ tsvz clear people.tsvz # empty it, keeping the header and markers
82
+ tsvz scrub people.tsvz # compact it (for .tsvz: archival maintenance)
83
+ tsvz verify people.tsvz # check #_checksum_*_# segments; exit 4 on a mismatch
84
+ tsvz parts events.tsvz # list the parts of a multi-part store
85
+ tsvz has people.tsvz carol # exit 0 if carol is live, 3 if not
86
+ tsvz pop people.tsvz carol # print carol's row and delete it
87
+ tsvz serve people.tsvz & # keep the store loaded; tsvz commands then use it
88
+ tsvz stop people.tsvz
89
+ tsvz people.tsvz append carol Carol 5 # the 3.39 form still works
90
+ tsvz -V
91
+ ```
92
+
93
+ ## What TSVZ 4 does with `.tsvz` files
94
+
95
+ - **Reads** everything a conformant writer can produce: markers
96
+ (`#_defaults_#`, `#_strip_trailing_whites_#`, `#_fill_empty_with_default_#`,
97
+ `#_return_defaults_when_missing_#`, ...), escapes (`<sep>`, `<LF>`, `<lt>`,
98
+ `<#>`), tombstones, `#_checksum_<algo>_#` segments (crc32 and every
99
+ fixed-length hashlib algorithm), compressed parts, and **multi-part stores**
100
+ (`store.tsvz.<hex>`, part 0 first, `.rotated` parts skipped).
101
+ - **Writes** one file: the named file, or the highest-numbered part of a
102
+ multi-part store. Rows are written as given (no padding); a lone key is a
103
+ tombstone; a row of empty cells (`k\t\t`) is a live row.
104
+ - **Compaction is manual.** `scrubTabularFile` / `tsvz scrub f.tsvz` rewrites a
105
+ single-part store in place (same inode). If another writer commits while it
106
+ runs, it reads the store again; after three tries it writes nothing, with a
107
+ warning (`clearTabularFile` does the same). The automatic rewrite options of
108
+ 3.39 (`rewrite_on_load`, `rewrite_on_exit`, `rewrite_interval`, `rewrite()`,
109
+ `mapToFile()`, `hardMapToFile()`) are ignored for `.tsvz`, with a warning.
110
+ - **Missing keys:** `t[key]` returns the defaults row while
111
+ `#_return_defaults_when_missing_#` is true (its default); `in`, `get`,
112
+ `setdefault` and `pop` behave like a dict.
113
+ - **`#` keys** are stored (`<#>key`). Keys of the form `#_name_#` are marker
114
+ writes: `t['#_defaults_#'] = [...]` sets the defaults, and so does
115
+ `setDefaults([...])` once the object is constructed.
116
+
117
+ ## Command line
118
+
119
+ `tsvz` follows [tsvz-spec-v1 §20](tsvz-spec-v1.md#20-command-line-interface):
120
+ `tsvz [OPTION ...] OPERATION STORE [ARG ...]`, with options anywhere before a
121
+ lone `--`. The 3.39 form `tsvz STORE [OPERATION] [ARG ...]` works too; a store
122
+ named like an operation then needs a path prefix (`./read`).
123
+
124
+ - **stdout carries only rows.** It gets a table on a terminal and records
125
+ otherwise; `--format table|records` overrides. Records use the store's
126
+ delimiter and escaping, so `tsvz read A | tsvz set B -` copies a store of
127
+ the same variant.
128
+ - **stderr carries every message.** `-q` prints errors only; `-v` adds detail.
129
+ - **Exit status:**
130
+ - 0: done.
131
+ - 1: failed or refused. Also a missing store for `read`, `get`, `scrub`, `verify` and `parts`,
132
+ a store that `set`, `append` or `delete` could not create, and a `.tsvz` part that could not
133
+ be read (the rows of the other parts are still printed).
134
+ - 2: usage error.
135
+ - 3: `get` found a missing key.
136
+ - 4: `verify` found a checksum mismatch.
137
+ - **Arguments are literal.** `tsvz set s.tsvz k 'a<b'` stores `a<b`. Values may
138
+ begin with `-` (`-5`); after `--` every argument is positional. `set KEY`
139
+ alone deletes KEY, as in 3.39. `set KEY ''` stores an empty cell in a `.tsvz`
140
+ store; in a `.tsv` file it deletes KEY, as in 3.39.
141
+ - **Bulk input.** `set STORE -` and `delete STORE -` read records or keys from
142
+ stdin and append them in one write. An unterminated last line is ignored,
143
+ with a warning.
144
+ - **TSVZ extensions:**
145
+ - `--x-header H` and `--x-defaults KEY<d>D1<d>...` decode backslash escapes such as `\t`;
146
+ - `--x-strict` / `--x-force` turn 3.39's column and header checks on or off.
147
+
148
+ ## Serving a store
149
+
150
+ `tsvz serve STORE` keeps a store loaded in one process and answers requests on a
151
+ local socket ([tsvz-spec-v1 §21](tsvz-spec-v1.md#21-write-handler-protocol)).
152
+ `tsvz` commands and `TSVZ.TSVZClient` find it through the pointer file
153
+ `STORE.serve` and use it instead of re-reading the store. Their output and exit
154
+ status stay the same.
155
+
156
+ - **One appender.** The server batches the writes it receives into single appends.
157
+ Other writers may still append to the files; the server reads what they add
158
+ before it answers.
159
+ - **Acknowledgement.** A write is acknowledged once it is queued (`#_write_ack_#`
160
+ `memory`, the default), or after `fsync` (`#_write_ack_#` `disk`, or
161
+ `TSVZClient(..., sync=True)`). Reads always see acknowledged writes.
162
+ - **Access.** Only the user who started the server can connect (socket mode 0600).
163
+ `--x-group G` or `--x-mode 0660` opens it on purpose. The server listens on a
164
+ Unix-domain socket only; reaching it from other hosts is left to a separate
165
+ adapter (for example TCP with TLS) that would relay to the socket.
166
+ - **Trust.** Clients use `STORE.serve` only when it is a regular file owned by
167
+ them or by the store's owner, not writable by anyone else, naming a socket that
168
+ belongs to that owner. Otherwise they warn and use the
169
+ files. `serve` never writes through a symbolic link or into a file it did not
170
+ create.
171
+ - **Lifetime.** It runs in the foreground until `tsvz stop STORE`, SIGINT or
172
+ SIGTERM, or `--x-idle-timeout S` seconds without connections. It then writes
173
+ and `fsync`s everything and removes `STORE.serve`.
174
+ - **Bypassing it.** `--x-direct` makes a `tsvz` command use the files. Command
175
+ lines with `--x-header`, `--x-defaults` or `--x-strict`, or another delimiter
176
+ than the server's, use the files too.
177
+
178
+ ```python
179
+ import TSVZ
180
+
181
+ with TSVZ.TSVZClient('people.tsvz') as people: # served, or in this process when no server runs
182
+ people['dave'] = ['dave', 'Dave', '41']
183
+ print(people['dave'], len(people), 'alice' in people)
184
+ print(people.pop('dave'))
185
+ ```
186
+
187
+ `TSVZClient` has `TSVZed`'s semantics for reading and setting keys, `del`, `in`,
188
+ `len`, iteration, `pop`, `popitem`, `setdefault`, `update`, `clear` and
189
+ `setDefaults`. Its writes return once they are written (raising `OSError` if they
190
+ cannot be), one client may be shared between threads, and without a server it
191
+ warns once and works on the files in-process, finishing its writes when the
192
+ program exits.
193
+
194
+ ## Fault tolerance
195
+
196
+ TSVZ keeps your data and keeps going when a file is not what it expects, and
197
+ says so on stderr (or through `teeLogger`) with one summarised line per problem:
198
+
199
+ ```
200
+ TSVZ warning: data.tsvz: invalid UTF-8 replaced with U+FFFD (3 occurrences, first at line 17)
201
+ ```
202
+
203
+ Examples: bytes after the last newline (ignored on read; truncated before the
204
+ next append, and quoted), invalid UTF-8, a byte order mark, a newer
205
+ `#_version_#`, a marker with a bad value, a checksum mismatch, a damaged
206
+ compressed part (read up to the damage; repaired in place before the next
207
+ append, with the original saved as `<part>.damaged-<time>`), an unreadable
208
+ part, and a delimiter or encoding argument that conflicts with the extension.
209
+
210
+ ## Breaking changes from 3.39
211
+
212
+ ### `.tsv`, `.csv`, `.nsv`, `.psv`: bug fixes only
213
+
214
+ | # | Change |
215
+ |---|---|
216
+ | L1 | Appending after an unterminated last line writes `\n` first; 3.39 merged the two lines. |
217
+ | L2 | Setting a row whose values are all empty deletes it in memory too (it already meant delete on disk), with a warning. |
218
+ | L3 | `TSVZedLite` on a compressed file keeps rows in memory; 3.39 read compressed bytes as text. Rows held in memory (also `#` keys) are returned directly. |
219
+ | L4 | New warnings on stderr (invalid encoding, lines dropped by `strict`); data unchanged. |
220
+ | L5 | A damaged compressed file is read up to the damage instead of raising. |
221
+ | L6 | `mapToFile` no longer glues a row it adds at the end of the file to the next one. |
222
+
223
+ ### `.tsvz`, `.csvz`, `.nsvz`, `.psvz`: now tsvz-spec-v1
224
+
225
+ | # | Change |
226
+ |---|---|
227
+ | S1 | `.csvz/.nsvz/.psvz` use `, \0 \|` (3.39: tab). A conflicting `delimiter=` or `encoding=` is overridden, with a warning. |
228
+ | S2 | A file written by 3.39 under one of these names reads differently (header line becomes data, `key\t\t` deletes come back, `</sep/>` stays literal, an unterminated last line is dropped, single-column files read as empty, empty cells are not defaulted, NULs are not stripped). **Rename such a file to `.tsv`.** |
229
+ | S3 | `t[missing]` returns the defaults row instead of raising `KeyError` (see above). |
230
+ | S4 | `#` keys are stored; `#_name_#` keys are marker writes. |
231
+ | S5 | Automatic rewrites are ignored with a warning; `rewrite_on_load` effectively defaults to `False`; `move_to_end` only reorders memory. |
232
+ | S6 | Rows are never trimmed or dropped for their width; `strict` no longer drops rows; `appendLinesTabularFile` does not pad. |
233
+ | S7 | The header is written as a `#` comment; a new file without a header is empty. |
234
+ | S8 | An unterminated tail is ignored on read and truncated on append. |
235
+ | S9 | `clear` keeps the active markers; scrub keeps the header comment and drops other comments, custom markers and checksums. |
236
+ | S10 | Files named `x.tsvz.<hex>` beside `x.tsvz` are read as parts of the store. |
237
+ | S11 | `lastLineOnly` reads forward (slower on large files). |
238
+
239
+ ### The `tsvz` command
240
+
241
+ 3.39 command lines still parse, including abbreviated long options (`--delim`)
242
+ and `-d=,`, except the ones C5 and C9 list.
243
+
244
+ | # | Change |
245
+ |---|---|
246
+ | C1 | `read` into a pipe or a file prints records instead of the table; `--format table` gives the table. |
247
+ | C2 | Every message goes to stderr: "Created …", "File not found", header mismatches, warnings. |
248
+ | C3 | `read` and `scrub` of a missing store exit 1 with a message (3.39: exit 0, and `read` printed "File not found" on stdout). |
249
+ | C4 | `delete` deletes every KEY it is given (3.39: only the first). |
250
+ | C5 | `append` or `delete` without a KEY, or with an empty KEY, is a usage error with exit 2 (3.39 wrote an empty line). |
251
+ | C6 | A `clear` or `scrub` that writes nothing (one part of a multi-part store, a store that kept changing) exits 1. |
252
+ | C7 | `-c/--header`, `--defaults`, `-s/--strict` and `-f/--force` are undocumented aliases of the `--x-` options. `--x-header` and `--x-defaults` keep non-ASCII text. |
253
+ | C8 | Usage errors print tsvz's own message, exit 2; the `-h` text is new. |
254
+ | C9 | Arguments after STORE for `read`, `clear` and `scrub` are a usage error, exit 2 (3.39 ignored them, so `clear junk` cleared the file). |
255
+ | C10 | The table (a terminal, or `--format table`) of `get`, `keys`, `pop`, `popitem`, `setdefault`, and of `read` on a `.tsvz`-family store, shows the rows only: no row is drawn as a header, and no row is cut to the first row's or the terminal's width. `read` of a `.tsv`-family file keeps 3.39's table, whose first row is the file's header line. |
256
+
257
+ ## Spec deviations and interpretations
258
+
259
+ None of these fails a conformance case: `conformance/reference-baseline.json`,
260
+ which lists the cases this implementation does not pass, is empty (see
261
+ Conformance testing).
262
+
263
+ - §17.5: a numbered part opened directly is read alone, with a warning.
264
+ - §14.5: the defaults-on-missing read applies to `t[key]` only.
265
+ - §18.6: locking is the 3.39 exclusive `lockf` per write plus an in-process lock; readers do not lock.
266
+ - §19.4: a scrub whose rows would read differently under the final markers writes them with stripping and fill-empty off, then restores the final markers after the rows.
267
+ - §20.7: `-q` also silences informational messages ("Created …"); only errors remain.
268
+ - §20.2: `scrub` of a multi-part store, and `clear` of one part of one, are refused with exit 1; `read`, `get` and `parts` of a numbered part read that part alone, with a warning.
269
+ - §20.2.4: on `.tsv`-family files `get` prints nothing for a missing key, `verify` checks nothing, and `parts` lists the file.
270
+ - §20.3: a bulk input line with an empty key is skipped, with a warning.
271
+ - §20.6: a write whose store cannot be created, and a `read`, `get` or `verify` that cannot read every part, exit 1 with an error that `-q` keeps.
272
+ - §21: a served `.tsv` file with a header line shows that line as a row, as `tsvz read` does (`TSVZed` hides it when given the header).
273
+ - §21.8: the server notices changes with `stat()`. It re-reads the part list while the store's directory changed less than 2 s ago, and reloads a part whose size stayed the same but whose inode or modification time changed. A rewrite in place that keeps a part's size and modification time is not seen until the part changes again.
274
+ - §21.9: `tsvz` commands and `TSVZClient` ask the server to answer writes once they are written (`--x-written`), so a write the server cannot make fails as it would on the files. While writes fail, every answer carries a warning.
275
+ - §21.3: `tsvz serve` needs Unix-domain sockets. Where Python has none (Windows), it exits 1, and `tsvz` commands and `TSVZClient` work on the files.
276
+ - §20.7: on `.tsv`-family files a `-d` longer than one character is used, as in 3.39, with a warning; a `-d` that does not decode is a usage error.
277
+
278
+ ## Website
279
+
280
+ `website/` holds the tsvz.org site. `website/tsvz_site.py` (standard library
281
+ only, Python 3.8+) renders `website/index.md`, `website/spec-glance.md` and
282
+ `tsvz-spec-v1.md`, and serves HTML to browsers and markdown to agents.
283
+
284
+ ```bash
285
+ python3 website/tsvz_site.py serve -p 8765 # http://127.0.0.1:8765/
286
+ python3 website/tsvz_site.py build /tmp/site # static files for any host
287
+ ```
288
+
289
+ `deploy/tsvz-site.service` runs it under systemd; restart it to publish an edit.
290
+
291
+ ## Conformance testing
292
+
293
+ `tsvzconform.py` tests how closely an implementation of tsvz-spec-v1, in any
294
+ language, follows the spec. Its cases are JSON files in `conformance/cases/`,
295
+ each naming the § clauses it tests, and it drives an implementation only
296
+ through the spec's command line (§20) and handler socket (§21).
297
+
298
+ ```bash
299
+ python3 tsvzconform.py --impl ./tsvz --feature crc32,sha256,handler
300
+ python3 tsvzconform.py --impl ./tsvz --class reader --only 13
301
+ ```
302
+
303
+ Each failure is printed with its input, what was expected, what came back and
304
+ a command line that reruns it alone; the summary gives a verdict for each
305
+ conformance class (reader, writer, cli, handler). `conformance/README.md`
306
+ explains the options and the features to claim, and how to load the cases into
307
+ another language's tests.
308
+
309
+ ## Benchmarking implementations
310
+
311
+ `tsvzbench.py` compares implementations of tsvz-spec-v1, in any language, side
312
+ by side on one machine. It drives each one only through the spec's command
313
+ line (§20) and, when it has one, its handler socket (§21), so any conformant
314
+ `tsvz` tool can be measured as it is.
315
+
316
+ ```bash
317
+ python3 tsvzbench.py --impl ref='python3 TSVZ.py' --impl new='../tsvz-rs/target/release/tsvz' \
318
+ --suite quick --work-dir /mnt/fast-disk/tsvzbench
319
+ ```
320
+
321
+ - `--impl NAME=COMMAND`, repeatable: the first is the baseline, and every other
322
+ implementation gets a `×` ratio against it (below 1 is faster). A relative
323
+ COMMAND runs from the directory you start tsvzbench in.
324
+ - `--suite smoke|quick|standard|large`: `smoke` checks a setup in seconds,
325
+ `quick` takes about a minute, `standard` (the default, up to 1M rows) about
326
+ five, and `large` (up to 10M rows) twenty or more and ~5 GB of memory for the
327
+ harness itself.
328
+ - `--only cold,bulk,perop,maint` picks families: one-shot replays, `set -` and
329
+ `delete -` batches, single operations (CLI and handler), and scrub/clear.
330
+ - `--work-dir DIR` keeps the generated fixtures between runs. Put it on the disk
331
+ you mean to measure: not a tmpfs, and not a FUSE file system such as mergerfs
332
+ (the harness warns about those). The default is a `tsvzbench-*` directory here.
333
+ - `--json PATH` also writes every raw sample.
334
+
335
+ The harness writes its own input stores, so every implementation reads the same
336
+ bytes, and checks every result against its own replay model: `✓` right, `✗`
337
+ wrong output or files, `! N` an unexpected exit status, `⏱` timed out, `refused`
338
+ an allowed refusal. It exits 1 when any cell failed. Each command runs under a
339
+ small launcher that measures it with `wait4`; a peak RSS shown as `≤8.0 MiB` is
340
+ the launcher's own size, so the true peak is no higher. Compare an
341
+ implementation with itself to see the noise: a few percent per cell, but the
342
+ socket rows, whose latencies are tens of microseconds, can move 10–20% between
343
+ runs, because a single run can land in a slower mode as a whole.
344
+
345
+ ## Tests
346
+
347
+ ```bash
348
+ python3 -m pytest TSVZ_test.py -q # everything, incl. 3.39 differential tests
349
+ python3 -m pytest website/tsvz_site_test.py -q # the tsvz.org site
350
+ python3 -m pytest tsvzbench_test.py -q # the benchmark harness
351
+ python3 -m pytest tsvzconform_test.py -q # the conformance runner and cases, against the reference
352
+ python3 benchTSVZ.py /tmp/b.tsv -n 100000 --compare-old # benchmark (not a test)
353
+ ```
354
+
355
+ `TSVZ_old.py` is the frozen 3.39 module the differential tests compare against;
356
+ it is not installed.
357
+
358
+ ## License
359
+
360
+ GPL-3.0-or-later © Yufei Pan (pan@zopyr.us)