Skip to content

Commit 2247a35

Browse files
committed
InsertActionSorter refactoring to make things more explicit (NH-3037, closes #67)
1 parent 1fb26c4 commit 2247a35

File tree

1 file changed

+31
-44
lines changed

1 file changed

+31
-44
lines changed

src/NHibernate/Engine/ActionQueue.cs

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -532,25 +532,7 @@ public void Sort()
532532
// the entity associated with the current action.
533533
var currentEntity = action.Instance;
534534

535-
int batchNumber;
536-
if (_latestBatches.ContainsKey(entityName))
537-
{
538-
// There is already an existing batch for this type of entity.
539-
// Check to see if the latest batch is acceptable.
540-
batchNumber = FindBatchNumber(action, entityName);
541-
}
542-
else
543-
{
544-
// add an entry for this type of entity.
545-
// we can be assured that all referenced entities have already
546-
// been processed,
547-
// so specify that this entity is with the latest batch.
548-
// doing the batch number before adding the name to the list is
549-
// a faster way to get an accurate number.
550-
551-
batchNumber = _actionBatches.Count;
552-
_latestBatches[entityName] = batchNumber;
553-
}
535+
var batchNumber = GetBatchNumber(action, entityName);
554536
_entityBatchNumber[currentEntity] = batchNumber;
555537
AddToBatch(batchNumber, action);
556538
}
@@ -567,23 +549,31 @@ public void Sort()
567549
}
568550
}
569551

570-
/// <summary>
571-
/// Finds an acceptable batch for this entity to be a member as part of the <see cref="InsertActionSorter" />
572-
/// </summary>
573-
/// <param name="action">The action being sorted</param>
574-
/// <param name="entityName">The name of the entity affected by the action</param>
575-
/// <returns>An appropriate batch number; todo document this process better</returns>
576-
private int FindBatchNumber(EntityInsertAction action, string entityName)
552+
private int GetBatchNumber(EntityInsertAction action, string entityName)
577553
{
578-
// loop through all the associated entities and make sure they have been
579-
// processed before the latest
580-
// batch associated with this entity type.
581-
582-
// the current batch number is the latest batch for this entity type.
583-
var latestBatchNumberForType = _latestBatches[entityName];
554+
int batchNumber;
555+
if (_latestBatches.TryGetValue(entityName, out batchNumber))
556+
{
557+
// There is already an existing batch for this type of entity.
558+
// Check to see if the latest batch is acceptable.
559+
if (IsProcessedAfterAllAssociatedEntities(action, batchNumber))
560+
return batchNumber;
561+
}
562+
563+
// add an entry for this type of entity.
564+
// we can be assured that all referenced entities have already
565+
// been processed,
566+
// so specify that this entity is with the latest batch.
567+
// doing the batch number before adding the name to the list is
568+
// a faster way to get an accurate number.
569+
570+
batchNumber = _actionBatches.Count;
571+
_latestBatches[entityName] = batchNumber;
572+
return batchNumber;
573+
}
584574

585-
// loop through all the associations of the current entity and make sure that they are processed
586-
// before the current batch number
575+
private bool IsProcessedAfterAllAssociatedEntities(EntityInsertAction action, int latestBatchNumberForType)
576+
{
587577
var propertyValues = action.State;
588578
var propertyTypes = action.Persister.ClassMetadata.PropertyTypes;
589579

@@ -592,23 +582,20 @@ private int FindBatchNumber(EntityInsertAction action, string entityName)
592582
var value = propertyValues[i];
593583
var type = propertyTypes[i];
594584

595-
if (type.IsEntityType && value != null)
585+
if (type.IsEntityType &&
586+
value != null)
596587
{
597588
// find the batch number associated with the current association, if any.
598589
int associationBatchNumber;
599-
if (_entityBatchNumber.TryGetValue(value, out associationBatchNumber) && associationBatchNumber > latestBatchNumberForType)
590+
if (_entityBatchNumber.TryGetValue(value, out associationBatchNumber) &&
591+
associationBatchNumber > latestBatchNumberForType)
600592
{
601-
// create a new batch for this type. The batch number is the number of current batches.
602-
latestBatchNumberForType = _actionBatches.Count;
603-
_latestBatches[entityName] = latestBatchNumberForType;
604-
// since this entity will now be processed in the latest possible batch,
605-
// we can be assured that it will come after all other associations,
606-
// there's not need to continue checking.
607-
break;
593+
return false;
608594
}
609595
}
610596
}
611-
return latestBatchNumberForType;
597+
598+
return true;
612599
}
613600

614601
private void AddToBatch(int batchNumber, EntityInsertAction action)

0 commit comments

Comments
 (0)