Yield native import scans by elapsed time and bounded work

This commit is contained in:
2026-11-09 19:41:00 +08:00
parent fd6c166f4b
commit 180f66b2af
9 changed files with 67 additions and 16 deletions
+5 -5
View File
@@ -1,3 +1,4 @@
import {createImportBudget,type ImportScheduling} from './import-budget.ts';
import {validateImportFileRetention,type FileRetirement,type PreviousImportFile} from './import-file-retention.ts';
import { validateDuplicateCheckpoint, type DuplicateCheckpoint } from '../canvas-core/duplicate-checkpoint.ts';
import type { Change, Id, Json, Result, Target, Point, Availability } from '../canvas-core/types.ts';
@@ -66,25 +67,24 @@ export function validateImportJob(job: ImportJob): void {
const yieldTask=()=>new Promise<void>(resolve=>setTimeout(resolve,0));
/** Seekable bounded NDJSON parser. Fingerprint must identify the host's immutable, durable byte source. */
export function createNdjsonImportSource(options:{bytes:Blob;fingerprint:string;maxRecordBytes?:number;chunkBytes?:number;yieldTask?:()=>Promise<void>}):ImportSource {
export function createNdjsonImportSource(options:{bytes:Blob;fingerprint:string;maxRecordBytes?:number;chunkBytes?:number}&ImportScheduling):ImportSource {
const max=options.maxRecordBytes??256*1024,chunk=options.chunkBytes??16*1024;
if(!(options.bytes instanceof Blob)||!options.fingerprint||!Number.isSafeInteger(max)||max<1||!Number.isSafeInteger(chunk)||chunk<1||chunk>max)throw fault('invalid_import_source');
const sourceBytes=options.bytes,sourceYield=options.yieldTask??yieldTask;
const sourceBytes=options.bytes;
let cache={offset:-1,bytes:new Uint8Array()};
return Object.freeze({fingerprint:options.fingerprint,parserVersion:1,
async read(cursor:number,signal:AbortSignal):Promise<ImportLine|null>{
if(!Number.isSafeInteger(cursor)||cursor<0||cursor>sourceBytes.size)throw fault('invalid_import_cursor');
if(signal.aborted)throw fault('import_aborted');if(cursor===sourceBytes.size)return null;
const parts:Uint8Array[]=[],start=cursor;let total=0,oversized=false,scanned=0;
const parts:Uint8Array[]=[],start=cursor,budget=createImportBudget(signal,options);let total=0,oversized=false;
while(cursor<sourceBytes.size){
if(signal.aborted)throw fault('import_aborted');
if(cursor<cache.offset||cursor>=cache.offset+cache.bytes.length)cache={offset:cursor,bytes:new Uint8Array(await sourceBytes.slice(cursor,cursor+chunk).arrayBuffer())};
if(signal.aborted)throw fault('import_aborted');
const begin=cursor-cache.offset,newline=cache.bytes.indexOf(10,begin),end=newline<0?cache.bytes.length:newline,part=cache.bytes.subarray(begin,end);total+=part.length;
if(total>max){oversized=true;parts.length=0;}else if(!oversized)parts.push(part);
cursor=cache.offset+end+(newline<0?0:1);scanned+=end-begin;
cursor=cache.offset+end+(newline<0?0:1);await budget.step(end-begin);
if(newline>=0||cursor===sourceBytes.size)break;
if(scanned>=64*1024){scanned=0;await sourceYield();}
}
const base={cursor:start,nextCursor:cursor,key:`byte:${start}`};
if(oversized)return{...base,error:'record_too_large'};