-
Notifications
You must be signed in to change notification settings - Fork 348
/
lostreaming.scala
43 lines (33 loc) · 1.62 KB
/
lostreaming.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright (c) 2013-2018 Rob Norris and Contributors
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.postgres.hi
import cats.implicits._
import cats.effect.ContextShift
import doobie._, doobie.implicits._
import fs2.Stream
import fs2.{io => FS2IO}
import java.io.{InputStream, OutputStream}
import org.postgresql.largeobject.LargeObject
import scala.concurrent.ExecutionContext
object lostreaming {
def createLOFromStream(data: Stream[ConnectionIO, Byte], blockingEc: ExecutionContext): ConnectionIO[Long] =
createLO.flatMap { oid =>
Stream.bracket(openLO(oid))(closeLO)
.flatMap(lo => data.to(FS2IO.writeOutputStream(getOutputStream(lo), blockingEc)))
.compile.drain.as(oid)
}
def createStreamFromLO(oid: Long, chunkSize: Int, blockingEc: ExecutionContext): Stream[ConnectionIO, Byte] =
Stream.bracket(openLO(oid))(closeLO)
.flatMap(lo => FS2IO.readInputStream(getInputStream(lo), chunkSize, blockingEc))
private val createLO: ConnectionIO[Long] =
PHC.pgGetLargeObjectAPI(PFLOM.createLO)
private def openLO(oid: Long): ConnectionIO[LargeObject] =
PHC.pgGetLargeObjectAPI(PFLOM.open(oid))
private def closeLO(lo: LargeObject): ConnectionIO[Unit] =
PHC.pgGetLargeObjectAPI(PFLOM.embed(lo, PFLO.close))
private def getOutputStream(lo: LargeObject): ConnectionIO[OutputStream] =
PHC.pgGetLargeObjectAPI(PFLOM.embed(lo, PFLO.getOutputStream))
private def getInputStream(lo: LargeObject): ConnectionIO[InputStream] =
PHC.pgGetLargeObjectAPI(PFLOM.embed(lo, PFLO.getInputStream))
}