@@ -740,7 +740,7 @@ <ASPECT_UNION extends RecordTemplate> URN createAspectsWithCallbacks(@Nonnull UR
740740 boolean isTestModeFalseForAll = aspectCreateLambdas .stream ().filter (aspectCreateLambda -> aspectCreateLambda .getIngestionParams ().isTestMode ()).collect (
741741 Collectors .toList ()).isEmpty ();
742742
743- int numRows = createNewAspect (urn , aspectCreateLambdas , aspectValues , auditStamp , trackingContext , isTestModeFalseForAll );
743+ int numRows = createNewAssetWithAspects (urn , aspectCreateLambdas , aspectValues , auditStamp , trackingContext , isTestModeFalseForAll );
744744 for (RecordTemplate aspectValue : aspectValues ) {
745745 // For each aspect, we need to trigger emit MAE
746746 // In new asset creation, old value is null
@@ -962,54 +962,6 @@ public <ASPECT extends RecordTemplate> URN create(@Nonnull URN urn,
962962 );
963963 }
964964
965- /**
966- * The common method that can be used for both: deletion of aspects and entity.
967- *
968- * @param urn the URN for the entity the aspects are attached to
969- * @param aspectClasses Aspect Classes of the aspects being deleted, must be supported aspect types in
970- * {@code ASPECT_UNION}
971- * @param auditStamp the audit stamp of this action
972- * @param trackingContext the tracking context for the operation
973- * @param ingestionParams ingestion parameters
974- * @param deleteAll if true, delete the entire asset, else mark aspects as deleted iteratively in a
975- * transaction
976- * @param maxTransactionRetry maximum number of transaction retries before throwing an exception
977- * @return a collection of the deleted aspects (their value before deletion), each wrapped in an instance of
978- * {@link ASPECT_UNION}
979- */
980- protected Collection <ASPECT_UNION > deleteCommon (@ Nonnull URN urn ,
981- @ Nonnull Set <Class <? extends RecordTemplate >> aspectClasses ,
982- @ Nonnull AuditStamp auditStamp , int maxTransactionRetry ,
983- @ Nullable IngestionTrackingContext trackingContext ,
984- @ Nullable IngestionParams ingestionParams ,
985- boolean deleteAll ) {
986-
987- // TODO: Handle pre-deletion callbacks if any
988-
989- // If deleteAll is true, delete entire asset, else mark aspects as deleted iteratively in a transaction
990- if (deleteAll ) {
991- Map <Class <? extends RecordTemplate >, Optional <? extends RecordTemplate >>
992- results = permanentDelete (urn , aspectClasses , auditStamp , maxTransactionRetry , trackingContext , ingestionParams .isTestMode ());
993- Collection <RecordTemplate > deletedAspects = new ArrayList <>();
994- results .forEach ((key , value ) -> {
995- // Check if aspect value present to avoid null pointer exception
996- if (value .isPresent ()) {
997- DeleteResult deleteResult = new DeleteResult (value .get (), key );
998- deletedAspects .add (unwrapDeleteResult (urn , deleteResult , auditStamp , trackingContext , ChangeType .DELETE_ALL ));
999- }
1000- });
1001-
1002- return deletedAspects .stream ()
1003- .filter (Objects ::nonNull )
1004- .map (x -> ModelUtils .newEntityUnion (_aspectUnionClass , x )).collect (Collectors .toList ());
1005- } else {
1006- // TODO: delete aspects implementation can be moved here instead of in addCommon()
1007- // Add common method should be used only for create and update
1008- return Collections .emptyList ();
1009- }
1010-
1011- //TODO: Handle post-ingestion callbacks if any
1012- }
1013965
1014966 /**
1015967 * Delete asset and all its aspects atomically.
@@ -1076,9 +1028,36 @@ public Collection<ASPECT_UNION> deleteAll(@Nonnull URN urn,
10761028 int maxTransactionRetry ,
10771029 @ Nullable IngestionTrackingContext trackingContext ,
10781030 @ Nullable IngestionParams ingestionParams ) {
1031+
10791032 IngestionParams nonNullIngestionParams = ingestionParams == null
10801033 ? new IngestionParams ().setIngestionMode (IngestionMode .LIVE ).setTestMode (false ) : ingestionParams ;
1081- return deleteCommon (urn , aspectClasses , auditStamp , maxTransactionRetry , trackingContext , nonNullIngestionParams , true );
1034+
1035+ final Map <Class <?>, RecordTemplate > results = new HashMap <>();
1036+ runInTransactionWithRetry (() -> {
1037+ aspectClasses .forEach (aspectClass -> {
1038+ try {
1039+ RecordTemplate deletedAspect = delete (urn , aspectClass , auditStamp , maxTransactionRetry , trackingContext );
1040+ results .put (aspectClass , deletedAspect );
1041+ } catch (NullPointerException e ) {
1042+ log .warn ("Aspect {} for urn {} does not exist" , aspectClass .getName (), urn );
1043+ }
1044+ });
1045+
1046+ permanentDelete (urn , nonNullIngestionParams .isTestMode ());
1047+ return results ;
1048+ }, maxTransactionRetry );
1049+
1050+
1051+ Collection <RecordTemplate > deletedAspects = new ArrayList <>();
1052+ results .forEach ((key , value ) -> {
1053+ // Check if aspect value present to avoid null pointer exception
1054+ DeleteResult deleteResult = new DeleteResult (value , key );
1055+ deletedAspects .add (unwrapDeleteResult (urn , deleteResult , auditStamp , trackingContext , ChangeType .DELETE_ALL ));
1056+ });
1057+
1058+ return deletedAspects .stream ()
1059+ .filter (Objects ::nonNull )
1060+ .map (x -> ModelUtils .newEntityUnion (_aspectUnionClass , x )).collect (Collectors .toList ());
10821061 }
10831062
10841063 /**
@@ -1340,24 +1319,19 @@ protected abstract <ASPECT extends RecordTemplate> long saveLatest(@Nonnull URN
13401319 @ Nullable ASPECT newEntry , @ Nonnull AuditStamp newAuditStamp , boolean isSoftDeleted ,
13411320 @ Nullable IngestionTrackingContext trackingContext , boolean isTestMode );
13421321
1343- protected abstract <ASPECT_UNION extends RecordTemplate > int createNewAspect (@ NonNull URN urn ,
1322+ protected abstract <ASPECT_UNION extends RecordTemplate > int createNewAssetWithAspects (@ NonNull URN urn ,
13441323 @ Nonnull List <AspectCreateLambda <? extends RecordTemplate >> aspectCreateLambdas ,
13451324 @ Nonnull List <? extends RecordTemplate > aspectValues , @ Nonnull AuditStamp newAuditStamp ,
13461325 @ Nullable IngestionTrackingContext trackingContext , boolean isTestMode );
13471326
13481327 /**
1349- * Permanently deletes the entity from the table.
1350- * @param urn the URN for the entity the aspect is attached to
1351- * @param aspectClasses Aspect Classes of the aspects being deleted, must be supported aspect types in {@code ASPECT_UNION}
1352- * @param auditStamp the audit stamp of this action
1353- * @param maxTransactionRetry maximum number of transaction retries before throwing an exception
1354- * @param trackingContext the tracking context for the operation
1328+ * Mark the asset as deleted.
1329+ *
1330+ * @param urn the URN for the entity the aspect is attached to
13551331 * @param isTestMode whether the test mode is enabled or not
1356- * @return a map of the deleted aspects (their value before deletion), each wrapped in an instance of {@link ASPECT_UNION}
1332+ * @return the number of rows updated in delete operation.
13571333 */
1358- protected abstract Map <Class <? extends RecordTemplate >, Optional <? extends RecordTemplate >> permanentDelete (@ Nonnull URN urn ,
1359- @ Nonnull Set <Class <? extends RecordTemplate >> aspectClasses , @ Nullable AuditStamp auditStamp ,
1360- int maxTransactionRetry , @ Nullable IngestionTrackingContext trackingContext , boolean isTestMode );
1334+ protected abstract int permanentDelete (@ Nonnull URN urn , boolean isTestMode );
13611335
13621336 /**
13631337 * Saves the new value of an aspect to entity tables. This is used when backfilling metadata from the old schema to
@@ -1572,13 +1546,13 @@ protected abstract <ASPECT extends RecordTemplate> void insert(@Nonnull URN urn,
15721546 /**
15731547 * Update an aspect for an entity with specific version and {@link AuditStamp} with optimistic locking.
15741548 *
1575- * @param urn {@link Urn} for the entity
1576- * @param value the aspect to update
1577- * @param aspectClass the type of aspect to update
1549+ * @param urn {@link Urn} for the entity
1550+ * @param value the aspect to update
1551+ * @param aspectClass the type of aspect to update
15781552 * @param newAuditStamp the {@link AuditStamp} for the new aspect
1579- * @param version the version for the aspect
1580- * @param oldTimestamp the timestamp for the old aspect
1581- * @param isTestMode whether the test mode is enabled or not
1553+ * @param version the version for the aspect
1554+ * @param oldTimestamp the timestamp for the old aspect
1555+ * @param isTestMode whether the test mode is enabled or not
15821556 */
15831557 protected abstract <ASPECT extends RecordTemplate > void updateWithOptimisticLocking (@ Nonnull URN urn ,
15841558 @ Nullable RecordTemplate value , @ Nonnull Class <ASPECT > aspectClass , @ Nonnull AuditStamp newAuditStamp ,
0 commit comments