Skip to content

Avoid recursive calls in BatchFetchQueue #2016

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 3 commits into from
Feb 23, 2019
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
224 changes: 105 additions & 119 deletions src/NHibernate/Async/Engine/BatchFetchQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal async Task<object[]> GetCollectionBatchAsync(ICollectionPersister colle
foreach (KeyValuePair<CollectionEntry, IPersistentCollection> me in map)
{
cancellationToken.ThrowIfCancellationRequested();
if (await (ProcessKeyAsync(me)).ConfigureAwait(false))
if (ProcessKey(me) ?? await (CheckCacheAndProcessResultAsync()).ConfigureAwait(false))
{
return keys;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
{
for (var j = 0; j < collectionKeys.Count; j++)
{
if (await (ProcessKeyAsync(collectionKeys[indexes[j]].Key)).ConfigureAwait(false))
if (ProcessKey(collectionKeys[indexes[j]].Key) == true)
{
return true;
}
Expand All @@ -118,7 +118,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
var results = await (AreCachedAsync(collectionKeys, indexes, collectionPersister, batchableCache, checkCache, cancellationToken)).ConfigureAwait(false);
for (var j = 0; j < results.Length; j++)
{
if (!results[j] && await (ProcessKeyAsync(collectionKeys[indexes[j]].Key, true)).ConfigureAwait(false))
if (!results[j] && ProcessKey(collectionKeys[indexes[j]].Key, true) == true)
{
return true;
}
Expand All @@ -132,91 +132,84 @@ async Task<bool> CheckCacheAndProcessResultAsync()
return false;
}

Task<bool> ProcessKeyAsync(KeyValuePair<CollectionEntry, IPersistentCollection> me, bool ignoreCache = false)
bool? ProcessKey(KeyValuePair<CollectionEntry, IPersistentCollection> me, bool ignoreCache = false)
{
try
var ce = me.Key;
var collection = me.Value;
if (ce.LoadedKey == null)
{
var ce = me.Key;
var collection = me.Value;
if (ce.LoadedKey == null)
{
// the LoadedKey of the CollectionEntry might be null as it might have been reset to null
// (see for example Collections.ProcessDereferencedCollection()
// and CollectionEntry.AfterAction())
// though we clear the queue on flush, it seems like a good idea to guard
// against potentially null LoadedKey:s
return Task.FromResult<bool>(false);
}
// the LoadedKey of the CollectionEntry might be null as it might have been reset to null
// (see for example Collections.ProcessDereferencedCollection()
// and CollectionEntry.AfterAction())
// though we clear the queue on flush, it seems like a good idea to guard
// against potentially null LoadedKey:s
return false;
}

if (collection.WasInitialized)
{
log.Warn("Encountered initialized collection in BatchFetchQueue, this should not happen.");
return Task.FromResult<bool>(false);
}
if (collection.WasInitialized)
{
log.Warn("Encountered initialized collection in BatchFetchQueue, this should not happen.");
return false;
}

if (checkForEnd && (index == map.Count || index >= keyIndex.Value + batchSize))
if (checkForEnd && (index == map.Count || index >= keyIndex.Value + batchSize))
{
return true;
}
if (collectionPersister.KeyType.IsEqual(key, ce.LoadedKey, collectionPersister.Factory))
{
if (collectionEntries != null)
{
return Task.FromResult<bool>(true);
collectionEntries[0] = ce;
}
if (collectionPersister.KeyType.IsEqual(key, ce.LoadedKey, collectionPersister.Factory))
keyIndex = index;
}
else if (!checkCache || batchableCache == null)
{
if (index < map.Count && (!keyIndex.HasValue || index < keyIndex.Value))
{
if (collectionEntries != null)
{
collectionEntries[0] = ce;
}
keyIndex = index;
collectionKeys.Add(new KeyValuePair<KeyValuePair<CollectionEntry, IPersistentCollection>, int>(me, index));
return false;
}
else if (!checkCache || batchableCache == null)
{
if (index < map.Count && (!keyIndex.HasValue || index < keyIndex.Value))
{
collectionKeys.Add(new KeyValuePair<KeyValuePair<CollectionEntry, IPersistentCollection>, int>(me, index));
return Task.FromResult<bool>(false);
}

// No need to check "!checkCache || !IsCached(ce.LoadedKey, collectionPersister)":
// "batchableCache == null" already means there is no cache, so IsCached can only yield false.
// (This method is now removed.)
if (collectionEntries != null)
{
collectionEntries[i] = ce;
}
keys[i++] = ce.LoadedKey;
}
else if (ignoreCache)
// No need to check "!checkCache || !IsCached(ce.LoadedKey, collectionPersister)":
// "batchableCache == null" already means there is no cache, so IsCached can only yield false.
// (This method is now removed.)
if (collectionEntries != null)
{
if (collectionEntries != null)
{
collectionEntries[i] = ce;
}
keys[i++] = ce.LoadedKey;
collectionEntries[i] = ce;
}
else
keys[i++] = ce.LoadedKey;
}
else if (ignoreCache)
{
if (collectionEntries != null)
{
collectionKeys.Add(new KeyValuePair<KeyValuePair<CollectionEntry, IPersistentCollection>, int>(me, index));
// Check the cache only when we have collected as many keys as are needed to fill the batch,
// that are after the demanded key.
if (!keyIndex.HasValue || index < keyIndex.Value + batchSize)
{
return Task.FromResult<bool>(false);
}
return CheckCacheAndProcessResultAsync();
collectionEntries[i] = ce;
}
if (i == batchSize)
keys[i++] = ce.LoadedKey;
}
else
{
collectionKeys.Add(new KeyValuePair<KeyValuePair<CollectionEntry, IPersistentCollection>, int>(me, index));
// Check the cache only when we have collected as many keys as are needed to fill the batch,
// that are after the demanded key.
if (!keyIndex.HasValue || index < keyIndex.Value + batchSize)
{
i = 1; // End of array, start filling again from start
if (index == map.Count || keyIndex.HasValue)
{
checkForEnd = true;
return Task.FromResult<bool>(index == map.Count || index >= keyIndex.Value + batchSize);
}
return false;
}
return Task.FromResult<bool>(false);
return null;
}
catch (Exception ex)
if (i == batchSize)
{
return Task.FromException<bool>(ex);
i = 1; // End of array, start filling again from start
if (index == map.Count || keyIndex.HasValue)
{
checkForEnd = true;
return index == map.Count || index >= keyIndex.Value + batchSize;
}
}
return false;
}
}

Expand Down Expand Up @@ -273,7 +266,7 @@ internal async Task<object[]> GetEntityBatchAsync(IEntityPersister persister, ob
foreach (var key in set)
{
cancellationToken.ThrowIfCancellationRequested();
if (await (ProcessKeyAsync(key)).ConfigureAwait(false))
if (ProcessKey(key) ?? await (CheckCacheAndProcessResultAsync()).ConfigureAwait(false))
{
return ids;
}
Expand Down Expand Up @@ -306,7 +299,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
{
for (var j = 0; j < entityKeys.Count; j++)
{
if (await (ProcessKeyAsync(entityKeys[indexes[j]].Key)).ConfigureAwait(false))
if (ProcessKey(entityKeys[indexes[j]].Key) == true)
{
return true;
}
Expand All @@ -317,7 +310,7 @@ async Task<bool> CheckCacheAndProcessResultAsync()
var results = await (AreCachedAsync(entityKeys, indexes, persister, batchableCache, checkCache, cancellationToken)).ConfigureAwait(false);
for (var j = 0; j < results.Length; j++)
{
if (!results[j] && await (ProcessKeyAsync(entityKeys[indexes[j]].Key, true)).ConfigureAwait(false))
if (!results[j] && ProcessKey(entityKeys[indexes[j]].Key, true) == true)
{
return true;
}
Expand All @@ -331,62 +324,55 @@ async Task<bool> CheckCacheAndProcessResultAsync()
return false;
}

Task<bool> ProcessKeyAsync(EntityKey key, bool ignoreCache = false)
bool? ProcessKey(EntityKey key, bool ignoreCache = false)
{
try
//TODO: this needn't exclude subclasses...
if (checkForEnd && (index == set.Count || index >= idIndex.Value + batchSize))
{
//TODO: this needn't exclude subclasses...
if (checkForEnd && (index == set.Count || index >= idIndex.Value + batchSize))
{
return Task.FromResult<bool>(true);
}
if (persister.IdentifierType.IsEqual(id, key.Identifier))
{
idIndex = index;
}
else if (!checkCache || batchableCache == null)
{
if (index < set.Count && (!idIndex.HasValue || index < idIndex.Value))
{
entityKeys.Add(new KeyValuePair<EntityKey, int>(key, index));
return Task.FromResult<bool>(false);
}

// No need to check "!checkCache || !IsCached(key, persister)": "batchableCache == null"
// already means there is no cache, so IsCached can only yield false. (This method is now
// removed.)
ids[i++] = key.Identifier;
}
else if (ignoreCache)
{
ids[i++] = key.Identifier;
}
else
return true;
}
if (persister.IdentifierType.IsEqual(id, key.Identifier))
{
idIndex = index;
}
else if (!checkCache || batchableCache == null)
{
if (index < set.Count && (!idIndex.HasValue || index < idIndex.Value))
{
entityKeys.Add(new KeyValuePair<EntityKey, int>(key, index));
// Check the cache only when we have collected as many keys as are needed to fill the batch,
// that are after the demanded key.
if (!idIndex.HasValue || index < idIndex.Value + batchSize)
{
return Task.FromResult<bool>(false);
}
return CheckCacheAndProcessResultAsync();
return false;
}
if (i == batchSize)

// No need to check "!checkCache || !IsCached(key, persister)": "batchableCache == null"
// already means there is no cache, so IsCached can only yield false. (This method is now
// removed.)
ids[i++] = key.Identifier;
}
else if (ignoreCache)
{
ids[i++] = key.Identifier;
}
else
{
entityKeys.Add(new KeyValuePair<EntityKey, int>(key, index));
// Check the cache only when we have collected as many keys as are needed to fill the batch,
// that are after the demanded key.
if (!idIndex.HasValue || index < idIndex.Value + batchSize)
{
i = 1; // End of array, start filling again from start
if (index == set.Count || idIndex.HasValue)
{
checkForEnd = true;
return Task.FromResult<bool>(index == set.Count || index >= idIndex.Value + batchSize);
}
return false;
}
return Task.FromResult<bool>(false);
return null;
}
catch (Exception ex)
if (i == batchSize)
{
return Task.FromException<bool>(ex);
i = 1; // End of array, start filling again from start
if (index == set.Count || idIndex.HasValue)
{
checkForEnd = true;
return index == set.Count || index >= idIndex.Value + batchSize;
}
}
return false;
}
}

Expand Down
20 changes: 10 additions & 10 deletions src/NHibernate/Engine/BatchFetchQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ internal object[] GetCollectionBatch(ICollectionPersister collectionPersister, o

foreach (KeyValuePair<CollectionEntry, IPersistentCollection> me in map)
{
if (ProcessKey(me))
if (ProcessKey(me) ?? CheckCacheAndProcessResult())
{
return keys;
}
Expand Down Expand Up @@ -276,7 +276,7 @@ bool CheckCacheAndProcessResult()
{
for (var j = 0; j < collectionKeys.Count; j++)
{
if (ProcessKey(collectionKeys[indexes[j]].Key))
if (ProcessKey(collectionKeys[indexes[j]].Key) == true)
{
return true;
}
Expand All @@ -287,7 +287,7 @@ bool CheckCacheAndProcessResult()
var results = AreCached(collectionKeys, indexes, collectionPersister, batchableCache, checkCache);
for (var j = 0; j < results.Length; j++)
{
if (!results[j] && ProcessKey(collectionKeys[indexes[j]].Key, true))
if (!results[j] && ProcessKey(collectionKeys[indexes[j]].Key, true) == true)
{
return true;
}
Expand All @@ -301,7 +301,7 @@ bool CheckCacheAndProcessResult()
return false;
}

bool ProcessKey(KeyValuePair<CollectionEntry, IPersistentCollection> me, bool ignoreCache = false)
bool? ProcessKey(KeyValuePair<CollectionEntry, IPersistentCollection> me, bool ignoreCache = false)
{
var ce = me.Key;
var collection = me.Value;
Expand Down Expand Up @@ -367,7 +367,7 @@ bool ProcessKey(KeyValuePair<CollectionEntry, IPersistentCollection> me, bool ig
{
return false;
}
return CheckCacheAndProcessResult();
return null;
}
if (i == batchSize)
{
Expand Down Expand Up @@ -427,7 +427,7 @@ internal object[] GetEntityBatch(IEntityPersister persister, object id, int batc

foreach (var key in set)
{
if (ProcessKey(key))
if (ProcessKey(key) ?? CheckCacheAndProcessResult())
{
return ids;
}
Expand Down Expand Up @@ -459,7 +459,7 @@ bool CheckCacheAndProcessResult()
{
for (var j = 0; j < entityKeys.Count; j++)
{
if (ProcessKey(entityKeys[indexes[j]].Key))
if (ProcessKey(entityKeys[indexes[j]].Key) == true)
{
return true;
}
Expand All @@ -470,7 +470,7 @@ bool CheckCacheAndProcessResult()
var results = AreCached(entityKeys, indexes, persister, batchableCache, checkCache);
for (var j = 0; j < results.Length; j++)
{
if (!results[j] && ProcessKey(entityKeys[indexes[j]].Key, true))
if (!results[j] && ProcessKey(entityKeys[indexes[j]].Key, true) == true)
{
return true;
}
Expand All @@ -484,7 +484,7 @@ bool CheckCacheAndProcessResult()
return false;
}

bool ProcessKey(EntityKey key, bool ignoreCache = false)
bool? ProcessKey(EntityKey key, bool ignoreCache = false)
{
//TODO: this needn't exclude subclasses...
if (checkForEnd && (index == set.Count || index >= idIndex.Value + batchSize))
Expand Down Expand Up @@ -521,7 +521,7 @@ bool ProcessKey(EntityKey key, bool ignoreCache = false)
{
return false;
}
return CheckCacheAndProcessResult();
return null;
}
if (i == batchSize)
{
Expand Down