save lww ref data to state

This commit is contained in:
Dmitry Zuikov 2024-03-15 13:47:29 +03:00
parent 85d1df2fd3
commit 9e0468079b
5 changed files with 51 additions and 7 deletions

View File

@ -162,7 +162,7 @@ export key refs = do
subscribeLWWRef key subscribeLWWRef key
lww@LWWBlockData{..} <- waitOrInitLWWRef (lww, LWWBlockData{..}) <- waitOrInitLWWRef
let puk0 = fromLwwRefKey key let puk0 = fromLwwRefKey key

View File

@ -91,7 +91,11 @@ importRepoWait lwwKey = do
fetchLWWRef lwwKey fetchLWWRef lwwKey
next (IWaitLWWBlock (pred w)) next (IWaitLWWBlock (pred w))
Just (LWWBlockData{..}) -> do Just (LWWRef{..}, LWWBlockData{..}) -> do
withState do
insertLww lwwKey lwwSeq lwwRefLogPubKey
void $ try @_ @SomeException (getRefLogMerkle lwwRefLogPubKey) void $ try @_ @SomeException (getRefLogMerkle lwwRefLogPubKey)
subscribeRefLog lwwRefLogPubKey subscribeRefLog lwwRefLogPubKey
pause @'Seconds 0.25 pause @'Seconds 0.25

View File

@ -1,9 +1,13 @@
module HBS2.Git.Client.RefLog where module HBS2.Git.Client.RefLog
( module HBS2.Git.Client.RefLog
, module HBS2.Peer.Proto.RefLog
) where
import HBS2.Git.Client.Prelude import HBS2.Git.Client.Prelude
import HBS2.Git.Client.App.Types import HBS2.Git.Client.App.Types
import HBS2.Git.Data.RefLog import HBS2.Git.Data.RefLog
import HBS2.Git.Data.LWWBlock import HBS2.Git.Data.LWWBlock
import HBS2.Peer.Proto.RefLog
data RefLogRequestTimeout = RefLogRequestTimeout data RefLogRequestTimeout = RefLogRequestTimeout
deriving (Show,Typeable) deriving (Show,Typeable)

View File

@ -10,14 +10,16 @@ import HBS2.Git.Client.Prelude
import HBS2.Git.Client.App.Types import HBS2.Git.Client.App.Types
import HBS2.Git.Client.Config import HBS2.Git.Client.Config
import HBS2.Peer.Proto.RefLog
import HBS2.Git.Data.RefLog import HBS2.Git.Data.RefLog
import HBS2.Git.Data.LWWBlock
import DBPipe.SQLite import DBPipe.SQLite
import Data.Maybe import Data.Maybe
import Data.List qualified as List import Data.List qualified as List
import Text.InterpolatedString.Perl6 (qc) import Text.InterpolatedString.Perl6 (qc)
import Data.Word
newtype Base58Field a = Base58Field { fromBase58Field :: a } newtype Base58Field a = Base58Field { fromBase58Field :: a }
deriving stock (Eq,Ord,Generic) deriving stock (Eq,Ord,Generic)
@ -28,6 +30,9 @@ instance Pretty (AsBase58 a) => ToField (Base58Field a) where
instance IsString a => FromField (Base58Field a) where instance IsString a => FromField (Base58Field a) where
fromField = fmap (Base58Field . fromString) . fromField @String fromField = fmap (Base58Field . fromString) . fromField @String
instance FromField (RefLogKey HBS2Basic) where
fromField = fmap fromString . fromField @String
instance ToField HashRef where instance ToField HashRef where
toField h = toField @String (show $ pretty h) toField h = toField @String (show $ pretty h)
@ -46,6 +51,8 @@ instance FromField GitRef where
instance FromField GitHash where instance FromField GitHash where
fromField = fmap fromString . fromField @String fromField = fmap fromString . fromField @String
instance FromField (LWWRefKey HBS2Basic) where
fromField = fmap fromString . fromField @String
createStateDir :: (GitPerks m, MonadReader GitEnv m) => m () createStateDir :: (GitPerks m, MonadReader GitEnv m) => m ()
createStateDir = do createStateDir = do
@ -74,6 +81,7 @@ evolveDB = withState do
createBundleKeyTable createBundleKeyTable
createBundleObjectTable createBundleObjectTable
createNewGK0Table createNewGK0Table
createLwwTable
commitAll commitAll
createTxTable :: MonadIO m => DBPipeM m () createTxTable :: MonadIO m => DBPipeM m ()
@ -156,6 +164,19 @@ create table if not exists newgk0
) )
|] |]
createLwwTable :: MonadIO m => DBPipeM m ()
createLwwTable = do
ddl [qc|
create table if not exists lww
( hash text not null
, seq text not null
, reflog text not null
, primary key (hash,seq,reflog)
)
|]
existsTx :: MonadIO m => HashRef -> DBPipeM m Bool existsTx :: MonadIO m => HashRef -> DBPipeM m Bool
existsTx txHash = do existsTx txHash = do
select @(Only Bool) [qc| select @(Only Bool) [qc|
@ -346,3 +367,16 @@ limit 1
|] (Only (Base58Field reflog)) <&> listToMaybe |] (Only (Base58Field reflog)) <&> listToMaybe
insertLww :: MonadIO m => LWWRefKey HBS2Basic -> Word64 -> RefLogId -> DBPipeM m ()
insertLww lww snum reflog = do
insert [qc|
INSERT INTO lww (hash, seq, reflog) VALUES (?, ?, ?)
ON CONFLICT (hash,seq,reflog) DO NOTHING
|] (Base58Field lww, snum, Base58Field reflog)
selectAllLww :: MonadIO m => DBPipeM m [(LWWRefKey HBS2Basic, Word64, RefLogId)]
selectAllLww = do
select_ [qc|
SELECT hash, seq, reflog FROM lww
|] <&> fmap (over _3 (fromRefLogKey @HBS2Basic))

View File

@ -65,6 +65,7 @@ data LWWBlockOpError =
instance Exception LWWBlockOpError instance Exception LWWBlockOpError
{- HLINT ignore "Functor law" -}
readLWWBlock :: forall e s m . ( MonadIO m readLWWBlock :: forall e s m . ( MonadIO m
, Signatures s , Signatures s
@ -75,11 +76,11 @@ readLWWBlock :: forall e s m . ( MonadIO m
) )
=> AnyStorage => AnyStorage
-> LWWRefKey s -> LWWRefKey s
-> m (Maybe (LWWBlockData e)) -> m (Maybe (LWWRef e, LWWBlockData e))
readLWWBlock sto k = runMaybeT do readLWWBlock sto k = runMaybeT do
LWWRef{..} <- runExceptT (readLWWRef @e sto k) w@LWWRef{..} <- runExceptT (readLWWRef @e sto k)
>>= toMPlus >>= toMPlus
>>= toMPlus >>= toMPlus
@ -88,6 +89,7 @@ readLWWBlock sto k = runMaybeT do
<&> deserialiseOrFail @(LWWBlock e) <&> deserialiseOrFail @(LWWBlock e)
>>= toMPlus >>= toMPlus
<&> lwwBlockData <&> lwwBlockData
<&> (w,)
initLWWRef :: forall e s m . ( MonadIO m initLWWRef :: forall e s m . ( MonadIO m
, MonadError LWWBlockOpError m , MonadError LWWBlockOpError m