Skip to content

A peer-to-peer stack for code collaboration

Correctly order results when loading initial state

Thomas Scholtes233c6466967d
diff --git a/exe/Server.hs b/exe/Server.hs
index 1fd016e..26f373a 100644
--- a/exe/Server.hs
+++ b/exe/Server.hs
@@ -113,7 +113,9 @@ loadState conn = do
let go acc x = eval x >>= \v -> pure (acc Seq.|> (x, v))
let st = runIdentity $ runLang pureEnv $ foldM go mempty vals
case st of
- (Left err, _) -> panic $ show err
+ (Left err, _) -> do
+ logInfo "Failed to load machine" [("machine", name)]
+ panic $ show err
(Right pairs, st') -> do
let c = Chain { chainName = name
, chainState = st'
diff --git a/exe/Server/DB.hs b/exe/Server/DB.hs
index d62479b..6668bb9 100644
--- a/exe/Server/DB.hs
+++ b/exe/Server/DB.hs
@@ -25,11 +25,14 @@ getSinceDB conn name id
-- | Get all txs in all chains. Useful after a server restart.
getAllDB :: Connection -> IO [(Text, [Value])]
getAllDB conn = do
- res :: [(Int, Text, Value)] <- query_ conn "SELECT id, chain, expr FROM txs"
+ -- The @ORDER BY@ clauses are important, because
+ -- * 'groupBy' only groups adjacent items in a list, and
+ -- * inputs are evaluated in order
+ res :: [(Int, Text, Value)] <- query_ conn "SELECT id, chain, expr FROM txs ORDER BY chain ASC, id ASC"
+ -- TODO fold over the results and use a 'Map' to group chain inputs
let grouped :: [[(Text, Value)]]
grouped
- = fmap (\(_, a, b) -> (a, b)) . sortOn (\(a,_,_) -> a)
- <$> groupBy (\(_, l, _) (_, r, _) -> l == r) res
+ = fmap (\(_, a, b) -> (a, b)) <$> groupBy (\(_, l, _) (_, r, _) -> l == r) res
pure [ (fst (head each), snd <$> each) | each <- grouped ]