-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setting up Mongo Vector as an extension of the primary Mongo extension
- Loading branch information
Showing
17 changed files
with
160 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 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
24 changes: 24 additions & 0 deletions
24
...nsions/Mongo/Cosmos.DataTransfer.MongoExtension/Cosmos.DataTransfer.MongoExtension.csproj
This file contains 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,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<OutputType>Exe</OutputType> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" /> | ||
<PackageReference Include="MongoDB.Driver" Version="2.19.1" /> | ||
<PackageReference Include="System.ComponentModel.Composition" Version="6.0.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\Interfaces\Cosmos.DataTransfer.Interfaces\Cosmos.DataTransfer.Interfaces.csproj" /> | ||
</ItemGroup> | ||
|
||
<Target Name="PublishToExtensionsFolder" AfterTargets="Build" Condition=" '$(Configuration)' == 'Debug' "> | ||
<Exec Command="dotnet publish --configuration $(Configuration) --no-build -p:PublishProfile=PublishToExtensionsFolder" /> | ||
</Target> | ||
|
||
</Project> |
2 changes: 1 addition & 1 deletion
2
...nsfer.MongoVectorExtension/IRepository.cs → ...ataTransfer.MongoExtension/IRepository.cs
This file contains 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
2 changes: 1 addition & 1 deletion
2
...fer.MongoVectorExtension/MongoDataItem.cs → ...aTransfer.MongoExtension/MongoDataItem.cs
This file contains 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
58 changes: 58 additions & 0 deletions
58
Extensions/Mongo/Cosmos.DataTransfer.MongoExtension/MongoDataSinkExtension.cs
This file contains 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,58 @@ | ||
using System.ComponentModel.Composition; | ||
using Cosmos.DataTransfer.Interfaces; | ||
using Cosmos.DataTransfer.MongoExtension.Settings; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.Logging; | ||
using MongoDB.Bson; | ||
|
||
namespace Cosmos.DataTransfer.MongoExtension; | ||
[Export(typeof(IDataSinkExtension))] | ||
public class MongoDataSinkExtension : IDataSinkExtensionWithSettings | ||
{ | ||
public string DisplayName => "MongoDB"; | ||
|
||
public async Task WriteAsync(IAsyncEnumerable<IDataItem> dataItems, IConfiguration config, IDataSourceExtension dataSource, ILogger logger, CancellationToken cancellationToken = default) | ||
{ | ||
var settings = config.Get<MongoSinkSettings>(); | ||
settings.Validate(); | ||
|
||
if (!string.IsNullOrEmpty(settings.ConnectionString) && !string.IsNullOrEmpty(settings.DatabaseName) && !string.IsNullOrEmpty(settings.Collection)) | ||
{ | ||
var context = new Context(settings.ConnectionString, settings.DatabaseName); | ||
var repo = context.GetRepository<BsonDocument>(settings.Collection); | ||
|
||
var batchSize = settings.BatchSize ?? 1000; | ||
|
||
var objects = new List<BsonDocument>(); | ||
int itemCount = 0; | ||
await foreach (var item in dataItems.WithCancellation(cancellationToken)) | ||
{ | ||
var dict = item.BuildDynamicObjectTree(); | ||
objects.Add(new BsonDocument(dict)); | ||
itemCount++; | ||
|
||
if (objects.Count == batchSize) | ||
{ | ||
await repo.AddRange(objects); | ||
logger.LogInformation("Added {ItemCount} items to collection '{Collection}'", itemCount, settings.Collection); | ||
objects.Clear(); | ||
} | ||
} | ||
|
||
if (objects.Any()) | ||
{ | ||
await repo.AddRange(objects); | ||
} | ||
|
||
if (itemCount > 0) | ||
logger.LogInformation("Added {ItemCount} total items to collection '{Collection}'", itemCount, settings.Collection); | ||
else | ||
logger.LogWarning("No items added to collection '{Collection}'", settings.Collection); | ||
} | ||
} | ||
|
||
public IEnumerable<IDataExtensionSettings> GetSettings() | ||
{ | ||
yield return new MongoSinkSettings(); | ||
} | ||
} |
8 changes: 4 additions & 4 deletions
8
...tension/MongoVectorDataSourceExtension.cs → ...ongoExtension/MongoDataSourceExtension.cs
This file contains 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
2 changes: 1 addition & 1 deletion
2
...r.MongoVectorExtension/MongoRepository.cs → ...ransfer.MongoExtension/MongoRepository.cs
This file contains 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 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 @@ | ||
Console.WriteLine("Starting Mongo extension"); |
24 changes: 24 additions & 0 deletions
24
...s.DataTransfer.MongoExtension/Properties/PublishProfiles/PublishToExtensionsFolder.pubxml
This file contains 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,24 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- | ||
https://go.microsoft.com/fwlink/?LinkID=208121. | ||
--> | ||
<Project> | ||
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> | ||
<Configuration>Debug</Configuration> | ||
<Platform>Any CPU</Platform> | ||
<PublishDir>..\..\..\Core\Cosmos.DataTransfer.Core\bin\Debug\net6.0\Extensions</PublishDir> | ||
<PublishProtocol>FileSystem</PublishProtocol> | ||
<_TargetId>Folder</_TargetId> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<SelfContained>false</SelfContained> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)' != 'Debug' "> | ||
<Configuration>Release</Configuration> | ||
<Platform>Any CPU</Platform> | ||
<PublishDir>..\..\..\Core\Cosmos.DataTransfer.Core\bin\Release\net6.0\Extensions</PublishDir> | ||
<PublishProtocol>FileSystem</PublishProtocol> | ||
<_TargetId>Folder</_TargetId> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<SelfContained>false</SelfContained> | ||
</PropertyGroup> | ||
</Project> |
This file contains 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
10 changes: 10 additions & 0 deletions
10
Extensions/Mongo/Cosmos.DataTransfer.MongoExtension/Settings/MongoSinkSettings.cs
This file contains 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,10 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
|
||
namespace Cosmos.DataTransfer.MongoExtension.Settings; | ||
public class MongoSinkSettings : MongoBaseSettings | ||
{ | ||
[Required] | ||
public string? Collection { get; set; } | ||
|
||
public int? BatchSize { get; set; } | ||
} |
2 changes: 1 addition & 1 deletion
2
...Extension/Settings/MongoSourceSettings.cs → ...Extension/Settings/MongoSourceSettings.cs
This file contains 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 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 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
1 change: 1 addition & 0 deletions
1
...nsions/Mongo/Cosmos.DataTransfer.MongoVectorExtension/Settings/MongoVectorSinkSettings.cs
This file contains 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 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