ResultSet is closed using toStream

25 views
Skip to first unread message

des.m...@gmail.com

unread,
Jul 13, 2016, 9:31:09 AM7/13/16
to Squeryl
Hi

In order to avoid placing large amounts of data in memory I want to be able to return my ResultSet in a stream - and I should be able to do it according to this post: https://groups.google.com/forum/#!searchin/squeryl/stream/squeryl/aZMsfikBejA/rFI59-Owte4J

But I have tried changing a query like the one below:

val results = transaction {
 
from(table)(r => select(t)).toList
}



to

val results = transaction {
 
from(table)(r => select(t)).toStream
}



and this causes the exception "The ResultSet is closed" once I try to access the stream:

results match {
 
case h #:: t => // Causes an exception
...



Is there I way I can actually use a stream?

Thanks
Des

Thomas Suckow

unread,
Jul 29, 2016, 1:37:17 PM7/29/16
to Squeryl
The stream is only useable inside the transaction block.

des.m...@gmail.com

unread,
Jul 30, 2016, 2:42:58 PM7/30/16
to Squeryl
I used the pagination feature to provide stream type access and this way usage of the stream is not restricted to just inside a transaction block:

    implicit def pagedStreamer[T](query: => Query[T])  = new {

       
def asStream(pageSize: Int): Stream[T] = next(List(), 0, pageSize)

       
private def next(currentSet: List[T], offset: Int, pageSize: Int): Stream[T] = currentSet match {
           
case h :: t => h #:: next(t, offset, pageSize)
           
case _ =>
                transaction
{ query.page(offset, pageSize).toList } match {
                   
case Nil => Stream.empty
                   
case nextPage => next(nextPage, offset + pageSize, pageSize)
               
}
       
}
   
}

Reply all
Reply to author
Forward
0 new messages