-
Notifications
You must be signed in to change notification settings - Fork 643
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
AWS S3: Add getObjectByRanges to S3 API #2982
Open
gael-ft
wants to merge
2
commits into
akka:main
Choose a base branch
from
gael-ft:byte-range-s3-download
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
128 changes: 128 additions & 0 deletions
128
s3/src/main/scala/akka/stream/alpakka/s3/impl/MergeOrderedN.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,128 @@ | ||
package akka.stream.alpakka.s3.impl | ||
|
||
import akka.annotation.InternalApi | ||
import akka.stream.stage.{GraphStage, GraphStageLogic, InHandler, OutHandler} | ||
import akka.stream.{Attributes, Inlet, Outlet, UniformFanInShape} | ||
|
||
import scala.collection.{immutable, mutable} | ||
|
||
@InternalApi private[impl] object MergeOrderedN { | ||
/** @see [[MergeOrderedN]] */ | ||
def apply[T](inputPorts: Int, breadth: Int) = | ||
new MergeOrderedN[T](inputPorts, breadth) | ||
} | ||
|
||
/** | ||
* Takes multiple streams (in ascending order of input ports) whose elements will be pushed only if all elements from the | ||
* previous stream(s) are already pushed downstream. | ||
* | ||
* The `breadth` controls how many upstream are pulled in parallel. | ||
* That means elements might be received in any order, but will be buffered (if necessary) until their time comes. | ||
* | ||
* '''Emits when''' the next element from upstream (in ascending order of input ports) is available | ||
* | ||
* '''Backpressures when''' downstream backpressures | ||
* | ||
* '''Completes when''' all upstreams complete and there are no more buffered elements | ||
* | ||
* '''Cancels when''' downstream cancels | ||
*/ | ||
@InternalApi private[impl] final class MergeOrderedN[T](val inputPorts: Int, val breadth: Int) extends GraphStage[UniformFanInShape[T, T]] { | ||
require(inputPorts > 1, "input ports must be > 1") | ||
require(breadth > 0, "breadth must be > 0") | ||
|
||
val in: immutable.IndexedSeq[Inlet[T]] = Vector.tabulate(inputPorts)(i => Inlet[T]("MergeOrderedN.in" + i)) | ||
val out: Outlet[T] = Outlet[T]("MergeOrderedN.out") | ||
override val shape: UniformFanInShape[T, T] = UniformFanInShape(out, in: _*) | ||
|
||
override def createLogic(inheritedAttributes: Attributes): GraphStageLogic = new GraphStageLogic(shape) with OutHandler { | ||
private val bufferByInPort = mutable.Map.empty[Int, mutable.Queue[T]] // Queue must not be empty, if so entry should be removed | ||
private var currentHeadInPortIdx = 0 | ||
private var currentLastInPortIdx = 0 | ||
private val overallLastInPortIdx = inputPorts - 1 | ||
|
||
setHandler(out, this) | ||
|
||
in.zipWithIndex.foreach { case (inPort, idx) => | ||
setHandler(inPort, new InHandler { | ||
override def onPush(): Unit = { | ||
val elem = grab(inPort) | ||
if (currentHeadInPortIdx != idx || !isAvailable(out)) { | ||
bufferByInPort.updateWith(idx) { | ||
case Some(inPortBuffer) => | ||
Some(inPortBuffer.enqueue(elem)) | ||
case None => | ||
val inPortBuffer = mutable.Queue.empty[T] | ||
inPortBuffer.enqueue(elem) | ||
Some(inPortBuffer) | ||
} | ||
} else { | ||
pushUsingQueue(Some(elem)) | ||
} | ||
tryPull(inPort) | ||
} | ||
|
||
override def onUpstreamFinish(): Unit = { | ||
if (canCompleteStage) | ||
completeStage() | ||
else if (canSlideFrame) | ||
slideFrame() | ||
} | ||
}) | ||
} | ||
|
||
override def onPull(): Unit = pushUsingQueue() | ||
|
||
private def pushUsingQueue(next: Option[T] = None): Unit = { | ||
val maybeBuffer = bufferByInPort.get(currentHeadInPortIdx) | ||
if (maybeBuffer.forall(_.isEmpty) && next.nonEmpty) { | ||
push(out, next.get) | ||
} else if (maybeBuffer.exists(_.nonEmpty) && next.nonEmpty) { | ||
maybeBuffer.get.enqueue(next.get) | ||
push(out, maybeBuffer.get.dequeue()) | ||
} else if (maybeBuffer.exists(_.nonEmpty) && next.isEmpty) { | ||
push(out, maybeBuffer.get.dequeue()) | ||
} else { | ||
// Both empty | ||
} | ||
|
||
if (maybeBuffer.exists(_.isEmpty)) | ||
bufferByInPort.remove(currentHeadInPortIdx) | ||
|
||
if (canCompleteStage) | ||
completeStage() | ||
else if (canSlideFrame) | ||
slideFrame() | ||
} | ||
|
||
override def preStart(): Unit = { | ||
if (breadth >= inputPorts) { | ||
in.foreach(pull) | ||
currentLastInPortIdx = overallLastInPortIdx | ||
} else { | ||
in.slice(0, breadth).foreach(pull) | ||
currentLastInPortIdx = breadth - 1 | ||
} | ||
} | ||
|
||
private def canSlideFrame: Boolean = | ||
(!bufferByInPort.contains(currentHeadInPortIdx) || bufferByInPort(currentHeadInPortIdx).isEmpty) && | ||
isClosed(in(currentHeadInPortIdx)) | ||
|
||
private def canCompleteStage: Boolean = | ||
canSlideFrame && currentHeadInPortIdx == overallLastInPortIdx | ||
|
||
private def slideFrame(): Unit = { | ||
currentHeadInPortIdx += 1 | ||
|
||
if (isAvailable(out)) | ||
pushUsingQueue() | ||
|
||
if (currentLastInPortIdx != overallLastInPortIdx) | ||
currentLastInPortIdx += 1 | ||
|
||
if (!hasBeenPulled(in(currentLastInPortIdx))) | ||
tryPull(in(currentLastInPortIdx)) | ||
} | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
ConcatPreFetch
or something like that would be more correct , merge in Akka streams generally mean emit in any order.