Giving a File Import a Size Budget

An importer can declare limits on bytes, records, and parsed expansion so it uses resources predictably on valid and invalid inputs.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Check more than one measure
  2. Stop at a documented boundary
  3. Budget bytes, records and memory independently
  4. Track what the input actually delivers

Check more than one measure

A short file may have many small records. A recursively structured document can use more memory after parsing than its bytes suggest. Collection memory distinguishes stored representations.

Stop at a documented boundary

If an import allows up to one thousand records, test counts 999, 1000, and 1001. Say whether partial rows are committed before a failure. Complete file replacement illustrates why publication can wait for validation.[1]

Budget bytes, records and memory independently

A file limited to one megabyte can still contain a very large single record. Conversely, thousands of tiny records can fit below the byte limit while exceeding an application row limit. Enforce the constraints relevant to actual processing resources. See line-by-line reads.

Example limits: 1 MB input, 10,000 rows, 32 kB per row
A 48 kB single row passes total-size limit but fails row-size limit
An empty file may satisfy budgets while failing application rules

Track what the input actually delivers

An initial metadata size can be absent or change during reading. Count streamed bytes or records as necessary and stop with a clear reason when a bound is exceeded. Test exactly-at-limit and just-over-limit inputs.

References

  1. Python Software Foundation, io: Core Tools for Working with Streams ↩