With Scala, how can I convert an Iterable[t]
to a Future[Seq[T]]
?
Basically I want to read all the values returned by a query on Apache Ignite, that returns an IgniteCursor.
I want to read from that cursor in a non blocking way.
I can write:
val iterable = cursor.asScala
val result = iterable.toList
Future{
result
}
But I think that this code is blocking, not async. I'm right?
Does trasforming an Iterable
to a Future[Seq]
make sense?
UPDATE
My goal is to get a small list of records without blocking the caller thread or a worker thread because I will receive many concurrent calls.
My Iterable
is actually an IgniteCursor
so internally I suppose it will perform some network/database operations.
Usually there are methods to perform these operations asyncronously.
For example to read a single value I can use getAsync
instead of get
.
For the cursor I just have getAll
function, so my idea was to use the Iterable
in a smart way.
My understanding is that when using async methods threads are not blocked but they are free to perform other tasks until network operation is complete.
I would expect a function that returns a Future
/IgniteFuture
or a callback.
There is a way to fetch all the records without keep a thread blocked?
Finally to correctly free up resources I need to call close
function. If I write Future(cursor.asScala.toList)
when should the close method be called? On the onComplete
of the Future
?
Another easy solution is to write Future{cursor.getAll.asScala}
but I suppose that internally a worker thread will be blocked to wait for all the records.
Maybe I missing something?
UPDATE 2
In other words there is a way to fetch a list of records from Ignite using "Asynchronous Non-Blocking IO"?