diff options
| -rw-r--r-- | src/Erebos/Object/Internal.hs | 34 | ||||
| -rw-r--r-- | src/Erebos/Storable.hs | 2 | ||||
| -rw-r--r-- | src/Erebos/Storage/Graph.hs | 2 | ||||
| -rw-r--r-- | src/Erebos/Storage/Head.hs | 2 | ||||
| -rw-r--r-- | src/Erebos/Storage/Internal.hs | 39 |
5 files changed, 41 insertions, 38 deletions
diff --git a/src/Erebos/Object/Internal.hs b/src/Erebos/Object/Internal.hs index 5c81bf5..b624d1c 100644 --- a/src/Erebos/Object/Internal.hs +++ b/src/Erebos/Object/Internal.hs @@ -1,8 +1,9 @@ module Erebos.Object.Internal ( Storage, PartialStorage, StorageCompleteness, - Ref, PartialRef, RefDigest, + Ref, PartialRef, RefDigest, Ref'(..), refDigest, refFromDigest, + refStorage, readRef, showRef, readRefDigest, showRefDigest, refDigestFromByteString, hashToRefDigest, @@ -59,6 +60,7 @@ import Data.ByteString.Lazy qualified as BL import Data.ByteString.Lazy.Char8 qualified as BLC import Data.Char import Data.Function +import Data.Hashable import Data.Maybe import Data.Ratio import Data.Text (Text) @@ -80,6 +82,33 @@ import Erebos.UUID qualified as U import Erebos.Util +data Ref' c = Ref (Storage' c) RefDigest + +type Ref = Ref' Complete +type PartialRef = Ref' Partial + +instance Eq (Ref' c) where + Ref _ d1 == Ref _ d2 = d1 == d2 + +instance Show (Ref' c) where + show ref@(Ref st _) = show st ++ ":" ++ BC.unpack (showRef ref) + +instance BA.ByteArrayAccess (Ref' c) where + length (Ref _ dgst) = BA.length dgst + withByteArray (Ref _ dgst) = BA.withByteArray dgst + +instance Hashable (Ref' c) where + hashWithSalt salt = hashWithSalt salt . refDigest + +refStorage :: Ref' c -> Storage' c +refStorage (Ref st _) = st + +refDigest :: Ref' c -> RefDigest +refDigest (Ref _ dgst) = dgst + +showRef :: Ref' c -> ByteString +showRef = showRefDigest . refDigest + zeroRef :: Storage' c -> Ref' c zeroRef s = Ref s (RefDigest h) where h = case digestFromByteString $ B.replicate (hashDigestSize $ digestAlgo h) 0 of @@ -257,6 +286,9 @@ lazyLoadBytes :: forall c. StorageCompleteness c => Ref' c -> LoadResult c BL.By lazyLoadBytes ref | isZeroRef ref = returnLoadResult (return BL.empty :: c BL.ByteString) lazyLoadBytes ref = returnLoadResult $ unsafePerformIO $ ioLoadBytes ref +ioLoadBytes :: StorageCompleteness c => Ref' c -> IO (c BL.ByteString) +ioLoadBytes (Ref st dgst) = unsafeLoadBytes st dgst + unsafeDeserializeObject :: Storage' c -> BL.ByteString -> Except ErebosError (Object' c, BL.ByteString) unsafeDeserializeObject _ bytes | BL.null bytes = return (ZeroObject, bytes) unsafeDeserializeObject st bytes = diff --git a/src/Erebos/Storable.hs b/src/Erebos/Storable.hs index cd0d97d..ddbe06c 100644 --- a/src/Erebos/Storable.hs +++ b/src/Erebos/Storable.hs @@ -11,7 +11,7 @@ defined here as well. module Erebos.Storable ( Storable(..), ZeroStorable(..), StorableText(..), StorableDate(..), StorableUUID(..), - StorageCompleteness(..), + StorageCompleteness, Store, StoreRec, storeBlob, storeRec, storeZero, diff --git a/src/Erebos/Storage/Graph.hs b/src/Erebos/Storage/Graph.hs index 1312941..67daa3b 100644 --- a/src/Erebos/Storage/Graph.hs +++ b/src/Erebos/Storage/Graph.hs @@ -39,7 +39,7 @@ import Data.Set qualified as S import System.IO.Unsafe (unsafePerformIO) -import Erebos.Object +import Erebos.Object.Internal import Erebos.Storable.Internal import Erebos.Storage.Internal import Erebos.Util diff --git a/src/Erebos/Storage/Head.hs b/src/Erebos/Storage/Head.hs index 87212f0..f6ddcfa 100644 --- a/src/Erebos/Storage/Head.hs +++ b/src/Erebos/Storage/Head.hs @@ -29,7 +29,7 @@ import Control.Monad.Reader import Data.Bifunctor import Data.Typeable -import Erebos.Object +import Erebos.Object.Internal import Erebos.Storable.Internal import Erebos.Storage.Backend import Erebos.Storage.Internal diff --git a/src/Erebos/Storage/Internal.hs b/src/Erebos/Storage/Internal.hs index c7255ef..e6fafe4 100644 --- a/src/Erebos/Storage/Internal.hs +++ b/src/Erebos/Storage/Internal.hs @@ -1,13 +1,11 @@ module Erebos.Storage.Internal ( Storage'(..), Storage, PartialStorage, - Ref'(..), Ref, PartialRef, RefDigest(..), WatchID, startWatchID, nextWatchID, WatchList(..), WatchListItem(..), watchListAdd, watchListDel, - refStorage, - refDigest, refDigestFromByteString, - showRef, showRefDigest, showRefDigestParts, + refDigestFromByteString, + showRefDigest, showRefDigestParts, readRefDigest, hashToRefDigest, @@ -176,36 +174,9 @@ newtype RefDigest = RefDigest (Digest Blake2b_256) instance Show RefDigest where show = BC.unpack . showRefDigest -data Ref' c = Ref (Storage' c) RefDigest - -type Ref = Ref' Complete -type PartialRef = Ref' Partial - -instance Eq (Ref' c) where - Ref _ d1 == Ref _ d2 = d1 == d2 - -instance Show (Ref' c) where - show ref@(Ref st _) = show st ++ ":" ++ BC.unpack (showRef ref) - -instance ByteArrayAccess (Ref' c) where - length (Ref _ dgst) = BA.length dgst - withByteArray (Ref _ dgst) = BA.withByteArray dgst - instance Hashable RefDigest where hashWithSalt salt ref = salt `xor` unsafePerformIO (BA.withByteArray ref peek) -instance Hashable (Ref' c) where - hashWithSalt salt ref = salt `xor` unsafePerformIO (BA.withByteArray ref peek) - -refStorage :: Ref' c -> Storage' c -refStorage (Ref st _) = st - -refDigest :: Ref' c -> RefDigest -refDigest (Ref _ dgst) = dgst - -showRef :: Ref' c -> ByteString -showRef = showRefDigest . refDigest - showRefDigestParts :: RefDigest -> (ByteString, ByteString) showRefDigestParts x = (BC.pack "blake2", showHex x) @@ -243,18 +214,18 @@ type Partial = Either RefDigest class (Traversable compl, Monad compl, Typeable compl) => StorageCompleteness compl where type LoadResult compl a :: Type returnLoadResult :: compl a -> LoadResult compl a - ioLoadBytes :: Ref' compl -> IO (compl BL.ByteString) + unsafeLoadBytes :: Storage' compl -> RefDigest -> IO (compl BL.ByteString) instance StorageCompleteness Complete where type LoadResult Complete a = a returnLoadResult = runIdentity - ioLoadBytes ref@(Ref st dgst) = maybe (error $ "Ref not found in complete storage: "++show ref) Identity + unsafeLoadBytes st dgst = maybe (error $ "Ref not found in complete storage: "++show dgst) Identity <$> ioLoadBytesFromStorage st dgst instance StorageCompleteness Partial where type LoadResult Partial a = Either RefDigest a returnLoadResult = id - ioLoadBytes (Ref st dgst) = maybe (Left dgst) Right <$> ioLoadBytesFromStorage st dgst + unsafeLoadBytes st dgst = maybe (Left dgst) Right <$> ioLoadBytesFromStorage st dgst ioLoadBytesFromStorage :: Storage' c -> RefDigest -> IO (Maybe BL.ByteString) ioLoadBytesFromStorage Storage {..} dgst = |