{-# OPTIONS_HADDOCK not-home #-}
module Database.LSMTree.Internal.Index
(
IndexType (Compact, Ordinary),
indexToIndexType,
Index (CompactIndex, OrdinaryIndex),
search,
sizeInPages,
headerLBS,
finalLBS,
fromSBS,
IndexAcc (CompactIndexAcc, OrdinaryIndexAcc),
newWithDefaults,
appendSingle,
appendMulti,
unsafeEnd
)
where
import Control.Arrow (second)
import Control.DeepSeq (NFData (rnf))
import Control.Monad.ST.Strict (ST)
import Data.ByteString.Lazy (LazyByteString)
import Data.ByteString.Short (ShortByteString)
import Data.Word (Word32)
import Database.LSMTree.Internal.Chunk (Chunk)
import Database.LSMTree.Internal.Entry (NumEntries)
import Database.LSMTree.Internal.Index.Compact (IndexCompact)
import qualified Database.LSMTree.Internal.Index.Compact as Compact (finalLBS,
fromSBS, headerLBS, search, sizeInPages)
import Database.LSMTree.Internal.Index.CompactAcc (IndexCompactAcc)
import qualified Database.LSMTree.Internal.Index.CompactAcc as Compact
(appendMulti, appendSingle, newWithDefaults, unsafeEnd)
import Database.LSMTree.Internal.Index.Ordinary (IndexOrdinary)
import qualified Database.LSMTree.Internal.Index.Ordinary as Ordinary (finalLBS,
fromSBS, headerLBS, search, sizeInPages)
import Database.LSMTree.Internal.Index.OrdinaryAcc (IndexOrdinaryAcc)
import qualified Database.LSMTree.Internal.Index.OrdinaryAcc as Ordinary
(appendMulti, appendSingle, newWithDefaults, unsafeEnd)
import Database.LSMTree.Internal.Page (NumPages, PageSpan)
import Database.LSMTree.Internal.Serialise (SerialisedKey)
data IndexType = Compact | Ordinary
deriving stock (IndexType -> IndexType -> Bool
(IndexType -> IndexType -> Bool)
-> (IndexType -> IndexType -> Bool) -> Eq IndexType
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: IndexType -> IndexType -> Bool
== :: IndexType -> IndexType -> Bool
$c/= :: IndexType -> IndexType -> Bool
/= :: IndexType -> IndexType -> Bool
Eq, Int -> IndexType -> ShowS
[IndexType] -> ShowS
IndexType -> String
(Int -> IndexType -> ShowS)
-> (IndexType -> String)
-> ([IndexType] -> ShowS)
-> Show IndexType
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> IndexType -> ShowS
showsPrec :: Int -> IndexType -> ShowS
$cshow :: IndexType -> String
show :: IndexType -> String
$cshowList :: [IndexType] -> ShowS
showList :: [IndexType] -> ShowS
Show)
instance NFData IndexType where
rnf :: IndexType -> ()
rnf IndexType
Compact = ()
rnf IndexType
Ordinary = ()
data Index
= CompactIndex !IndexCompact
| OrdinaryIndex !IndexOrdinary
deriving stock (Index -> Index -> Bool
(Index -> Index -> Bool) -> (Index -> Index -> Bool) -> Eq Index
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Index -> Index -> Bool
== :: Index -> Index -> Bool
$c/= :: Index -> Index -> Bool
/= :: Index -> Index -> Bool
Eq, Int -> Index -> ShowS
[Index] -> ShowS
Index -> String
(Int -> Index -> ShowS)
-> (Index -> String) -> ([Index] -> ShowS) -> Show Index
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Index -> ShowS
showsPrec :: Int -> Index -> ShowS
$cshow :: Index -> String
show :: Index -> String
$cshowList :: [Index] -> ShowS
showList :: [Index] -> ShowS
Show)
instance NFData Index where
rnf :: Index -> ()
rnf (CompactIndex IndexCompact
index) = IndexCompact -> ()
forall a. NFData a => a -> ()
rnf IndexCompact
index
rnf (OrdinaryIndex IndexOrdinary
index) = IndexOrdinary -> ()
forall a. NFData a => a -> ()
rnf IndexOrdinary
index
indexToIndexType :: Index -> IndexType
indexToIndexType :: Index -> IndexType
indexToIndexType CompactIndex{} = IndexType
Compact
indexToIndexType OrdinaryIndex{} = IndexType
Ordinary
search :: SerialisedKey -> Index -> PageSpan
search :: SerialisedKey -> Index -> PageSpan
search SerialisedKey
key (CompactIndex IndexCompact
index) = SerialisedKey -> IndexCompact -> PageSpan
Compact.search SerialisedKey
key IndexCompact
index
search SerialisedKey
key (OrdinaryIndex IndexOrdinary
index) = SerialisedKey -> IndexOrdinary -> PageSpan
Ordinary.search SerialisedKey
key IndexOrdinary
index
sizeInPages :: Index -> NumPages
sizeInPages :: Index -> NumPages
sizeInPages (CompactIndex IndexCompact
index) = IndexCompact -> NumPages
Compact.sizeInPages IndexCompact
index
sizeInPages (OrdinaryIndex IndexOrdinary
index) = IndexOrdinary -> NumPages
Ordinary.sizeInPages IndexOrdinary
index
headerLBS :: IndexType -> LazyByteString
IndexType
Compact = LazyByteString
Compact.headerLBS
headerLBS IndexType
Ordinary = LazyByteString
Ordinary.headerLBS
finalLBS :: NumEntries -> Index -> LazyByteString
finalLBS :: NumEntries -> Index -> LazyByteString
finalLBS NumEntries
entryCount (CompactIndex IndexCompact
index) = NumEntries -> IndexCompact -> LazyByteString
Compact.finalLBS NumEntries
entryCount IndexCompact
index
finalLBS NumEntries
entryCount (OrdinaryIndex IndexOrdinary
index) = NumEntries -> IndexOrdinary -> LazyByteString
Ordinary.finalLBS NumEntries
entryCount IndexOrdinary
index
fromSBS :: IndexType -> ShortByteString -> Either String (NumEntries, Index)
fromSBS :: IndexType -> ShortByteString -> Either String (NumEntries, Index)
fromSBS IndexType
Compact ShortByteString
input = (IndexCompact -> Index)
-> (NumEntries, IndexCompact) -> (NumEntries, Index)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second IndexCompact -> Index
CompactIndex ((NumEntries, IndexCompact) -> (NumEntries, Index))
-> Either String (NumEntries, IndexCompact)
-> Either String (NumEntries, Index)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShortByteString -> Either String (NumEntries, IndexCompact)
Compact.fromSBS ShortByteString
input
fromSBS IndexType
Ordinary ShortByteString
input = (IndexOrdinary -> Index)
-> (NumEntries, IndexOrdinary) -> (NumEntries, Index)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second IndexOrdinary -> Index
OrdinaryIndex ((NumEntries, IndexOrdinary) -> (NumEntries, Index))
-> Either String (NumEntries, IndexOrdinary)
-> Either String (NumEntries, Index)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ShortByteString -> Either String (NumEntries, IndexOrdinary)
Ordinary.fromSBS ShortByteString
input
data IndexAcc s = CompactIndexAcc !(IndexCompactAcc s)
| OrdinaryIndexAcc !(IndexOrdinaryAcc s)
newWithDefaults :: IndexType -> ST s (IndexAcc s)
newWithDefaults :: forall s. IndexType -> ST s (IndexAcc s)
newWithDefaults IndexType
Compact = IndexCompactAcc s -> IndexAcc s
forall s. IndexCompactAcc s -> IndexAcc s
CompactIndexAcc (IndexCompactAcc s -> IndexAcc s)
-> ST s (IndexCompactAcc s) -> ST s (IndexAcc s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ST s (IndexCompactAcc s)
forall s. ST s (IndexCompactAcc s)
Compact.newWithDefaults
newWithDefaults IndexType
Ordinary = IndexOrdinaryAcc s -> IndexAcc s
forall s. IndexOrdinaryAcc s -> IndexAcc s
OrdinaryIndexAcc (IndexOrdinaryAcc s -> IndexAcc s)
-> ST s (IndexOrdinaryAcc s) -> ST s (IndexAcc s)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ST s (IndexOrdinaryAcc s)
forall s. ST s (IndexOrdinaryAcc s)
Ordinary.newWithDefaults
appendSingle :: (SerialisedKey, SerialisedKey)
-> IndexAcc s
-> ST s (Maybe Chunk)
appendSingle :: forall s.
(SerialisedKey, SerialisedKey) -> IndexAcc s -> ST s (Maybe Chunk)
appendSingle (SerialisedKey, SerialisedKey)
pageInfo (CompactIndexAcc IndexCompactAcc s
indexAcc) = (SerialisedKey, SerialisedKey)
-> IndexCompactAcc s -> ST s (Maybe Chunk)
forall s.
(SerialisedKey, SerialisedKey)
-> IndexCompactAcc s -> ST s (Maybe Chunk)
Compact.appendSingle
(SerialisedKey, SerialisedKey)
pageInfo
IndexCompactAcc s
indexAcc
appendSingle (SerialisedKey, SerialisedKey)
pageInfo (OrdinaryIndexAcc IndexOrdinaryAcc s
indexAcc) = (SerialisedKey, SerialisedKey)
-> IndexOrdinaryAcc s -> ST s (Maybe Chunk)
forall s.
(SerialisedKey, SerialisedKey)
-> IndexOrdinaryAcc s -> ST s (Maybe Chunk)
Ordinary.appendSingle
(SerialisedKey, SerialisedKey)
pageInfo
IndexOrdinaryAcc s
indexAcc
appendMulti :: (SerialisedKey, Word32) -> IndexAcc s -> ST s [Chunk]
appendMulti :: forall s. (SerialisedKey, Word32) -> IndexAcc s -> ST s [Chunk]
appendMulti (SerialisedKey, Word32)
pagesInfo (CompactIndexAcc IndexCompactAcc s
indexAcc) = (SerialisedKey, Word32) -> IndexCompactAcc s -> ST s [Chunk]
forall s.
(SerialisedKey, Word32) -> IndexCompactAcc s -> ST s [Chunk]
Compact.appendMulti
(SerialisedKey, Word32)
pagesInfo
IndexCompactAcc s
indexAcc
appendMulti (SerialisedKey, Word32)
pagesInfo (OrdinaryIndexAcc IndexOrdinaryAcc s
indexAcc) = (SerialisedKey, Word32) -> IndexOrdinaryAcc s -> ST s [Chunk]
forall s.
(SerialisedKey, Word32) -> IndexOrdinaryAcc s -> ST s [Chunk]
Ordinary.appendMulti
(SerialisedKey, Word32)
pagesInfo
IndexOrdinaryAcc s
indexAcc
unsafeEnd :: IndexAcc s -> ST s (Maybe Chunk, Index)
unsafeEnd :: forall s. IndexAcc s -> ST s (Maybe Chunk, Index)
unsafeEnd (CompactIndexAcc IndexCompactAcc s
indexAcc) = (IndexCompact -> Index)
-> (Maybe Chunk, IndexCompact) -> (Maybe Chunk, Index)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second IndexCompact -> Index
CompactIndex ((Maybe Chunk, IndexCompact) -> (Maybe Chunk, Index))
-> ST s (Maybe Chunk, IndexCompact) -> ST s (Maybe Chunk, Index)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
IndexCompactAcc s -> ST s (Maybe Chunk, IndexCompact)
forall s. IndexCompactAcc s -> ST s (Maybe Chunk, IndexCompact)
Compact.unsafeEnd IndexCompactAcc s
indexAcc
unsafeEnd (OrdinaryIndexAcc IndexOrdinaryAcc s
indexAcc) = (IndexOrdinary -> Index)
-> (Maybe Chunk, IndexOrdinary) -> (Maybe Chunk, Index)
forall b c d. (b -> c) -> (d, b) -> (d, c)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (d, b) (d, c)
second IndexOrdinary -> Index
OrdinaryIndex ((Maybe Chunk, IndexOrdinary) -> (Maybe Chunk, Index))
-> ST s (Maybe Chunk, IndexOrdinary) -> ST s (Maybe Chunk, Index)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
IndexOrdinaryAcc s -> ST s (Maybe Chunk, IndexOrdinary)
forall s. IndexOrdinaryAcc s -> ST s (Maybe Chunk, IndexOrdinary)
Ordinary.unsafeEnd IndexOrdinaryAcc s
indexAcc