-- SPDX-FileCopyrightText: 2026 Alexandra de Wit
--
-- SPDX-License-Identifier: MIT

{- | Opaque serving documents with ecosystem-specific inject/project pairs.
The pipeline carries source snapshot scope separately and delegates wire access to adapters.
-}
module Ecluse.Core.Registry.CachedDocument (
    CachedDoc,
    weighCachedDoc,
    estimateValueBytes,
    npmCached,
    pypiSimpleCached,

    -- * npm's packed full reads and their renders
    npmPacked,
    npmRendered,

    -- * PyPI's packed full reads and their renders
    pypiPacked,
    pypiRendered,

    -- * Cargo's native records, packed full reads and their renders
    cargoCached,
    cargoPacked,
    cargoRendered,

    -- * RubyGems' native rows
    rubyGemsCached,
) where

import Data.Aeson (Value (..))
import Data.Aeson.Key qualified as Key
import Data.Aeson.KeyMap qualified as KeyMap
import Data.Scientific (coefficient)
import Data.Text.Internal qualified as Text
import Math.NumberTheory.Logarithms (integerLog2)

import Ecluse.Core.Package.Entry (EntryKey (ArrayEntry, RecordEntry))
import Ecluse.Core.Registry.Cargo.Document (CargoDocument, PackedIndex, cargoDocument, cargoRecords, packedIndexDocument, packedIndexResident)
import Ecluse.Core.Registry.Json.Packed (LinePlan, RenderPlan (planMembers), linePlanResident, linePlanValues, planResident, planValue)
import Ecluse.Core.Registry.Npm.Document (PackedPackument (packumentTop), packumentResident, packumentValue)
import Ecluse.Core.Registry.PyPI.Document (PackedSimple, SimpleDocument, packedEnvelope, packedSimpleDocument, packedSimpleResident, simpleDocument, simpleEnvelope, simpleFiles)
import Ecluse.Core.Registry.RubyGems.Document (GemDocument, gemDocumentBytes)
import Ecluse.Core.Server.MemoryModel (chargeForResident)

-- | Serving documents held by the pipeline and permitted caches. Derived 'Show' and 'Eq' support debugging and tests, not wire projection.
data CachedDoc
    = CachedNpm Value ~Int64
    | CachedPyPISimple SimpleDocument ~Int64
    | PackedNpm PackedPackument
    | RenderedNpm RenderPlan
    | PackedPyPI PackedSimple
    | RenderedPyPI RenderPlan
    | CachedCargo CargoDocument ~Int64
    | PackedCargo PackedIndex
    | RenderedCargo LinePlan
    | CachedRubyGems GemDocument ~Int64
    deriving stock (CachedDoc -> CachedDoc -> Bool
(CachedDoc -> CachedDoc -> Bool)
-> (CachedDoc -> CachedDoc -> Bool) -> Eq CachedDoc
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: CachedDoc -> CachedDoc -> Bool
== :: CachedDoc -> CachedDoc -> Bool
$c/= :: CachedDoc -> CachedDoc -> Bool
/= :: CachedDoc -> CachedDoc -> Bool
Eq, Int -> CachedDoc -> ShowS
[CachedDoc] -> ShowS
CachedDoc -> String
(Int -> CachedDoc -> ShowS)
-> (CachedDoc -> String)
-> ([CachedDoc] -> ShowS)
-> Show CachedDoc
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> CachedDoc -> ShowS
showsPrec :: Int -> CachedDoc -> ShowS
$cshow :: CachedDoc -> String
show :: CachedDoc -> String
$cshowList :: [CachedDoc] -> ShowS
showList :: [CachedDoc] -> ShowS
Show)

-- | Estimate compact cache units without measuring retained heap. Packed storage estimates use the consumer's expansion factor.
weighCachedDoc :: CachedDoc -> Int64
weighCachedDoc :: CachedDoc -> Int64
weighCachedDoc = \case
    CachedNpm Value
_ Int64
charge -> Int64
charge
    CachedPyPISimple SimpleDocument
_ Int64
charge -> Int64
charge
    PackedNpm PackedPackument
packed -> Value -> Int64
estimateValueBytes (Object -> Value
Object (PackedPackument -> Object
packumentTop PackedPackument
packed)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
chargeForResident (PackedPackument -> Int
packumentResident PackedPackument
packed))
    RenderedNpm RenderPlan
plan -> Value -> Int64
estimateValueBytes (Object -> Value
Object (RenderPlan -> Object
planMembers RenderPlan
plan)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
chargeForResident (RenderPlan -> Int
planResident RenderPlan
plan))
    PackedPyPI PackedSimple
document -> Value -> Int64
estimateValueBytes (Object -> Value
Object (PackedSimple -> Object
packedEnvelope PackedSimple
document)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
chargeForResident (PackedSimple -> Int
packedSimpleResident PackedSimple
document))
    RenderedPyPI RenderPlan
