module Ecluse.Core.Registry.Cargo.Document (
CargoDocument,
cargoDocument,
cargoRecords,
PackedIndex (..),
packCargoDocument,
packedIndexDocument,
packedIndexPlan,
packedIndexResident,
) where
import Data.Aeson (Value)
import Data.Primitive.SmallArray (smallArrayFromList)
import Ecluse.Core.Package.Entry (EntryKey)
import Ecluse.Core.Registry.Json.Packed (DocTable, LinePlan (..), Packed, Piece (..), docTable, packedResident, packedValue, tableResident)
import Ecluse.Core.Registry.Json.Writer (packTree)
import Ecluse.Core.Strict (strictElements)
newtype CargoDocument = CargoDocument {CargoDocument -> [(EntryKey, Value)]
cargoRecords :: [(EntryKey, Value)]}
deriving stock (CargoDocument -> CargoDocument -> Bool
(CargoDocument -> CargoDocument -> Bool)
-> (CargoDocument -> CargoDocument -> Bool) -> Eq CargoDocument
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CargoDocument -> CargoDocument -> Bool
== :: CargoDocument -> CargoDocument -> Bool
$c/= :: CargoDocument -> CargoDocument -> Bool
/= :: CargoDocument -> CargoDocument -> Bool
Eq, Int -> CargoDocument -> ShowS
[CargoDocument] -> ShowS
CargoDocument -> String
(Int -> CargoDocument -> ShowS)
-> (CargoDocument -> String)
-> ([CargoDocument] -> ShowS)
-> Show CargoDocument
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CargoDocument -> ShowS
showsPrec :: Int -> CargoDocument -> ShowS
$cshow :: CargoDocument -> String
show :: CargoDocument -> String
$cshowList :: [CargoDocument] -> ShowS
showList :: [CargoDocument] -> ShowS
Show)
cargoDocument :: [(EntryKey, Value)] -> CargoDocument
cargoDocument :: [(EntryKey, Value)] -> CargoDocument
cargoDocument = [(EntryKey, Value)] -> CargoDocument
CargoDocument ([(EntryKey, Value)] -> CargoDocument)
-> ([(EntryKey, Value)] -> [(EntryKey, Value)])
-> [(EntryKey, Value)]
-> CargoDocument
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(EntryKey, Value)] -> [(EntryKey, Value)]
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements
data PackedIndex = PackedIndex
{ PackedIndex -> DocTable
indexTable :: DocTable
, PackedIndex -> [(EntryKey, Packed)]
indexRecords :: [(EntryKey, Packed)]
}
deriving stock (PackedIndex -> PackedIndex -> Bool
(PackedIndex -> PackedIndex -> Bool)
-> (PackedIndex -> PackedIndex -> Bool) -> Eq PackedIndex
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PackedIndex -> PackedIndex -> Bool
== :: PackedIndex -> PackedIndex -> Bool
$c/= :: PackedIndex -> PackedIndex -> Bool
/= :: PackedIndex -> PackedIndex -> Bool
Eq, Int -> PackedIndex -> ShowS
[PackedIndex] -> ShowS
PackedIndex -> String
(Int -> PackedIndex -> ShowS)
-> (PackedIndex -> String)
-> ([PackedIndex] -> ShowS)
-> Show PackedIndex
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PackedIndex -> ShowS
showsPrec :: Int -> PackedIndex -> ShowS
$cshow :: PackedIndex -> String
show :: PackedIndex -> String
$cshowList :: [PackedIndex] -> ShowS
showList :: [PackedIndex] -> ShowS
Show)
packCargoDocument :: CargoDocument -> PackedIndex
packCargoDocument :: CargoDocument -> PackedIndex
packCargoDocument CargoDocument
document = DocTable -> [(EntryKey, Packed)] -> PackedIndex
PackedIndex (SmallArray Text -> DocTable
docTable ([Text] -> SmallArray Text
forall a. [a] -> SmallArray a
smallArrayFromList [])) ([(EntryKey, Packed)] -> [(EntryKey, Packed)]
forall (t :: * -> *) a. Foldable t => t a -> t a
strictElements [let !record :: Packed
record = [Text] -> Value -> Packed
packTree [] Value
value in (EntryKey
key, Packed
record) | (EntryKey
key, Value
value) <- CargoDocument -> [(EntryKey, Value)]
cargoRecords CargoDocument
document])
packedIndexDocument :: PackedIndex -> Maybe CargoDocument
packedIndexDocument :: PackedIndex -> Maybe CargoDocument
packedIndexDocument PackedIndex
document = [(EntryKey, Value)] -> CargoDocument
cargoDocument ([(EntryKey, Value)] -> CargoDocument)
-> Maybe [(EntryKey, Value)] -> Maybe CargoDocument
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ((EntryKey, Packed) -> Maybe (EntryKey, Value))
-> [(EntryKey, Packed)] -> Maybe [(EntryKey, Value)]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
forall (f :: * -> *) a b.
Applicative f =>
(a -> f b) -> [a] -> f [b]
traverse (\(EntryKey
key, Packed
value) -> (EntryKey
key,) (Value -> (EntryKey, Value))
-> Maybe Value -> Maybe (EntryKey, Value)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> DocTable -> Packed -> Maybe Value
packedValue (PackedIndex -> DocTable
indexTable PackedIndex
document) Packed
value) (PackedIndex -> [(EntryKey, Packed)]
indexRecords PackedIndex
document)
packedIndexPlan :: PackedIndex -> LinePlan
packedIndexPlan :: PackedIndex -> LinePlan
packedIndexPlan PackedIndex
document = SmallArray DocTable -> [Piece] -> LinePlan
LinePlan ([DocTable] -> SmallArray DocTable
forall a. [a] -> SmallArray a
smallArrayFromList [PackedIndex -> DocTable
indexTable PackedIndex
document]) [Int -> Packed -> Piece
Piece Int
0 Packed
value | (EntryKey
_, Packed
value) <- PackedIndex -> [(EntryKey, Packed)]
indexRecords PackedIndex
document]
packedIndexResident :: PackedIndex -> Int
packedIndexResident :: PackedIndex -> Int
packedIndexResident PackedIndex
document = DocTable -> Int
tableResident (PackedIndex -> DocTable
indexTable PackedIndex
document) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ [Int] -> Int
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int
64 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Packed -> Int
packedResident Packed
value | (EntryKey
_, Packed
value) <- PackedIndex -> [(EntryKey, Packed)]
indexRecords PackedIndex
document]