-
Notifications
You must be signed in to change notification settings - Fork 845
[MEDI] Allow Pipeline and Reader to work with any Source #7090
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
adamsitnik
wants to merge
9
commits into
dotnet:main
Choose a base branch
from
adamsitnik:anySource
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b06ca5b
first attempt: introduce IIngestionDocumentReader<TSource>, remove In…
adamsitnik d4b53aa
keep IngestionDocumentReader to reduce the number of breaking changes
adamsitnik 50ae76f
restore the ability to process multiple files at once to reduce the n…
adamsitnik cf29d07
add test that shows what the changes allow for
adamsitnik 074674c
take advantage of the new interface and support DataContent as an inp…
adamsitnik 4d1d604
avoid code duplication
adamsitnik 875f7c0
change the order of generic type parameters to be more natural: we ta…
adamsitnik 4a307ce
address code review feedback
adamsitnik ffeb773
fix the build?
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/Libraries/Microsoft.Extensions.DataIngestion.Abstractions/IIngestionDocumentReader.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace Microsoft.Extensions.DataIngestion; | ||
|
|
||
| /// <summary> | ||
| /// Reads source content and converts it to an <see cref="IngestionDocument"/>. | ||
| /// </summary> | ||
| /// <typeparam name="TSource">The type of the source to read from. Sample: <see cref="FileInfo"/>, <see cref="Stream"/>, etc.</typeparam> | ||
| public interface IIngestionDocumentReader<TSource> | ||
| { | ||
| /// <summary> | ||
| /// Reads a source and converts it to an <see cref="IngestionDocument"/>. | ||
| /// </summary> | ||
| /// <param name="source">The source to read.</param> | ||
| /// <param name="identifier">The unique identifier for the document.</param> | ||
| /// <param name="mediaType">The media type of the file.</param> | ||
| /// <param name="cancellationToken">The token to monitor for cancellation requests.</param> | ||
| /// <returns>A task representing the asynchronous read operation.</returns> | ||
| /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="identifier"/> is <see langword="null"/> or empty.</exception> | ||
| Task<IngestionDocument> ReadAsync(TSource source, string identifier, string? mediaType = null, CancellationToken cancellationToken = default); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
src/Libraries/Microsoft.Extensions.DataIngestion/FileSystemIngestionExtensions.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.IO; | ||
| using System.Runtime.CompilerServices; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Shared.Diagnostics; | ||
| using static Microsoft.Extensions.DataIngestion.DiagnosticsConstants; | ||
|
|
||
| namespace Microsoft.Extensions.DataIngestion; | ||
|
|
||
| #pragma warning disable IDE0058 // Expression value is never used | ||
| #pragma warning disable IDE0063 // Use simple 'using' statement | ||
| #pragma warning disable CA1031 // Do not catch general exception types | ||
|
|
||
| /// <summary> | ||
| /// Provides a set of File System extension methods for the <see cref="IngestionPipeline{TChunk, TSource}"/> class. | ||
| /// </summary> | ||
| public static class FileSystemIngestionExtensions | ||
| { | ||
| /// <summary> | ||
| /// Processes all files in the specified directory that match the given search pattern and option. | ||
| /// </summary> | ||
| /// <typeparam name="TChunk">The type of the chunk content.</typeparam> | ||
| /// <param name="pipeline">The ingestion pipeline.</param> | ||
| /// <param name="directory">The directory to process.</param> | ||
| /// <param name="searchPattern">The search pattern for file selection.</param> | ||
| /// <param name="searchOption">The search option for directory traversal.</param> | ||
| /// <param name="cancellationToken">The cancellation token for the operation.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public static async IAsyncEnumerable<IngestionResult> ProcessAsync<TChunk>( | ||
| this IngestionPipeline<FileInfo, TChunk> pipeline, | ||
| DirectoryInfo directory, string searchPattern = "*.*", | ||
| SearchOption searchOption = SearchOption.TopDirectoryOnly, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
| { | ||
| Throw.IfNull(pipeline); | ||
| Throw.IfNull(directory); | ||
| Throw.IfNullOrEmpty(searchPattern); | ||
| Throw.IfOutOfRange((int)searchOption, (int)SearchOption.TopDirectoryOnly, (int)SearchOption.AllDirectories); | ||
|
|
||
| using (Activity? rootActivity = pipeline.ActivitySource.StartActivity(ProcessDirectory.ActivityName)) | ||
| { | ||
| rootActivity?.SetTag(ProcessDirectory.DirectoryPathTagName, directory.FullName) | ||
| .SetTag(ProcessDirectory.SearchPatternTagName, searchPattern) | ||
| .SetTag(ProcessDirectory.SearchOptionTagName, searchOption.ToString()); | ||
| pipeline.Logger?.ProcessingDirectory(directory.FullName, searchPattern, searchOption); | ||
|
|
||
| var files = directory.GetFiles(searchPattern, searchOption); | ||
adamsitnik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| await foreach (var ingestionResult in pipeline.ProcessAsync(files, rootActivity, cancellationToken).ConfigureAwait(false)) | ||
| { | ||
| yield return ingestionResult; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Processes the specified files. | ||
| /// </summary> | ||
| /// <typeparam name="TChunk">The type of the chunk content.</typeparam> | ||
| /// <param name="pipeline">The ingestion pipeline.</param> | ||
| /// <param name="files">The collection of files to process.</param> | ||
| /// <param name="cancellationToken">The cancellation token for the operation.</param> | ||
| /// <returns>A task representing the asynchronous operation.</returns> | ||
| public static async IAsyncEnumerable<IngestionResult> ProcessAsync<TChunk>( | ||
| this IngestionPipeline<FileInfo, TChunk> pipeline, | ||
| IEnumerable<FileInfo> files, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
| { | ||
| Throw.IfNull(pipeline); | ||
| Throw.IfNull(files); | ||
|
|
||
| using (Activity? rootActivity = pipeline.ActivitySource.StartActivity(ProcessFiles.ActivityName)) | ||
| { | ||
| await foreach (var ingestionResult in pipeline.ProcessAsync(files, rootActivity, cancellationToken).ConfigureAwait(false)) | ||
| { | ||
| yield return ingestionResult; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static async IAsyncEnumerable<IngestionResult> ProcessAsync<TChunk>( | ||
| this IngestionPipeline<FileInfo, TChunk> pipeline, | ||
| IEnumerable<FileInfo> files, Activity? rootActivity, | ||
| [EnumeratorCancellation] CancellationToken cancellationToken) | ||
| { | ||
| #if NET | ||
| if (System.Linq.Enumerable.TryGetNonEnumeratedCount(files, out int count)) | ||
| #else | ||
| if (files is IReadOnlyCollection<FileInfo> { Count: int count }) | ||
| #endif | ||
| { | ||
| rootActivity?.SetTag(ProcessFiles.FileCountTagName, count); | ||
| pipeline.Logger?.LogFileCount(count); | ||
| } | ||
|
|
||
| foreach (FileInfo fileInfo in files) | ||
| { | ||
| using (Activity? processFileActivity = pipeline.ActivitySource.StartActivity(ProcessFile.ActivityName, ActivityKind.Internal, parentContext: rootActivity?.Context ?? default)) | ||
| { | ||
| processFileActivity?.SetTag(ProcessFile.FilePathTagName, fileInfo.FullName); | ||
|
|
||
| Exception? failure = null; | ||
| IngestionDocument? document = null; | ||
|
|
||
| try | ||
| { | ||
| document = await pipeline.ProcessAsync(fileInfo, fileInfo.FullName, fileInfo.GetMediaType(), cancellationToken).ConfigureAwait(false); | ||
| } | ||
| catch (Exception e) | ||
| { | ||
| failure = e; | ||
| } | ||
|
|
||
| string documentId = document?.Identifier ?? fileInfo.FullName; | ||
| yield return new IngestionResult(documentId, document, failure); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.