plan -> Value -> Int64
estimateValueBytes (Object -> Value
Object (RenderPlan -> Object
planMembers RenderPlan
plan)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
chargeForResident (RenderPlan -> Int
planResident RenderPlan
plan))
    CachedCargo CargoDocument
_ Int64
charge -> Int64
charge
    PackedCargo PackedIndex
document -> Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
chargeForResident (PackedIndex -> Int
packedIndexResident PackedIndex
document))
    RenderedCargo LinePlan
plan -> Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Int
chargeForResident (LinePlan -> Int
linePlanResident LinePlan
plan))
    CachedRubyGems GemDocument
_ Int64
charge -> Int64
charge

-- | Explicit cases require updates for new ecosystems. npm projects packed or rendered forms to trees, refusing missing strings or tables.
npmCached :: (Value -> CachedDoc, CachedDoc -> Maybe Value)
npmCached :: (Value -> CachedDoc, CachedDoc -> Maybe Value)
npmCached = (\Value
v -> Value -> Int64 -> CachedDoc
CachedNpm Value
v (Value -> Int64
estimateValueBytes Value
v), CachedDoc -> Maybe Value
project)
  where
    project :: CachedDoc -> Maybe Value
project = \case
        CachedNpm Value
v Int64
_ -> Value -> Maybe Value
forall a. a -> Maybe a
Just Value
v
        PackedNpm PackedPackument
packed -> PackedPackument -> Maybe Value
packumentValue PackedPackument
packed
        RenderedNpm RenderPlan
plan -> RenderPlan -> Maybe Value
planValue RenderPlan
plan
        CachedPyPISimple SimpleDocument
_ Int64
_ -> Maybe Value
forall a. Maybe a
Nothing
        PackedPyPI PackedSimple
_ -> Maybe Value
forall a. Maybe a
Nothing
        RenderedPyPI RenderPlan
_ -> Maybe Value
forall a. Maybe a
Nothing
        CachedCargo CargoDocument
_ Int64
_ -> Maybe Value
forall a. Maybe a
Nothing
        PackedCargo PackedIndex
_ -> Maybe Value
forall a. Maybe a
Nothing
        RenderedCargo LinePlan
_ -> Maybe Value
forall a. Maybe a
Nothing
        CachedRubyGems GemDocument
_ Int64
_ -> Maybe Value
forall a. Maybe a
Nothing

