Skip to content

Commit 70ef00b

Browse files
authored
Refactor document export to support streaming (#299)
1 parent 41a3b0d commit 70ef00b

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/Typesense/TypesenseClient.cs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -565,17 +565,36 @@ public async Task<List<T>> ExportDocuments<T>(string collection, ExportParameter
565565

566566
ArgumentNullException.ThrowIfNull(exportParameters);
567567

568+
var docs = ExportDocumentsAsStream<T>(collection, exportParameters, ctk).ConfigureAwait(false);
569+
570+
List<T> documents = new();
571+
await foreach (var doc in docs)
572+
{
573+
documents.Add(doc);
574+
}
575+
return documents;
576+
}
577+
578+
public async IAsyncEnumerable<T> ExportDocumentsAsStream<T>(string collection, ExportParameters exportParameters, [EnumeratorCancellation] CancellationToken ctk = default)
579+
{
580+
if (string.IsNullOrWhiteSpace(collection))
581+
throw new ArgumentException("cannot be null or whitespace.", nameof(collection));
582+
583+
ArgumentNullException.ThrowIfNull(exportParameters);
584+
568585
var parameters = CreateUrlParameters(exportParameters);
569586
var lines = GetLines($"/collections/{collection}/documents/export?{parameters}", ctk).ConfigureAwait(false);
570-
List<T> documents = new();
587+
571588
await foreach (var line in lines)
572589
{
573590
if (string.IsNullOrWhiteSpace(line))
574591
continue;
575-
documents.Add(JsonSerializer.Deserialize<T>(line, _jsonNameCaseInsensitiveTrue) ??
576-
throw new ArgumentException("Null is not valid for documents"));
592+
593+
ctk.ThrowIfCancellationRequested();
594+
595+
yield return JsonSerializer.Deserialize<T>(line, _jsonNameCaseInsensitiveTrue) ??
596+
throw new ArgumentException("Null is not valid for documents");
577597
}
578-
return documents;
579598
}
580599

581600
public async Task<KeyResponse> CreateKey(Key key)

0 commit comments

Comments
 (0)