Skip to content

Commit

Permalink
Merge pull request #53 from Etherna/fix/MODM-134-index-builder
Browse files Browse the repository at this point in the history
fix issue building indexes with secondary model maps
  • Loading branch information
tmm360 authored Jun 15, 2022
2 parents 36891d7 + 5bdce31 commit e47e577
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/MongODM.Core/Repositories/CollectionRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public override Task BuildIndexesAsync(CancellationToken cancellationToken = def
}));

//referenced documents
var dependencies = DbContext.SchemaRegistry.GetIdMemberDependenciesFromRootModel(typeof(TModel));
var dependencies = DbContext.SchemaRegistry.GetIdMemberDependenciesFromRootModel(typeof(TModel), true);

var idPaths = dependencies
.Select(dependency => dependency.MemberPathToString())
Expand Down
3 changes: 2 additions & 1 deletion src/MongODM.Core/Serialization/Mapping/ISchemaRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ IModelMapsSchemaBuilder<TModel> AddModelMapsSchema<TModel>(
/// Get all id member dependencies from a root model type
/// </summary>
/// <param name="modelType">The model type</param>
/// <param name="onlyFromActiveModelMap">If true, ignore secondary model maps</param>
/// <returns>The list of member dependencies</returns>
IEnumerable<MemberDependency> GetIdMemberDependenciesFromRootModel(Type modelType);
IEnumerable<MemberDependency> GetIdMemberDependenciesFromRootModel(Type modelType, bool onlyFromActiveModelMap = false);

/// <summary>
/// Get all member dependencies that points to a specific member definition
Expand Down
7 changes: 7 additions & 0 deletions src/MongODM.Core/Serialization/Mapping/MemberDependency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ public class MemberDependency
// Constructors.
public MemberDependency(
IModelMap rootModelMap,
bool rootModelMapIsActive,
IEnumerable<OwnedBsonMemberMap> memberPath,
bool useCascadeDelete)
{
MemberPath = memberPath ?? throw new ArgumentNullException(nameof(memberPath));
if (!memberPath.Any())
throw new ArgumentException("Member path can't be empty", nameof(memberPath));
RootModelMap = rootModelMap ?? throw new ArgumentNullException(nameof(rootModelMap));
RootModelMapIsActive = rootModelMapIsActive;
UseCascadeDelete = useCascadeDelete;
}

Expand Down Expand Up @@ -119,6 +121,11 @@ public IEnumerable<OwnedBsonMemberMap> MemberPathToLastEntityModelId
/// </summary>
public IModelMap RootModelMap { get; }

/// <summary>
/// True if root model map is the currently active in schema
/// </summary>
public bool RootModelMapIsActive { get; }

/// <summary>
/// True if requested to apply cascade delete
/// </summary>
Expand Down
9 changes: 6 additions & 3 deletions src/MongODM.Core/Serialization/Mapping/SchemaRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,12 @@ public BsonElement GetActiveModelMapIdBsonElement(Type modelType)
return activeModelMapIdBsonElement[modelType];
}

public IEnumerable<MemberDependency> GetIdMemberDependenciesFromRootModel(Type modelType)
public IEnumerable<MemberDependency> GetIdMemberDependenciesFromRootModel(Type modelType, bool onlyFromActiveModelMap = false)
{
Freeze(); //needed for initialization

if (modelTypeToReferencedIdMemberMapsDictionary.TryGetValue(modelType, out List<MemberDependency> dependencies))
return dependencies;
return dependencies.Where(d => !onlyFromActiveModelMap || d.RootModelMapIsActive);
return Array.Empty<MemberDependency>();
}

Expand Down Expand Up @@ -244,6 +244,7 @@ protected override void FreezeAction()
foreach (var modelMap in schema.AllMapsDictionary.Values)
CompileDependencyRegisters(
modelMap,
modelMap == schema.ActiveMap,
modelMap.BsonClassMap,
default,
Array.Empty<OwnedBsonMemberMap>());
Expand All @@ -267,6 +268,7 @@ protected override void FreezeAction()
// Helpers.
private void CompileDependencyRegisters(
IModelMap modelMap,
bool modelMapIsActive,
BsonClassMap currentClassMap,
BsonClassMap? lastEntityClassMap,
IEnumerable<OwnedBsonMemberMap> ownedBsonMemberPath,
Expand All @@ -292,6 +294,7 @@ private void CompileDependencyRegisters(
// Identify current member with its root model map, the path from current model map, and cascade delete information.
var memberDependency = new MemberDependency(
modelMap,
modelMapIsActive,
currentMemberPath,
useCascadeDeleteSetting ?? false);

Expand Down Expand Up @@ -332,7 +335,7 @@ private void CompileDependencyRegisters(
{
bool? useCascadeDelete = (memberSerializer as IReferenceContainerSerializer)?.UseCascadeDelete;
foreach (var childClassMap in schemaSerializer.AllChildClassMaps)
CompileDependencyRegisters(modelMap, childClassMap, lastEntityClassMap, currentMemberPath, useCascadeDelete);
CompileDependencyRegisters(modelMap, modelMapIsActive, childClassMap, lastEntityClassMap, currentMemberPath, useCascadeDelete);
}
}
}
Expand Down

0 comments on commit e47e577

Please sign in to comment.