Skip to content

Commit

Permalink
Flatten committed changes into one object
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Nov 11, 2024
1 parent 2cdfbb5 commit 3ba9566
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,25 @@ data class InMemoryEditablePlan(
private val lookupActivityType: (String) -> ActivityType
) : EditablePlan, Plan by plan {

private val commits = mutableListOf<Commit>()
private data class Commit(
val diff: List<Edit>,

/**
* A record of the simulation results objects that were up-to-date when the commit
* was created.
*
* This has SHARED OWNERSHIP with [InMemoryEditablePlan]; the editable plan may add more to
* this list AFTER the commit is created.
*/
val upToDateSimResultsSet: MutableSet<WeakReference<MerlinToProcedureSimulationResultsAdapter>>
)

private var committedChanges = Commit(listOf(), mutableSetOf())
var uncommittedChanges = mutableListOf<Edit>()
private set

val totalDiff: List<Edit>
get() = commits.flatMap { it.diff }
get() = committedChanges.diff

// Jointly owned set of up-to-date simulation results. See class-level comment for algorithm explanation.
private var upToDateSimResultsSet: MutableSet<WeakReference<MerlinToProcedureSimulationResultsAdapter>> = mutableSetOf()
Expand Down Expand Up @@ -108,11 +121,11 @@ data class InMemoryEditablePlan(
// Probably unnecessary, but shared ownership freaks me out enough already.
if (uncommittedChanges.isEmpty()) return

val committedEdits = uncommittedChanges
val newCommittedChanges = uncommittedChanges
uncommittedChanges = mutableListOf()

// Create a commit that shares ownership of the simResults set.
commits.add(Commit(committedEdits, upToDateSimResultsSet))
committedChanges = Commit(committedChanges.diff + newCommittedChanges, upToDateSimResultsSet)
}

override fun rollback(): List<Edit> {
Expand All @@ -131,10 +144,10 @@ data class InMemoryEditablePlan(
for (simResult in upToDateSimResultsSet) {
simResult.get()?.stale = true
}
for (simResult in commits.last().upToDateSimResultsSet) {
for (simResult in committedChanges.upToDateSimResultsSet) {
simResult.get()?.stale = false
}
upToDateSimResultsSet = commits.last().upToDateSimResultsSet
upToDateSimResultsSet = committedChanges.upToDateSimResultsSet
return result
}

Expand Down

0 comments on commit 3ba9566

Please sign in to comment.