-- | PyPI's boundary pair, spelled out arm by arm for the same reason as 'npmCached'.
pypiSimpleCached :: (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
pypiSimpleCached :: (SimpleDocument -> CachedDoc, CachedDoc -> Maybe SimpleDocument)
pypiSimpleCached = (\SimpleDocument
v -> SimpleDocument -> Int64 -> CachedDoc
CachedPyPISimple SimpleDocument
v (SimpleDocument -> Int64
simpleBytes SimpleDocument
v), CachedDoc -> Maybe SimpleDocument
project)
  where
    project :: CachedDoc -> Maybe SimpleDocument
project = \case
        CachedPyPISimple SimpleDocument
v Int64
_ -> SimpleDocument -> Maybe SimpleDocument
forall a. a -> Maybe a
Just SimpleDocument
v
        CachedNpm Value
_ Int64
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        PackedNpm PackedPackument
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        RenderedNpm RenderPlan
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        PackedPyPI PackedSimple
document -> PackedSimple -> Maybe SimpleDocument
packedSimpleDocument PackedSimple
document
        RenderedPyPI RenderPlan
plan -> Value -> Maybe SimpleDocument
fromValue (Value -> Maybe SimpleDocument)
-> Maybe Value -> Maybe SimpleDocument
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< RenderPlan -> Maybe Value
planValue RenderPlan
plan
        CachedCargo CargoDocument
_ Int64
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        PackedCargo PackedIndex
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        RenderedCargo LinePlan
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        CachedRubyGems GemDocument
_ Int64
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
    fromValue :: Value -> Maybe SimpleDocument
fromValue = \case
        Object Object
fields -> case Key -> Object -> Maybe Value
forall v. Key -> KeyMap v -> Maybe v
KeyMap.lookup Key
"files" Object
fields of
            Just (Array Array
files) -> SimpleDocument -> Maybe SimpleDocument
forall a. a -> Maybe a
Just (Object -> [(EntryKey, Value)] -> SimpleDocument
simpleDocument Object
fields ((Int -> Value -> (EntryKey, Value))
-> [Int] -> [Value] -> [(EntryKey, Value)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Int
position Value
value -> (Int -> EntryKey
ArrayEntry Int
position, Value
value)) [Int
0 ..] (Array -> [Value]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
files)))
            Maybe Value
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing
        Value
_ -> Maybe SimpleDocument
forall a. Maybe a
Nothing

-- | npm's packed full read, and its packed form when the document is one.
npmPacked :: (PackedPackument -> CachedDoc, CachedDoc -> Maybe PackedPackument)
npmPacked :: (PackedPackument -> CachedDoc, CachedDoc -> Maybe PackedPackument)
npmPacked = (PackedPackument -> CachedDoc
PackedNpm, \case PackedNpm PackedPackument
packed -> PackedPackument -> Maybe PackedPackument
forall a. a -> Maybe a
Just PackedPackument
packed; CachedDoc
_ -> Maybe PackedPackument
forall a. Maybe a
Nothing)

-- | An assembled npm listing that renders from packed releases, and its plan when the document is one.
npmRendered :: (RenderPlan -> CachedDoc, CachedDoc -> Maybe RenderPlan)
npmRendered :: (RenderPlan -> CachedDoc, CachedDoc -> Maybe RenderPlan)
npmRendered = (RenderPlan -> CachedDoc
RenderedNpm, \case RenderedNpm RenderPlan
plan -> RenderPlan -> Maybe RenderPlan
forall a. a -> Maybe a
Just RenderPlan
plan; CachedDoc
_ -> Maybe RenderPlan
forall a. Maybe a
Nothing)

-- | PyPI's packed full-read document, available without materialising its files.
pypiPacked :: (PackedSimple -> CachedDoc, CachedDoc -> Maybe PackedSimple)
pypiPacked :: (PackedSimple -> CachedDoc, CachedDoc -> Maybe PackedSimple)
pypiPacked = (PackedSimple -> CachedDoc
PackedPyPI, \case PackedPyPI PackedSimple
document -> PackedSimple -> Maybe PackedSimple
forall a. a -> Maybe a
Just PackedSimple
document; CachedDoc
_ -> Maybe PackedSimple
forall a. Maybe a
Nothing)

-- | An assembled PyPI listing that renders directly from packed files and source tables.
pypiRendered :: (RenderPlan -> CachedDoc, CachedDoc -> Maybe RenderPlan)
pypiRendered :: (RenderPlan -> CachedDoc, CachedDoc -> Maybe RenderPlan)
pypiRendered = (RenderPlan -> CachedDoc
RenderedPyPI, \case RenderedPyPI RenderPlan
plan -> RenderPlan -> Maybe RenderPlan
forall a. a -> Maybe a
Just RenderPlan
plan; CachedDoc
_ -> Maybe RenderPlan
forall a. Maybe a
Nothing)

-- | Cargo's native source records, preserving physical source coordinates on tree projection.
cargoCached :: (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached :: (CargoDocument -> CachedDoc, CachedDoc -> Maybe CargoDocument)
cargoCached = (\CargoDocument
document -> CargoDocument -> Int64 -> CachedDoc
CachedCargo CargoDocument
document (CargoDocument -> Int64
cargoBytes CargoDocument
document), CachedDoc -> Maybe CargoDocument
project)
  where
    project :: CachedDoc -> Maybe CargoDocument
project = \case
        CachedCargo CargoDocument
document Int64
_ -> CargoDocument -> Maybe CargoDocument
forall a. a -> Maybe a
Just CargoDocument
document
        PackedCargo PackedIndex
document -> PackedIndex -> Maybe CargoDocument
packedIndexDocument PackedIndex
document
        RenderedCargo LinePlan
plan -> [(EntryKey, Value)] -> CargoDocument
cargoDocument ([(EntryKey, Value)] -> CargoDocument)
-> ([Value] -> [(EntryKey, Value)]) -> [Value] -> CargoDocument
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Int -> Value -> (EntryKey, Value))
-> [Int] -> [Value] -> [(EntryKey, Value)]
forall a b c. (a -> b -> c) -> [a] -> [b] -> [c]
zipWith (\Int
position Value
value -> (Int -> EntryKey
RecordEntry Int
position, Value
value)) [Int
0 ..] ([Value] -> CargoDocument) -> Maybe [Value] -> Maybe CargoDocument
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> LinePlan -> Maybe [Value]
linePlanValues LinePlan
plan
        CachedNpm Value
_ Int64
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing
        CachedPyPISimple SimpleDocument
_ Int64
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing
        PackedNpm PackedPackument
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing
        RenderedNpm RenderPlan
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing
        PackedPyPI PackedSimple
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing
        RenderedPyPI RenderPlan
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing
        CachedRubyGems GemDocument
_ Int64
_ -> Maybe CargoDocument
forall a. Maybe a
Nothing

-- | Cargo's packed full source remains available without materialising native records.
cargoPacked :: (PackedIndex -> CachedDoc, CachedDoc -> Maybe PackedIndex)
cargoPacked :: (PackedIndex -> CachedDoc, CachedDoc -> Maybe PackedIndex)
cargoPacked = (PackedIndex -> CachedDoc
PackedCargo, \case PackedCargo PackedIndex
document -> PackedIndex -> Maybe PackedIndex
forall a. a -> Maybe a
Just PackedIndex
document; CachedDoc
_ -> Maybe PackedIndex
forall a. Maybe a
Nothing)

-- | A native output plan keeps ordered records over their separate source tables.
cargoRendered :: (LinePlan -> CachedDoc, CachedDoc -> Maybe LinePlan)
cargoRendered :: (LinePlan -> CachedDoc, CachedDoc -> Maybe LinePlan)
cargoRendered = (LinePlan -> CachedDoc
RenderedCargo, \case RenderedCargo LinePlan
plan -> LinePlan -> Maybe LinePlan
forall a. a -> Maybe a
Just LinePlan
plan; CachedDoc
_ -> Maybe LinePlan
forall a. Maybe a
Nothing)

cargoBytes :: CargoDocument -> Int64
cargoBytes :: CargoDocument -> Int64
cargoBytes CargoDocument
document = [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
64 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Value -> Int64
estimateValueBytes Value
value | (EntryKey
_, Value
value) <- CargoDocument -> [(EntryKey, Value)]
cargoRecords CargoDocument
document]

simpleBytes :: SimpleDocument -> Int64
simpleBytes :: SimpleDocument -> Int64
simpleBytes SimpleDocument
document = Value -> Int64
estimateValueBytes (Object -> Value
Object (SimpleDocument -> Object
simpleEnvelope SimpleDocument
document)) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int64
12 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
32 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Value -> Int64
estimateValueBytes Value
value | (EntryKey
_, Value
value) <- SimpleDocument -> [(EntryKey, Value)]
simpleFiles SimpleDocument
document]

