-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#118: aggregation of errors into a single one, basic three implementa…
…tions, unit tests and small refactoring
- Loading branch information
Showing
29 changed files
with
799 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
core/src/main/scala/za/co/absa/fadb/status/FunctionStatusWithData.scala
This file was deleted.
Oops, something went wrong.
56 changes: 56 additions & 0 deletions
56
core/src/main/scala/za/co/absa/fadb/status/aggregation/StatusAggregation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright 2022 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.fadb.status.aggregation | ||
|
||
import za.co.absa.fadb.exceptions.StatusException | ||
import za.co.absa.fadb.status.{ExceptionOrStatusWithDataResultAgg, ExceptionOrStatusWithDataRow, FunctionStatusWithData} | ||
|
||
/** | ||
* `StatusAggregation` is a base trait that defines the interface for aggregating the statuses of a function invocation. | ||
* It provides a method to aggregate the error statuses into a single status information - this is typically needed | ||
* for database functions that retrieve multiple records. | ||
*/ | ||
trait StatusAggregation { | ||
|
||
private [aggregation] def gatherExceptions[R]( | ||
eithersWithException: Seq[ExceptionOrStatusWithDataRow[R]] | ||
): Seq[StatusException] = { | ||
|
||
eithersWithException.flatMap { | ||
case Left(exception) => Some(exception) | ||
case _ => None | ||
} | ||
} | ||
|
||
private [aggregation] def gatherDataWithStatuses[R]( | ||
eithersWithData: Seq[ExceptionOrStatusWithDataRow[R]] | ||
): Seq[FunctionStatusWithData[R]] = { | ||
eithersWithData.flatMap { | ||
case Left(_) => None | ||
case Right(dataWithStatuses) => Some(dataWithStatuses) | ||
} | ||
} | ||
|
||
/** | ||
* Aggregates the error status information into a single error. | ||
* | ||
* @param statusesWithData - The status of the function invocation with data. | ||
* @return Either a `StatusException` if the status code indicates an error, or the data (along with the status | ||
* information so that it's retrievable) if the status being returned doesn't indicate an error. | ||
*/ | ||
def aggregate[R](statusesWithData: Seq[ExceptionOrStatusWithDataRow[R]]): ExceptionOrStatusWithDataResultAgg[R] | ||
} |
41 changes: 41 additions & 0 deletions
41
...main/scala/za/co/absa/fadb/status/aggregation/implementations/AggregateByFirstError.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2022 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.fadb.status.aggregation.implementations | ||
|
||
import za.co.absa.fadb.status.aggregation.StatusAggregation | ||
import za.co.absa.fadb.status.{ExceptionOrStatusWithDataResultAgg, ExceptionOrStatusWithDataRow} | ||
|
||
/** | ||
* `AggregateByFirstError` is a trait that extends the `StatusAggregation` interface. | ||
* It provides an implementation for aggregating error statuses of a function invocation into a single error | ||
* by choosing the first error encountered to be the representative one (i.e. if there are multiple errors of other | ||
* types being returned, only the first one would be chosen and the rest would be ignored). | ||
*/ | ||
trait AggregateByFirstError extends StatusAggregation { | ||
|
||
override def aggregate[R](statusesWithData: Seq[ExceptionOrStatusWithDataRow[R]]): ExceptionOrStatusWithDataResultAgg[R] = { | ||
val firstError = gatherExceptions(statusesWithData).headOption | ||
|
||
val dataFinal = gatherDataWithStatuses(statusesWithData) | ||
|
||
firstError match { | ||
case Some(statusException) => Left(statusException) | ||
case None => Right(dataFinal) | ||
} | ||
} | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...c/main/scala/za/co/absa/fadb/status/aggregation/implementations/AggregateByFirstRow.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* Copyright 2022 ABSA Group Limited | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package za.co.absa.fadb.status.aggregation.implementations | ||
|
||
import za.co.absa.fadb.status.aggregation.StatusAggregation | ||
import za.co.absa.fadb.status.{ExceptionOrStatusWithDataResultAgg, ExceptionOrStatusWithDataRow} | ||
|
||
/** | ||
* `AggregateByFirstRow` is a trait that extends the `StatusAggregation` interface. | ||
* It provides an implementation for aggregating error statuses of a function invocation into a single error | ||
* by choosing the first row that was returned to be the representative one | ||
* (i.e. if there is an error on row two or later, it would be ignored). | ||
*/ | ||
trait AggregateByFirstRow extends StatusAggregation { | ||
|
||
override def aggregate[R](statusesWithData: Seq[ExceptionOrStatusWithDataRow[R]]): ExceptionOrStatusWithDataResultAgg[R] = { | ||
val firstRow = statusesWithData.headOption | ||
|
||
val dataFinal = gatherDataWithStatuses(statusesWithData) | ||
|
||
firstRow match { | ||
case Some(exceptionOrDataWithStatuses) => exceptionOrDataWithStatuses match { | ||
case Left(statusException) => Left(statusException) | ||
case Right(_) => Right(dataFinal) | ||
} | ||
case None => Right(Seq.empty) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.