Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion MEVD/src/AzureDocumentDB/AzureDocumentDB.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<AssemblyName>CommunityToolkit.VectorData.AzureDocumentDB</AssemblyName>
<RootNamespace>$(AssemblyName)</RootNamespace>
<TargetFrameworks>net10.0;net8.0;netstandard2.1;net472</TargetFrameworks>
Expand Down
27 changes: 16 additions & 11 deletions MEVD/src/AzureDocumentDB/MongoShared/MongoMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,10 @@ internal sealed class MongoMapper<TRecord> : IMongoMapper<TRecord>
/// <summary>A key property name of the data model.</summary>
private readonly string _keyPropertyModelName;

/// <summary>
/// Initializes a new instance of the <see cref="MongoMapper{TRecord}"/> class.
/// </summary>
/// <param name="model">The model.</param>
public MongoMapper(CollectionModel model)
static MongoMapper()
{
this._model = model;

var keyProperty = model.KeyProperty;
this._keyPropertyModelName = keyProperty.ModelName;
this._keyClrProperty = keyProperty.PropertyInfo;

// ConventionRegistry is process-wide and every Register call adds a new entry,
// so the conventions are registered once per record type instead of once per instance.
var conventionPack = new ConventionPack
{
new IgnoreExtraElementsConvention(ignoreExtraElements: true),
Expand All @@ -51,6 +43,19 @@ public MongoMapper(CollectionModel model)
type => type == typeof(TRecord));
}

/// <summary>
/// Initializes a new instance of the <see cref="MongoMapper{TRecord}"/> class.
/// </summary>
/// <param name="model">The model.</param>
public MongoMapper(CollectionModel model)
{
this._model = model;

var keyProperty = model.KeyProperty;
this._keyPropertyModelName = keyProperty.ModelName;
this._keyClrProperty = keyProperty.PropertyInfo;
}

public BsonDocument MapFromDataToStorageModel(TRecord dataModel, int recordIndex, IReadOnlyList<Embedding>?[]? generatedEmbeddings)
{
var document = dataModel.ToBsonDocument();
Expand Down
32 changes: 32 additions & 0 deletions MEVD/test/AzureDocumentDB.UnitTests/DocumentDBCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Bson.Serialization.Conventions;
using MongoDB.Driver;
using Moq;
using Xunit;
Expand Down Expand Up @@ -72,6 +73,28 @@ public void ConstructorWithImperativeModelInitializesCollection()
Assert.NotNull(collection);
}

[Fact]
public void ConstructorRegistersConventionsOncePerRecordType()
{
// Arrange
using var firstCollection = new DocumentDBCollection<string, ConventionTestModel>(
this._mockMongoDatabase.Object,
"collection");

var conventionCount = ConventionRegistry.Lookup(typeof(ConventionTestModel)).Conventions.Count();

// Act
for (var i = 0; i < 3; i++)
{
using var collection = new DocumentDBCollection<string, ConventionTestModel>(
this._mockMongoDatabase.Object,
"collection");
}

// Assert
Assert.Equal(conventionCount, ConventionRegistry.Lookup(typeof(ConventionTestModel)).Conventions.Count());
}

[Theory]
[MemberData(nameof(CollectionExistsData))]
public async Task CollectionExistsReturnsValidResultAsync(List<string> collections, string collectionName, bool expectedResult)
Expand Down Expand Up @@ -708,6 +731,15 @@ private sealed class VectorSearchModel
[VectorStoreVector(dimensions: 4, DistanceFunction = DistanceFunction.CosineDistance, IndexKind = IndexKind.IvfFlat)]
public ReadOnlyMemory<float> TestEmbedding2 { get; set; }
}

private sealed class ConventionTestModel
{
[VectorStoreKey]
public string? Id { get; set; }

[VectorStoreData]
public string? HotelName { get; set; }
}
#pragma warning restore CA1812

#endregion
Expand Down
Loading