-- | Estimate accounting bytes without encoding. This is neither measured heap nor exact JSON length.
estimateValueBytes :: Value -> Int64
estimateValueBytes :: Value -> Int64
estimateValueBytes = \case
    Object Object
fields -> Int64
2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
4 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Text -> Int64
textBytes (Key -> Text
Key.toText Key
key) Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Value -> Int64
estimateValueBytes Value
value | (Key
key, Value
value) <- Object -> [(Key, Value)]
forall v. KeyMap v -> [(Key, v)]
KeyMap.toList Object
fields]
    Array Array
items -> Int64
2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ [Int64] -> Int64
forall a (f :: * -> *). (Foldable f, Num a) => f a -> a
sum [Int64
1 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Value -> Int64
estimateValueBytes Value
value | Value
value <- Array -> [Value]
forall a. Vector a -> [a]
forall (t :: * -> *) a. Foldable t => t a -> [a]
toList Array
items]
    String Text
value -> Int64
2 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Text -> Int64
textBytes Text
value
    Number Scientific
number -> Int64
24 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Integer -> Int64
integerBytes (Scientific -> Integer
coefficient Scientific
number)
    Bool Bool
_ -> Int64
5
    Value
Null -> Int64
4

textBytes :: Text -> Int64
textBytes :: Text -> Int64
textBytes (Text.Text Array
_ Int
_ Int
len) = Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
len

integerBytes :: Integer -> Int64
integerBytes :: Integer -> Int64
integerBytes Integer
value
    | Integer
value Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
0 = Int64
8
    | Bool
otherwise = Int64
8 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
* (Int64
1 Int64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+ Int -> Int64
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Integer -> Int
integerLog2 (Integer -> Integer
forall a. Num a => a -> a
abs Integer
value) Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
64))

-- | RubyGems retains native artifact rows without reconstructing installation metadata from policy facts.
rubyGemsCached :: (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached :: (GemDocument -> CachedDoc, CachedDoc -> Maybe GemDocument)
rubyGemsCached = (\GemDocument
document -> GemDocument -> Int64 -> CachedDoc
CachedRubyGems GemDocument
document (GemDocument -> Int64
gemDocumentBytes GemDocument
document), CachedDoc -> Maybe GemDocument
project)
  where
    project :: CachedDoc -> Maybe GemDocument
project = \case
        CachedRubyGems GemDocument
document Int64
_ -> GemDocument -> Maybe GemDocument
forall a. a -> Maybe a
Just GemDocument
document
        CachedNpm Value
_ Int64
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        CachedPyPISimple SimpleDocument
_ Int64
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        PackedNpm PackedPackument
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        RenderedNpm RenderPlan
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        PackedPyPI PackedSimple
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        RenderedPyPI RenderPlan
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        CachedCargo CargoDocument
_ Int64
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        PackedCargo PackedIndex
_ -> Maybe GemDocument
forall a. Maybe a
Nothing
        RenderedCargo LinePlan
_ -> Maybe GemDocument
forall a. Maybe a
Nothing