-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
#247: Paging support in Reader module #316
Changes from all commits
1488d1f
96ffa33
8b69e3e
04b56bd
698b765
855a333
be90711
afd64a6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2021 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.atum.reader.exceptions | ||
|
||
class ReaderException(message: String) extends Exception(message) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2021 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.atum.reader.exceptions | ||
|
||
import sttp.model.{StatusCode, Uri} | ||
import za.co.absa.atum.model.envelopes.ErrorResponse | ||
|
||
abstract class RequestException(message: String) extends ReaderException(message) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could make this class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scalafmt formatting is off (hasn't been applied). |
||
|
||
|
||
object RequestException { | ||
type CirceError = io.circe.Error | ||
|
||
final case class HttpException( | ||
message: String, | ||
statusCode: StatusCode, | ||
errorResponse: ErrorResponse, | ||
request: Uri | ||
) extends RequestException(message) | ||
|
||
final case class ParsingException( | ||
message: String, | ||
body: String | ||
) extends RequestException(message) | ||
object ParsingException { | ||
def fromCirceError(error: CirceError, body: String): ParsingException = { | ||
ParsingException(error.getMessage, body) | ||
} | ||
} | ||
|
||
|
||
final case class NoDataException( | ||
message: String | ||
) extends RequestException(message) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2021 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.atum.reader.result | ||
|
||
import sttp.monad.MonadError | ||
|
||
abstract class AbstractPage [T <: Iterable[_], F[_]: MonadError] { | ||
def items: T | ||
def hasNext: Boolean | ||
def limit: Int | ||
def pageStart: Long | ||
def pageEnd: Long | ||
|
||
def pageSize: Int = (pageEnd - pageStart).toInt + 1 | ||
def hasPrior: Boolean = pageStart > 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if pageStart is 0 or negative but the absolute value is still lower than pageSize? Could this happen? |
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/* | ||
* Copyright 2021 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.atum.reader.result | ||
|
||
import sttp.monad.MonadError | ||
import sttp.monad.syntax._ | ||
import za.co.absa.atum.reader.core.RequestResult.{RequestFail, RequestResult} | ||
import za.co.absa.atum.reader.exceptions.RequestException.NoDataException | ||
import za.co.absa.atum.reader.result.GroupedPage.GroupPageRoller | ||
|
||
import scala.collection.immutable.ListMap | ||
|
||
case class GroupedPage[K, V, F[_]: MonadError]( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you elaborate on the usage of this class? What do we need it for? I often try to limit the scope of my PRs as much as possible. |
||
items: ListMap[K, Vector[V]], | ||
hasNext: Boolean, | ||
limit: Int, | ||
pageStart: Long, | ||
pageEnd: Long, | ||
private[reader] val pageRoller: GroupPageRoller[K, V, F] | ||
) extends AbstractPage[Map[K, Vector[V]], F] { | ||
|
||
def apply(key: K): Vector[V] = items(key) | ||
def keys: Iterable[K] = items.keys | ||
def groupCount: Int = items.size | ||
|
||
def map[K1, V1](f: ((K, Vector[V])) => (K1, Vector[V1])): GroupedPage[K1, V1, F] = { | ||
val newItems = items.map(f) | ||
val newPageRoller: GroupPageRoller[K1, V1, F] = (limit, offset) => pageRoller(limit, offset).map(_.map(_.map(f))) | ||
this.copy(items = newItems, pageRoller = newPageRoller) | ||
} | ||
|
||
def mapValues[B](f: V => B): GroupedPage[K, B, F] = { | ||
def mapper(item: (K, Vector[V])): (K, Vector[B]) = (item._1, item._2.map(f)) | ||
|
||
val newItems = items.map(mapper) | ||
val newPageRoller: GroupPageRoller[K, B, F] = (limit, offset) => pageRoller(limit, offset).map(_.map(_.mapValues(f))) | ||
this.copy(items = newItems, pageRoller = newPageRoller) | ||
|
||
} | ||
|
||
def prior(newPageSize: Int): F[RequestResult[GroupedPage[K, V, F]]] = { | ||
if (hasPrior) { | ||
val newOffset = (pageStart - limit).max(0) | ||
pageRoller(newPageSize, newOffset) | ||
} else { | ||
MonadError[F].unit(RequestFail(NoDataException("No prior page"))) | ||
} | ||
} | ||
|
||
def prior: F[RequestResult[GroupedPage[K, V, F]]] = prior(limit) | ||
|
||
def next(newPageSize: Int): F[RequestResult[GroupedPage[K, V, F]]] = { | ||
if (hasNext) { | ||
pageRoller(newPageSize, pageStart + limit) | ||
} else { | ||
MonadError[F].unit(RequestFail(NoDataException("No next page"))) | ||
} | ||
} | ||
|
||
def next: F[RequestResult[GroupedPage[K, V, F]]] = next(limit) | ||
|
||
def +(other: GroupedPage[K, V, F]): GroupedPage[K, V, F] = { | ||
val newItems = other.items.foldLeft(items) { case (acc, (k, v)) => | ||
if (acc.contains(k)) { | ||
acc.updated(k, acc(k) ++ v) | ||
} else { | ||
acc + (k -> v) | ||
} | ||
} | ||
val newHasNext = hasNext && other.hasNext | ||
val newPageStart = pageStart min other.pageStart | ||
val newPageEnd = pageEnd max other.pageEnd | ||
this.copy(items = newItems, hasNext = newHasNext, pageStart = newPageStart, pageEnd = newPageEnd) | ||
} | ||
} | ||
|
||
object GroupedPage { | ||
type GroupPageRoller[K, V, F[_]] = (Int, Long) => F[RequestResult[GroupedPage[K, V, F]]] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer this style to be explicit, WDYT?
import za.co.absa.atum.reader.core.RequestResult.RequestResult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am also in favor of using fully qualified imports.