partially broken stuff

This commit is contained in:
Arya Irani 2022-04-12 13:14:46 -05:00
parent e698acf0e8
commit 396ebc485e
7 changed files with 111 additions and 39 deletions

View File

@ -128,7 +128,8 @@ module U.Codebase.Sqlite.Queries
-- * sync temp entities
getMissingDependencyJwtsForTempEntity,
tempEntityExists,
-- insertTempEntity,
insertTempEntity,
deleteTempDependencies,
-- * db misc
createSchema,
@ -156,11 +157,14 @@ import Control.Monad.Reader (MonadReader (ask))
import qualified Control.Monad.Reader as Reader
import qualified Control.Monad.Writer as Writer
import qualified Data.Char as Char
import qualified Data.Foldable as Foldable
import qualified Data.List.Extra as List
import Data.List.NonEmpty (NonEmpty)
import qualified Data.List.NonEmpty as Nel
import Data.Maybe (fromJust)
import qualified Data.Set as Set
import Data.Set.NonEmpty (NESet)
import qualified Data.Set.NonEmpty as NESet
import Data.String.Here.Uninterpolated (here, hereFile)
import Database.SQLite.Simple
( FromRow,
@ -191,6 +195,8 @@ import qualified U.Codebase.Sqlite.JournalMode as JournalMode
import U.Codebase.Sqlite.ObjectType (ObjectType)
import qualified U.Codebase.Sqlite.Reference as Reference
import qualified U.Codebase.Sqlite.Referent as Referent
import U.Codebase.Sqlite.TempEntity (HashJWT)
import U.Codebase.Sqlite.TempEntityType (TempEntityType)
import U.Codebase.WatchKind (WatchKind)
import qualified U.Codebase.WatchKind as WatchKind
import qualified U.Util.Alternative as Alternative
@ -947,14 +953,16 @@ ancestorSql =
-- * share sync / temp entities
getMissingDependencyJwtsForTempEntity :: DB m => Base32Hex -> m [Text]
--
getMissingDependencyJwtsForTempEntity :: DB m => Base32Hex -> m (Maybe (NESet Text))
getMissingDependencyJwtsForTempEntity h =
queryAtoms
[here|
SELECT jwt FROM temp_entity_missing_dependency
WHERE dependent = ?
|]
(Only h)
NESet.nonEmptySet . Set.fromList
<$> queryAtoms
[here|
SELECT dependencyJwt FROM temp_entity_missing_dependency
WHERE dependent = ?
|]
(Only h)
tempEntityExists :: DB m => Base32Hex -> m Bool
tempEntityExists h = queryOne $ queryAtom sql (Only h)
@ -968,6 +976,42 @@ tempEntityExists h = queryOne $ queryAtom sql (Only h)
)
|]
-- | Insert a new `temp_entity` row, and its associated 1+ `temp_entity_missing_dependency` rows.
--
-- Preconditions:
-- 1. The entity does not already exist in "main" storage (`object` / `causal`)
-- 2. The entity does not already exist in `temp_entity`.
insertTempEntity :: DB m => Base32Hex -> ByteString -> TempEntityType -> NESet (Base32Hex, HashJWT) -> m ()
insertTempEntity dependentHash dependentBlob dependentType missingDependencies = do
execute
[here|
INSERT INTO temp_entity (hash, blob, typeId)
VALUES (?, ?, ?)
|]
(dependentHash, dependentBlob, dependentType)
executeMany
[here|
INSERT INTO temp_entity_missing_dependencies (dependent, dependency, dependencyJwt)
VALUES (?, ?, ?)
|]
( NESet.map
(\(dependencyHash, dependencyHashJwt) -> (dependentHash, dependencyHash, dependencyHashJwt))
missingDependencies
)
-- | takes a dependent's hash and multiple dependency hashes
deleteTempDependencies :: (DB m, Foldable f) => Base32Hex -> f Base32Hex -> m ()
deleteTempDependencies dependent (Foldable.toList -> dependencies) =
executeMany sql (map (dependent,) dependencies)
where
sql =
[here|
DELETE FROM temp_entity_missing_dependencies
WHERE dependent = ?
AND dependency = ?
|]
-- * helper functions
-- | composite input, atomic List output
@ -1060,8 +1104,8 @@ execute_ q = do
header <- debugHeader
liftIO . queryTrace_ (header ++ " " ++ "execute_") q $ SQLite.execute_ c q
executeMany :: (DB m, ToRow q, Show q) => SQLite.Query -> [q] -> m ()
executeMany q r = do
executeMany :: (DB m, Foldable f, ToRow q, Show q) => SQLite.Query -> f q -> m ()
executeMany q (Foldable.toList -> r) = do
c <- Reader.reader Connection.underlying
header <- debugHeader
liftIO . queryTrace (header ++ " " ++ "executeMany") q r $ SQLite.executeMany c q r

View File

@ -729,7 +729,7 @@ putTempEntity = \case
putFoldable putHashJWT branchDefnLookup
putFoldable putHashJWT branchPatchLookup
putFoldable (putPair putHashJWT putHashJWT) branchChildLookup
putSyncCausal TempEntity.TempCausalFormat {valueHash, parents} = do
putSyncCausal TempEntity.TempCausalFormat' {valueHash, parents} = do
putHashJWT valueHash
putFoldable putHashJWT parents
putSyncFullPatch lids bytes = do
@ -827,7 +827,7 @@ getTempNamespaceFormat =
getTempCausalFormat :: MonadGet m => m TempEntity.TempCausalFormat
getTempCausalFormat =
TempEntity.TempCausalFormat
TempEntity.TempCausalFormat'
<$> getHashJWT
<*> getVector getHashJWT

View File

@ -26,4 +26,6 @@ type TempPatchFormat = Patch.SyncPatchFormat' HashJWT Text Base32Hex HashJWT
type TempNamespaceFormat = Namespace.SyncBranchFormat' HashJWT Text HashJWT HashJWT (HashJWT, HashJWT)
data TempCausalFormat = TempCausalFormat {valueHash :: HashJWT, parents :: Vector HashJWT}
type TempCausalFormat = TempCausalFormat' HashJWT HashJWT
data TempCausalFormat' valueHash parent = TempCausalFormat' {valueHash :: valueHash, parents :: Vector parent}

View File

@ -42,6 +42,7 @@ dependencies:
- generic-lens
- monad-validate
- mtl
- nonempty-containers
- safe
- sqlite-simple
- text

View File

@ -46,15 +46,15 @@ create table temp_entity (
-- +------------------------+
--
-- temp_entity_missing_dependency
-- +-----------------------------------+
-- | dependent | dependency | jwt |
-- |===================================|
-- | #foo | #bar | aT.Eb.cx |
-- +-----------------------------------+
-- +----------------------------------------+
-- | dependent | dependency | dependencyJwt |
-- |========================================|
-- | #foo | #bar | aT.Eb.cx |
-- +----------------------------------------+
create table temp_entity_missing_dependency (
dependent text not null references temp_entity(hash),
dependency text not null,
jwt text not null,
dependencyJwt text not null,
unique (dependent, dependency)
);
create index temp_entity_missing_dependency_ix_dependent on temp_entity_missing_dependency (dependent);

View File

@ -83,6 +83,7 @@ library
, lens
, monad-validate
, mtl
, nonempty-containers
, safe
, sqlite-simple
, text

View File

@ -18,13 +18,17 @@ import qualified Data.Set as Set
import Data.Set.NonEmpty (NESet)
import qualified Data.Set.NonEmpty as NESet
import U.Codebase.HashTags (CausalHash (unCausalHash))
import qualified U.Codebase.Sqlite.Branch.Format as NamespaceFormat
import U.Codebase.Sqlite.Connection (Connection)
import U.Codebase.Sqlite.DbId (HashId)
import U.Codebase.Sqlite.DbId (CausalHashId, HashId)
import qualified U.Codebase.Sqlite.Decl.Format as DeclFormat
import qualified U.Codebase.Sqlite.Patch.Format as PatchFormat
import qualified U.Codebase.Sqlite.Queries as Q
import qualified U.Codebase.Sqlite.Serialization as S
import U.Codebase.Sqlite.TempEntity (TempEntity)
import qualified U.Codebase.Sqlite.TempEntity as TempEntity
import qualified U.Codebase.Sqlite.TempEntityType as TempEntity
import qualified U.Codebase.Sqlite.Term.Format as TermFormat
import qualified U.Util.Base32Hex as Base32Hex
import qualified U.Util.Hash as Hash
import Unison.Prelude
@ -218,28 +222,24 @@ download conn repoName = do
NEMap.toList entities & foldMapM \(hash, entity) -> do
let putInMainStorage :: Share.Hash -> Share.Entity Text Share.Hash Share.HashJWT -> IO ()
putInMainStorage _hash _entity = undefined
let putInTempStorage :: Share.Hash -> Share.Entity Text Share.Hash Share.HashJWT -> IO ()
putInTempStorage _hash entity = do
let putInTempStorage :: Share.Hash -> Share.Entity Text Share.Hash Share.HashJWT -> NESet Share.DecodedHashJWT -> IO ()
putInTempStorage _hash entity _missingDependencies = do
-- convert the blob to the data type we have a serializer for
let tempEntity = makeTempEntity entity
_entityType = tempEntityType entity
entityType = tempEntityType entity
-- serialize the blob
let _bytes = runPutS (S.putTempEntity tempEntity)
let bytes = runPutS (S.putTempEntity tempEntity)
-- insert the blob
undefined
let insertMissingDependencies = undefined
-- select dependency
-- from temp_entity_missing_dependency
-- where dependent = <this entity>
let getTempEntityMissingDependencies :: Share.Entity Text Share.Hash Share.HashJWT -> IO (Set Share.DecodedHashJWT)
getTempEntityMissingDependencies = undefined
runDB do
Q.insertTempEntity _hash _bytes _entityType _missingDependencies
inMainStorage hash >>= \case
True -> pure Set.empty
inMainStorage hash >>= \case
True -> pure Set.empty
False ->
inTempStorage hash >>= \case
True -> getTempEntityMissingDependencies entity
False -> do
missingDependencies <- Set.filterM (inMainStorage . decodedHashJWTHash) (directDepsOfEntity entity)
if Set.null missingDependencies
missingDependencies <- Set.filterM (inMainStorage . decodedHashJWTHash) (directDepsOfEntity entity)
if Set.null missingDependencies
then putInMainStorage hash entity
@ -247,6 +247,7 @@ download conn repoName = do
putInTempStorage hash entity
insertMissingDependencies hash missingDependencies
pure missingDependencies
case NESet.nonEmptySet missingDependencies0 of
case NESet.nonEmptySet missingDependencies0 of
Nothing -> pure ()
@ -408,11 +409,34 @@ _uploadEntities = undefined
makeTempEntity :: Share.Entity Text Share.Hash Share.HashJWT -> TempEntity
makeTempEntity e = case e of
Share.TC _ -> (TempEntity.TC _)
Share.DC _ -> (TempEntity.DC _)
Share.P _ -> (TempEntity.P _)
Share.N _ -> (TempEntity.N _)
Share.C _ -> (TempEntity.C _)
Share.P _ -> undefined -- (TempEntity.P _)
Share.N _ -> undefined -- (TempEntity.N _)
Share.C _ -> undefined -- (TempEntity.C _)
-- have to convert from Entity format to TempEntity format (`makeTempEntity` on 414)
-- also have to convert from TempEntity format to Sync format — this means exchanging Text for TextId and `Base32Hex`es for `HashId`s and/or `ObjectId`s
convertTempTermComponent :: TempEntity.TempTermFormat -> IO TermFormat.SyncTermFormat
convertTempTermComponent = do
undefined
convertTempDeclComponent :: TempEntity.TempDeclFormat -> IO DeclFormat.SyncDeclFormat
convertTempDeclComponent = do
undefined
convertTempPatch :: TempEntity.TempPatchFormat -> IO PatchFormat.SyncPatchFormat
convertTempPatch = do
undefined
convertTempNamespace :: TempEntity.TempNamespaceFormat -> IO NamespaceFormat.SyncBranchFormat
convertTempNamespace = do
undefined
convertTempCausal :: TempEntity.TempCausalFormat -> IO (TempEntity.TempCausalFormat' CausalHashId CausalHashId) -- could probably use a better type name here
convertTempCausal = do
undefined
tempEntityType :: Share.Entity Text Share.Hash Share.HashJWT -> TempEntity.TempEntityType
tempEntityType :: Share.Entity Text Share.Hash Share.HashJWT -> TempEntity.TempEntityType
tempEntityType = \case