-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Flow reader methods to read checkpoints
* `Page` class to nicely handle paginated results * `GroupedPage` class to handle paginated results that can be grouped
- Loading branch information
Showing
5 changed files
with
155 additions
and
18 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
31 changes: 31 additions & 0 deletions
31
reader/src/main/scala/za/co/absa/atum/reader/result/AbstractPage.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,31 @@ | ||
/* | ||
* Copyright 2024 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 za.co.absa.atum.reader.basic.RequestResult.RequestResult | ||
|
||
abstract class AbstractPage [T <: Iterable[_], F[_]: MonadError] { | ||
def items: T | ||
def hasNext: Boolean | ||
def limit: Int | ||
def offset: Long | ||
|
||
def pageSize: Int = items.size | ||
def hasPrior: Boolean = offset > 0 | ||
} | ||
|
93 changes: 93 additions & 0 deletions
93
reader/src/main/scala/za/co/absa/atum/reader/result/GroupedPage.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,93 @@ | ||
/* | ||
* Copyright 2024 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.basic.RequestResult.{RequestFail, RequestResult} | ||
import za.co.absa.atum.reader.exceptions.RequestException.NoDataException | ||
import za.co.absa.atum.reader.result.GroupedPage.GroupPageRoller | ||
import za.co.absa.atum.reader.result.Page.PageRoller | ||
|
||
import scala.collection.immutable.ListMap | ||
|
||
case class GroupedPage[K, V, F[_]: MonadError]( | ||
items: ListMap[K, Vector[V]], | ||
hasNext: Boolean, | ||
limit: Int, | ||
offset: 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 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 flatten: Page[V, F] = { | ||
val newItems = items.values.flatten.toVector | ||
val newPageRoller: PageRoller[V, F] = (limit, offset) => pageRoller(limit, offset).map(_.map(_.flatten)) | ||
Page( | ||
items = newItems, | ||
hasNext = hasNext, | ||
limit = limit, | ||
offset = offset, | ||
pageRoller = newPageRoller | ||
) | ||
} | ||
|
||
def flatMap[K1, T](f: ((K, Vector[V])) => (K1, Vector[T])): Page[T, F] = { | ||
map(f).flatten | ||
} | ||
|
||
def prior(newPageSize: Int): F[RequestResult[GroupedPage[K, V, F]]] = { | ||
if (hasPrior) { | ||
val newOffset = (offset - 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, offset + limit) | ||
} else { | ||
MonadError[F].unit(RequestFail(NoDataException("No next page"))) | ||
} | ||
} | ||
|
||
def next: F[RequestResult[GroupedPage[K, V, F]]] = next(limit) | ||
} | ||
|
||
object GroupedPage { | ||
type GroupPageRoller[K, V, F[_]] = (Int, Long) => F[RequestResult[GroupedPage[K, V, F]]] | ||
} |
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