module Database.Haskey.Alloc.Concurrent.Environment where
import Control.Applicative ((<$>))
import Control.Monad.State
import Data.Binary (Binary)
import Data.Set (Set)
import Data.Typeable (Typeable)
import Data.Word (Word32)
import qualified Data.Binary as B
import qualified Data.Set as S
import STMContainers.Map (Map)
import Data.BTree.Primitives
import Database.Haskey.Alloc.Concurrent.FreePages.Tree
data StateType = TypeData
| TypeIndex
data S (t :: StateType) a where
DataState :: a -> S 'TypeData a
IndexState :: a -> S 'TypeIndex a
deriving (Typeable)
deriving instance Show a => Show (S t a)
instance Binary a => Binary (S 'TypeData a) where
put (DataState a) = B.put a
get = DataState <$> B.get
instance Binary a => Binary (S 'TypeIndex a) where
put (IndexState a) = B.put a
get = IndexState <$> B.get
instance Functor (S t) where
f `fmap` (DataState v) = DataState (f v)
f `fmap` (IndexState v) = IndexState (f v)
getSValue :: S t a -> a
getSValue (DataState a) = a
getSValue (IndexState a) = a
newtype ReaderEnv hnds = ReaderEnv { readerHnds :: hnds }
data FileState stateType = FileState {
fileStateNewlyFreedPages :: ![NewlyFreed]
, fileStateOriginalNumPages :: !(S stateType PageId)
, fileStateNewNumPages :: !(S stateType PageId)
, fileStateDirtyPages :: !(Set PageId)
, fileStateFreeTree :: !(S stateType FreeTree)
, fileStateCachedFreePages :: !(S stateType [FreePage])
}
data WriterEnv hnds = WriterEnv
{ writerHnds :: !hnds
, writerTxId :: !TxId
, writerReaders :: Map TxId Integer
, writerIndexFileState :: FileState 'TypeIndex
, writerDataFileState :: FileState 'TypeData
, writerQueryFreeTreeOn :: !Bool
, writerDirtyOverflows :: !(Set DirtyOverflow)
, writerOverflowCounter :: !Word32
, writerRemovedOverflows :: ![OldOverflow]
}
newWriter :: hnd -> TxId -> Map TxId Integer
-> S 'TypeData PageId -> S 'TypeIndex PageId
-> S 'TypeData [FreePage] -> S 'TypeIndex [FreePage]
-> S 'TypeData FreeTree -> S 'TypeIndex FreeTree
-> WriterEnv hnd
newWriter hnd tx readers
numDataPages numIndexPages
dataFreePages indexFreePages
dataFreeTree indexFreeTree =
WriterEnv {
writerHnds = hnd
, writerTxId = tx
, writerReaders = readers
, writerIndexFileState = newFileState numIndexPages indexFreePages indexFreeTree
, writerDataFileState = newFileState numDataPages dataFreePages dataFreeTree
, writerQueryFreeTreeOn = True
, writerDirtyOverflows = S.empty
, writerOverflowCounter = 0
, writerRemovedOverflows = []
}
where
newFileState numPages freePages freeTree = FileState {
fileStateNewlyFreedPages = []
, fileStateOriginalNumPages = numPages
, fileStateNewNumPages = numPages
, fileStateDirtyPages = S.empty
, fileStateCachedFreePages = freePages
, fileStateFreeTree = freeTree
}
newtype NewlyFreed = NewlyFreed PageId deriving (Eq, Ord, Show)
newtype FreePage = FreePage PageId deriving (Binary, Eq, Ord, Show)
newtype Dirty = Dirty PageId deriving (Eq, Ord, Show)
freePage :: (Functor m, MonadState (WriterEnv hnd) m) => S stateType PageId -> m ()
freePage pid@(DataState pid') = do
dirty' <- dirty pid
modify' $ \e ->
e { writerDataFileState =
updateFileState (writerDataFileState e) DataState
dirty' pid'
}
freePage pid@(IndexState pid') = do
dirty' <- dirty pid
modify' $ \e ->
e { writerIndexFileState =
updateFileState (writerIndexFileState e) IndexState
dirty' pid'
}
updateFileState :: FileState t
-> (forall a. a -> S t a)
-> Maybe Dirty
-> PageId
-> FileState t
updateFileState e cons dirty' pid' =
if | Just (Dirty p) <- dirty' ->
e { fileStateCachedFreePages =
cons $ FreePage p : getSValue (fileStateCachedFreePages e) }
| p <- pid' ->
e { fileStateNewlyFreedPages =
NewlyFreed p : fileStateNewlyFreedPages e }
dirty :: (Functor m, MonadState (WriterEnv hnd) m) => S stateType PageId -> m (Maybe Dirty)
dirty pid = case pid of
DataState p -> (page p . fileStateDirtyPages . writerDataFileState) <$> get
IndexState p -> (page p . fileStateDirtyPages . writerIndexFileState) <$> get
where
page p dirtyPages
| p `S.member` dirtyPages = Just (Dirty p)
| otherwise = Nothing
touchPage :: MonadState (WriterEnv hnd) m => S stateType PageId -> m ()
touchPage (DataState pid) = do
modify' $ \e ->
let dirtyPages = fileStateDirtyPages (writerDataFileState e) in
e { writerDataFileState = (writerDataFileState e) {
fileStateDirtyPages = S.insert pid dirtyPages }
}
modify' $ \e ->
let oldNum = getSValue $ fileStateNewNumPages (writerDataFileState e)
newNum = max oldNum (pid + 1)
in e { writerDataFileState = (writerDataFileState e) {
fileStateNewNumPages = DataState newNum }
}
touchPage (IndexState pid) = do
modify' $ \e ->
let dirtyPages = fileStateDirtyPages (writerIndexFileState e) in
e { writerIndexFileState = (writerIndexFileState e) {
fileStateDirtyPages = S.insert pid dirtyPages }
}
modify' $ \e ->
let oldNum = getSValue $ fileStateNewNumPages (writerIndexFileState e)
newNum = max oldNum (pid + 1)
in e { writerIndexFileState = (writerIndexFileState e) {
fileStateNewNumPages = IndexState newNum }
}
newtype DirtyOverflow = DirtyOverflow OverflowId deriving (Eq, Ord, Show)
newtype OldOverflow = OldOverflow OverflowId deriving (Eq, Ord, Show)
touchOverflow :: MonadState (WriterEnv hnd) m => OverflowId -> m ()
touchOverflow i = modify' $
\e -> e { writerDirtyOverflows =
S.insert (DirtyOverflow i) (writerDirtyOverflows e) }
overflowType :: MonadState (WriterEnv hnd) m => OverflowId -> m (Either DirtyOverflow OldOverflow)
overflowType i = do
dirty' <- gets $ \e -> S.member (DirtyOverflow i) (writerDirtyOverflows e)
if dirty' then return $ Left (DirtyOverflow i)
else return $ Right (OldOverflow i)
removeOldOverflow :: MonadState (WriterEnv hdn) m => OldOverflow -> m ()
removeOldOverflow i =
modify' $ \e -> e { writerRemovedOverflows = i : writerRemovedOverflows e }