module FoundationDB.Layer.Subspace
(
Subspace (..),
subspace,
prefixedSubspace,
extend,
pack,
unpack,
contains,
subspaceRangeQuery,
getLast,
subspaceKey,
)
where
import Data.ByteString (ByteString)
import qualified Data.ByteString as BS
import Data.Sequence (Seq (Empty, (:<|)))
import FoundationDB
import FoundationDB.Layer.Tuple
newtype Subspace = Subspace {Subspace -> ByteString
rawPrefix :: ByteString}
deriving (Int -> Subspace -> ShowS
[Subspace] -> ShowS
Subspace -> String
(Int -> Subspace -> ShowS)
-> (Subspace -> String) -> ([Subspace] -> ShowS) -> Show Subspace
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Subspace] -> ShowS
$cshowList :: [Subspace] -> ShowS
show :: Subspace -> String
$cshow :: Subspace -> String
showsPrec :: Int -> Subspace -> ShowS
$cshowsPrec :: Int -> Subspace -> ShowS
Show, Subspace -> Subspace -> Bool
(Subspace -> Subspace -> Bool)
-> (Subspace -> Subspace -> Bool) -> Eq Subspace
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Subspace -> Subspace -> Bool
$c/= :: Subspace -> Subspace -> Bool
== :: Subspace -> Subspace -> Bool
$c== :: Subspace -> Subspace -> Bool
Eq, Eq Subspace
Eq Subspace
-> (Subspace -> Subspace -> Ordering)
-> (Subspace -> Subspace -> Bool)
-> (Subspace -> Subspace -> Bool)
-> (Subspace -> Subspace -> Bool)
-> (Subspace -> Subspace -> Bool)
-> (Subspace -> Subspace -> Subspace)
-> (Subspace -> Subspace -> Subspace)
-> Ord Subspace
Subspace -> Subspace -> Bool
Subspace -> Subspace -> Ordering
Subspace -> Subspace -> Subspace
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Subspace -> Subspace -> Subspace
$cmin :: Subspace -> Subspace -> Subspace
max :: Subspace -> Subspace -> Subspace
$cmax :: Subspace -> Subspace -> Subspace
>= :: Subspace -> Subspace -> Bool
$c>= :: Subspace -> Subspace -> Bool
> :: Subspace -> Subspace -> Bool
$c> :: Subspace -> Subspace -> Bool
<= :: Subspace -> Subspace -> Bool
$c<= :: Subspace -> Subspace -> Bool
< :: Subspace -> Subspace -> Bool
$c< :: Subspace -> Subspace -> Bool
compare :: Subspace -> Subspace -> Ordering
$ccompare :: Subspace -> Subspace -> Ordering
$cp1Ord :: Eq Subspace
Ord)
subspace ::
[Elem] ->
Subspace
subspace :: [Elem] -> Subspace
subspace [Elem]
es = ByteString -> Subspace
Subspace ([Elem] -> ByteString
forall (t :: * -> *). Traversable t => t Elem -> ByteString
encodeTupleElems [Elem]
es)
prefixedSubspace ::
ByteString ->
[Elem] ->
Subspace
prefixedSubspace :: ByteString -> [Elem] -> Subspace
prefixedSubspace ByteString
prefix [Elem]
tuple = ByteString -> Subspace
Subspace (ByteString -> [Elem] -> ByteString
forall (t :: * -> *).
Traversable t =>
ByteString -> t Elem -> ByteString
encodeTupleElemsWPrefix ByteString
prefix [Elem]
tuple)
subspaceKey :: Subspace -> ByteString
subspaceKey :: Subspace -> ByteString
subspaceKey = Subspace -> ByteString
rawPrefix
extend ::
Subspace ->
[Elem] ->
Subspace
extend :: Subspace -> [Elem] -> Subspace
extend (Subspace ByteString
prfx) =
ByteString -> [Elem] -> Subspace
prefixedSubspace ByteString
prfx
pack :: Subspace -> [Elem] -> ByteString
pack :: Subspace -> [Elem] -> ByteString
pack Subspace
sub = ByteString -> [Elem] -> ByteString
forall (t :: * -> *).
Traversable t =>
ByteString -> t Elem -> ByteString
encodeTupleElemsWPrefix (Subspace -> ByteString
rawPrefix Subspace
sub)
unpack :: Subspace -> ByteString -> Either String [Elem]
unpack :: Subspace -> ByteString -> Either String [Elem]
unpack Subspace
sub = ByteString -> ByteString -> Either String [Elem]
decodeTupleElemsWPrefix (Subspace -> ByteString
rawPrefix Subspace
sub)
contains ::
Subspace ->
ByteString ->
Bool
contains :: Subspace -> ByteString -> Bool
contains Subspace
sub = ByteString -> ByteString -> Bool
BS.isPrefixOf (Subspace -> ByteString
rawPrefix Subspace
sub)
subspaceRangeQuery :: Subspace -> RangeQuery
subspaceRangeQuery :: Subspace -> RangeQuery
subspaceRangeQuery Subspace
s =
RangeQuery :: KeySelector -> KeySelector -> Maybe Int -> Bool -> RangeQuery
RangeQuery
{ rangeBegin :: KeySelector
rangeBegin = ByteString -> KeySelector
FirstGreaterOrEq (ByteString
k ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> [Word8] -> ByteString
BS.pack [Word8
0x00]),
rangeEnd :: KeySelector
rangeEnd = ByteString -> KeySelector
FirstGreaterOrEq (ByteString
k ByteString -> ByteString -> ByteString
forall a. Semigroup a => a -> a -> a
<> [Word8] -> ByteString
BS.pack [Word8
0xff]),
rangeLimit :: Maybe Int
rangeLimit = Maybe Int
forall a. Maybe a
Nothing,
rangeReverse :: Bool
rangeReverse = Bool
False
}
where
k :: ByteString
k = Subspace -> [Elem] -> ByteString
pack Subspace
s []
getLast :: Subspace -> Transaction (Maybe (ByteString, ByteString))
getLast :: Subspace -> Transaction (Maybe (ByteString, ByteString))
getLast Subspace
sub = do
Future RangeResult
rr <-
RangeQuery -> Transaction (Future RangeResult)
getRange
(Subspace -> RangeQuery
subspaceRangeQuery Subspace
sub)
{ rangeLimit :: Maybe Int
rangeLimit = Int -> Maybe Int
forall a. a -> Maybe a
Just Int
1,
rangeReverse :: Bool
rangeReverse = Bool
True
}
RangeResult
kvs <- Future RangeResult -> Transaction RangeResult
forall a. Future a -> Transaction a
await Future RangeResult
rr
case RangeResult
kvs of
RangeDone Seq (ByteString, ByteString)
Empty -> Maybe (ByteString, ByteString)
-> Transaction (Maybe (ByteString, ByteString))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (ByteString, ByteString)
forall a. Maybe a
Nothing
RangeDone ((ByteString, ByteString)
kv :<| Seq (ByteString, ByteString)
_) -> Maybe (ByteString, ByteString)
-> Transaction (Maybe (ByteString, ByteString))
forall (m :: * -> *) a. Monad m => a -> m a
return ((ByteString, ByteString) -> Maybe (ByteString, ByteString)
forall a. a -> Maybe a
Just (ByteString, ByteString)
kv)
RangeMore ((ByteString, ByteString)
kv :<| Seq (ByteString, ByteString)
_) Future RangeResult
_ -> Maybe (ByteString, ByteString)
-> Transaction (Maybe (ByteString, ByteString))
forall (m :: * -> *) a. Monad m => a -> m a
return ((ByteString, ByteString) -> Maybe (ByteString, ByteString)
forall a. a -> Maybe a
Just (ByteString, ByteString)
kv)
RangeMore Seq (ByteString, ByteString)
Empty Future RangeResult
_ -> Maybe (ByteString, ByteString)
-> Transaction (Maybe (ByteString, ByteString))
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe (ByteString, ByteString)
forall a. Maybe a
Nothing