insertBatch method
Implementation
void insertBatch(List<ZvecInsertRequest> documents) {
_ensureOpen();
if (documents.isEmpty) {
return;
}
final dimension = this.dimension;
for (final document in documents) {
if (document.vector.length != dimension) {
throw ZvecException(
-1,
'Vector dimension ${document.vector.length} does not match collection dimension $dimension.',
);
}
}
final needsRichInsert = documents.any(
(document) => document.content != null || document.metadata != null,
);
if (needsRichInsert) {
for (final document in documents) {
insert(
id: document.id,
vector: document.vector,
content: document.content,
metadata: document.metadata,
hash: document.hash,
);
}
return;
}
final ids = calloc<Pointer<Utf8>>(documents.length);
final hashes = calloc<Pointer<Utf8>>(documents.length);
final vectors = calloc<Float>(documents.length * dimension);
try {
for (var index = 0; index < documents.length; index++) {
ids[index] = documents[index].id.toNativeUtf8();
hashes[index] = _nullableUtf8(documents[index].hash);
for (var vectorIndex = 0; vectorIndex < dimension; vectorIndex++) {
vectors[index * dimension + vectorIndex] =
documents[index].vector[vectorIndex];
}
}
final status = _bindings.zvecInsertBatch(
_handle,
ids,
vectors,
hashes,
dimension,
documents.length,
);
_throwIfFailed(status);
} finally {
for (var index = 0; index < documents.length; index++) {
calloc.free(ids[index]);
_freeNullableUtf8(hashes[index]);
}
calloc.free(ids);
calloc.free(hashes);
calloc.free(vectors);
}
}