Skip to content

perf: append measurements to CSV and SQLite instead of rewriting - #85

Merged
firefly-cpp merged 1 commit into
mainfrom
perf/append-csv-sqlite
Nov 21, 2026
Merged

firefly-cpp merged 1 commit into
mainfrom
perf/append-csv-sqlite

Conversation

@firefly-cpp

Copy link
Copy Markdown
Owner

Problem

Every POST /measure read the whole data file, added one row with pd.concat and wrote the whole file again (SQLite: to_sql(if_exists='replace'), i.e. the table was recreated). Consequences:

  • Storing gets slower as data grows. Measured per measurement:

    Format 1,000 rows 10,000 rows 100,000 rows 1,000,000 rows
    CSV before 5.5 ms 18.7 ms 184 ms (not measured)
    CSV after 2.3 ms 3.0 ms 3.0 ms 2.6 ms
    SQLite before 10.3 ms 54.1 ms 202 ms (not measured)
    SQLite after 2.9 ms 2.9 ms 3.0 ms 3.1 ms

    100,000 rows is about a year of 5-minute measurements; a Raspberry Pi is several times slower than the machine used here.

  • All data stayed in memory (Processing.df).

  • Concurrent measurements were lost. With 6 processes (like gunicorn workers) sending 150 measurements, only 43 (CSV), 37 (SQLite) and 36 (JSON) were stored, and many requests failed.

Changes

  • CSV: only the header is read (nrows=0), then the row is appended. If the header lacks a configured column (column added to the configuration, timestamps enabled later), the file is rewritten once with the new column; then appending continues. Stored columns that are no longer configured stay and are empty for new rows. A file without a trailing newline is handled.
  • SQLite: INSERT of the new row; missing columns are added with ALTER TABLE. New tables are created with untyped columns, so values keep the type they were sent with (with pandas' type inference, a first measurement with a missing value made a column TEXT permanently). Lists/objects are stored as JSON instead of failing. Existing databases created by older versions keep working.
  • JSON, XML: still rewrite the whole file, now under the lock. Appending to them is planned for a separate PR.
  • Locking: Processing.storage_lock() uses a lock file next to the data file (data/.data.csv.lock): fcntl.flock on Linux/macOS, msvcrt.locking on Windows. It works between threads and processes. /data, /export and /explore take the same lock, so they never read a half-written row.
  • Values are stored as sent: the row is built with dtype=object, so e.g. 21 is no longer written as 21.0, and earlier rows are no longer re-formatted by later measurements (e.g. "007" → 7).
  • Data folder race: os.makedirs(..., exist_ok=True); before, several processes creating it at the same time got a 500 error.
  • Processing.df holds only the last stored row after a measurement (all data after /data or /export, as before).
  • Processing.load() is shared by /data, /export and /explore.

File formats are unchanged, so existing data files keep working without conversion.

Tests

  • New tests/test_storage.py (20 tests): CSV append and file content, only the header is read, earlier values unchanged, new/removed columns, timestamps enabled later, missing trailing newline, empty file, quoting; SQLite insert, no full read, ALTER TABLE with a quoted column name, JSON for lists/objects, first value missing, databases from older versions; concurrent writes from threads and from 4 processes for all four formats; reading while writing.
  • 19 of them fail on the previous code, including the multi-process test for all formats.
  • Full suite: 104 passed. ruff check passes.

Generated by Claude Code

Every measurement read the whole data file, added one row and wrote
the whole file again, so storing became slower as data grew (e.g. 184
ms per measurement with 100,000 CSV rows), all data was kept in memory,
and concurrent requests could lose measurements.

- CSV: only the header is read and the row is appended (about 3 ms per
  measurement with 1,000,000 rows). If the header lacks a configured
  column (e.g. a column was added, or timestamps enabled later), the
  file is rewritten once with the new column
- SQLite: rows are inserted; missing columns are added with ALTER
  TABLE. New tables have untyped columns, so values keep their type (a
  first measurement with a missing value no longer makes a column
  text). Lists and objects are stored as JSON
- JSON and XML still rewrite the file (to be changed separately)
- Lock the stored data while writing and reading (lock file next to
  the data file, works between threads and processes on Linux, macOS
  and Windows), so concurrent measurements, also from several gunicorn
  workers, are all stored, and /data, /export and /explore never read
  a half-written row
- Values are stored as sent (e.g. 21 no longer becomes 21.0), and
  earlier rows are no longer changed by later measurements
- Fix a 500 error when several processes create the data folder at the
  same time
- Keep only the last stored row in memory after a measurement
- Document the storage and the lock file, and add tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VCvw8haRnmyrVKrLkz5Yf8
@firefly-cpp
firefly-cpp merged commit c6872f2 into main Nov 21, 2026
16 checks passed
@firefly-cpp
firefly-cpp deleted the perf/append-csv-sqlite branch November 21, 2026 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants