|
| 1 | +import { StreamBuilder } from '../../d2.js' |
| 2 | +import { |
| 3 | + DataMessage, |
| 4 | + MessageType, |
| 5 | + IStreamBuilder, |
| 6 | + PipedOperator, |
| 7 | +} from '../../types.js' |
| 8 | +import { MultiSet } from '../../multiset.js' |
| 9 | +import { |
| 10 | + DifferenceStreamReader, |
| 11 | + DifferenceStreamWriter, |
| 12 | + UnaryOperator, |
| 13 | +} from '../../graph.js' |
| 14 | +import { Version, Antichain } from '../../order.js' |
| 15 | +import { SQLiteDb, SQLiteStatement } from '../database.js' |
| 16 | + |
| 17 | +interface CollectionRow { |
| 18 | + version: string |
| 19 | + collection: string |
| 20 | +} |
| 21 | + |
| 22 | +interface CollectionParams { |
| 23 | + version: string |
| 24 | + collection: string |
| 25 | +} |
| 26 | + |
| 27 | +/** |
| 28 | + * Operator that buffers collections at each version, persisting state to SQLite |
| 29 | + * Ensures that completed versions are sent to the output as a whole, and in order |
| 30 | + */ |
| 31 | +export class BufferOperatorSQLite<T> extends UnaryOperator<T> { |
| 32 | + #preparedStatements: { |
| 33 | + insert: SQLiteStatement<CollectionParams> |
| 34 | + update: SQLiteStatement<CollectionParams> |
| 35 | + get: SQLiteStatement<[string], CollectionRow> |
| 36 | + delete: SQLiteStatement<[string]> |
| 37 | + getAllVersions: SQLiteStatement<[], CollectionRow> |
| 38 | + } |
| 39 | + |
| 40 | + constructor( |
| 41 | + id: number, |
| 42 | + inputA: DifferenceStreamReader<T>, |
| 43 | + output: DifferenceStreamWriter<T>, |
| 44 | + initialFrontier: Antichain, |
| 45 | + db: SQLiteDb, |
| 46 | + ) { |
| 47 | + super(id, inputA, output, initialFrontier) |
| 48 | + |
| 49 | + // Initialize database |
| 50 | + db.exec(` |
| 51 | + CREATE TABLE IF NOT EXISTS buffer_collections_${this.id} ( |
| 52 | + version TEXT PRIMARY KEY, |
| 53 | + collection TEXT NOT NULL |
| 54 | + ) |
| 55 | + `) |
| 56 | + db.exec(` |
| 57 | + CREATE INDEX IF NOT EXISTS buffer_collections_${this.id}_version |
| 58 | + ON buffer_collections_${this.id}(version); |
| 59 | + `) |
| 60 | + |
| 61 | + // Prepare statements |
| 62 | + this.#preparedStatements = { |
| 63 | + insert: db.prepare( |
| 64 | + `INSERT INTO buffer_collections_${this.id} (version, collection) VALUES (@version, @collection)`, |
| 65 | + ), |
| 66 | + update: db.prepare( |
| 67 | + `UPDATE buffer_collections_${this.id} SET collection = @collection WHERE version = @version`, |
| 68 | + ), |
| 69 | + get: db.prepare( |
| 70 | + `SELECT collection FROM buffer_collections_${this.id} WHERE version = ?`, |
| 71 | + ), |
| 72 | + delete: db.prepare( |
| 73 | + `DELETE FROM buffer_collections_${this.id} WHERE version = ?`, |
| 74 | + ), |
| 75 | + getAllVersions: db.prepare( |
| 76 | + `SELECT version, collection FROM buffer_collections_${this.id}`, |
| 77 | + ), |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + run(): void { |
| 82 | + for (const message of this.inputMessages()) { |
| 83 | + if (message.type === MessageType.DATA) { |
| 84 | + const { version, collection } = message.data as DataMessage<T> |
| 85 | + |
| 86 | + // Get existing collection or create new one |
| 87 | + const existingData = this.#preparedStatements.get.get(version.toJSON()) |
| 88 | + const existingCollection = existingData |
| 89 | + ? MultiSet.fromJSON(existingData.collection) |
| 90 | + : new MultiSet<T>() |
| 91 | + |
| 92 | + // Merge collections |
| 93 | + existingCollection.extend(collection) |
| 94 | + |
| 95 | + // Store updated collection |
| 96 | + if (existingData) { |
| 97 | + this.#preparedStatements.update.run({ |
| 98 | + version: version.toJSON(), |
| 99 | + collection: existingCollection.toJSON(), |
| 100 | + }) |
| 101 | + } else { |
| 102 | + this.#preparedStatements.insert.run({ |
| 103 | + version: version.toJSON(), |
| 104 | + collection: existingCollection.toJSON(), |
| 105 | + }) |
| 106 | + } |
| 107 | + } else if (message.type === MessageType.FRONTIER) { |
| 108 | + const frontier = message.data as Antichain |
| 109 | + if (!this.inputFrontier().lessEqual(frontier)) { |
| 110 | + throw new Error('Invalid frontier update') |
| 111 | + } |
| 112 | + this.setInputFrontier(frontier) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Find versions that are complete (not covered by input frontier) |
| 117 | + const allVersions = this.#preparedStatements.getAllVersions.all() |
| 118 | + const finishedVersions = allVersions |
| 119 | + .map((row) => ({ |
| 120 | + version: Version.fromJSON(row.version), |
| 121 | + collection: MultiSet.fromJSON<T>(row.collection), |
| 122 | + })) |
| 123 | + .filter(({ version }) => !this.inputFrontier().lessEqualVersion(version)) |
| 124 | + |
| 125 | + // Process and remove finished versions |
| 126 | + for (const { version, collection } of finishedVersions) { |
| 127 | + this.#preparedStatements.delete.run(version.toJSON()) |
| 128 | + this.output.sendData(version, collection) |
| 129 | + } |
| 130 | + |
| 131 | + if (!this.outputFrontier.lessEqual(this.inputFrontier())) { |
| 132 | + throw new Error('Invalid frontier state') |
| 133 | + } |
| 134 | + if (this.outputFrontier.lessThan(this.inputFrontier())) { |
| 135 | + this.outputFrontier = this.inputFrontier() |
| 136 | + this.output.sendFrontier(this.outputFrontier) |
| 137 | + } |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +/** |
| 142 | + * Buffers the elements in the stream |
| 143 | + * Ensures that completed versions are sent to the output as a whole, and in order |
| 144 | + * Persists state to SQLite |
| 145 | + * @param db - The SQLite database |
| 146 | + */ |
| 147 | +export function buffer<T>(db: SQLiteDb): PipedOperator<T, T> { |
| 148 | + return (stream: IStreamBuilder<T>): IStreamBuilder<T> => { |
| 149 | + const output = new StreamBuilder<T>( |
| 150 | + stream.graph, |
| 151 | + new DifferenceStreamWriter<T>(), |
| 152 | + ) |
| 153 | + const operator = new BufferOperatorSQLite<T>( |
| 154 | + stream.graph.getNextOperatorId(), |
| 155 | + stream.connectReader(), |
| 156 | + output.writer, |
| 157 | + stream.graph.frontier(), |
| 158 | + db, |
| 159 | + ) |
| 160 | + stream.graph.addOperator(operator) |
| 161 | + stream.graph.addStream(output.connectReader()) |
| 162 | + return output |
| 163 | + } |
| 164 | +} |
0 commit comments