upsertChunks method
- List<
ContentChunkModel> chunks, - List<
List< embeddingsdouble> >
Store chunks with their embeddings locally.
Uses INSERT OR REPLACE for idempotent upserts.
Implementation
void upsertChunks(
List<ContentChunkModel> chunks,
List<List<double>> embeddings,
) {
if (_db == null) return;
if (chunks.length != embeddings.length) {
throw ArgumentError(
'chunks and embeddings must have the same length: '
'${chunks.length} vs ${embeddings.length}',
);
}
final stmt = _db!.prepare('''
INSERT OR REPLACE INTO local_chunks
(id, topic_id, book_id, chunk_index, content, page_number,
token_count, metadata, embedding)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''');
try {
_db!.execute('BEGIN TRANSACTION');
for (int i = 0; i < chunks.length; i++) {
final c = chunks[i];
stmt.execute([
c.id,
c.topicId,
c.bookId,
c.chunkIndex,
c.content,
c.pageNumber,
c.tokenCount,
jsonEncode(c.metadata),
jsonEncode(embeddings[i]),
]);
}
_db!.execute('COMMIT');
} catch (e) {
_db!.execute('ROLLBACK');
rethrow;
} finally {
stmt.close();
}
}