Browse Source

feature(db): index on ticker,timeframe

master
Denis Tereshkin 6 years ago
parent
commit
b8fcee4c84
  1. 2
      src/ATrade/MDS/Database.hs
  2. 47
      test/Integration/Database.hs

2
src/ATrade/MDS/Database.hs

@ -52,10 +52,12 @@ initDatabase config = do
infoM "DB" $ "Initializing DB" infoM "DB" $ "Initializing DB"
conn <- connectSqlite3 (T.unpack $ dbPath config) conn <- connectSqlite3 (T.unpack $ dbPath config)
makeSchema conn makeSchema conn
makeIndex conn
infoM "DB" $ "Schema updated" infoM "DB" $ "Schema updated"
return conn return conn
where where
makeSchema conn = runRaw conn "CREATE TABLE IF NOT EXISTS bars (id SERIAL PRIMARY KEY, ticker TEXT, timestamp BIGINT, timeframe INTEGER, open NUMERIC(20, 10), high NUMERIC(20, 10), low NUMERIC(20, 10), close NUMERIC(20,10), volume BIGINT);" makeSchema conn = runRaw conn "CREATE TABLE IF NOT EXISTS bars (id SERIAL PRIMARY KEY, ticker TEXT, timestamp BIGINT, timeframe INTEGER, open NUMERIC(20, 10), high NUMERIC(20, 10), low NUMERIC(20, 10), close NUMERIC(20,10), volume BIGINT);"
makeIndex conn = runRaw conn "CREATE INDEX IF NOT EXISTS idx_bars ON bars (ticker, timeframe);"
closeDatabase :: MdsHandle -> IO () closeDatabase :: MdsHandle -> IO ()
closeDatabase = disconnect closeDatabase = disconnect

47
test/Integration/Database.hs

@ -3,34 +3,45 @@ module Integration.Database (
testDatabase testDatabase
) where ) where
import Test.Tasty import Test.Tasty
import Test.Tasty.HUnit import Test.Tasty.HUnit
import ATrade.MDS.Database import ATrade.MDS.Database
import ATrade.Types import ATrade.Types
import Control.Exception import Control.Exception
import Data.DateTime import Data.DateTime
import Data.Time.Clock import qualified Data.Text as T
import qualified Data.Text as T import Data.Time.Clock
import qualified Data.Vector as V import qualified Data.Vector as V
import System.IO.Temp import System.IO.Temp
testDatabase :: TestTree testDatabase :: TestTree
testDatabase = testGroup "Database tests" [ testOpenClose testDatabase = testGroup "Database tests" [
testOpenClose
, testOpenCloseTwice
, testPutGet , testPutGet
, testGetReturnsSorted ] , testGetReturnsSorted ]
testOpenClose :: TestTree testOpenClose :: TestTree
testOpenClose = testCase "Open/Close" $ testOpenClose = testCase "Open/Close" $
withSystemTempDirectory "test" $ \fp -> do withSystemTempDirectory "test" $ \fp -> do
let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty
db <- initDatabase dbConfig db <- initDatabase dbConfig
closeDatabase db closeDatabase db
testOpenCloseTwice :: TestTree
testOpenCloseTwice = testCase "Open/Close twice" $
withSystemTempDirectory "test" $ \fp -> do
let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty
db <- initDatabase dbConfig
closeDatabase db
db2 <- initDatabase dbConfig
closeDatabase db2
bar :: UTCTime -> Price -> Price -> Price -> Price -> Integer -> Bar bar :: UTCTime -> Price -> Price -> Price -> Price -> Integer -> Bar
bar dt o h l c v = Bar { barSecurity = "FOO", bar dt o h l c v = Bar { barSecurity = "FOO",
barTimestamp = dt, barTimestamp = dt,
barOpen = o, barOpen = o,
barHigh = h, barHigh = h,
@ -39,9 +50,9 @@ bar dt o h l c v = Bar { barSecurity = "FOO",
barVolume = v } barVolume = v }
testPutGet :: TestTree testPutGet :: TestTree
testPutGet = testCase "Put/Get" $ testPutGet = testCase "Put/Get" $
withSystemTempDirectory "test" $ \fp -> do withSystemTempDirectory "test" $ \fp -> do
let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty
bracket (initDatabase dbConfig) closeDatabase $ \db -> do bracket (initDatabase dbConfig) closeDatabase $ \db -> do
putData db "FOO" interval (timeframeMinute 1) bars putData db "FOO" interval (timeframeMinute 1) bars
retrievedBars <- (snd . head) <$> getData db "FOO" interval (timeframeMinute 1) retrievedBars <- (snd . head) <$> getData db "FOO" interval (timeframeMinute 1)
@ -56,9 +67,9 @@ testPutGet = testCase "Put/Get" $
] ]
testGetReturnsSorted :: TestTree testGetReturnsSorted :: TestTree
testGetReturnsSorted = testCase "Get returns sorted vector" $ testGetReturnsSorted = testCase "Get returns sorted vector" $
withSystemTempDirectory "test" $ \fp -> do withSystemTempDirectory "test" $ \fp -> do
let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty
bracket (initDatabase dbConfig) closeDatabase $ \db -> do bracket (initDatabase dbConfig) closeDatabase $ \db -> do
putData db "FOO" interval (timeframeMinute 1) bars putData db "FOO" interval (timeframeMinute 1) bars
retrievedBars <- (snd . head) <$> getData db "FOO" interval (timeframeMinute 1) retrievedBars <- (snd . head) <$> getData db "FOO" interval (timeframeMinute 1)

Loading…
Cancel
Save