Skip to content

Skip Topological sorting if not required #2006

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 4 commits into from
Feb 17, 2019
Merged
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
112 changes: 73 additions & 39 deletions src/NHibernate/Loader/JoinWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ protected virtual bool IsTooManyCollections
get { return false; }
}

//Since v5.3
[Obsolete("This class is not used and will be removed in a future version.")]
public class DependentAlias
{
public string Alias { get; set; }
public string[] DependsOn { get; set; }
}

readonly List<DependentAlias> _dependentAliases = new List<DependentAlias>();

protected JoinWalker(ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
{
this.factory = factory;
Expand Down Expand Up @@ -181,7 +181,7 @@ private void AddAssociationToJoinTree(IAssociationType type, string[] aliasedLhs
enabledFilters,
GetSelectMode(path));
assoc.ValidateJoin(path);
AddAssociation(subalias, assoc);
AddAssociation(assoc);

int nextDepth = currentDepth + 1;

Expand All @@ -204,10 +204,25 @@ protected virtual SelectMode GetSelectMode(string path)
return SelectMode.Undefined;
}

private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
private struct DependentAlias2
{
public DependentAlias2(string alias, ICollection<string> dependsOn)
{
Alias = alias;
DependsOn = dependsOn;
}

public string Alias { get; }
public ICollection<string> DependsOn { get; }
}

/// <summary>
/// Returns list of indexes in sorted order
/// </summary>
private static int[] GetTopologicalSortOrder(IList<DependentAlias2> fields)
{
TopologicalSorter g = new TopologicalSorter(fields.Count);
Dictionary<string, int> indexes = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
Dictionary<string, int> indexes = new Dictionary<string, int>(fields.Count, StringComparer.OrdinalIgnoreCase);

// add vertices
for (int i = 0; i < fields.Count; i++)
Expand All @@ -218,14 +233,12 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
// add edges
for (int i = 0; i < fields.Count; i++)
{
var dependentAlias = fields[i];
if (dependentAlias.DependsOn != null)
var dependentFields = fields[i].DependsOn;
if (dependentFields != null)
{
for (int j = 0; j < dependentAlias.DependsOn.Length; j++)
foreach (var dependentField in dependentFields)
{
var dependentField = dependentAlias.DependsOn[j];
int end;
if (indexes.TryGetValue(dependentField, out end))
if (indexes.TryGetValue(dependentField, out var end))
{
g.AddEdge(i, end);
}
Expand All @@ -236,31 +249,40 @@ private static int[] GetTopologicalSortOrder(List<DependentAlias> fields)
return g.Sort();
}

/// <summary>
/// Adds an association and extracts the aliases the association's 'with clause' is dependent on
/// </summary>
private void AddAssociation(string subalias, OuterJoinableAssociation association)
private static List<DependentAlias2> GetDependentAliases(IList<OuterJoinableAssociation> associations)
{
var dependentAlias = new DependentAlias
var dependentAliases = new List<DependentAlias2>(associations.Count);
foreach (var association in associations)
{
Alias = subalias,
};
_dependentAliases.Add(dependentAlias);
dependentAliases.Add(new DependentAlias2(association.RHSAlias, GetDependsOn(association)));
}

var on = association.On.ToString();
if (!string.IsNullOrEmpty(on))
return dependentAliases;
}

private static HashSet<string> GetDependsOn(OuterJoinableAssociation association)
{
if (SqlStringHelper.IsEmpty(association.On))
return null;

var dependencies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (Match match in aliasRegex.Matches(association.On.ToString()))
{
var dependencies = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
foreach (Match match in aliasRegex.Matches(on))
{
string alias = match.Value;
if (string.Equals(alias, subalias, StringComparison.OrdinalIgnoreCase))
continue;
dependencies.Add(alias);
}
dependentAlias.DependsOn = dependencies.ToArray();
string alias = match.Value;
if (string.Equals(alias, association.RHSAlias, StringComparison.OrdinalIgnoreCase))
continue;

dependencies.Add(alias);
}

return dependencies;
}

/// <summary>
/// Adds an association
/// </summary>
private void AddAssociation(OuterJoinableAssociation association)
{
associations.Add(association);
}

Expand Down Expand Up @@ -360,7 +382,7 @@ internal void AddExplicitEntityJoinAssociation(
Factory,
enabledFilters,
GetSelectMode(path));
AddAssociation(tableAlias, assoc);
AddAssociation(assoc);
}

private void WalkEntityAssociationTree(IAssociationType associationType, IOuterJoinLoadable persister,
Expand Down Expand Up @@ -799,16 +821,9 @@ protected SqlString MergeOrderings(string ass, string orderBy) {
/// </summary>
protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associations)
{
IList<OuterJoinableAssociation> sortedAssociations = new List<OuterJoinableAssociation>();

var indices = GetTopologicalSortOrder(_dependentAliases);
for (int index = indices.Length - 1; index >= 0; index--)
{
sortedAssociations.Add(associations[indices[index]]);
}

JoinFragment outerjoin = Dialect.CreateOuterJoinFragment();

var sortedAssociations = GetSortedAssociations(associations);
OuterJoinableAssociation last = null;
foreach (OuterJoinableAssociation oj in sortedAssociations)
{
Expand Down Expand Up @@ -840,6 +855,25 @@ protected JoinFragment MergeOuterJoins(IList<OuterJoinableAssociation> associati
return outerjoin;
}

private static IList<OuterJoinableAssociation> GetSortedAssociations(IList<OuterJoinableAssociation> associations)
{
if (associations.Count < 2)
return associations;

var fields = GetDependentAliases(associations);
if (!fields.Exists(a => a.DependsOn?.Count > 0))
return associations;

var indexes = GetTopologicalSortOrder(fields);
var sortedAssociations = new List<OuterJoinableAssociation>(associations.Count);
for (int index = indexes.Length - 1; index >= 0; index--)
{
sortedAssociations.Add(associations[indexes[index]]);
}

return sortedAssociations;
}

/// <summary>
/// Count the number of instances of IJoinable which are actually
/// also instances of ILoadable, or are one-to-many associations
Expand Down