Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14a0ad3b85 | ||
|
|
4c06bdd39c | ||
|
|
e8f61d7bb9 |
No files matched your search
@@ -35,3 +35,5 @@ randfec/m2/p3-driver.log
|
||||
/experiments/bchcheck
|
||||
/testwork-int/
|
||||
/testwork-bench/
|
||||
/lzcsv
|
||||
/testwork-lzcsv/
|
||||
@@ -62,7 +62,18 @@ ifeq ($(NVCC),)
|
||||
CUDA_STUB_OBJ = bch_cuda_stub.o chase_cuda_stub.o
|
||||
endif
|
||||
|
||||
all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
|
||||
# lzcsv: standalone high-speed CSV compressor (column transpose + xz).
|
||||
# Completely separate from the lzply FEC core: own source (lzcsv-src/lzcsv.c),
|
||||
# own binary, links only liblzma -- no liblzply.a, no liblz, no CUDA.
|
||||
# Built only when lzma.h is present; skipped cleanly otherwise (e.g. a
|
||||
# minimal CI image without liblzma-dev), so `make all` never fails for
|
||||
# lack of it.
|
||||
LZCSV =
|
||||
ifneq ($(wildcard /usr/include/lzma.h),)
|
||||
LZCSV = lzcsv
|
||||
endif
|
||||
|
||||
all: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair $(LZCSV)
|
||||
|
||||
# Shared core: the v5 archive format, the per-entry pipeline
|
||||
# (lzip -> BCH), header I/O, scan, and filters. The
|
||||
@@ -256,10 +267,17 @@ endif
|
||||
# testdata/ is generated (it includes a 50 MiB random file) and gitignored,
|
||||
# so a fresh clone has none. Build it on demand so `make test` works
|
||||
# straight from a clone.
|
||||
# lzcsv: standalone CSV compressor. CPU-only, links liblzma.
|
||||
lzcsv-src/lzcsv.o: lzcsv-src/lzcsv.c
|
||||
$(CC) $(CFLAGS) -c -o $@ lzcsv-src/lzcsv.c
|
||||
|
||||
lzcsv: lzcsv-src/lzcsv.o
|
||||
$(CC) $(CFLAGS) -o $@ lzcsv-src/lzcsv.o -llzma
|
||||
|
||||
testdata:
|
||||
@if [ ! -d testdata/mixed ]; then ./tests/make_testdata.sh; fi
|
||||
|
||||
test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair
|
||||
test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair $(LZCSV)
|
||||
./bch_test
|
||||
./rs_test
|
||||
./crc16_test
|
||||
@@ -288,6 +306,13 @@ test: testdata bch_test rs_test crc16_test lzply lzply_mount lzply_stat lzply_he
|
||||
elif [ -x ../tests/run_integration_tests.sh ]; then \
|
||||
../tests/run_integration_tests.sh; \
|
||||
else echo "run_integration_tests.sh not found"; exit 1; fi
|
||||
if [ -x ./lzcsv ]; then \
|
||||
if [ -x ./tests/run_lzcsv_tests.sh ]; then \
|
||||
./tests/run_lzcsv_tests.sh ./lzcsv; \
|
||||
elif [ -x ../tests/run_lzcsv_tests.sh ]; then \
|
||||
../tests/run_lzcsv_tests.sh ./lzcsv; \
|
||||
else echo "run_lzcsv_tests.sh not found"; exit 1; fi \
|
||||
else echo "lzcsv: skipped (no liblzma)"; fi
|
||||
|
||||
# Integrated experiment winners (docs/INTEGRATION.md): the ECC harness
|
||||
# (interleave d64, small-block BCH, LDPC r90) and the compression
|
||||
@@ -313,6 +338,6 @@ install: lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply
|
||||
install -m 755 mount.lzply /sbin/mount.lzply
|
||||
|
||||
clean:
|
||||
rm -f lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair bch_test rs_test crc16_test bch_cuda_test bch_bench chase_bench_cpu chase_bench_cuda *.o liblzply.a liblzrs.a
|
||||
rm -f lzply lzply_mount lzply_stat lzply_heal lzply_check lzply_refresh lzply_hyper lz_rs lzply_deep_repair lzcsv lzcsv-src/lzcsv.o bch_test rs_test crc16_test bch_cuda_test bch_bench chase_bench_cpu chase_bench_cuda *.o liblzply.a liblzrs.a
|
||||
|
||||
.PHONY: all test install clean exp experiments
|
||||
@@ -53,6 +53,35 @@ BCH-decode -> decompress (on read)
|
||||
and `-datestart` / `-dateend` / `-year` filter on it (see below).
|
||||
File mode is preserved too; ownership is not.
|
||||
|
||||
## Download
|
||||
|
||||
Just want the executables? Grab the latest release, **v0.1.1**, for your
|
||||
machine — no building needed:
|
||||
|
||||
| Platform | Download | Size | Needs |
|
||||
| -------- | -------- | ---- | ----- |
|
||||
| Linux x86_64, CPU-only | [lzply-v0.1.1-linux-x86_64-cpu.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-linux-x86_64-cpu.tar.gz) | 390,376 B | liblz.so.1 |
|
||||
| Linux x86_64, CUDA | [lzply-v0.1.1-linux-x86_64-cuda.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-linux-x86_64-cuda.tar.gz) | 2,440,044 B | libcudart.so.13 + liblz.so.1 |
|
||||
| Linux ARM64 (Raspberry Pi) | [lzply-v0.1.1-linux-aarch64-rpi.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-linux-aarch64-rpi.tar.gz) | 345,708 B | liblz.so.1 |
|
||||
| macOS arm64 (Apple Silicon) | [lzply-v0.1.1-macos-arm64.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-macos-arm64.tar.gz) | 326,010 B | liblz.so.1 |
|
||||
| macOS x86_64 (Intel) | [lzply-v0.1.1-macos-x86_64.tar.gz](https://github.com/cnmcdee/lzply/releases/download/v0.1.1/lzply-v0.1.1-macos-x86_64.tar.gz) | 343,953 B | liblz.so.1 |
|
||||
|
||||
Each archive holds `lzply`, `lz_rs`, `lzply_check`, `lzply_deep_repair`,
|
||||
`lzply_heal`, `lzply_hyper`, `lzply_refresh`, `lzply_stat` — plus
|
||||
`lzply_mount` on Linux x86_64 only — and a README. Unpack it and put the
|
||||
binaries somewhere on your `PATH`. On Debian/Ubuntu install the `liblz1`
|
||||
package if a binary complains about a missing `liblz.so.1`; on macOS run
|
||||
`brew install lzlib`. The CUDA build additionally needs the CUDA runtime
|
||||
(`libcudart.so.13`) installed just to start — with no CUDA device present
|
||||
it falls back to the CPU path, byte-identical.
|
||||
|
||||
Full release page (all assets, checksums, and what's new):
|
||||
https://github.com/cnmcdee/lzply/releases/tag/v0.1.1
|
||||
|
||||
Note: `lzcsv`, the standalone CSV compressor in `lzcsv-src/` (see its
|
||||
README there), is built from source — there is no binary release for it
|
||||
yet.
|
||||
|
||||
## The tools
|
||||
|
||||
Seven programs, one family. All are plain C, built by `make`
|
||||
@@ -273,7 +302,7 @@ Write-ups: [171 seconds to a third of a second: deep-repair Chase on CUDA runs 5
|
||||
## Building
|
||||
|
||||
```sh
|
||||
apt install build-essential liblz-dev # Debian/Ubuntu
|
||||
apt install build-essential liblz-dev libfuse3-dev fuse3 # Debian/Ubuntu
|
||||
make # produces ./lzply, ./lz_rs, ./lzply_mount, ./lzply_stat, ./lzply_heal, ./lzply_check, ./lzply_refresh, ./lzply_deep_repair
|
||||
make test # BCH + RS + CRC16 unit tests + full archive test suites (lzply + lz_rs + deep repair)
|
||||
sudo make install # /usr/local/bin/lzply* (including lz_rs), /sbin/mount.lzply
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
# lzcsv — high-speed CSV compression
|
||||
|
||||
`lzcsv` compresses CSV files by transposing them column-wise and then
|
||||
compressing with xz (liblzma). Same-field values become adjacent long
|
||||
runs, which xz's matcher uses far better than alternating fields on
|
||||
every row. It is a standalone tool in the lzply repo, completely
|
||||
separate from the lzply FEC core: it links only liblzma, no
|
||||
`liblzply.a`, no liblz, no CUDA.
|
||||
|
||||
The idea comes from hot_qwen Cycle 1 (Dec 2026): the coordinator
|
||||
proposed grid-aware compression, hot_qwen tried to disprove it
|
||||
(prior art, failure modes) and called it testable, and the test on
|
||||
14 CSVs gave −13.9% vs `xz -6`, 13/14 files, all round-trips
|
||||
byte-exact. This tool is that test turned into a real command.
|
||||
|
||||
## Format (LZCSV v1)
|
||||
|
||||
```
|
||||
bytes 0..7 magic "LZCSV001"
|
||||
bytes 8..15 uint64 LE original (CSV) size
|
||||
bytes 16..23 uint64 LE transposed (COLT1) size
|
||||
byte 24 xz preset (0-9)
|
||||
byte 25 flags (bit 0 = extreme)
|
||||
bytes 26.. xz stream (liblzma, preset, CRC64 check) holding COLT1
|
||||
```
|
||||
|
||||
The transposed stream is COLT1, byte-compatible with the Cycle 1
|
||||
Python prototype (`/root/hot-qwen/cycle1/transpose.py`):
|
||||
|
||||
```
|
||||
line 1: COLT1 <nrows> <ncols> <crlf:0|1> <trailing_nl:0|1> <uniform:0|1>
|
||||
if uniform=0: line "COUNTS <len>" then <len> bytes of comma-separated
|
||||
per-row field counts (ragged rows; padding stripped on decode)
|
||||
per column j: line "COL <len>" then exactly <len> bytes:
|
||||
the column's raw field texts joined by "\n" (no trailing newline)
|
||||
```
|
||||
|
||||
The compressed payload is the same liblzma preset-6 stream `xz -6`
|
||||
produces on the same transposed bytes (within a few bytes: the
|
||||
encoder's output varies slightly with how it is fed — 1500 vs 1504
|
||||
bytes on tips.csv — same algorithm, same preset, not a different
|
||||
compressor).
|
||||
|
||||
Integrity chain: liblzma verifies the CRC64 of the xz stream, the
|
||||
COLT1 decoder is strict (magic, counts, column field counts, no
|
||||
trailing data), and the container header's original size must match
|
||||
the reconstructed output exactly.
|
||||
|
||||
## Build
|
||||
|
||||
```
|
||||
make lzcsv # needs liblzma-dev; skipped cleanly without it
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
```
|
||||
lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv> compress (preset 6)
|
||||
lzcsv d [-f] [-v] <in.lzcsv> <out.csv> decompress
|
||||
lzcsv test <file.csv> ... in-memory round-trip check
|
||||
lzcsv info <file.lzcsv> show container header
|
||||
lzcsv raw-enc <in.csv> <out.colt> write COLT1 intermediate
|
||||
lzcsv raw-dec <in.colt> <out.csv> decode COLT1 intermediate
|
||||
lzcsv selftest built-in synthetic tests
|
||||
```
|
||||
|
||||
## Measured results (hot_house, xz/liblzma 5.8.1)
|
||||
|
||||
14 corpus CSVs (12,096,190 bytes, `/root/fftdict-shared/corpus2`):
|
||||
|
||||
| file | xz -6 | xz -9e | lzcsv -6 | lzcsv -9e | lzcsv-6 vs xz-6 |
|
||||
|---|---|---|---|---|---|
|
||||
| covid-countries.csv | 646,112 | 654,748 | 464,786 | 464,446 | −28.1% |
|
||||
| gen-log.csv | 38,060 | 38,100 | 29,346 | 29,626 | −22.9% |
|
||||
| gen-numeric.csv | 171,016 | 171,256 | 136,142 | 137,990 | −20.4% |
|
||||
| hydra-passwords.csv | 79,832 | 79,816 | 70,434 | 70,050 | −11.8% |
|
||||
| gapminder.csv | 24,352 | 24,472 | 21,506 | 21,694 | −11.7% |
|
||||
| diabetes.csv | 8,076 | 8,080 | 7,422 | 7,446 | −8.1% |
|
||||
| default-passwords.csv | 25,900 | 25,892 | 23,898 | 23,906 | −7.7% |
|
||||
| tips.csv | 1,636 | 1,640 | 1,526 | 1,502 | −6.7% |
|
||||
| mam.csv | 192,364 | 192,496 | 179,690 | 179,906 | −6.6% |
|
||||
| iab.csv | 128,396 | 128,392 | 120,114 | 120,342 | −6.5% |
|
||||
| stocks.csv | 24,092 | 24,132 | 22,698 | 22,718 | −5.8% |
|
||||
| oui.csv | 718,664 | 713,760 | 692,506 | 687,758 | −3.6% |
|
||||
| iris.csv | 824 | 828 | 818 | 818 | −0.7% |
|
||||
| pin-codes.csv | 12,864 | 12,844 | 13,994 | 14,066 | **+8.8%** |
|
||||
| **aggregate** | **2,072,188 (0.1713)** | **2,076,456 (0.1717)** | **1,784,880 (0.1476)** | **1,782,268 (0.1473)** | **−13.9%** |
|
||||
|
||||
- lzcsv -6 wins on 13/14 files vs xz -6 and vs xz -9e. No file flips
|
||||
at any preset. xz's best does not erase the win: direct xz -9e is
|
||||
slightly *worse* than xz -6 on these CSVs (2,076,456 vs 2,072,188),
|
||||
and lzcsv -6 beats direct xz -9e by −14.0%.
|
||||
- Encode time (sums of per-file medians, 4 parallel workers,
|
||||
approximate): xz -6 3.59 s, lzcsv -6 4.08 s. Decode: xz -6 0.14 s,
|
||||
lzcsv -6 0.19 s. Roughly comparable; the transpose itself is
|
||||
negligible next to xz.
|
||||
|
||||
18 held-out CSVs (4,431,565 bytes — 5 downloaded real-world files
|
||||
plus 13 generated edge cases):
|
||||
|
||||
| aggregate | xz -6 | xz -9e | lzcsv -6 | lzcsv -9e | lzcsv-6 vs xz-6 |
|
||||
|---|---|---|---|---|---|
|
||||
| bytes | 844,752 (0.1906) | 842,764 (0.1902) | 730,756 (0.1649) | 726,864 (0.1640) | **−13.5%** |
|
||||
|
||||
- lzcsv -6 wins on 9/18. Best held-out wins: flights.csv −21.9%,
|
||||
text.csv −21.4%, diamonds.csv −18.9%, taxis.csv −13.3%.
|
||||
- All 9 losses are files under ~1 KB (or a single column): the fixed
|
||||
overhead — COLT1 header + per-column headers + the 26-byte LZCSV
|
||||
header + the xz stream itself — dominates on tiny files
|
||||
(empty.csv: 0 → 98 bytes; tiny.csv: 24 → 134 bytes). Single-column
|
||||
files gain nothing from transposition (there is only one column)
|
||||
and xz -9e beats lzcsv there (single-col.csv: 1,152 vs 1,474).
|
||||
- Honest scope: lzcsv wins on real-world CSVs above a few KB and
|
||||
loses on tiny files. A size gate (transpose only above a
|
||||
row/column threshold, pass small files through as plain xz) is the
|
||||
obvious next step and is left for a later cycle.
|
||||
|
||||
## Limitations (documented, tested)
|
||||
|
||||
- Fields holding a raw newline (embedded newlines in quoted fields)
|
||||
are rejected with a clear error — the COLT1 column join uses
|
||||
`"\n"`, so such a field cannot be represented. Same as the
|
||||
prototype. The test suite checks the rejection is clean.
|
||||
- Files with mixed line endings (some rows CRLF, some LF) are
|
||||
rejected: the single crlf flag cannot represent them, and the
|
||||
prototype would silently mis-round-trip such a file.
|
||||
- In-memory: the whole file is read into RAM (CSVs are small;
|
||||
a 5.5 MB file uses tens of MB during the transpose).
|
||||
- CSV only. Other file types gain nothing and are not the tool's job.
|
||||
|
||||
## Tests
|
||||
|
||||
```
|
||||
./lzcsv selftest # built-in synthetic cases
|
||||
bash tests/run_lzcsv_tests.sh ./lzcsv # full suite (also in `make test`)
|
||||
python3 lzcsv-src/bench.py # benchmark (corpus + held-out)
|
||||
```
|
||||
|
||||
The suite checks: selftest, `--help`, byte-exact round-trips on the
|
||||
14 corpus CSVs and the 18 held-out CSVs, byte-identical COLT1 output
|
||||
vs the Python prototype (oracle) plus cross-decoding both ways,
|
||||
clean rejection of embedded-newline input, and clean failure on
|
||||
truncated / bad-magic input.
|
||||
@@ -0,0 +1,32 @@
|
||||
covid-countries.csv 5512931 646112 654748 464786 464446 1.9201659050013404 1.9516634100000374 0.05220619498868473 0.07482820301083848
|
||||
default-passwords.csv 90856 25900 25892 23898 23906 0.025861336005618796 0.021365027991123497 0.0034550470008980483 0.002496671979315579
|
||||
diabetes.csv 23873 8076 8080 7422 7446 0.011048309010220692 0.009692073974292725 0.0023416100011672825 0.0014151250070426613
|
||||
gapminder.csv 82079 24352 24472 21506 21694 0.021015542006352916 0.015395115013234317 0.003706499992404133 0.002185456018196419
|
||||
gen-log.csv 373147 38060 38100 29346 29626 0.08884992098319344 0.0807244160096161 0.004460240976186469 0.004076949000591412
|
||||
gen-numeric.csv 971501 171016 171256 136142 137990 0.2775053840014152 0.2098268090048805 0.012041809997754171 0.015114195994101465
|
||||
hydra-passwords.csv 515376 79832 79816 70434 70050 0.10549499999615364 0.07998127699829638 0.006305735005298629 0.00882071300293319
|
||||
iab.csv 381908 128396 128392 120114 120342 0.08515957900090143 0.08999008699902333 0.006552350998390466 0.007555139978649095
|
||||
iris.csv 4601 824 828 818 818 0.00544012599857524 0.004784169985214248 0.0016728210030123591 0.0010296890104655176
|
||||
mam.csv 608108 192364 192496 179690 179906 0.16979076401912607 0.1856548530049622 0.00886850300594233 0.010595135012408718
|
||||
oui.csv 3372302 718664 713760 692506 687758 0.8365058559866156 1.3890049190085847 0.02745902701281011 0.05935608199797571
|
||||
pin-codes.csv 83641 12864 12844 13994 14066 0.016631082020467147 0.016714841010980308 0.003020479023689404 0.0030090670043136925
|
||||
stocks.csv 67924 24092 24132 22698 22718 0.01886416898923926 0.017659361008554697 0.003195147990481928 0.0021861470013391227
|
||||
tips.csv 7943 1636 1640 1526 1502 0.007472163008060306 0.004107314976863563 0.0019683470018208027 0.0010860360052902251
|
||||
crlf.csv 57 124 124 170 170 0.004888466995907947 0.007338372990489006 0.0019021829939447343 0.002908758004195988
|
||||
diamonds.csv 2772143 445196 445344 361098 359134 2.396331233001547 1.2090837279974949 0.033903217990882695 0.041442929999902844
|
||||
empty.csv 0 32 32 98 98 0.0018080949957948178 0.0020065490098204464 0.0013358870055526495 0.0006844690069556236
|
||||
flights.csv 2350 648 648 506 506 0.005633962980937213 0.00863322100485675 0.0015973990084603429 0.0018542629841249436
|
||||
header-only.csv 15 80 80 118 118 0.004120881989365444 0.006564754992723465 0.0027778010116890073 0.0021728830179199576
|
||||
narrow.csv 78337 18748 19016 17790 17926 0.032430835999548435 0.029884938994655386 0.005319860007148236 0.0038035030011087656
|
||||
no-trailing-nl.csv 11 76 76 122 122 0.0062791359960101545 0.0024431619967799634 0.003309124003862962 0.000765912001952529
|
||||
numeric.csv 471030 213188 211892 203770 204030 0.33525239600567147 0.3318267239956185 0.015222632006043568 0.017531852994579822
|
||||
one-row-no-nl.csv 5 72 72 114 114 0.002797479013679549 0.00583920898498036 0.0027009370096493512 0.0027930409996770322
|
||||
penguins.csv 13478 2412 2428 2182 2166 0.010619982989737764 0.009998533001635224 0.003402178001124412 0.003114015009487048
|
||||
quotes.csv 115 184 184 230 230 0.007522066996898502 0.0073039180133491755 0.003594912006519735 0.001310628023929894
|
||||
ragged.csv 30 96 96 166 166 0.006454857997596264 0.007829547015717253 0.002639713027747348 0.002263063011923805
|
||||
single-col.csv 31890 1448 1152 1474 1550 0.020343487994978204 0.019372289010789245 0.003808081994066015 0.0029039290093351156
|
||||
taxis.csv 869349 114268 113392 99090 96730 0.3596663609787356 0.2504055729950778 0.01067515800241381 0.01706002399441786
|
||||
text.csv 71105 13912 13960 10938 10974 0.0370593529951293 0.03674623399274424 0.004957627010298893 0.003607544000260532
|
||||
tiny.csv 24 88 88 134 134 0.00707226200029254 0.004299127991544083 0.003431374003412202 0.0024710540019441396
|
||||
titanic.csv 60302 18816 18808 17410 17374 0.03209059499204159 0.034872953983722255 0.0049819239939097315 0.004012949008028954
|
||||
wide.csv 61324 15364 15372 15346 15322 0.026844223000807688 0.02351557300426066 0.003356151981279254 0.003094587998930365
|
||||
|
@@ -0,0 +1,129 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Benchmark lzcsv vs xz on the 14 corpus CSVs and the held-out set.
|
||||
Methods per file: direct xz -6, direct xz -9e, lzcsv -6, lzcsv -9e.
|
||||
Sizes are single-run (deterministic); times are median of 3.
|
||||
Usage: python3 lzcsv-src/bench.py [--csv-only] [--heldout-only]
|
||||
Writes lzcsv-src/bench-results.tsv and prints the tables.
|
||||
"""
|
||||
import concurrent.futures
|
||||
import glob
|
||||
import os
|
||||
import statistics
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
|
||||
ROOT = "/root/lzply"
|
||||
LZ = os.path.join(ROOT, "lzcsv")
|
||||
CORPUS = "/root/fftdict-shared/corpus2"
|
||||
HELD = "/root/hot-qwen/lzcsv-heldout"
|
||||
WORK = "/tmp/lzcsv-bench"
|
||||
os.makedirs(WORK, exist_ok=True)
|
||||
|
||||
def run_cmd(cmd):
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run(cmd, capture_output=True)
|
||||
dt = time.perf_counter() - t0
|
||||
return r.returncode, dt
|
||||
|
||||
def xz_size_time(path, preset):
|
||||
# encode: xz -preset -c path -> stdout (captured); time it
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run(["xz", preset, "-c", path], capture_output=True, check=True)
|
||||
dt = time.perf_counter() - t0
|
||||
# decode time: xz -dc the compressed bytes
|
||||
t0 = time.perf_counter()
|
||||
subprocess.run(["xz", "-dc"], input=r.stdout, capture_output=True, check=True)
|
||||
ddt = time.perf_counter() - t0
|
||||
return len(r.stdout), dt, ddt
|
||||
|
||||
def lzcsv_size_time(path, preset_args, tag):
|
||||
comp = os.path.join(WORK, tag + ".lzcsv")
|
||||
dec = os.path.join(WORK, tag + ".csv")
|
||||
enc_times, dec_times = [], []
|
||||
size = None
|
||||
for _ in range(3):
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run([LZ, "c"] + preset_args + ["-f", path, comp], capture_output=True)
|
||||
enc_times.append(time.perf_counter() - t0)
|
||||
assert r.returncode == 0, f"lzcsv c failed on {path}: {r.stderr.decode()[:200]}"
|
||||
size = os.path.getsize(comp)
|
||||
t0 = time.perf_counter()
|
||||
r = subprocess.run([LZ, "d", "-f", comp, dec], capture_output=True)
|
||||
dec_times.append(time.perf_counter() - t0)
|
||||
assert r.returncode == 0, f"lzcsv d failed on {path}: {r.stderr.decode()[:200]}"
|
||||
return size, statistics.median(enc_times), statistics.median(dec_times)
|
||||
|
||||
def bench_one(path):
|
||||
name = os.path.basename(path)
|
||||
orig = os.path.getsize(path)
|
||||
tag = name.replace(".csv", "")
|
||||
row = {"file": name, "orig": orig}
|
||||
# direct xz -6 / -9e (encode median of 3 for time; size from first run)
|
||||
for preset, key in [("-6", "xz6"), ("-9e", "xz9e")]:
|
||||
sizes, ets, dts = [], [], []
|
||||
for _ in range(3):
|
||||
s, et, dt = xz_size_time(path, preset)
|
||||
sizes.append(s); ets.append(et); dts.append(dt)
|
||||
row[key] = sizes[0]
|
||||
row[key + "_enc"] = statistics.median(ets)
|
||||
row[key + "_dec"] = statistics.median(dts)
|
||||
# lzcsv -6 / -9e
|
||||
for args, key in [([], "lz6"), (["-p", "9", "-e"], "lz9e")]:
|
||||
s, et, dt = lzcsv_size_time(path, args, tag + "-" + key)
|
||||
row[key] = s
|
||||
row[key + "_enc"] = et
|
||||
row[key + "_dec"] = dt
|
||||
return row
|
||||
|
||||
def table(rows, title):
|
||||
print(f"\n## {title}")
|
||||
print("file\torig\txz-6\txz-9e\tlzcsv-6\tlzcsv-9e\tlz6-vs-xz6\tlz6-vs-xz9e\tenc-xz6\tenc-lz6\tdec-xz6\tdec-lz6")
|
||||
t = {k: 0 for k in ("orig", "xz6", "xz9e", "lz6", "lz9e", "xz6_enc", "lz6_enc", "xz6_dec", "lz6_dec")}
|
||||
for r in rows:
|
||||
print(f"{r['file']}\t{r['orig']}\t{r['xz6']}\t{r['xz9e']}\t{r['lz6']}\t{r['lz9e']}"
|
||||
f"\t{100.0*(r['lz6']-r['xz6'])/r['xz6']:+.1f}%\t{100.0*(r['lz6']-r['xz9e'])/r['xz9e']:+.1f}%"
|
||||
f"\t{r['xz6_enc']:.3f}\t{r['lz6_enc']:.3f}\t{r['xz6_dec']:.3f}\t{r['lz6_dec']:.3f}")
|
||||
for k in t:
|
||||
t[k] += r[k]
|
||||
n = len(rows)
|
||||
print(f"AGGREGATE\t{t['orig']}\t{t['xz6']}\t{t['xz9e']}\t{t['lz6']}\t{t['lz9e']}"
|
||||
f"\t{100.0*(t['lz6']-t['xz6'])/t['xz6']:+.1f}%\t{100.0*(t['lz6']-t['xz9e'])/t['xz9e']:+.1f}%"
|
||||
f"\t{t['xz6_enc']:.2f}\t{t['lz6_enc']:.2f}\t{t['xz6_dec']:.2f}\t{t['lz6_dec']:.2f}")
|
||||
print(f"ratios: xz-6 {t['xz6']/t['orig']:.4f} xz-9e {t['xz9e']/t['orig']:.4f} "
|
||||
f"lzcsv-6 {t['lz6']/t['orig']:.4f} lzcsv-9e {t['lz9e']/t['orig']:.4f}")
|
||||
wins6 = sum(1 for r in rows if r['lz6'] < r['xz6'])
|
||||
wins9e = sum(1 for r in rows if r['lz6'] < r['xz9e'])
|
||||
print(f"lzcsv-6 wins vs xz-6: {wins6}/{n}; vs xz-9e: {wins9e}/{n}")
|
||||
return t
|
||||
|
||||
def main():
|
||||
csv_only = "--heldout-only" not in sys.argv
|
||||
held_only = "--csv-only" not in sys.argv
|
||||
sets = []
|
||||
if csv_only:
|
||||
sets.append(("14 corpus CSVs", sorted(glob.glob(os.path.join(CORPUS, "*.csv")))))
|
||||
if held_only:
|
||||
held = sorted(f for f in glob.glob(os.path.join(HELD, "*.csv"))
|
||||
if os.path.basename(f) != "embedded-nl.csv")
|
||||
sets.append(("18 held-out CSVs", held))
|
||||
all_rows = []
|
||||
for title, files in sets:
|
||||
print(f"benchmarking {len(files)} files ({title}) ...", flush=True)
|
||||
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as ex:
|
||||
rows = list(ex.map(bench_one, files))
|
||||
rows.sort(key=lambda r: r["file"])
|
||||
table(rows, title)
|
||||
all_rows.extend(rows)
|
||||
with open(os.path.join(ROOT, "lzcsv-src", "bench-results.tsv"), "a") as f:
|
||||
for r in rows:
|
||||
f.write("\t".join(str(r[k]) for k in
|
||||
("file", "orig", "xz6", "xz9e", "lz6", "lz9e",
|
||||
"xz6_enc", "lz6_enc", "xz6_dec", "lz6_dec")) + "\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
# fresh results file each run
|
||||
p = os.path.join(ROOT, "lzcsv-src", "bench-results.tsv")
|
||||
if os.path.exists(p):
|
||||
os.remove(p)
|
||||
main()
|
||||
@@ -0,0 +1,756 @@
|
||||
/* lzcsv - high-speed CSV compression: column transpose + xz (liblzma).
|
||||
*
|
||||
* A standalone tool in the lzply repo, completely separate from the
|
||||
* lzply FEC core (it does not link liblzply.a). Idea from hot_qwen
|
||||
* Cycle 1 (Dec 2026): transpose a CSV column-wise so same-field values
|
||||
* become adjacent long runs, then compress with xz. On the 14 CSVs of
|
||||
* the fftdict corpus2 the transposed stream compresses to 0.1475 with
|
||||
* xz -6 vs 0.1713 direct (-13.9%), 13/14 files win, all round-trips
|
||||
* byte-exact.
|
||||
*
|
||||
* Container (LZCSV v1):
|
||||
* bytes 0..7 magic "LZCSV001"
|
||||
* bytes 8..15 uint64 LE original (CSV) size
|
||||
* bytes 16..23 uint64 LE transposed (COLT1) size
|
||||
* byte 24 xz preset (0-9)
|
||||
* byte 25 flags (bit 0 = extreme)
|
||||
* bytes 26.. xz stream (liblzma, preset, CRC64 check) holding COLT1
|
||||
*
|
||||
* COLT1 (byte-compatible with /root/hot-qwen/cycle1/transpose.py):
|
||||
* line 1: COLT1 <nrows> <ncols> <crlf:0|1> <trailing_nl:0|1> <uniform:0|1>
|
||||
* if uniform=0: line "COUNTS <len>" then <len> bytes of comma-separated
|
||||
* per-row field counts (ragged rows; padding fields stripped on decode)
|
||||
* per column j: line "COL <len>" then exactly <len> bytes:
|
||||
* the column's raw field texts joined by "\n" (no trailing newline).
|
||||
* Fields containing a raw newline are rejected (same as the prototype).
|
||||
* Mixed line endings (some rows CRLF, some LF) are rejected: the single
|
||||
* crlf flag cannot represent them, and the prototype would silently
|
||||
* mis-round-trip such a file. Consistent endings round-trip exactly.
|
||||
*
|
||||
* Build: make lzcsv (needs liblzma-dev; skipped cleanly without it)
|
||||
* Usage: lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv>
|
||||
* lzcsv d [-f] [-v] <in.lzcsv> <out.csv>
|
||||
* lzcsv test <file.csv> ...
|
||||
* lzcsv info <file.lzcsv>
|
||||
* lzcsv raw-enc <in.csv> <out.colt>
|
||||
* lzcsv raw-dec <in.colt> <out.csv>
|
||||
* lzcsv selftest
|
||||
* lzcsv --help
|
||||
*/
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <lzma.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define LZCSV_MAGIC "LZCSV001"
|
||||
#define LZCSV_MAGIC_LEN 8
|
||||
#define LZCSV_HEADER_LEN 26
|
||||
#define LZCSV_VERSION "1.0"
|
||||
|
||||
static const char *prog = "lzcsv";
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* small helpers */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void die(const char *fmt, ...) {
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
fprintf(stderr, "%s: ", prog);
|
||||
vfprintf(stderr, fmt, ap);
|
||||
fputc('\n', stderr);
|
||||
va_end(ap);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
static void *xmalloc(size_t n) {
|
||||
void *p = malloc(n ? n : 1);
|
||||
if (!p) die("out of memory");
|
||||
return p;
|
||||
}
|
||||
|
||||
static void *xrealloc(void *p, size_t n) {
|
||||
void *q = realloc(p, n ? n : 1);
|
||||
if (!q) die("out of memory");
|
||||
return q;
|
||||
}
|
||||
|
||||
/* Growable byte buffer. */
|
||||
typedef struct {
|
||||
uint8_t *v;
|
||||
size_t n, cap;
|
||||
} buf_t;
|
||||
|
||||
static void buf_init(buf_t *b) { b->v = NULL; b->n = 0; b->cap = 0; }
|
||||
|
||||
static void buf_reserve(buf_t *b, size_t extra) {
|
||||
if (b->n + extra <= b->cap) return;
|
||||
size_t nc = b->cap ? b->cap : 4096;
|
||||
while (nc < b->n + extra) nc *= 2;
|
||||
b->v = xrealloc(b->v, nc);
|
||||
b->cap = nc;
|
||||
}
|
||||
|
||||
static void buf_put(buf_t *b, const void *p, size_t n) {
|
||||
buf_reserve(b, n);
|
||||
memcpy(b->v + b->n, p, n);
|
||||
b->n += n;
|
||||
}
|
||||
|
||||
static void buf_putc(buf_t *b, uint8_t c) { buf_put(b, &c, 1); }
|
||||
|
||||
static void buf_printf(buf_t *b, const char *fmt, ...) {
|
||||
char tmp[256];
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
int n = vsnprintf(tmp, sizeof(tmp), fmt, ap);
|
||||
va_end(ap);
|
||||
if (n < 0) die("format error");
|
||||
if ((size_t)n >= sizeof(tmp)) die("internal: formatted header too long");
|
||||
buf_put(b, tmp, (size_t)n);
|
||||
}
|
||||
|
||||
static void buf_free(buf_t *b) { free(b->v); b->v = NULL; b->n = b->cap = 0; }
|
||||
|
||||
static uint8_t *read_file(const char *path, size_t *len_out) {
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) die("cannot open %s: %s", path, strerror(errno));
|
||||
struct stat st;
|
||||
if (fstat(fileno(f), &st) != 0) die("cannot stat %s: %s", path, strerror(errno));
|
||||
if (st.st_size < 0) die("%s: bad size", path);
|
||||
size_t len = (size_t)st.st_size;
|
||||
uint8_t *data = xmalloc(len);
|
||||
if (len && fread(data, 1, len, f) != len) die("cannot read %s", path);
|
||||
fclose(f);
|
||||
*len_out = len;
|
||||
return data;
|
||||
}
|
||||
|
||||
static void write_file(const char *path, const uint8_t *data, size_t len, int force) {
|
||||
if (!force) {
|
||||
struct stat st;
|
||||
if (stat(path, &st) == 0) die("%s already exists (use -f to overwrite)", path);
|
||||
}
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) die("cannot open %s for writing: %s", path, strerror(errno));
|
||||
if (len && fwrite(data, 1, len, f) != len) die("cannot write %s", path);
|
||||
if (fclose(f) != 0) die("cannot close %s", path);
|
||||
}
|
||||
|
||||
static uint64_t rd64le(const uint8_t *p) {
|
||||
uint64_t v = 0;
|
||||
for (int i = 7; i >= 0; i--) v = (v << 8) | p[i];
|
||||
return v;
|
||||
}
|
||||
|
||||
static void wr64le(uint8_t *p, uint64_t v) {
|
||||
for (int i = 0; i < 8; i++) { p[i] = (uint8_t)(v & 0xff); v >>= 8; }
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* CSV parsing (RFC 4180 quote rules, byte-identical to the COLT1 */
|
||||
/* Python prototype's split_rows). Fields are slices into the input. */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
typedef struct { const uint8_t *p; size_t n; } slice_t;
|
||||
|
||||
typedef struct {
|
||||
slice_t *fields; /* flat, row-major, unpadded */
|
||||
size_t *row_start; /* index of each row's first field in fields[] */
|
||||
size_t *row_count; /* field count per row (before padding) */
|
||||
size_t nrows, ncols;
|
||||
int crlf, trailing_nl, uniform;
|
||||
} csv_t;
|
||||
|
||||
static void csv_free(csv_t *c) {
|
||||
free(c->fields); free(c->row_start); free(c->row_count);
|
||||
memset(c, 0, sizeof(*c));
|
||||
}
|
||||
|
||||
/* Parse data into rows/fields. Returns 0 on success, -1 with *err set
|
||||
* on a rejected file (embedded newline / mixed line endings). */
|
||||
static int csv_parse(const uint8_t *data, size_t len, csv_t *csv, const char **err) {
|
||||
memset(csv, 0, sizeof(*csv));
|
||||
size_t fcap = 0, fn = 0;
|
||||
size_t rcap = 0, rn = 0;
|
||||
int in_q = 0;
|
||||
int crlf = 0, trailing_nl = 0;
|
||||
int crlf_rows = 0, lf_rows = 0;
|
||||
size_t cur_start = 0; /* start of the current field */
|
||||
size_t row_field_start = 0; /* fields[] index where the current row began */
|
||||
size_t i = 0;
|
||||
|
||||
while (i < len) {
|
||||
uint8_t b = data[i];
|
||||
if (in_q) {
|
||||
if (b == 0x22) { /* " */
|
||||
if (i + 1 < len && data[i + 1] == 0x22) { i += 2; continue; }
|
||||
in_q = 0;
|
||||
}
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
if (b == 0x22 && i == cur_start) {
|
||||
/* quote opens a quoted field only at field start (RFC 4180);
|
||||
* a quote mid-field is a literal character */
|
||||
in_q = 1;
|
||||
i++;
|
||||
} else if (b == 0x2c) { /* , */
|
||||
if (fn == fcap) { fcap = fcap ? fcap * 2 : 256; csv->fields = xrealloc(csv->fields, fcap * sizeof(slice_t)); }
|
||||
csv->fields[fn].p = data + cur_start;
|
||||
csv->fields[fn].n = i - cur_start;
|
||||
fn++;
|
||||
cur_start = i + 1;
|
||||
i++;
|
||||
} else if (b == 0x0a) { /* \n: end of row */
|
||||
size_t fend = i;
|
||||
int this_crlf = 0;
|
||||
if (i > cur_start && data[i - 1] == 0x0d) { fend = i - 1; this_crlf = 1; crlf = 1; }
|
||||
if (this_crlf) crlf_rows++; else lf_rows++;
|
||||
if (fn == fcap) { fcap = fcap ? fcap * 2 : 256; csv->fields = xrealloc(csv->fields, fcap * sizeof(slice_t)); }
|
||||
csv->fields[fn].p = data + cur_start;
|
||||
csv->fields[fn].n = fend - cur_start;
|
||||
fn++;
|
||||
if (rn == rcap) { rcap = rcap ? rcap * 2 : 64; csv->row_start = xrealloc(csv->row_start, rcap * sizeof(size_t)); csv->row_count = xrealloc(csv->row_count, rcap * sizeof(size_t)); }
|
||||
csv->row_start[rn] = row_field_start;
|
||||
csv->row_count[rn] = fn - row_field_start;
|
||||
rn++;
|
||||
row_field_start = fn;
|
||||
cur_start = i + 1;
|
||||
i++;
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
/* EOF: leftover field/row (mirrors the prototype exactly). */
|
||||
if (cur_start < len || fn > row_field_start) {
|
||||
if (fn == fcap) { fcap = fcap ? fcap * 2 : 256; csv->fields = xrealloc(csv->fields, fcap * sizeof(slice_t)); }
|
||||
csv->fields[fn].p = data + cur_start;
|
||||
csv->fields[fn].n = len - cur_start;
|
||||
fn++;
|
||||
if (rn == rcap) { rcap = rcap ? rcap * 2 : 64; csv->row_start = xrealloc(csv->row_start, rcap * sizeof(size_t)); csv->row_count = xrealloc(csv->row_count, rcap * sizeof(size_t)); }
|
||||
csv->row_start[rn] = row_field_start;
|
||||
csv->row_count[rn] = fn - row_field_start;
|
||||
rn++;
|
||||
trailing_nl = 0;
|
||||
} else {
|
||||
trailing_nl = 1; /* data ended exactly on a newline (or is empty) */
|
||||
}
|
||||
if (len == 0) trailing_nl = 0;
|
||||
|
||||
csv->nrows = rn;
|
||||
csv->crlf = crlf;
|
||||
csv->trailing_nl = trailing_nl;
|
||||
|
||||
size_t ncols = 0;
|
||||
int uniform = 1;
|
||||
for (size_t r = 0; r < rn; r++) {
|
||||
if (csv->row_count[r] > ncols) ncols = csv->row_count[r];
|
||||
if (r && csv->row_count[r] != csv->row_count[0]) uniform = 0;
|
||||
}
|
||||
csv->ncols = ncols;
|
||||
csv->uniform = rn ? uniform : 1;
|
||||
|
||||
if (crlf_rows && lf_rows) {
|
||||
*err = "mixed line endings (some rows CRLF, some LF); the single crlf flag cannot represent them";
|
||||
return -1;
|
||||
}
|
||||
/* COLT1 joins column fields with "\n": a field holding a raw newline
|
||||
* cannot be represented. Same rejection as the prototype. */
|
||||
for (size_t k = 0; k < fn; k++) {
|
||||
if (memchr(csv->fields[k].p, '\n', csv->fields[k].n)) {
|
||||
*err = "field contains a raw newline (embedded newlines in quoted fields are not supported by the COLT1 transpose)";
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* field (row i, col j), honouring ragged-row padding with empty fields */
|
||||
static slice_t csv_field(const csv_t *csv, size_t i, size_t j) {
|
||||
static const uint8_t empty = 0;
|
||||
if (j < csv->row_count[i]) return csv->fields[csv->row_start[i] + j];
|
||||
slice_t s = { &empty, 0 };
|
||||
return s;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* COLT1 transpose (byte-compatible with transpose.py) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int colt_encode(const uint8_t *data, size_t len, buf_t *out, const char **err) {
|
||||
csv_t csv;
|
||||
if (csv_parse(data, len, &csv, err) != 0) return -1;
|
||||
buf_printf(out, "COLT1 %zu %zu %d %d %d\n", csv.nrows, csv.ncols,
|
||||
csv.crlf, csv.trailing_nl, csv.uniform);
|
||||
if (!csv.uniform) {
|
||||
buf_t cb;
|
||||
buf_init(&cb);
|
||||
for (size_t r = 0; r < csv.nrows; r++) {
|
||||
if (r) buf_putc(&cb, ',');
|
||||
buf_printf(&cb, "%zu", csv.row_count[r]);
|
||||
}
|
||||
buf_printf(out, "COUNTS %zu\n", cb.n);
|
||||
buf_put(out, cb.v, cb.n);
|
||||
buf_free(&cb);
|
||||
}
|
||||
for (size_t j = 0; j < csv.ncols; j++) {
|
||||
size_t blen = 0;
|
||||
for (size_t r = 0; r < csv.nrows; r++) blen += csv_field(&csv, r, j).n;
|
||||
if (csv.nrows) blen += csv.nrows - 1; /* "\n" separators */
|
||||
buf_printf(out, "COL %zu\n", blen);
|
||||
for (size_t r = 0; r < csv.nrows; r++) {
|
||||
if (r) buf_putc(out, '\n');
|
||||
slice_t f = csv_field(&csv, r, j);
|
||||
buf_put(out, f.p, f.n);
|
||||
}
|
||||
}
|
||||
csv_free(&csv);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Parse one "WORD <num>\n" header line at *pos. Advances *pos past it. */
|
||||
static int colt_header(const uint8_t *data, size_t len, size_t *pos,
|
||||
const char *word, size_t *num) {
|
||||
size_t p = *pos;
|
||||
size_t eol = p;
|
||||
while (eol < len && data[eol] != '\n') eol++;
|
||||
if (eol >= len) return -1;
|
||||
size_t wlen = strlen(word);
|
||||
if (eol - p < wlen + 2 || memcmp(data + p, word, wlen) != 0 || data[p + wlen] != ' ')
|
||||
return -1;
|
||||
char tmp[32];
|
||||
size_t nlen = eol - (p + wlen + 1);
|
||||
if (nlen == 0 || nlen >= sizeof(tmp)) return -1;
|
||||
memcpy(tmp, data + p + wlen + 1, nlen);
|
||||
tmp[nlen] = 0;
|
||||
char *end = NULL;
|
||||
unsigned long v = strtoul(tmp, &end, 10);
|
||||
if (!end || *end) return -1;
|
||||
*num = (size_t)v;
|
||||
*pos = eol + 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int colt_decode(const uint8_t *data, size_t len, buf_t *out, const char **err) {
|
||||
size_t pos = 0;
|
||||
size_t eol = 0;
|
||||
while (eol < len && data[eol] != '\n') eol++;
|
||||
if (eol >= len) { *err = "COLT1: truncated header"; return -1; }
|
||||
char hdr[128];
|
||||
if (eol >= sizeof(hdr)) { *err = "COLT1: header line too long"; return -1; }
|
||||
memcpy(hdr, data, eol);
|
||||
hdr[eol] = 0;
|
||||
long nrows, ncols, crlf, tnl, uniform;
|
||||
if (sscanf(hdr, "COLT1 %ld %ld %ld %ld %ld", &nrows, &ncols, &crlf, &tnl, &uniform) != 5 ||
|
||||
nrows < 0 || ncols < 0 || (crlf != 0 && crlf != 1) || (tnl != 0 && tnl != 1) ||
|
||||
(uniform != 0 && uniform != 1)) {
|
||||
*err = "COLT1: bad magic/header";
|
||||
return -1;
|
||||
}
|
||||
if ((nrows == 0) != (ncols == 0)) {
|
||||
*err = "COLT1: row/column counts inconsistent";
|
||||
return -1;
|
||||
}
|
||||
pos = eol + 1;
|
||||
|
||||
size_t *counts = NULL;
|
||||
if (nrows > 0) {
|
||||
counts = xmalloc((size_t)nrows * sizeof(size_t));
|
||||
for (long r = 0; r < nrows; r++) counts[r] = (size_t)ncols;
|
||||
}
|
||||
if (!uniform) {
|
||||
size_t clen = 0;
|
||||
if (colt_header(data, len, &pos, "COUNTS", &clen) != 0) { *err = "COLT1: bad COUNTS header"; free(counts); return -1; }
|
||||
if (pos + clen > len) { *err = "COLT1: truncated COUNTS blob"; free(counts); return -1; }
|
||||
/* parse comma-separated decimal counts */
|
||||
size_t r = 0;
|
||||
size_t p = pos;
|
||||
size_t end = pos + clen;
|
||||
while (p < end) {
|
||||
if (r >= (size_t)nrows) { *err = "COLT1: too many counts"; free(counts); return -1; }
|
||||
unsigned long v = 0;
|
||||
int ndig = 0;
|
||||
while (p < end && data[p] >= '0' && data[p] <= '9') { v = v * 10 + (data[p] - '0'); p++; ndig++; }
|
||||
if (!ndig) { *err = "COLT1: bad COUNTS blob"; free(counts); return -1; }
|
||||
counts[r++] = (size_t)v;
|
||||
if (p < end && data[p] == ',') p++;
|
||||
else if (p < end) { *err = "COLT1: bad COUNTS blob"; free(counts); return -1; }
|
||||
}
|
||||
if (r != (size_t)nrows) { *err = "COLT1: counts length mismatch"; free(counts); return -1; }
|
||||
pos = end;
|
||||
}
|
||||
|
||||
/* columns: split each COL blob on "\n" into nrows slices */
|
||||
slice_t *cols = NULL; /* ncols x nrows, column-major */
|
||||
if (ncols > 0 && nrows > 0) cols = xmalloc((size_t)ncols * (size_t)nrows * sizeof(slice_t));
|
||||
for (long j = 0; j < ncols; j++) {
|
||||
size_t blen = 0;
|
||||
if (colt_header(data, len, &pos, "COL", &blen) != 0) { *err = "COLT1: bad COL header"; free(counts); free(cols); return -1; }
|
||||
if (pos + blen > len) { *err = "COLT1: truncated COL blob"; free(counts); free(cols); return -1; }
|
||||
const uint8_t *blob = data + pos;
|
||||
pos += blen;
|
||||
if (blen == 0) {
|
||||
for (long r = 0; r < nrows; r++) { cols[(size_t)j * (size_t)nrows + r].p = blob; cols[(size_t)j * (size_t)nrows + r].n = 0; }
|
||||
} else {
|
||||
size_t start = 0;
|
||||
long r = 0;
|
||||
for (size_t k = 0; k <= blen; k++) {
|
||||
if (k == blen || blob[k] == '\n') {
|
||||
if (r >= nrows) { *err = "COLT1: column field count mismatch"; free(counts); free(cols); return -1; }
|
||||
cols[(size_t)j * (size_t)nrows + r].p = blob + start;
|
||||
cols[(size_t)j * (size_t)nrows + r].n = k - start;
|
||||
r++;
|
||||
start = k + 1;
|
||||
}
|
||||
}
|
||||
if (r != nrows) { *err = "COLT1: column field count mismatch"; free(counts); free(cols); return -1; }
|
||||
}
|
||||
}
|
||||
if (pos != len) { *err = "COLT1: trailing data after last column"; free(counts); free(cols); return -1; }
|
||||
|
||||
if (ncols == 0) { free(counts); free(cols); return 0; } /* empty input */
|
||||
const uint8_t *eolb = crlf ? (const uint8_t *)"\r\n" : (const uint8_t *)"\n";
|
||||
size_t eollen = crlf ? 2 : 1;
|
||||
for (long i = 0; i < nrows; i++) {
|
||||
for (size_t j = 0; j < counts[i]; j++) {
|
||||
if (j) buf_putc(out, ',');
|
||||
slice_t f = cols[j * (size_t)nrows + (size_t)i];
|
||||
buf_put(out, f.p, f.n);
|
||||
}
|
||||
buf_put(out, eolb, eollen);
|
||||
}
|
||||
if (!tnl && out->n >= eollen) out->n -= eollen;
|
||||
free(counts);
|
||||
free(cols);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* xz via liblzma (easy encoder, CRC64 check: same stream as xz -6) */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static int xz_compress(const uint8_t *in, size_t in_len, uint32_t preset,
|
||||
uint8_t *out, size_t out_cap, size_t *out_len) {
|
||||
/* NOTE: out_pos is in/out and MUST start at 0 (it is the write offset,
|
||||
* not the buffer size); passing the capacity here made the encoder
|
||||
* think the buffer was already full -> LZMA_BUF_ERROR on every call. */
|
||||
size_t out_pos = 0;
|
||||
lzma_ret r = lzma_easy_buffer_encode(preset, LZMA_CHECK_CRC64, NULL,
|
||||
in, in_len, out, &out_pos, out_cap);
|
||||
if (r != LZMA_OK) return -1;
|
||||
*out_len = out_pos;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int xz_decompress(const uint8_t *in, size_t in_len, uint8_t *out,
|
||||
size_t *out_len) {
|
||||
uint64_t memlimit = UINT64_MAX;
|
||||
size_t in_pos = 0, out_pos = 0;
|
||||
lzma_ret r = lzma_stream_buffer_decode(&memlimit, 0, NULL,
|
||||
in, &in_pos, in_len,
|
||||
out, &out_pos, *out_len);
|
||||
if (r != LZMA_OK) return -1;
|
||||
if (in_pos != in_len) return -1; /* trailing data */
|
||||
*out_len = out_pos;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* LZCSV container */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void put_header(uint8_t *h, uint64_t orig, uint64_t trans,
|
||||
uint8_t preset, uint8_t flags) {
|
||||
memcpy(h, LZCSV_MAGIC, LZCSV_MAGIC_LEN);
|
||||
wr64le(h + 8, orig);
|
||||
wr64le(h + 16, trans);
|
||||
h[24] = preset;
|
||||
h[25] = flags;
|
||||
}
|
||||
|
||||
static int get_header(const uint8_t *h, size_t len, uint64_t *orig,
|
||||
uint64_t *trans, uint8_t *preset, uint8_t *flags) {
|
||||
if (len < LZCSV_HEADER_LEN) return -1;
|
||||
if (memcmp(h, LZCSV_MAGIC, LZCSV_MAGIC_LEN) != 0) return -1;
|
||||
*orig = rd64le(h + 8);
|
||||
*trans = rd64le(h + 16);
|
||||
*preset = h[24];
|
||||
*flags = h[25];
|
||||
if (*preset > 9 || (*flags & ~1u)) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ */
|
||||
/* commands */
|
||||
/* ------------------------------------------------------------------ */
|
||||
|
||||
static void usage(FILE *f) {
|
||||
fprintf(f,
|
||||
"lzcsv %s - high-speed CSV compression (column transpose + xz)\n"
|
||||
"usage: lzcsv <cmd> [options] <in> [out]\n"
|
||||
" c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv> compress (preset 6 default)\n"
|
||||
" d [-f] [-v] <in.lzcsv> <out.csv> decompress\n"
|
||||
" test <file.csv> ... in-memory round-trip check\n"
|
||||
" info <file.lzcsv> show container header\n"
|
||||
" raw-enc <in.csv> <out.colt> write COLT1 intermediate\n"
|
||||
" raw-dec <in.colt> <out.csv> decode COLT1 intermediate\n"
|
||||
" selftest built-in synthetic tests\n"
|
||||
" --help this text\n"
|
||||
"options: -p N xz preset 0-9 (default 6); -e extreme (-9e style);\n"
|
||||
" -f overwrite output; -v print sizes\n"
|
||||
"The COLT1 transpose is byte-compatible with the hot_qwen Cycle 1\n"
|
||||
"Python prototype. Fields holding a raw newline and files with mixed\n"
|
||||
"line endings are rejected (documented limitation).\n", LZCSV_VERSION);
|
||||
}
|
||||
|
||||
static int do_compress(const char *in_path, const char *out_path,
|
||||
int preset, int extreme, int force, int verbose) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
buf_t trans;
|
||||
buf_init(&trans);
|
||||
const char *err = NULL;
|
||||
if (colt_encode(in, in_len, &trans, &err) != 0) {
|
||||
free(in);
|
||||
die("%s: %s", in_path, err);
|
||||
}
|
||||
size_t bound = lzma_stream_buffer_bound(trans.n);
|
||||
uint8_t *xz = xmalloc(bound);
|
||||
size_t xz_len = 0;
|
||||
uint32_t pr = (uint32_t)preset | (extreme ? LZMA_PRESET_EXTREME : 0);
|
||||
if (xz_compress(trans.v, trans.n, pr, xz, bound, &xz_len) != 0) {
|
||||
free(in); buf_free(&trans); free(xz);
|
||||
die("%s: xz compression failed", in_path);
|
||||
}
|
||||
buf_t file;
|
||||
buf_init(&file);
|
||||
uint8_t hdr[LZCSV_HEADER_LEN];
|
||||
put_header(hdr, (uint64_t)in_len, (uint64_t)trans.n, (uint8_t)preset, extreme ? 1 : 0);
|
||||
buf_put(&file, hdr, LZCSV_HEADER_LEN);
|
||||
buf_put(&file, xz, xz_len);
|
||||
write_file(out_path, file.v, file.n, force);
|
||||
if (verbose)
|
||||
printf("%s -> %s: %zu -> %zu bytes (ratio %.4f, preset %d%s)\n",
|
||||
in_path, out_path, in_len, file.n,
|
||||
in_len ? (double)file.n / (double)in_len : 0.0,
|
||||
preset, extreme ? "e" : "");
|
||||
free(in); buf_free(&trans); free(xz); buf_free(&file);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_decompress(const char *in_path, const char *out_path,
|
||||
int force, int verbose) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
uint64_t orig = 0, trans = 0;
|
||||
uint8_t preset = 0, flags = 0;
|
||||
if (get_header(in, in_len, &orig, &trans, &preset, &flags) != 0) {
|
||||
free(in);
|
||||
die("%s: not an LZCSV file (bad magic/header)", in_path);
|
||||
}
|
||||
if (trans > (uint64_t)SIZE_MAX || orig > (uint64_t)SIZE_MAX) {
|
||||
free(in);
|
||||
die("%s: implausible sizes in header", in_path);
|
||||
}
|
||||
uint8_t *colt = xmalloc((size_t)trans ? (size_t)trans : 1);
|
||||
size_t colt_len = (size_t)trans;
|
||||
if (xz_decompress(in + LZCSV_HEADER_LEN, in_len - LZCSV_HEADER_LEN,
|
||||
colt, &colt_len) != 0) {
|
||||
free(in); free(colt);
|
||||
die("%s: xz stream corrupt or truncated", in_path);
|
||||
}
|
||||
if (colt_len != (size_t)trans) {
|
||||
free(in); free(colt);
|
||||
die("%s: transposed size mismatch (header says %llu, got %zu)",
|
||||
in_path, (unsigned long long)trans, colt_len);
|
||||
}
|
||||
buf_t out;
|
||||
buf_init(&out);
|
||||
const char *err = NULL;
|
||||
if (colt_decode(colt, colt_len, &out, &err) != 0) {
|
||||
free(in); free(colt); buf_free(&out);
|
||||
die("%s: COLT1 decode failed: %s", in_path, err);
|
||||
}
|
||||
if (out.n != (size_t)orig) {
|
||||
free(in); free(colt); buf_free(&out);
|
||||
die("%s: output size mismatch (header says %llu, got %zu)",
|
||||
in_path, (unsigned long long)orig, out.n);
|
||||
}
|
||||
write_file(out_path, out.v, out.n, force);
|
||||
if (verbose)
|
||||
printf("%s -> %s: %zu -> %zu bytes\n", in_path, out_path, in_len, out.n);
|
||||
free(in); free(colt); buf_free(&out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_raw_enc(const char *in_path, const char *out_path, int force) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
buf_t trans;
|
||||
buf_init(&trans);
|
||||
const char *err = NULL;
|
||||
if (colt_encode(in, in_len, &trans, &err) != 0) {
|
||||
free(in);
|
||||
die("%s: %s", in_path, err);
|
||||
}
|
||||
write_file(out_path, trans.v, trans.n, force);
|
||||
free(in); buf_free(&trans);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_raw_dec(const char *in_path, const char *out_path, int force) {
|
||||
size_t in_len = 0;
|
||||
uint8_t *in = read_file(in_path, &in_len);
|
||||
buf_t out;
|
||||
buf_init(&out);
|
||||
const char *err = NULL;
|
||||
if (colt_decode(in, in_len, &out, &err) != 0) {
|
||||
free(in); buf_free(&out);
|
||||
die("%s: %s", in_path, err);
|
||||
}
|
||||
write_file(out_path, out.v, out.n, force);
|
||||
free(in); buf_free(&out);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int do_info(const char *path) {
|
||||
size_t len = 0;
|
||||
uint8_t *in = read_file(path, &len);
|
||||
uint64_t orig = 0, trans = 0;
|
||||
uint8_t preset = 0, flags = 0;
|
||||
if (get_header(in, len, &orig, &trans, &preset, &flags) != 0) {
|
||||
free(in);
|
||||
die("%s: not an LZCSV file (bad magic/header)", path);
|
||||
}
|
||||
printf("%s: LZCSV v1, original %llu bytes, transposed %llu bytes, "
|
||||
"xz preset %d%s, container %zu bytes\n",
|
||||
path, (unsigned long long)orig, (unsigned long long)trans,
|
||||
preset, flags ? "e" : "", len);
|
||||
free(in);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* In-memory round-trip of one file; returns 0 on success. */
|
||||
static int roundtrip_mem(const uint8_t *data, size_t len, const char **err) {
|
||||
buf_t trans;
|
||||
buf_init(&trans);
|
||||
if (colt_encode(data, len, &trans, err) != 0) { buf_free(&trans); return -1; }
|
||||
buf_t out;
|
||||
buf_init(&out);
|
||||
if (colt_decode(trans.v, trans.n, &out, err) != 0) { buf_free(&trans); buf_free(&out); return -1; }
|
||||
int ok = (out.n == len && (len == 0 || memcmp(out.v, data, len) == 0));
|
||||
if (!ok) *err = "round-trip output differs from input";
|
||||
buf_free(&trans); buf_free(&out);
|
||||
return ok ? 0 : -1;
|
||||
}
|
||||
|
||||
static int do_test(int nfiles, char **files) {
|
||||
int fail = 0;
|
||||
for (int i = 0; i < nfiles; i++) {
|
||||
size_t len = 0;
|
||||
uint8_t *data = read_file(files[i], &len);
|
||||
const char *err = NULL;
|
||||
if (roundtrip_mem(data, len, &err) == 0) {
|
||||
printf("OK %s (%zu bytes)\n", files[i], len);
|
||||
} else {
|
||||
printf("FAIL %s: %s\n", files[i], err);
|
||||
fail++;
|
||||
}
|
||||
free(data);
|
||||
}
|
||||
return fail ? 1 : 0;
|
||||
}
|
||||
|
||||
static int selftest_one(const char *name, const uint8_t *data, size_t len) {
|
||||
const char *err = NULL;
|
||||
if (roundtrip_mem(data, len, &err) == 0) {
|
||||
printf("PASS %s\n", name);
|
||||
return 0;
|
||||
}
|
||||
printf("FAIL %s: %s\n", name, err);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static int do_selftest(void) {
|
||||
int fail = 0;
|
||||
fail += selftest_one("empty", (const uint8_t *)"", 0);
|
||||
fail += selftest_one("one-row-no-nl", (const uint8_t *)"a,b,c", 5);
|
||||
fail += selftest_one("header-only", (const uint8_t *)"col1,col2,col3\n", 15);
|
||||
fail += selftest_one("crlf", (const uint8_t *)"name,age\r\nann,34\r\nbob,29\r\n", 26);
|
||||
fail += selftest_one("ragged", (const uint8_t *)"a,b,c\n1,2\n3,4,5,6,7\n8\n", 22);
|
||||
fail += selftest_one("quotes", (const uint8_t *)"id,note\n1,\"hello, world\"\n2,\"say \"\"hi\"\"\"\n3,plain \"quoted\" text\n", 62);
|
||||
fail += selftest_one("no-trailing-nl", (const uint8_t *)"x,y\n1,2\n3,4", 11);
|
||||
fail += selftest_one("single-col", (const uint8_t *)"v\n1\n22\n333\n", 11);
|
||||
fail += selftest_one("empty-fields", (const uint8_t *)"a,,c\n,,\n1,2,\n", 13);
|
||||
fail += selftest_one("quoted-empty", (const uint8_t *)"a,b\n\"\",x\n", 9);
|
||||
printf("selftest: %s\n", fail ? "FAIL" : "all pass");
|
||||
return fail ? 1 : 0;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) { usage(stderr); return 2; }
|
||||
const char *cmd = argv[1];
|
||||
if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h") || !strcmp(cmd, "help")) {
|
||||
usage(stdout);
|
||||
return 0;
|
||||
}
|
||||
if (!strcmp(cmd, "selftest")) return do_selftest();
|
||||
|
||||
int preset = 6, extreme = 0, force = 0, verbose = 0;
|
||||
int i = 2;
|
||||
/* options come after the subcommand */
|
||||
while (i < argc && argv[i][0] == '-' && argv[i][1] != 0) {
|
||||
if (!strcmp(argv[i], "-p")) {
|
||||
if (++i >= argc) { fprintf(stderr, "%s: -p needs a preset\n", prog); return 2; }
|
||||
preset = atoi(argv[i]);
|
||||
if (preset < 0 || preset > 9) { fprintf(stderr, "%s: preset must be 0-9\n", prog); return 2; }
|
||||
} else if (!strcmp(argv[i], "-e")) {
|
||||
extreme = 1;
|
||||
} else if (!strcmp(argv[i], "-f")) {
|
||||
force = 1;
|
||||
} else if (!strcmp(argv[i], "-v")) {
|
||||
verbose = 1;
|
||||
} else {
|
||||
fprintf(stderr, "%s: unknown option %s\n", prog, argv[i]);
|
||||
return 2;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
int npos = argc - i;
|
||||
char **pos = argv + i;
|
||||
|
||||
if (!strcmp(cmd, "c")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv c [-p N] [-e] [-f] [-v] <in.csv> <out.lzcsv>\n"); return 2; }
|
||||
return do_compress(pos[0], pos[1], preset, extreme, force, verbose);
|
||||
}
|
||||
if (!strcmp(cmd, "d")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv d [-f] [-v] <in.lzcsv> <out.csv>\n"); return 2; }
|
||||
return do_decompress(pos[0], pos[1], force, verbose);
|
||||
}
|
||||
if (!strcmp(cmd, "test")) {
|
||||
if (npos < 1) { fprintf(stderr, "usage: lzcsv test <file.csv> ...\n"); return 2; }
|
||||
return do_test(npos, pos);
|
||||
}
|
||||
if (!strcmp(cmd, "info")) {
|
||||
if (npos != 1) { fprintf(stderr, "usage: lzcsv info <file.lzcsv>\n"); return 2; }
|
||||
return do_info(pos[0]);
|
||||
}
|
||||
if (!strcmp(cmd, "raw-enc")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv raw-enc <in.csv> <out.colt>\n"); return 2; }
|
||||
return do_raw_enc(pos[0], pos[1], force);
|
||||
}
|
||||
if (!strcmp(cmd, "raw-dec")) {
|
||||
if (npos != 2) { fprintf(stderr, "usage: lzcsv raw-dec <in.colt> <out.csv>\n"); return 2; }
|
||||
return do_raw_dec(pos[0], pos[1], force);
|
||||
}
|
||||
fprintf(stderr, "%s: unknown command '%s'\n", prog, cmd);
|
||||
usage(stderr);
|
||||
return 2;
|
||||
}
|
||||
Executable
+144
@@ -0,0 +1,144 @@
|
||||
#!/bin/bash
|
||||
# run_lzcsv_tests.sh - test suite for lzcsv (standalone CSV compressor).
|
||||
# Usage: run_lzcsv_tests.sh [path-to-lzcsv]
|
||||
# Follows the repo's test conventions (PASS/FAIL counters, ok/bad).
|
||||
# The corpus tests run only when the fftdict corpus is present
|
||||
# (/root/fftdict-shared/corpus2); otherwise they are skipped, and the
|
||||
# synthetic round-trips (generated here) still run. The oracle check
|
||||
# runs only when the Python COLT1 prototype is present.
|
||||
set -u
|
||||
LZCSV="${1:-./lzcsv}"
|
||||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||
ROOT="$(dirname "$SCRIPT_DIR")"
|
||||
WORK="$ROOT/testwork-lzcsv"
|
||||
CORPUS="/root/fftdict-shared/corpus2"
|
||||
HELD="/root/hot-qwen/lzcsv-heldout"
|
||||
PY="/root/hot-qwen/cycle1/transpose.py"
|
||||
PASS=0
|
||||
FAIL=0
|
||||
SKIP=0
|
||||
|
||||
ok() { PASS=$((PASS+1)); echo "PASS: $1"; }
|
||||
bad() { FAIL=$((FAIL+1)); echo "FAIL: $1"; }
|
||||
skip() { SKIP=$((SKIP+1)); echo "SKIP: $1"; }
|
||||
|
||||
rm -rf "$WORK"
|
||||
mkdir -p "$WORK"
|
||||
[ -x "$LZCSV" ] || { echo "lzcsv binary not found: $LZCSV"; exit 1; }
|
||||
|
||||
# --- 1. selftest + help ---------------------------------------------
|
||||
if "$LZCSV" selftest > "$WORK/selftest.log" 2>&1; then ok "selftest"; else bad "selftest"; cat "$WORK/selftest.log"; fi
|
||||
if "$LZCSV" --help > "$WORK/help.log" 2>&1; then ok "--help"; else bad "--help"; fi
|
||||
|
||||
# --- 2. synthetic round-trips (generated here, always run) -----------
|
||||
printf 'a,b,c\n1,2,3\n4,5,6\n' > "$WORK/synth1.csv"
|
||||
printf 'name,age,city\r\nann,34,oslo\r\nbob,29,bergen\r\n' > "$WORK/synth-crlf.csv"
|
||||
printf 'a,b,c\n1,2\n3,4,5,6,7\n8\n' > "$WORK/synth-ragged.csv"
|
||||
printf 'id,note\n1,"hello, world"\n2,"say ""hi"""\n3,plain "quoted" text\n' > "$WORK/synth-quotes.csv"
|
||||
printf 'x,y\n1,2\n3,4' > "$WORK/synth-noeol.csv"
|
||||
printf 'col1,col2,col3\n' > "$WORK/synth-header.csv"
|
||||
: > "$WORK/synth-empty.csv"
|
||||
for f in "$WORK"/synth*.csv; do
|
||||
n="$(basename "$f")"
|
||||
if "$LZCSV" c -f "$f" "$WORK/$n.lzcsv" > /dev/null 2>&1 && \
|
||||
"$LZCSV" d -f "$WORK/$n.lzcsv" "$WORK/$n.rt.csv" > /dev/null 2>&1 && \
|
||||
cmp -s "$f" "$WORK/$n.rt.csv"; then
|
||||
ok "round-trip $n"
|
||||
else
|
||||
bad "round-trip $n"
|
||||
fi
|
||||
done
|
||||
# in-memory test subcommand over the synthetic set
|
||||
if "$LZCSV" test "$WORK"/synth*.csv > "$WORK/test.log" 2>&1; then ok "lzcsv test (synthetic)"; else bad "lzcsv test (synthetic)"; cat "$WORK/test.log"; fi
|
||||
|
||||
# --- 3. corpus round-trips (only where the corpus exists) ------------
|
||||
if [ -d "$CORPUS" ]; then
|
||||
n=0; ok_n=0
|
||||
for f in "$CORPUS"/*.csv; do
|
||||
n=$((n+1))
|
||||
b="$(basename "$f")"
|
||||
if "$LZCSV" c -f "$f" "$WORK/$b.lzcsv" > /dev/null 2>&1 && \
|
||||
"$LZCSV" d -f "$WORK/$b.lzcsv" "$WORK/$b.rt.csv" > /dev/null 2>&1 && \
|
||||
cmp -s "$f" "$WORK/$b.rt.csv"; then
|
||||
ok_n=$((ok_n+1))
|
||||
else
|
||||
bad "corpus round-trip $b"
|
||||
fi
|
||||
done
|
||||
[ "$ok_n" = "$n" ] && ok "corpus round-trips $ok_n/$n"
|
||||
else
|
||||
skip "corpus round-trips (no $CORPUS)"
|
||||
fi
|
||||
|
||||
# --- 4. held-out round-trips (only where the set exists) --------------
|
||||
if [ -d "$HELD" ]; then
|
||||
n=0; ok_n=0
|
||||
for f in "$HELD"/*.csv; do
|
||||
b="$(basename "$f")"
|
||||
[ "$b" = "embedded-nl.csv" ] && continue
|
||||
n=$((n+1))
|
||||
if "$LZCSV" c -f "$f" "$WORK/h-$b.lzcsv" > /dev/null 2>&1 && \
|
||||
"$LZCSV" d -f "$WORK/h-$b.lzcsv" "$WORK/h-$b.rt.csv" > /dev/null 2>&1 && \
|
||||
cmp -s "$f" "$WORK/h-$b.rt.csv"; then
|
||||
ok_n=$((ok_n+1))
|
||||
else
|
||||
bad "held-out round-trip $b"
|
||||
fi
|
||||
done
|
||||
[ "$ok_n" = "$n" ] && ok "held-out round-trips $ok_n/$n"
|
||||
else
|
||||
skip "held-out round-trips (no $HELD)"
|
||||
fi
|
||||
|
||||
# --- 5. oracle: COLT1 output byte-identical to the Python prototype --
|
||||
if [ -f "$PY" ] && [ -d "$CORPUS" ]; then
|
||||
n=0; ok_n=0
|
||||
for f in "$CORPUS"/*.csv; do
|
||||
n=$((n+1))
|
||||
b="$(basename "$f")"
|
||||
python3 "$PY" enc "$f" "$WORK/$b.py.colt" 2>/dev/null
|
||||
"$LZCSV" raw-enc -f "$f" "$WORK/$b.c.colt" 2>/dev/null
|
||||
if cmp -s "$WORK/$b.py.colt" "$WORK/$b.c.colt"; then
|
||||
ok_n=$((ok_n+1))
|
||||
else
|
||||
bad "oracle raw-enc $b"
|
||||
fi
|
||||
# cross-decode both ways
|
||||
"$LZCSV" raw-dec -f "$WORK/$b.py.colt" "$WORK/$b.c-dec.csv" 2>/dev/null
|
||||
python3 "$PY" dec "$WORK/$b.c.colt" "$WORK/$b.py-dec.csv" 2>/dev/null
|
||||
cmp -s "$WORK/$b.c-dec.csv" "$f" || bad "C raw-dec of Python .colt $b"
|
||||
cmp -s "$WORK/$b.py-dec.csv" "$f" || bad "Python dec of C .colt $b"
|
||||
done
|
||||
[ "$ok_n" = "$n" ] && ok "oracle raw-enc byte-identical $ok_n/$n"
|
||||
else
|
||||
skip "oracle check (no prototype or no corpus)"
|
||||
fi
|
||||
|
||||
# --- 6. clean rejections ----------------------------------------------
|
||||
printf 'id,note\n1,"first line\nsecond line"\n2,plain\n' > "$WORK/embedded.csv"
|
||||
if "$LZCSV" c "$WORK/embedded.csv" "$WORK/embedded.lzcsv" > /dev/null 2>&1; then
|
||||
bad "embedded-newline input must be rejected"
|
||||
else
|
||||
ok "embedded-newline input rejected cleanly"
|
||||
fi
|
||||
# truncated input
|
||||
if [ -f "$WORK/synth1.csv.lzcsv" ]; then
|
||||
head -c 20 "$WORK/synth1.csv.lzcsv" > "$WORK/trunc.lzcsv"
|
||||
if "$LZCSV" d "$WORK/trunc.lzcsv" "$WORK/trunc.csv" > /dev/null 2>&1; then
|
||||
bad "truncated input must fail"
|
||||
else
|
||||
ok "truncated input fails cleanly"
|
||||
fi
|
||||
printf 'NOTLZCSV' > "$WORK/badmagic.lzcsv"
|
||||
cat "$WORK/synth1.csv.lzcsv" >> "$WORK/badmagic.lzcsv"
|
||||
if "$LZCSV" d "$WORK/badmagic.lzcsv" "$WORK/bm.csv" > /dev/null 2>&1; then
|
||||
bad "bad-magic input must fail"
|
||||
else
|
||||
ok "bad-magic input fails cleanly"
|
||||
fi
|
||||
fi
|
||||
# info works
|
||||
if "$LZCSV" info "$WORK/synth1.csv.lzcsv" > /dev/null 2>&1; then ok "info"; else bad "info"; fi
|
||||
|
||||
echo "lzcsv tests: $PASS passed, $FAIL failed, $SKIP skipped"
|
||||
[ "$FAIL" = 0 ]
|
||||
Reference in new issue
Block a user