From ce36509c67d6d325bf39b3aec9d7df6ff42a11dc Mon Sep 17 00:00:00 2001 From: voidlizard Date: Tue, 20 May 2025 11:59:15 +0300 Subject: [PATCH] wip, seems references work --- bf6/ncq-migrate.ss | 5 +++- hbs2-storage-ncq/lib/HBS2/Storage/NCQ.hs | 9 ++++--- hbs2-tests/test/TCQ.hs | 31 ++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/bf6/ncq-migrate.ss b/bf6/ncq-migrate.ss index 8dbf1434..07a9def6 100644 --- a/bf6/ncq-migrate.ss +++ b/bf6/ncq-migrate.ss @@ -66,7 +66,10 @@ (import-blocks) (import-refs) +(debug) -; ; (println OKAY) +(ncq:fossilize ncq) + +(println done) diff --git a/hbs2-storage-ncq/lib/HBS2/Storage/NCQ.hs b/hbs2-storage-ncq/lib/HBS2/Storage/NCQ.hs index bf7d4075..88d1e14e 100644 --- a/hbs2-storage-ncq/lib/HBS2/Storage/NCQ.hs +++ b/hbs2-storage-ncq/lib/HBS2/Storage/NCQ.hs @@ -473,7 +473,7 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do what' <- race (pause @'Seconds 1) $ atomically do stop <- readTVar ncqStopped q <- tryPeekTQueue indexQ - if not ( stop || isJust q) then + if not (stop || isJust q) then STM.retry else do STM.flushTQueue indexQ @@ -539,7 +539,9 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do fsize <- getFdStatus fh <&> PFS.fileSize pure (0,fromIntegral fsize) - if sz < ncqMinLog then do + now <- readTVarIO ncqIndexNow + + if sz < ncqMinLog && now <= 0 then do ((h, (fromIntegral off, fromIntegral len)) : ) <$> next (written', rest) else do pure [(h, (fromIntegral off, fromIntegral len))] @@ -568,7 +570,7 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do bw <- readTVar wbytes writeTVar ncqNotWritten (max 0 (b0 - bw)) - indexNow <- readTVarIO ncqIndexNow + indexNow <- atomically $ stateTVar ncqIndexNow (,0) when (fromIntegral size >= ncqMinLog || indexNow > 0) do @@ -596,7 +598,6 @@ ncqStorageRun ncq@NCQStorage{..} = flip runContT pure do -- то есть должны отнять 1 после индексации. modifyTVar ncqCurrentUsage (IntMap.insertWith (+) (fromIntegral fdr) 1) writeTQueue indexQ (fdr, fossilized) - writeTVar ncqIndexNow 0 closeFd fh writeBinaryFileDurable (ncqGetCurrentSizeName ncq) (N.bytestring64 0) diff --git a/hbs2-tests/test/TCQ.hs b/hbs2-tests/test/TCQ.hs index 43b020bd..a35db230 100644 --- a/hbs2-tests/test/TCQ.hs +++ b/hbs2-tests/test/TCQ.hs @@ -16,6 +16,10 @@ import HBS2.Merkle import HBS2.Storage import HBS2.Storage.Simple import HBS2.Storage.Operations.ByteString +import HBS2.Net.Auth.Credentials +import HBS2.Peer.Proto.RefLog +import HBS2.Peer.Proto.LWWRef +import HBS2.Data.Types.SignedBox import HBS2.System.Logger.Simple.ANSI @@ -319,10 +323,37 @@ main = do [ isOpaqueOf @TCQ -> Just tcq, HashLike w ] -> lift do ncq <- getNCQ tcq ref <- ncqStorageGetRef ncq w + debug $ "ref" <+> pretty w <+> pretty ref pure $ maybe nil (mkSym . show . pretty) ref e -> throwIO $ BadFormException @C (mkList e) + entry $ bindMatch "ncq:get:reflog" $ \case + [ isOpaqueOf @TCQ -> Just tcq, SignPubKeyLike reflog ] -> lift do + ncq <- getNCQ tcq + let sto = AnyStorage ncq + let ha = hashObject @HbSync (RefLogKey @HBS2Basic reflog) + debug $ "refhash" <+> pretty ha + ref <- getRef sto (RefLogKey @HBS2Basic reflog) + pure $ maybe nil (mkSym . show . pretty) ref + + e -> throwIO $ BadFormException @C (mkList e) + + entry $ bindMatch "ncq:get:lwwref" $ \case + [ isOpaqueOf @TCQ -> Just tcq, SignPubKeyLike lww ] -> lift do + ncq <- getNCQ tcq + let sto = AnyStorage ncq + val <- runMaybeT do + rv <- getRef sto (LWWRefKey @HBS2Basic lww) >>= toMPlus + getBlock sto rv >>= toMPlus + <&> unboxSignedBox @(LWWRef 'HBS2Basic) @HBS2Basic + >>= toMPlus + <&> snd + + pure $ maybe nil (mkSym . show . pretty) val + + e -> throwIO $ BadFormException @C (mkList e) + entry $ bindMatch "ncq:refhash" $ \case [ isOpaqueOf @TCQ -> Just tcq, HashLike w ] -> lift do ncq <- getNCQ tcq