added my database experiments

This commit is contained in:
Joey Hess 2014-03-20 13:20:25 -04:00
parent a5411590df
commit f4bb822e54
3 changed files with 130 additions and 0 deletions

20
Footype.hs Normal file
View file

@ -0,0 +1,20 @@
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell,
OverloadedStrings, GADTs, FlexibleContexts #-}
module Footype where
import Database.Persist hiding (Key)
import Database.Persist.TH
import Database.Persist.Sqlite hiding (Key)
import Control.Monad.IO.Class (liftIO)
import Control.Monad
import Data.Time.Clock
import Types.Key
import Types.UUID
import Types.MetaData
-- has to be in a separate file from foo.hs for silly reasons
derivePersistField "Key"
derivePersistField "UUID"
derivePersistField "MetaField"

74
foo.hs Normal file
View file

@ -0,0 +1,74 @@
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell,
OverloadedStrings, GADTs, FlexibleContexts #-}
import Database.Persist hiding (Key)
import Database.Persist.TH
import Database.Persist.Sqlite hiding (Key)
import Control.Monad.IO.Class (liftIO)
import Control.Monad
import Data.Time.Clock
import Data.Maybe
import Types.Key
import Types.UUID
import Types.MetaData
import Footype
data RemoteFsckTime
share [mkPersist sqlSettings, mkSave "entityDefs", mkMigrate "migrateAll"] [persistLowerCase|
CachedKey
key Key
KeyOutdex key
deriving Show
AssociatedFiles
keyId CachedKeyId Eq
associatedFile FilePath
KeyIdOutdex keyId associatedFile
deriving Show
CachedMetaField
field MetaField
FieldOutdex field
deriving Show
CachedMetaData
keyId CachedKeyId Eq
fieldId CachedMetaFieldId Eq
metaValue String
deriving Show
LastFscked
keyId CachedKeyId Eq
localFscked Int Maybe
|]
main :: IO ()
main = query
query :: IO ()
query = runSqlite "foo.db" $ do
forM_ [1..1000] $ \i -> do
Just k <- getBy $ KeyOutdex (fromJust $ file2key $ "WORM--" ++ show i)
selectList [AssociatedFilesKeyId ==. entityKey k] []
query2 :: IO ()
query2 = runSqlite "foo.db" $ do
forM_ [1..1] $ \i -> do
Just f <- getBy $ FieldOutdex (fromJust $ toMetaField "tag")
liftIO $ print f
fs <- selectList [CachedMetaDataFieldId ==. entityKey f] []
liftIO $ print $ length fs
populate :: IO ()
populate = runSqlite "foo.db" $ do
runMigration migrateAll
t <- insert $ CachedMetaField (fromJust $ toMetaField "tag")
f <- insert $ CachedMetaField (fromJust $ toMetaField "foo")
forM_ [1..30000] $ \i -> do
k <- insert $ CachedKey (fromJust $ file2key $ "WORM--" ++ show i)
liftIO $ print k
insert $ AssociatedFiles k (show i)
insert $ AssociatedFiles k ("and" ++ show (i + 1))
insert $ CachedMetaData k f (show i)
insert $ CachedMetaData k t "bar"

36
fooes.hs Normal file
View file

@ -0,0 +1,36 @@
{-# LANGUAGE QuasiQuotes, TypeFamilies, GeneralizedNewtypeDeriving, TemplateHaskell,
OverloadedStrings, GADTs, FlexibleContexts #-}
import Database.Persist.TH
import Database.Persist.Sqlite (runSqlite)
import Control.Monad.IO.Class (liftIO)
import Control.Monad
import Database.Esqueleto hiding (Key)
share [mkPersist sqlSettings, mkSave "entityDefs", mkMigrate "migrateAll"] [persistLowerCase|
CachedKey
key String
UniqueKey key
deriving Show
AssociatedFiles
key CachedKeyId Eq
file FilePath
UniqueKeyFile key file
deriving Show
|]
main :: IO ()
main = runSqlite "foo.db" $ do
runMigration migrateAll
forM_ [1..30000] $ \i -> do
k <- insert $ CachedKey (show i)
liftIO $ print k
insert $ AssociatedFiles k (show i)
[(k2)] <- select $ from $ \k -> do
where_ (k ^. CachedKeyKey ==. val (show i))
return (k ^. CachedKeyId)
liftIO $ print (2, k2)
delete $ from $ \f -> do
where_ (f ^. AssociatedFilesKey ==. k2)