This commit is contained in:
Dmitry Zuikov 2023-01-11 16:44:02 +03:00
parent 742b3e57dd
commit 5ce0f8c48f
3 changed files with 40 additions and 9 deletions

View File

@ -83,8 +83,6 @@ simpleStorageInit opts = liftIO $ do
pdir <- canonicalizePath (fromPrefix prefix) pdir <- canonicalizePath (fromPrefix prefix)
print (pretty pdir)
tbq <- TBMQ.newTBMQueueIO (fromIntegral (fromQueueSize qSize)) tbq <- TBMQ.newTBMQueueIO (fromIntegral (fromQueueSize qSize))
tstop <- TV.newTVarIO False tstop <- TV.newTVarIO False

View File

@ -11,6 +11,7 @@ import Data.Functor
import Options.Applicative import Options.Applicative
import Prettyprinter import Prettyprinter
import System.Directory import System.Directory
import Control.Monad.Trans.Maybe
-- import System.FilePath.Posix -- import System.FilePath.Posix
import System.IO import System.IO
@ -35,12 +36,23 @@ newtype OptInputFile = OptInputFile { unOptFile :: FilePath }
deriving newtype (Eq,Ord,IsString) deriving newtype (Eq,Ord,IsString)
deriving stock (Data) deriving stock (Data)
newtype Opts = newtype MerkleHash = MerkleHash { fromMerkleHash :: Hash HbSync }
Opts { deriving newtype (Eq,Ord,IsString,Pretty)
optInputFile :: Maybe OptInputFile deriving stock (Data,Generic)
newtype StoreOpts =
StoreOpts {
storeInputFile :: Maybe OptInputFile
} }
deriving stock (Data) deriving stock (Data)
newtype CatOpts =
CatOpts {
catMerkleHash :: Maybe MerkleHash
}
deriving stock (Data)
readChunked :: MonadIO m => Handle -> Int -> S.Stream (S.Of ByteString) m () readChunked :: MonadIO m => Handle -> Int -> S.Stream (S.Of ByteString) m ()
readChunked handle size = fuu readChunked handle size = fuu
where where
@ -51,7 +63,23 @@ readChunked handle size = fuu
S.yield chunk S.yield chunk
next next
runStore :: Opts -> SimpleStorage HbSync -> IO ()
runCat :: Data opts => opts -> SimpleStorage HbSync -> IO ()
runCat opts ss = do
void $ runMaybeT $ do
mhash <- MaybeT $ pure $ uniLastMay @MerkleHash opts <&> fromMerkleHash
liftIO $ walkMerkle mhash (getBlock ss) $ \(hr :: [HashRef]) -> do
forM_ hr $ \(HashRef h) -> do
mblk <- getBlock ss h
case mblk of
Nothing -> error $ show $ "missed block: " <+> pretty h
Just blk -> LBS.putStr blk
runStore :: Data opts => opts -> SimpleStorage HbSync -> IO ()
runStore opts ss = do runStore opts ss = do
let fname = uniLastMay @OptInputFile opts let fname = uniLastMay @OptInputFile opts
@ -60,8 +88,7 @@ runStore opts ss = do
hashes <- readChunked handle (fromIntegral defBlockSize) -- FIXME: to settings! hashes <- readChunked handle (fromIntegral defBlockSize) -- FIXME: to settings!
& S.mapM (\blk -> enqueueBlock ss (LBS.fromStrict blk) >> pure blk) & S.mapM (\blk -> enqueueBlock ss (LBS.fromStrict blk) >> pure blk)
& S.map hashObject & S.map (HashRef . hashObject)
& S.map HashRef
& S.toList_ & S.toList_
let pt = toPTree (MaxSize 8192) (MaxNum 8192) hashes -- FIXME: settings let pt = toPTree (MaxSize 8192) (MaxNum 8192) hashes -- FIXME: settings
@ -97,6 +124,7 @@ main = join . customExecParser (prefs showHelpOnError) $
where where
parser :: Parser (IO ()) parser :: Parser (IO ())
parser = hsubparser ( command "store" (info pStore (progDesc "store block")) parser = hsubparser ( command "store" (info pStore (progDesc "store block"))
<> command "cat" (info pCat (progDesc "cat block"))
) )
common = do common = do
@ -105,6 +133,10 @@ main = join . customExecParser (prefs showHelpOnError) $
pStore = do pStore = do
o <- common o <- common
file <- optional $ strArgument ( metavar "FILE" ) file <- optional $ strArgument ( metavar "FILE" )
pure $ withStore o (runStore ( Opts file )) pure $ withStore o (runStore ( StoreOpts file ))
pCat = do
o <- common
hash <- optional $ strArgument ( metavar "HASH" )
pure $ withStore o (runCat ( CatOpts hash ))

View File

@ -83,6 +83,7 @@ executable hbs2
, serialise , serialise
, streaming , streaming
, text , text
, transformers
, uniplate , uniplate