@@ -17,6 +17,7 @@ import GHC.Generics
1717import Data.Binary
1818import ProjectM36.TransactionGraph.Merge
1919import Data.Either (lefts, rights, isRight)
20+import Data.Monoid
2021
2122-- | Record a lookup for a specific transaction in the graph.
2223data TransactionIdLookup = TransactionIdLookup TransactionId |
@@ -28,11 +29,6 @@ data TransactionIdHeadBacktrack = TransactionIdHeadParentBacktrack Int | -- git
2829 TransactionIdHeadBranchBacktrack Int -- git ^: walk back one parent level to the nth arbitrarily-chosen parent
2930 deriving (Show, Eq, Binary, Generic)
3031
31−data EmptyCommitOption = AllowEmptyCommitOption | -- allow empty commit and add it to the graph
32− ForbidEmptyCommitOption | --return error on empty commit- probably the safest option
33− IgnoreEmptyCommitOption -- don't add the commit and don't add it to the graph (no-op)
34− deriving (Eq, Show, Binary, Generic)
35−
3632data MergeCommitOption = NoMergeCommitOption | --no special handling
3733 AutoMergeToHeadCommitOption -- branch, merge to current branch, commit- all-in-one in order to take advantage of atomic STM
3834
@@ -42,12 +38,12 @@ data TransactionGraphOperator = JumpToHead HeadName |
4238 Branch HeadName |
4339 DeleteBranch HeadName |
4440 MergeTransactions MergeStrategy HeadName HeadName |
45− Commit EmptyCommitOption MergeCommitOption |
41+ Commit |
4642 Rollback
4743 deriving (Eq, Show, Binary, Generic)
4844
4945isCommit :: TransactionGraphOperator -> Bool
50−isCommit (Commit _) = True
46+isCommit Commit = True
5147isCommit _ = False
5248
5349data ROTransactionGraphOperator = ShowGraph
@@ -232,17 +228,9 @@ evalGraphOp newId (DisconnectedTransaction parentId schemas' _) graph (Branch ne
232228 Left err -> Left err
233229 Right (_, newGraph) -> Right (newDiscon, newGraph)
234230
235−--a version of commit which adds the discon to a temporary branch and attempts to merge it to the head and commit it- deletes the merge branch so that the newTransId appears to be for a fast-forwardded commit
236−--this emulates a rebase based on the transaction merge code
237−evalGraphOp newTransId discon@(DisconnectedTransaction parentId schema' _) graph cmd@(Commit _ AutoMergeToHeadOption) = do
238− parentTrans <- transactionForId parentId graph
239− let tempBranchName = "mergebranch_" <> U.toText newTransId
240− evalGraphOp ? (Branch "junk
241−
242−
243231-- add the disconnected transaction to the graph
244232-- affects graph and disconnectedtransaction- the new disconnectedtransaction's parent is the freshly committed transaction
245−evalGraphOp newTransId discon@(DisconnectedTransaction parentId schemas' _) graph (Commit _) = case transactionForId parentId graph of
233+evalGraphOp newTransId discon@(DisconnectedTransaction parentId schemas' _) graph Commit = case transactionForId parentId graph of
246234 Left err -> Left err
247235 Right parentTransaction -> case headNameForTransaction parentTransaction graph of
248236 Nothing -> Left $ TransactionIsNotAHeadError parentId
@@ -491,4 +479,15 @@ backtrackGraph graph currentTid (TransactionIdHeadBranchBacktrack steps) = do
491479 Left (ParentCountTraversalError (S.size parents) steps)
492480 else
493481 pure (S.elemAt (steps - 1) parents)
494−
482+
483+-- | Create a temporary branch for commit, merge the result to head, delete the temporary branch. This is useful to atomically commit a transaction, avoiding a TransactionIsNotHeadError but trading it for a potential MergeError.
484+autoMergeToHead :: (TransactionId, TransactionId) -> DisconnectedTransaction -> HeadName -> MergeStrategy -> TransactionGraph -> Either RelationalError (DisconnectedTransaction, TransactionGraph)
485+autoMergeToHead (tempTransId, newCommitTransId) discon mergeToHeadName strat graph = do
486+ let tempBranchName = "mergebranch_" <> U.toText tempTransId
487+ --create the temp branch
488+ (discon', graph') <- evalGraphOp tempTransId discon graph (Branch tempBranchName)
489+ --create the merge
490+ (discon'', graph'') <- evalGraphOp newCommitTransId discon' graph' (MergeTransactions strat tempBranchName mergeToHeadName)
491+ --delete the temp branch
492+ evalGraphOp newCommitTransId discon'' graph'' (DeleteBranch tempBranchName)
493+