Skip to content

Improved collection batch fetching #1558

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

Merged
merged 9 commits into from
Jan 31, 2018

Conversation

gliljas
Copy link
Member

@gliljas gliljas commented Jan 26, 2018

Implemented as it was done in hibernate/hibernate-orm#337. HHH-1775

Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put in 6.0 milestone since it implies a breaking change. But I would have labeled it "possible breaking change" rather than "breaking change", since the modified interface is not implemented by most users. Most of them will not be impacted by this change.

map = new LinkedHashMap<CollectionEntry, IPersistentCollection>();
batchLoadableCollections.Add(persister.Role, map);
}
map[ce]=collection;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some formatting is lacking here.

private readonly IDictionary<EntityKey, object> batchLoadableEntityKeys = new LinkedHashMap<EntityKey, object>(8);

private readonly IDictionary<string, LinkedHashSet<EntityKey>> batchLoadableEntityKeys = new Dictionary<string, LinkedHashSet<EntityKey>>(8);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Undue whitespace addition.

CollectionEntry ce = (CollectionEntry) me.Value;
IPersistentCollection collection = (IPersistentCollection) me.Key;
if (!collection.WasInitialized && ce.LoadedPersister == collectionPersister)
foreach (KeyValuePair<CollectionEntry,IPersistentCollection> me in map)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after comma.

if (log.IsWarnEnabled())
{
log.Warn("Encountered initialized collection in BatchFetchQueue, this should not happen.");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally tend to minimize such code by eliminating the "isEnabled" guard for simple logs, since the logger itself is supposed to test this. I keep the guard only if the log involve more than calling the logger, like having some string computing before calling it.
The "should never happen" comment is also a bit redundant with the log message. So I would tend to only have one line here instead of five, the one doing the log.

@@ -202,9 +266,9 @@ public object[] GetEntityBatch(IEntityPersister persister,object id,int batchSiz
int end = -1;
bool checkForEnd = false;

foreach (EntityKey key in batchLoadableEntityKeys.Keys)
if (batchLoadableEntityKeys.TryGetValue(persister.EntityName,out var set))//TODO: this needn't exclude subclasses...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing space after comma. And I would rather have the comment on the line above instead of at end of line. I view most end of line comments as bad practice, as they can lack visibility.

{
if (key.EntityName.Equals(persister.EntityName))
foreach (var key in set)
{
//TODO: this needn't exclude subclasses...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comments now looks redundant.

@@ -53,6 +53,10 @@ private void EvictCollection(IPersistentCollection collection)
Session.PersistenceContext.CollectionEntries.Remove(collection);
if (log.IsDebugEnabled())
log.Debug("evicting collection: {0}", MessageHelper.CollectionInfoString(ce.LoadedPersister, collection, ce.LoadedKey, Session));
if (ce.LoadedPersister?.BatchSize > 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why some code test > 0 and other > 1? Shouldn't it be > 1 everywhere?

@fredericDelaporte fredericDelaporte added this to the 6.0 milestone Jan 27, 2018
@hazzik hazzik force-pushed the CollectionBatchFetching branch from 4f195c4 to 518797c Compare January 28, 2018 10:15
@hazzik
Copy link
Member

hazzik commented Jan 28, 2018

I've removed breaking changes. Please review.

@@ -302,6 +302,10 @@ public void PostInitialize(IPersistentCollection collection)
{
snapshot = LoadedPersister.IsMutable ? collection.GetSnapshot(LoadedPersister) : null;
collection.SetSnapshot(loadedKey, role, snapshot);
if (LoadedPersister.GetBatchSize() > 1)
{
collection.GetCurrentSession().PersistenceContext.BatchFetchQueue.RemoveBatchLoadableCollection(this);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is no more a binary breaking change but it is still a breaking one, isn't it? Because if anyone supplies its own IPersistentCollection not deriving from AbstractPersistentCollection, it will now throw here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if returning 0/1 on get batch size if this is not supported one?

@@ -835,6 +835,10 @@ public void AddUninitializedCollection(ICollectionPersister persister, IPersiste
{
CollectionEntry ce = new CollectionEntry(collection, persister, id, flushing);
AddCollection(collection, ce, id);
if (persister.GetBatchSize() > 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here but for ICollectionPersister.

return acp.GetBatchSize();
}

throw new InvalidOperationException("Only persisters of AbstractCollectionPersister are supported.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fredericDelaporte I think returning 1 here would fix this possible breaking change, wouldn't it?

@hazzik
Copy link
Member

hazzik commented Jan 28, 2018

Ok, now it does not have any breaking changes.

@gliljas
Copy link
Member Author

gliljas commented Jan 28, 2018

@hazzik Nice!

@hazzik hazzik modified the milestones: 6.0, 5.1 Jan 28, 2018
Copy link
Member

@fredericDelaporte fredericDelaporte left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think returning 1 here would fix this possible breaking change, wouldn't it?

Agreed.
(Well it may cause custom implementations to be less effective at batching in 5.1 up to 6.0 excluded, but I think it is a fair compromise.)

@@ -302,9 +302,20 @@ public void PostInitialize(IPersistentCollection collection)
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single argument PostInitialize should be obsoleted. Or is there any reason to keep it, beside avoiding a breaking change? It seems now only called from the two arguments PostInitialize.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@fredericDelaporte
Copy link
Member

As for #1560, I consider this one, if squashed, should be squashed by Alexander for slightly better attribution, since he has significantly contributed to it.

@hazzik hazzik merged commit c90b08c into nhibernate:master Jan 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants