Skip to content

Commit 7b1ce34

Browse files
committed
Fix bugs in depth generation
1 parent fdf775f commit 7b1ce34

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

sql/2024-12-16-00-00_entity_depths.sql

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -128,55 +128,53 @@ BEGIN
128128
FROM (
129129
-- direct term references
130130
( SELECT cd.depth AS depth
131-
FROM component_depth cd
132-
JOIN term_local_component_references cr
133-
ON cd.component_hash_id = cr.component_hash_id
131+
FROM namespace_terms nt
134132
JOIN terms t
135-
ON cr.term_id = t.id
136-
JOIN namespace_terms nt
137-
ON t.id = nt.term_id
133+
ON nt.term_id = t.id
134+
JOIN component_depth cd
135+
ON t.component_hash_id = cd.component_hash_id
138136
WHERE nt.namespace_hash_id = the_namespace_hash_id
139137
) UNION
140138
-- term metadata references
141139
( SELECT cd.depth AS depth
142-
FROM component_depth cd
143-
JOIN terms t
144-
ON cd.component_hash_id = t.component_hash_id
140+
FROM namespace_terms nt
145141
JOIN namespace_term_metadata ntm
146-
ON ntm.metadata_term_id = t.id
147-
JOIN namespace_terms nt
148142
ON ntm.named_term = nt.id
143+
JOIN terms t
144+
ON ntm.metadata_term_id = t.id
145+
JOIN component_depth cd
146+
ON t.component_hash_id = cd.component_hash_id
149147
WHERE nt.namespace_hash_id = the_namespace_hash_id
150148
) UNION
151149
-- direct constructor references
152150
( SELECT cd.depth AS depth
153-
FROM component_depth cd
151+
FROM namespace_terms nt
154152
JOIN constructors c
155-
ON cd.component_hash_id = c.constructor_type_component_hash_id
156-
JOIN namespace_terms nt
157153
ON c.id = nt.constructor_id
154+
JOIN types t
155+
ON c.type_id = t.id
156+
JOIN component_depth cd
157+
ON t.component_hash_id = cd.component_hash_id
158158
WHERE nt.namespace_hash_id = the_namespace_hash_id
159159
) UNION
160160
-- direct type references
161161
( SELECT cd.depth AS depth
162-
FROM component_depth cd
163-
JOIN type_local_component_references cr
164-
ON cd.component_hash_id = cr.component_hash_id
162+
FROM namespace_types nt
165163
JOIN types t
166-
ON cr.type_id = t.id
167-
JOIN namespace_types nt
168-
ON t.id = nt.type_id
164+
ON nt.type_id = t.id
165+
JOIN component_depth cd
166+
ON t.component_hash_id = cd.component_hash_id
169167
WHERE nt.namespace_hash_id = the_namespace_hash_id
170168
) UNION
171169
-- type metadata references
172170
( SELECT cd.depth AS depth
173-
FROM component_depth cd
174-
JOIN terms t
175-
ON cd.component_hash_id = t.component_hash_id
171+
FROM namespace_types nt
176172
JOIN namespace_type_metadata ntm
177-
ON ntm.metadata_term_id = t.id
178-
JOIN namespace_types nt
179173
ON ntm.named_type = nt.id
174+
JOIN terms t
175+
ON ntm.metadata_term_id = t.id
176+
JOIN component_depth cd
177+
ON t.component_hash_id = cd.component_hash_id
180178
WHERE nt.namespace_hash_id = the_namespace_hash_id
181179
)
182180
) AS refs;
@@ -202,19 +200,27 @@ BEGIN
202200
SELECT COALESCE(MAX(cd.depth), -1) INTO max_referenced_component_depth
203201
FROM (
204202
-- term references
205-
( SELECT from_term_component_hash_id AS component_hash_id
206-
FROM patch_term_mappings
207-
WHERE patch_id = the_patch_id
203+
( SELECT t.component_hash_id AS component_hash_id
204+
FROM patch_term_mappings ptm
205+
JOIN terms t
206+
ON ptm.to_term_id = t.id
207+
WHERE ptm.patch_id = the_patch_id
208208
) UNION
209209
-- constructor mappings
210-
( SELECT from_constructor_component_hash_id AS component_hash_id
211-
FROM patch_constructor_mappings
212-
WHERE patch_id = the_patch_id
210+
( SELECT t.component_hash_id AS component_hash_id
211+
FROM patch_constructor_mappings pcm
212+
JOIN constructors c
213+
ON pcm.to_constructor_id = c.id
214+
JOIN types t
215+
ON c.type_id = t.id
216+
WHERE pcm.patch_id = the_patch_id
213217
) UNION
214218
-- type references
215-
( SELECT from_type_component_hash_id AS component_hash_id
216-
FROM patch_type_mappings
217-
WHERE patch_id = the_patch_id
219+
( SELECT t.component_hash_id AS component_hash_id
220+
FROM patch_type_mappings ptm
221+
JOIN types t
222+
ON ptm.to_type_id = t.id
223+
WHERE ptm.patch_id = the_patch_id
218224
)
219225
) AS refs JOIN component_depth cd
220226
ON cd.component_hash_id = refs.component_hash_id;

src/Share/Web/UCM/SyncV2/Impl.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ batchSize :: Int32
4949
batchSize = 1000
5050

5151
streamSettings :: Hash32 -> Maybe SyncV2.BranchRef -> StreamInitInfo
52-
streamSettings rootCausalHash rootBranchRef = StreamInitInfo {version = SyncV2.Version 1, entitySorting = SyncV2.Unsorted, numEntities = Nothing, rootCausalHash, rootBranchRef}
52+
streamSettings rootCausalHash rootBranchRef = StreamInitInfo {version = SyncV2.Version 1, entitySorting = SyncV2.DependenciesFirst, numEntities = Nothing, rootCausalHash, rootBranchRef}
5353

5454
server :: Maybe UserId -> SyncV2.Routes WebAppServer
5555
server mayUserId =

src/Share/Web/UCM/SyncV2/Queries.hs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,30 +173,35 @@ allSerializedDependenciesOfCausalCursor cid exceptCausalHashes = do
173173
WHERE NOT EXISTS (SELECT FROM except_components ec WHERE ec.component_hash_id = ref.component_hash_id)
174174
)
175175
)
176-
(SELECT bytes.bytes, ch.base32
176+
(SELECT bytes.bytes, ch.base32, cd.depth
177177
FROM transitive_components tc
178178
JOIN serialized_components sc ON sc.user_id = #{ownerUserId} AND tc.component_hash_id = sc.component_hash_id
179179
JOIN bytes ON sc.bytes_id = bytes.id
180180
JOIN component_hashes ch ON tc.component_hash_id = ch.id
181+
JOIN component_depth cd ON ch.id = cd.component_hash_id
181182
)
182183
UNION ALL
183-
(SELECT bytes.bytes, ap.patch_hash
184+
(SELECT bytes.bytes, ap.patch_hash, pd.depth
184185
FROM all_patches ap
185186
JOIN serialized_patches sp ON ap.patch_id = sp.patch_id
186187
JOIN bytes ON sp.bytes_id = bytes.id
188+
JOIN patch_depth pd ON ap.patch_id = pd.patch_id
187189
)
188190
UNION ALL
189-
(SELECT bytes.bytes, an.namespace_hash
191+
(SELECT bytes.bytes, an.namespace_hash, nd.depth
190192
FROM all_namespaces an
191193
JOIN serialized_namespaces sn ON an.namespace_hash_id = sn.namespace_hash_id
192194
JOIN bytes ON sn.bytes_id = bytes.id
195+
JOIN namespace_depth nd ON an.namespace_hash_id = nd.namespace_hash_id
193196
)
194197
UNION ALL
195-
(SELECT bytes.bytes, tc.causal_hash
198+
(SELECT bytes.bytes, tc.causal_hash, cd.depth
196199
FROM transitive_causals tc
197200
JOIN serialized_causals sc ON tc.causal_id = sc.causal_id
198201
JOIN bytes ON sc.bytes_id = bytes.id
202+
JOIN causal_depth cd ON tc.causal_id = cd.causal_id
199203
)
204+
ORDER BY depth ASC
200205
|]
201206
pure cursor
202207

0 commit comments

Comments
 (0)