@@ -5,14 +5,15 @@ use diesel::prelude::*;
55use diesel:: r2d2:: { ConnectionManager , PooledConnection } ;
66use graph:: anyhow:: Context ;
77use graph:: blockchain:: block_stream:: FirehoseCursor ;
8- use graph:: components:: store:: write:: { EntityRef , EntityWrite , RowGroup , RowGroups } ;
8+ use graph:: components:: store:: write:: RowGroup ;
99use graph:: components:: store:: {
1010 Batch , DerivedEntityQuery , EntityKey , EntityType , PrunePhase , PruneReporter , PruneRequest ,
1111 PruningStrategy , StoredDynamicDataSource , VersionStats ,
1212} ;
1313use graph:: components:: versions:: VERSIONS ;
1414use graph:: data:: query:: Trace ;
1515use graph:: data:: subgraph:: { status, SPEC_VERSION_0_0_6 } ;
16+ use graph:: data:: value:: Word ;
1617use graph:: data_source:: CausalityRegion ;
1718use graph:: prelude:: futures03:: FutureExt ;
1819use graph:: prelude:: {
@@ -272,7 +273,8 @@ impl DeploymentStore {
272273 & self ,
273274 conn : & PgConnection ,
274275 layout : & Layout ,
275- key : & EntityKey ,
276+ entity_type : & EntityType ,
277+ entity_id : & Word ,
276278 ) -> Result < ( ) , StoreError > {
277279 // Collect all types that share an interface implementation with this
278280 // entity type, and make sure there are no conflicting IDs.
@@ -291,100 +293,71 @@ impl DeploymentStore {
291293 . expect ( "API schema should be present" )
292294 . clone ( ) ;
293295 let types_for_interface = schema. types_for_interface ( ) ;
294- let entity_type = key . entity_type . to_string ( ) ;
296+ let entity_type_str = entity_type. to_string ( ) ;
295297 let types_with_shared_interface = Vec :: from_iter (
296298 schema
297- . interfaces_for_type ( & key . entity_type )
299+ . interfaces_for_type ( entity_type)
298300 . into_iter ( )
299301 . flatten ( )
300302 . flat_map ( |interface| & types_for_interface[ & EntityType :: from ( interface) ] )
301303 . map ( EntityType :: from)
302- . filter ( |type_name| type_name != & key . entity_type ) ,
304+ . filter ( |type_name| type_name != entity_type) ,
303305 ) ;
304306
305307 if !types_with_shared_interface. is_empty ( ) {
306308 if let Some ( conflicting_entity) =
307- layout. conflicting_entity ( conn, & key . entity_id , types_with_shared_interface) ?
309+ layout. conflicting_entity ( conn, entity_id, types_with_shared_interface) ?
308310 {
309311 return Err ( StoreError :: ConflictingId (
310- entity_type ,
311- key . entity_id . to_string ( ) ,
312+ entity_type_str ,
313+ entity_id. to_string ( ) ,
312314 conflicting_entity,
313315 ) ) ;
314316 }
315317 }
316318 Ok ( ( ) )
317319 }
318320
319- fn apply_entity_modifications (
321+ fn apply_entity_modifications < ' a > (
320322 & self ,
321323 conn : & PgConnection ,
322324 layout : & Layout ,
323- inserts : & RowGroups < EntityWrite > ,
324- overwrites : & RowGroups < EntityWrite > ,
325- removes : & RowGroups < EntityRef > ,
325+ groups : impl Iterator < Item = & ' a RowGroup > ,
326326 stopwatch : & StopwatchMetrics ,
327327 ) -> Result < i32 , StoreError > {
328328 let mut count = 0 ;
329329
330- // Apply modification groups.
331- // Inserts:
332- for group in & inserts. groups {
330+ for group in groups {
331+ if group. has_clamps ( ) {
332+ count -= self . remove_entities ( group, conn, layout, stopwatch) ? as i32 ;
333+ }
333334 count += self . insert_entities ( group, conn, layout, stopwatch) ? as i32
334335 }
335336
336- // Overwrites:
337- for group in & overwrites. groups {
338- // we do not update the count since the number of entities remains the same
339- self . overwrite_entities ( group, conn, layout, stopwatch) ?;
340- }
341-
342- // Removals
343- for group in & removes. groups {
344- count -= self . remove_entities ( group, conn, layout, stopwatch) ? as i32 ;
345- }
346337 Ok ( count)
347338 }
348339
349340 fn insert_entities < ' a > (
350341 & ' a self ,
351- group : & ' a RowGroup < EntityWrite > ,
342+ group : & ' a RowGroup ,
352343 conn : & PgConnection ,
353344 layout : & ' a Layout ,
354345 stopwatch : & StopwatchMetrics ,
355346 ) -> Result < usize , StoreError > {
356347 let section = stopwatch. start_section ( "check_interface_entity_uniqueness" ) ;
357- for row in group. rows . iter ( ) {
348+ for row in group. writes ( ) {
358349 // WARNING: This will potentially execute 2 queries for each entity key.
359- self . check_interface_entity_uniqueness ( conn, layout, & row. key ) ?;
350+ self . check_interface_entity_uniqueness ( conn, layout, & group . entity_type , & row. id ( ) ) ?;
360351 }
361352 section. end ( ) ;
362353
363354 let _section = stopwatch. start_section ( "apply_entity_modifications_insert" ) ;
364355 layout. insert ( conn, group, stopwatch)
365356 }
366357
367- fn overwrite_entities < ' a > (
368- & ' a self ,
369- group : & ' a RowGroup < EntityWrite > ,
370- conn : & PgConnection ,
371- layout : & ' a Layout ,
372- stopwatch : & StopwatchMetrics ,
373- ) -> Result < usize , StoreError > {
374- let section = stopwatch. start_section ( "check_interface_entity_uniqueness" ) ;
375- for row in group. rows . iter ( ) {
376- // WARNING: This will potentially execute 2 queries for each entity key.
377- self . check_interface_entity_uniqueness ( conn, layout, & row. key ) ?;
378- }
379- section. end ( ) ;
380-
381- let _section = stopwatch. start_section ( "apply_entity_modifications_update" ) ;
382- layout. update ( conn, group, stopwatch)
383- }
384-
385358 fn remove_entities (
386359 & self ,
387- group : & RowGroup < EntityRef > ,
360+ group : & RowGroup ,
388361 conn : & PgConnection ,
389362 layout : & Layout ,
390363 stopwatch : & StopwatchMetrics ,
@@ -1137,9 +1110,7 @@ impl DeploymentStore {
11371110 let count = self . apply_entity_modifications (
11381111 & conn,
11391112 layout. as_ref ( ) ,
1140- & batch. inserts ,
1141- & batch. overwrites ,
1142- & batch. removes ,
1113+ batch. groups ( ) ,
11431114 stopwatch,
11441115 ) ?;
11451116 section. end ( ) ;
0 commit comments