{-# LANGUAGE OverloadedStrings #-}

module FoundationDB.Layer.Directory.Internal.Node where

import Data.ByteString (ByteString)
import Data.Text (Text)
import FoundationDB
import FoundationDB.Layer.Directory.Internal.Error
import FoundationDB.Layer.Subspace
import FoundationDB.Layer.Tuple

-- | Represents the result of searching for a node with 'find'.
data FoundNode = FoundNode
  { FoundNode -> Subspace
nodeNodeSS :: Subspace,
    FoundNode -> [Text]
nodePath :: [Text],
    FoundNode -> [Text]
targetPath :: [Text]
  }
  deriving (Int -> FoundNode -> ShowS
[FoundNode] -> ShowS
FoundNode -> String
(Int -> FoundNode -> ShowS)
-> (FoundNode -> String)
-> ([FoundNode] -> ShowS)
-> Show FoundNode
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [FoundNode] -> ShowS
$cshowList :: [FoundNode] -> ShowS
show :: FoundNode -> String
$cshow :: FoundNode -> String
showsPrec :: Int -> FoundNode -> ShowS
$cshowsPrec :: Int -> FoundNode -> ShowS
Show, FoundNode -> FoundNode -> Bool
(FoundNode -> FoundNode -> Bool)
-> (FoundNode -> FoundNode -> Bool) -> Eq FoundNode
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: FoundNode -> FoundNode -> Bool
$c/= :: FoundNode -> FoundNode -> Bool
== :: FoundNode -> FoundNode -> Bool
$c== :: FoundNode -> FoundNode -> Bool
Eq, Eq FoundNode
Eq FoundNode
-> (FoundNode -> FoundNode -> Ordering)
-> (FoundNode -> FoundNode -> Bool)
-> (FoundNode -> FoundNode -> Bool)
-> (FoundNode -> FoundNode -> Bool)
-> (FoundNode -> FoundNode -> Bool)
-> (FoundNode -> FoundNode -> FoundNode)
-> (FoundNode -> FoundNode -> FoundNode)
-> Ord FoundNode
FoundNode -> FoundNode -> Bool
FoundNode -> FoundNode -> Ordering
FoundNode -> FoundNode -> FoundNode
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 :: FoundNode -> FoundNode -> FoundNode
$cmin :: FoundNode -> FoundNode -> FoundNode
max :: FoundNode -> FoundNode -> FoundNode
$cmax :: FoundNode -> FoundNode -> FoundNode
>= :: FoundNode -> FoundNode -> Bool
$c>= :: FoundNode -> FoundNode -> Bool
> :: FoundNode -> FoundNode -> Bool
$c> :: FoundNode -> FoundNode -> Bool
<= :: FoundNode -> FoundNode -> Bool
$c<= :: FoundNode -> FoundNode -> Bool
< :: FoundNode -> FoundNode -> Bool
$c< :: FoundNode -> FoundNode -> Bool
compare :: FoundNode -> FoundNode -> Ordering
$ccompare :: FoundNode -> FoundNode -> Ordering
$cp1Ord :: Eq FoundNode
Ord)

getFoundNodeLayer :: FoundNode -> Transaction ByteString
getFoundNodeLayer :: FoundNode -> Transaction ByteString
getFoundNodeLayer n :: FoundNode
n@(FoundNode Subspace
ss [Text]
_ [Text]
_) = do
  Maybe ByteString
fv <- ByteString -> Transaction (Future (Maybe ByteString))
get (Subspace -> [Elem] -> ByteString
pack Subspace
ss [ByteString -> Elem
Bytes ByteString
"layer"]) Transaction (Future (Maybe ByteString))
-> (Future (Maybe ByteString) -> Transaction (Maybe ByteString))
-> Transaction (Maybe ByteString)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Future (Maybe ByteString) -> Transaction (Maybe ByteString)
forall a. Future a -> Transaction a
await
  case Maybe ByteString
fv of
    Maybe ByteString
Nothing ->
      String -> Transaction ByteString
forall a. String -> Transaction a
throwDirInternalError (String -> Transaction ByteString)
-> String -> Transaction ByteString
forall a b. (a -> b) -> a -> b
$
        String
"Failed to get node layer for node " String -> ShowS
forall a. [a] -> [a] -> [a]
++ FoundNode -> String
forall a. Show a => a -> String
show FoundNode
n
    Just ByteString
l -> ByteString -> Transaction ByteString
forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
l