Skip to content

Commit 8364264

Browse files
committed
yesod 1.4 and disable non-raw MongoDB
1 parent 2c51b12 commit 8364264

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

frameworks/Haskell/yesod/bench/bench.cabal

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,21 @@ executable bench
1616
TypeFamilies
1717
GADTs
1818
EmptyDataDecls
19+
CPP
1920

2021
build-depends: base >= 4.7 && < 5
21-
, yesod >= 1.2.5.2 && < 1.3
22-
, yesod-core == 1.2.14 && < 1.3
23-
, text >= 0.11 && < 1.2
24-
, persistent >= 1.3 && < 1.4
25-
, persistent-mysql >= 1.3 && < 1.4
26-
, persistent-mongoDB >= 1.3 && < 1.4
27-
, warp >= 2.1 && < 2.2
22+
, yesod >= 1.4 && < 1.5
23+
, yesod-core >= 1.4 && < 1.5
24+
, text >= 0.11 && < 1.3
25+
, persistent >= 2.1 && < 2.2
26+
, persistent-mysql >= 2.1 && < 2.2
27+
, persistent-mongoDB >= 2.1 && < 2.2
28+
, warp >= 3.0.2.2 && < 3.1
29+
, auto-update >= 0.1.1.4 && < 0.2
2830
, primitive >= 0.5
2931
, mwc-random >= 0.12
3032
, pool-conduit >= 0.1.2
3133
, network
3234
, mongoDB
35+
, monad-logger
36+
, mtl

frameworks/Haskell/yesod/bench/src/yesod.hs

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
{-# LANGUAGE EmptyDataDecls #-}
22
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
34
{-# LANGUAGE MultiParamTypeClasses #-}
45
{-# LANGUAGE OverloadedStrings #-}
56
{-# LANGUAGE QuasiQuotes #-}
67
{-# LANGUAGE RankNTypes #-}
78
{-# LANGUAGE RecordWildCards #-}
89
{-# LANGUAGE TemplateHaskell #-}
910
{-# LANGUAGE TypeFamilies #-}
11+
{-# LANGUAGE ViewPatterns #-}
12+
{-# LANGUAGE FlexibleInstances #-}
13+
{-# LANGUAGE FlexibleContexts #-}
1014
{-# OPTIONS_GHC -fno-warn-orphans #-}
1115
module Main (main, resourcesApp, Widget, WorldId) where
1216
import Control.Monad (replicateM)
17+
import Control.Monad.Logger (runNoLoggingT)
1318
import Control.Monad.Primitive (PrimState)
19+
import Control.Monad.Reader (ReaderT)
1420
import Data.Conduit.Pool (Pool)
1521
import Data.Int (Int64)
1622
import Data.Text (Text)
@@ -24,14 +30,18 @@ import System.Environment (getArgs)
2430
import qualified System.Random.MWC as R
2531
import Yesod hiding (Field)
2632

27-
mkPersist sqlSettings [persistLowerCase|
33+
mkPersist sqlSettings { mpsGeneric = True } [persistLowerCase|
2834
World sql=World
2935
randomNumber Int sql=randomNumber
36+
#ifdef MONGODB
37+
id Int64
38+
UniqueId
39+
#endif
3040
|]
3141

3242
data App = App
3343
{ appGen :: !(R.Gen (PrimState IO))
34-
, mySqlPool :: !(Pool My.Connection)
44+
, mySqlPool :: !(Pool My.SqlBackend)
3545
, mongoDBPool :: !(Pool Mongo.Connection)
3646
}
3747

@@ -43,8 +53,10 @@ mkYesod "App" [parseRoutes|
4353
/db DbR GET
4454
/dbs/#Int DbsR GET
4555

56+
#ifdef MONGODB
4657
/mongo/db MongoDbR GET
4758
/mongo/dbs/#Int MongoDbsR GET
59+
#endif
4860

4961
/mongo/raw/db MongoRawDbR GET
5062
/mongo/raw/dbs/#Int MongoRawDbsR GET
@@ -60,21 +72,25 @@ getJsonR = return $ object ["message" .= ("Hello, World!" :: Text)]
6072

6173

6274
getDbR :: Handler Value
63-
getDbR = getDb (intQuery runMySQL )
75+
getDbR = getDb (intQuery runMySQL My.toSqlKey)
6476

77+
#ifdef MONGODB
6578
getMongoDbR :: Handler Value
66-
getMongoDbR = getDb (intQuery runMongoDB )
79+
getMongoDbR = getDb (intQuery runMongoDB (getBy . UniqueId))
80+
#endif
6781

6882
getMongoRawDbR :: Handler Value
6983
getMongoRawDbR = getDb rawMongoIntQuery
7084

7185
getDbsR :: Int -> Handler Value
7286
getDbsR cnt = do
7387
App {..} <- getYesod
74-
multiRandomHandler (intQuery runMySQL) cnt
88+
multiRandomHandler (intQuery runMySQL My.toSqlKey) cnt
7589

90+
#ifdef MONGODB
7691
getMongoDbsR :: Int -> Handler Value
77-
getMongoDbsR cnt = multiRandomHandler (intQuery runMongoDB) cnt
92+
getMongoDbsR cnt = multiRandomHandler (intQuery runMongoDB (getBy . UniqueId)) cnt
93+
#endif
7894

7995
getMongoRawDbsR :: Int -> Handler Value
8096
getMongoRawDbsR cnt = multiRandomHandler rawMongoIntQuery cnt
@@ -100,21 +116,22 @@ runMySQL f = do
100116
App {..} <- getYesod
101117
My.runSqlPool f mySqlPool
102118

103-
intQuery :: forall (m :: * -> *) (m1 :: * -> *) val backend.
104-
(Monad m, PersistEntity val, PersistStore m1,
105-
PersistEntityBackend val ~ PersistMonadBackend m1) =>
106-
(m1 (Maybe val) -> m (Maybe (WorldGeneric backend)))
119+
intQuery :: (MonadIO m, PersistEntity val, PersistStore backend
120+
, backend ~ PersistEntityBackend val
121+
) =>
122+
(ReaderT backend m (Maybe val) -> m (Maybe (WorldGeneric backend)))
123+
-> (Int64 -> Key val)
107124
-> Int64 -> m Value
108-
intQuery db i = do
109-
Just x <- db $ get (Key $ PersistInt64 i)
125+
intQuery db toKey i = do
126+
Just x <- db $ get $ toKey i
110127
return $ jsonResult (worldRandomNumber x)
111128
where
112129
jsonResult :: Int -> Value
113130
jsonResult n = object ["id" .= i, "randomNumber" .= n]
114131

115132
rawMongoIntQuery :: Mongo.Val v => v -> Handler Value
116133
rawMongoIntQuery i = do
117-
Just x <- runMongoDB $ Mongo.findOne (Mongo.select ["id" =: i] "world")
134+
Just x <- runMongoDB $ Mongo.findOne (Mongo.select ["id" =: i] "World")
118135
return $ documentToJson x
119136

120137
multiRandomHandler :: ToJSON a
@@ -145,7 +162,7 @@ instance ToJSON Mongo.Value where
145162
main :: IO ()
146163
main = R.withSystemRandom $ \gen -> do
147164
[_cores, host] <- getArgs
148-
myPool <- My.createMySQLPool My.defaultConnectInfo
165+
myPool <- runNoLoggingT $ My.createMySQLPool My.defaultConnectInfo
149166
{ My.connectUser = "benchmarkdbuser"
150167
, My.connectPassword = "benchmarkdbpass"
151168
, My.connectDatabase = "hello_world"

0 commit comments

Comments
 (0)