Skip to content

Entity loaders lazy initialization #400

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

Closed
wants to merge 1 commit into from
Closed
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
39 changes: 26 additions & 13 deletions src/NHibernate/Loader/Entity/BatchingEntityLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@ namespace NHibernate.Loader.Entity
/// <seealso cref="EntityLoader"/>
public class BatchingEntityLoader : IUniqueEntityLoader
{
private readonly Loader[] loaders;
private readonly Dictionary<int, EntityLoader> loaders = new Dictionary<int, EntityLoader>();
private readonly int[] batchSizes;
private readonly IEntityPersister persister;
private readonly IOuterJoinLoadable persister;
private readonly IType idType;
private readonly LockMode lockMode;
private readonly ISessionFactoryImplementor factory;
private readonly IDictionary<string, IFilter> enabledFilters;

public BatchingEntityLoader(IEntityPersister persister, int[] batchSizes, Loader[] loaders)
public BatchingEntityLoader(IOuterJoinLoadable persister, int[] batchSizes, LockMode lockMode, ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
{
this.batchSizes = batchSizes;
this.loaders = loaders;
this.persister = persister;
this.lockMode = lockMode;
this.factory = factory;
this.enabledFilters = enabledFilters;
idType = persister.IdentifierType;
}

Expand All @@ -44,6 +49,17 @@ private object GetObjectFromList(IList results, object id, ISessionImplementor s
return null;
}

private EntityLoader GetEntityLoader(int index, int smallBatchSize)
{
EntityLoader loader;
if (!this.loaders.TryGetValue(index, out loader))
{
loader = new EntityLoader(this.persister, smallBatchSize, this.lockMode, this.factory, this.enabledFilters);
this.loaders[index] = loader;
}
return loader;
}

public object Load(object id, object optionalObject, ISessionImplementor session)
{
object[] batch =
Expand All @@ -57,14 +73,16 @@ public object Load(object id, object optionalObject, ISessionImplementor session
object[] smallBatch = new object[smallBatchSize];
Array.Copy(batch, 0, smallBatch, 0, smallBatchSize);

EntityLoader loader = this.GetEntityLoader(i, smallBatchSize);
IList results =
loaders[i].LoadEntityBatch(session, smallBatch, idType, optionalObject, persister.EntityName, id, persister);
loader.LoadEntityBatch(session, smallBatch, idType, optionalObject, persister.EntityName, id, persister);

return GetObjectFromList(results, id, session); //EARLY EXIT
}
}

return ((IUniqueEntityLoader) loaders[batchSizes.Length - 1]).Load(id, optionalObject, session);
int index = batchSizes.Length - 1;
return ((IUniqueEntityLoader) this.GetEntityLoader(index, batchSizes[index])).Load(id, optionalObject, session);
}

public static IUniqueEntityLoader CreateBatchingEntityLoader(IOuterJoinLoadable persister, int maxBatchSize,
Expand All @@ -73,13 +91,8 @@ public static IUniqueEntityLoader CreateBatchingEntityLoader(IOuterJoinLoadable
{
if (maxBatchSize > 1)
{
int[] batchSizesToCreate = ArrayHelper.GetBatchSizes(maxBatchSize);
Loader[] loadersToCreate = new Loader[batchSizesToCreate.Length];
for (int i = 0; i < batchSizesToCreate.Length; i++)
{
loadersToCreate[i] = new EntityLoader(persister, batchSizesToCreate[i], lockMode, factory, enabledFilters);
}
return new BatchingEntityLoader(persister, batchSizesToCreate, loadersToCreate);
int[] batchSizesToCreate = ArrayHelper.GetBatchSizes(maxBatchSize);
return new BatchingEntityLoader(persister, batchSizesToCreate, lockMode, factory, enabledFilters);
}
else
{
Expand Down