import { CompatibilityContext } from '../../compatibility.js'; import { PreparedEventDefinition } from '../../events/CompiledEventSourceQuery.js'; import type { SourceTableConfigMap } from '../../SourceTableConfig.js'; import { SyncConfig } from '../../SyncConfig.js'; import * as plan from '../plan.js'; import { PreparedStreamBucketDataSource } from './bucket_data_source.js'; import { StreamBucketSource, StreamInput } from './bucket_source.js'; import { PreparedParameterIndexLookupCreator } from './parameter_index_lookup_creator.js'; export interface StreamEvaluationContext { defaultSchema: string; /** * Source contents that were used to compile the sync plan. * * This is not used to evaluate rows, but required for all sync config instances. */ sourceText: string; } export class PrecompiledSyncConfig extends SyncConfig { /** Default schema used to prepare unqualified source-table references from the compiled plan. */ readonly defaultSchema: string; constructor( readonly plan: plan.SyncPlan, compatibility: CompatibilityContext, context: StreamEvaluationContext ) { super(context.sourceText); this.compatibility = compatibility; this.defaultSchema = context.defaultSchema; for (const event of plan.events) { this.eventDefinitions.push(new PreparedEventDefinition(event, context.defaultSchema)); } const preparedBuckets = new Map(); const preparedLookups = new Map(); for (const bucket of plan.buckets) { const prepared = new PreparedStreamBucketDataSource(bucket, context); preparedBuckets.set(bucket, prepared); this.bucketDataSources.push(prepared); } for (const parameter of plan.parameterIndexes) { const prepared = new PreparedParameterIndexLookupCreator(parameter, context); preparedLookups.set(parameter, prepared); this.bucketParameterLookupSources.push(prepared); } const streamInput: StreamInput = { ...context, preparedBuckets, preparedLookups }; for (const stream of plan.streams) { this.bucketSources.push(new StreamBucketSource(stream, streamInput)); } } override get sourceTableConfig(): SourceTableConfigMap { return this.plan.sourceTableConfig ?? {}; } }