I have a large video file sharded in a Cassandra table. I am trying stream it back to the API client, by using Source
streaming.
My service code looks like below,
def getShards(id: String, shards: Int) = {
def getShardsInternal(shardNo: Int, shards: Future[Array[Byte]]): Future[Array[Byte]] = {
if (shardNo == 0) shards
else getShardsInternal(shardNo - 1, shards.flatMap(x => Database.ShardModel.find(id, shardNo)))
}
getShardsInternal(shards, Future.successful(Array()))
}
In my AKKA HTTP route, I try to build a Source
from the returned future, as shown below,
def getAsset = get {
pathPrefix("asset") {
parameters('id) { id =>
complete {
val f = mediaService.getMetadata(id).flatMap { x =>
mediaService.getShards(id, x.shards)
}
Source.fromFuture(f)
}
}
}
}
I am not sure how Source.fromFuture
gets committed to the response. The future that is passed is essentially a series of flat-mapped futures expected to execute sequentially. However, I don't believe this will go back as a chunked byte stream back to the client.
Any pointers on this will be highly appreciated.
EDIT 1 I have been trying to narrow this down further with the following,
get {
pathPrefix("asset") {
parameters('id) { id =>
complete {
Source.fromFuture {
Future.successful("Hello".getBytes()).flatMap(x => Future.successful("World".getBytes()))
}
}
}
}
}
I was expecting this to return
[72,101,108,108,111,32,87,111,114,108,100]
However, I only get the result of the last future as below,
[[87,111,114,108,100]]
Kind regards Meeraj