upload(): INGEST 3
segment · lines 306–327View on GitHubChecks and takes room in the demo account's limits in one statement.
Calls json() Shared helpers upload(): INGEST 2 Claim the file name
// INGEST 3 · check the demo's limits (files, pages, characters) // Every limit in one statement, so two uploads at once cannot both fit in the last of the room.Every limit in one statement Never runs
WhatThe stage marker, and a comment explaining why the check is a single statement.
WhyA read-then-write check (SELECT the totals, compare them in JavaScript, then UPDATE) would let two concurrent uploads read the same totals, both pass, and together exceed the limit.
const room = await env.DB.prepare( `UPDATE demos SET files = files + 1, pages = pages + ?1, chars = chars + ?2, uploads = uploads + 1, used_at = datetime('now'), cleared_at = NULL WHERE id = ?3 AND files < ?4 AND pages + ?1 <= ?5 AND chars + ?2 <= ?6 RETURNING uploads`, ).bind(pageCount, chars, demo.id, LIMITS.files, LIMITS.pages, LIMITS.chars).first<{ uploads: number }>()The conditional UPDATE Ingestion
WhatOne UPDATE of the demo account's row whose WHERE clause holds the three limits and whose SET adds this file to the totals, ending in RETURNING uploads. Numbered parameters let pageCount (?1) and chars (?2) appear in both the SET and the WHERE clause.
WhySQLite runs one UPDATE atomically. The WHERE clause is evaluated against the row's current totals and the increments are applied in the same step, so of two uploads competing for the last of the room, only one matches. RETURNING uploads gives the count after this upload. INGEST 7 compares it with 1, so ordinarily only the account's first stored upload sends the email; a failed store at INGEST 5 takes the count back down. Setting used_at restarts the 10-day idle clock. The daily purge re-checks that clock inside its own delete transaction, so an upload that lands during a purge keeps all the account's files and chunks; vectors the purge had already deleted are gone, and those chunks are found by BM25 alone. Setting cleared_at to NULL removes the page's "index was deleted" notice once new documents arrive.
LIMITS.files = 5 (files < 5 before this one)LIMITS.pages = 300LIMITS.chars = 500_000IDLE_DAYS = 10
if (room === null) { // No room: the file row is deleted, then the demo is re-read to say which limit this file would pass. await forget() const now = (await env.DB.prepare('SELECT files, pages, chars FROM demos WHERE id = ?').bind(demo.id).first<Demo>()) ?? demoNo room: release the name, re-read the totals Ingestion
WhatWhen the UPDATE returns no row, forget() deletes the file row from INGEST 2. A SELECT then reads the account's current totals, falling back to the row the router read.
WhyThe UPDATE changed none of the account's totals, so the file row is the only thing to undo. The totals are re-read because the router's copy may be stale, and they are used only to word the message.
// The message names the first limit this file would pass. const limit = now.files >= LIMITS.files ? `The demo takes at most ${LIMITS.files} PDFs.` : now.pages + pageCount > LIMITS.pages ? `The demo takes at most ${LIMITS.pages} pages in all, and ${name} would take it to ${now.pages + pageCount}.` : `The demo takes at most ${LIMITS.chars / 1000} KB of text in all, and ${name} would take it past that.`Name the first limit passed Ingestion
WhatFiles are checked first (already 5), then pages (the current pages plus pageCount over 300, with the total the file would reach). The character limit is the remaining case, given as LIMITS.chars / 1000 KB.
WhyThe message tells the visitor which limit to work around. The last branch is a default with no check of its own, so if a concurrent removal freed room between the UPDATE and the re-read, the message names the character limit although no limit is now passed. "500 KB" names the 500,000 limit, which counts JavaScript string length (UTF-16 code units), not bytes.
LIMITS.chars / 1000 = 500 ("KB")
return json(413, { error: `${limit} ${name} was not added.` }) }413: name the limit, add nothing Ingestion
WhatAnswers 413 with the limit's sentence followed by "<name> was not added."
Why413 (Content Too Large) marks the refusal as one of size. The upload leaves no file row, no chunks and no change to the account's totals.