|
@@ -5,11 +5,9 @@ module Models.World
|
|
( World(..)
|
|
( World(..)
|
|
, fetchWorldById
|
|
, fetchWorldById
|
|
, getRandomWorld
|
|
, getRandomWorld
|
|
- , fetchRandomWorldsAsync
|
|
|
|
- , updateWorldsRandomAsync
|
|
|
|
|
|
+ , updateWorldRandom
|
|
) where
|
|
) where
|
|
|
|
|
|
-import Control.Concurrent.Async
|
|
|
|
import Data.Aeson
|
|
import Data.Aeson
|
|
import Data.Maybe
|
|
import Data.Maybe
|
|
import Data.Monoid ((<>))
|
|
import Data.Monoid ((<>))
|
|
@@ -54,24 +52,10 @@ getRandomWorld c = do
|
|
fetchWorldById i c
|
|
fetchWorldById i c
|
|
{-# INLINE getRandomWorld #-}
|
|
{-# INLINE getRandomWorld #-}
|
|
|
|
|
|
--- | Get n random Worlds in a concurrent way.
|
|
|
|
-fetchRandomWorldsAsync :: Int -> PG.Connection -> IO [World]
|
|
|
|
-fetchRandomWorldsAsync n c = do
|
|
|
|
- maybes <- mapConcurrently (\_ -> getRandomWorld c) [1..n]
|
|
|
|
- return $ catMaybes maybes
|
|
|
|
-{-# INLINE fetchRandomWorldsAsync #-}
|
|
|
|
-
|
|
|
|
-- | Update a World with a random number
|
|
-- | Update a World with a random number
|
|
-updateWorldRandom :: PG.Connection -> World -> IO World
|
|
|
|
-updateWorldRandom c (World _id _) = do
|
|
|
|
|
|
+updateWorldRandom :: World -> PG.Connection -> IO World
|
|
|
|
+updateWorldRandom (World _id _) c = do
|
|
i <- randomRIO (1, 10000)
|
|
i <- randomRIO (1, 10000)
|
|
_ <- PG.execute c "UPDATE World SET randomNumber = ? WHERE id = ?" (i, _id)
|
|
_ <- PG.execute c "UPDATE World SET randomNumber = ? WHERE id = ?" (i, _id)
|
|
return $ World _id i
|
|
return $ World _id i
|
|
{-# INLINE updateWorldRandom #-}
|
|
{-# INLINE updateWorldRandom #-}
|
|
-
|
|
|
|
--- | Update a bunch of Worlds in a concurrent way.
|
|
|
|
-updateWorldsRandomAsync :: [World] -> PG.Connection -> IO [World]
|
|
|
|
-updateWorldsRandomAsync ws c = do
|
|
|
|
- _ <- PG.execute_ c "SET synchronous_commit TO OFF"
|
|
|
|
- mapConcurrently (updateWorldRandom c) ws
|
|
|
|
-{-# INLINE updateWorldsRandomAsync #-}
|
|
|