mail_deduplicate.cache moduleΒΆ

Persistent cache of mail hashes, so a second run does not re-parse unchanged mails.

Hashing dominates a run, and it is pure work: the same mail, hashed with the same settings, always produces the same result. Storing that result lets a later run skip opening and parsing every mail it has already seen. See: https://github.com/kdeldycke/mail-deduplicate/issues/87

Correctness rests on two independent guards:

  • A fingerprint of every setting feeding the hash. Anything else invalidates the whole database, as no entry produced under different settings can be trusted.

  • A per-mail staleness key of (size, mtime), taken from the file backing the mail before it is read, so a mail modified mid-run is re-hashed by the next one instead of being trusted.

mail_deduplicate.cache.HASHING_VERSION = '1'ΒΆ

Bumped whenever a change to the hashing, or to the size it memoizes, makes the entries of earlier versions wrong.

Such an entry reads as valid, and would group its mail differently from the mails hashed afresh: the whole database is discarded instead, as for a change of settings.

mail_deduplicate.cache.SCHEMA_VERSION = '1'ΒΆ

Bumped whenever the table layout changes, to discard databases of older shapes.

mail_deduplicate.cache.CACHED_SETTINGS = ('hash_headers', 'hash_body', 'raw_headers', 'time_source')ΒΆ

Configuration keys whose value changes what gets cached.

hash_headers, hash_body and raw_headers change the hash itself. time_source changes the timestamp memoized alongside it. The minimal-headers floor is derived from the number of hash headers, so it is covered by them.

class mail_deduplicate.cache.StaleKey(size, mtime_ns)[source]ΒΆ

Bases: NamedTuple

Identifies the version of the file backing a mail.

Create new instance of StaleKey(size, mtime_ns)

size: intΒΆ

Alias for field number 0

mtime_ns: intΒΆ

Alias for field number 1

class mail_deduplicate.cache.CacheEntry(mail_hash, timestamp, mail_size)[source]ΒΆ

Bases: NamedTuple

What is worth keeping about a hashed mail.

The hash is the point. The two scalars are what dehydrate() memoizes to spare the later steps a re-read, so restoring them is what actually lets a cached mail stay unparsed. mail_size is only known when the body was hashed, and timestamp is None for a mail whose Date header could not be parsed.

Create new instance of CacheEntry(mail_hash, timestamp, mail_size)

mail_hash: strΒΆ

Alias for field number 0

timestamp: float | NoneΒΆ

Alias for field number 1

mail_size: int | NoneΒΆ

Alias for field number 2

mail_deduplicate.cache.default_cache_dir()[source]ΒΆ

Location of the cache database, following each platform’s conventions.

See click_extra.carapace.get_cache_dir() for the location on each platform.

Return type:

Path

mail_deduplicate.cache.default_cache_path()[source]ΒΆ

Full path of the cache database.

Return type:

Path

mail_deduplicate.cache.settings_fingerprint(conf)[source]ΒΆ

Digest of every setting that changes what a cached entry would hold.

The two versions go in with the settings, so a database of an earlier release is discarded the same way when either moved.

Return type:

str

mail_deduplicate.cache.open_cache(conf)[source]ΒΆ

Opens the hash cache the configuration asks for, if it asks for one at all.

A cache only ever saves work, so a database that cannot be opened, on a read-only or full filesystem for instance, is reported and skipped instead of taking the whole run down with it.

Return type:

HashCache | None

class mail_deduplicate.cache.HashCache(path, fingerprint)[source]ΒΆ

Bases: object

A mail hash cache backed by SQLite, keyed by mail identity.

Opened for the whole run and committed once at the end, so an interrupted run leaves the database as it found it.

WRITE_BATCH: int = 512ΒΆ

Rows buffered before being handed to SQLite in one executemany().

Inserting one row at a time costs more in statement overhead than in storage.

LOCK_TIMEOUT: float = 30.0ΒΆ

Seconds a run waits for another one to release the database.

Runs sharing a database only ever contend on the single write burst each of them performs at the end, so waiting it out beats failing. See commit() for what happens when the wait is not enough.

lookup(box, mail_id)[source]ΒΆ

Returns the entry cached for a mail, if it is still valid.

The staleness key is taken here, before the caller reads the mail, so a mail modified between this check and its hashing is caught by the next run rather than trusted.

Return type:

CacheEntry | None

store(source_path, mail_id, entry)[source]ΒΆ

Record the hash of a freshly read mail, under the key taken by lookup().

Silently skipped for a mail that was never looked up, as there is then no staleness key that provably predates the read.

Return type:

None

prune()[source]ΒΆ

Drops the entries of mails and boxes that are no longer there.

Two kinds of leftovers accumulate. A box that was deleted or moved keeps every one of its entries, and a mail deleted from a box that is still around keeps its own. Both are dropped here, and how many were removed is recorded as pruned for the run to report.

Only the boxes visited by this run are considered for their individual mails: every mail of a box that was not opened is missing from seen for the plain reason that nobody looked, which is not evidence that it is gone.

Return type:

None

forget(source_path, mail_id)[source]ΒΆ

Drop the staleness key of a mail that will not be recorded.

Return type:

None

commit()[source]ΒΆ

Flush everything recorded during the run, in one transaction.

Returns whether it went through. By this point the deduplication itself is done, so a database another run is holding, or a disk that filled up while we worked, costs the next run its head start and nothing more: it must not take down a run that has already produced its results.

Return type:

bool

close()[source]ΒΆ

Release the database handle, without ever failing the run over it.

Return type:

None