-
Notifications
You must be signed in to change notification settings - Fork 348
/
fastpath.scala
154 lines (134 loc) · 7.45 KB
/
fastpath.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) 2013-2017 Rob Norris
// This software is licensed under the MIT License (MIT).
// For more information see LICENSE or https://opensource.org/licenses/MIT
package doobie.postgres.free
import cats.~>
import cats.effect.Async
import cats.free.{ Free => FF } // alias because some algebras have an op called Free
import java.lang.String
import java.sql.ResultSet
import org.postgresql.fastpath.FastpathArg
import org.postgresql.fastpath.{ Fastpath => PGFastpath }
@SuppressWarnings(Array("org.wartremover.warts.Overloading"))
object fastpath { module =>
// Algebra of operations for PGFastpath. Each accepts a visitor as an alternatie to pattern-matching.
sealed trait FastpathOp[A] {
def visit[F[_]](v: FastpathOp.Visitor[F]): F[A]
}
// Free monad over FastpathOp.
type FastpathIO[A] = FF[FastpathOp, A]
// Module of instances and constructors of FastpathOp.
object FastpathOp {
// Given a PGFastpath we can embed a FastpathIO program in any algebra that understands embedding.
implicit val FastpathOpEmbeddable: Embeddable[FastpathOp, PGFastpath] =
new Embeddable[FastpathOp, PGFastpath] {
def embed[A](j: PGFastpath, fa: FF[FastpathOp, A]) = Embedded.Fastpath(j, fa)
}
// Interface for a natural tansformation FastpathOp ~> F encoded via the visitor pattern.
// This approach is much more efficient than pattern-matching for large algebras.
trait Visitor[F[_]] extends (FastpathOp ~> F) {
final def apply[A](fa: FastpathOp[A]): F[A] = fa.visit(this)
// Common
def raw[A](f: PGFastpath => A): F[A]
def embed[A](e: Embedded[A]): F[A]
def delay[A](a: () => A): F[A]
def handleErrorWith[A](fa: FastpathIO[A], f: Throwable => FastpathIO[A]): F[A]
def async[A](k: (Either[Throwable, A] => Unit) => Unit): F[A]
// PGFastpath
def addFunction(a: String, b: Int): F[Unit]
def addFunctions(a: ResultSet): F[Unit]
def fastpath(a: Int, b: Array[FastpathArg]): F[Array[Byte]]
def fastpath(a: Int, b: Boolean, c: Array[FastpathArg]): F[AnyRef]
def fastpath(a: String, b: Array[FastpathArg]): F[Array[Byte]]
def fastpath(a: String, b: Boolean, c: Array[FastpathArg]): F[AnyRef]
def getData(a: String, b: Array[FastpathArg]): F[Array[Byte]]
def getID(a: String): F[Int]
def getInteger(a: String, b: Array[FastpathArg]): F[Int]
def getLong(a: String, b: Array[FastpathArg]): F[Long]
def getOID(a: String, b: Array[FastpathArg]): F[Long]
}
// Common operations for all algebras.
final case class Raw[A](f: PGFastpath => A) extends FastpathOp[A] {
def visit[F[_]](v: Visitor[F]) = v.raw(f)
}
final case class Embed[A](e: Embedded[A]) extends FastpathOp[A] {
def visit[F[_]](v: Visitor[F]) = v.embed(e)
}
final case class Delay[A](a: () => A) extends FastpathOp[A] {
def visit[F[_]](v: Visitor[F]) = v.delay(a)
}
final case class HandleErrorWith[A](fa: FastpathIO[A], f: Throwable => FastpathIO[A]) extends FastpathOp[A] {
def visit[F[_]](v: Visitor[F]) = v.handleErrorWith(fa, f)
}
final case class Async1[A](k: (Either[Throwable, A] => Unit) => Unit) extends FastpathOp[A] {
def visit[F[_]](v: Visitor[F]) = v.async(k)
}
// PGFastpath-specific operations.
final case class AddFunction(a: String, b: Int) extends FastpathOp[Unit] {
def visit[F[_]](v: Visitor[F]) = v.addFunction(a, b)
}
final case class AddFunctions(a: ResultSet) extends FastpathOp[Unit] {
def visit[F[_]](v: Visitor[F]) = v.addFunctions(a)
}
final case class Fastpath(a: Int, b: Array[FastpathArg]) extends FastpathOp[Array[Byte]] {
def visit[F[_]](v: Visitor[F]) = v.fastpath(a, b)
}
final case class Fastpath1(a: Int, b: Boolean, c: Array[FastpathArg]) extends FastpathOp[AnyRef] {
def visit[F[_]](v: Visitor[F]) = v.fastpath(a, b, c)
}
final case class Fastpath2(a: String, b: Array[FastpathArg]) extends FastpathOp[Array[Byte]] {
def visit[F[_]](v: Visitor[F]) = v.fastpath(a, b)
}
final case class Fastpath3(a: String, b: Boolean, c: Array[FastpathArg]) extends FastpathOp[AnyRef] {
def visit[F[_]](v: Visitor[F]) = v.fastpath(a, b, c)
}
final case class GetData(a: String, b: Array[FastpathArg]) extends FastpathOp[Array[Byte]] {
def visit[F[_]](v: Visitor[F]) = v.getData(a, b)
}
final case class GetID(a: String) extends FastpathOp[Int] {
def visit[F[_]](v: Visitor[F]) = v.getID(a)
}
final case class GetInteger(a: String, b: Array[FastpathArg]) extends FastpathOp[Int] {
def visit[F[_]](v: Visitor[F]) = v.getInteger(a, b)
}
final case class GetLong(a: String, b: Array[FastpathArg]) extends FastpathOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.getLong(a, b)
}
final case class GetOID(a: String, b: Array[FastpathArg]) extends FastpathOp[Long] {
def visit[F[_]](v: Visitor[F]) = v.getOID(a, b)
}
}
import FastpathOp._
// Smart constructors for operations common to all algebras.
val unit: FastpathIO[Unit] = FF.pure[FastpathOp, Unit](())
def raw[A](f: PGFastpath => A): FastpathIO[A] = FF.liftF(Raw(f))
def embed[F[_], J, A](j: J, fa: FF[F, A])(implicit ev: Embeddable[F, J]): FF[FastpathOp, A] = FF.liftF(Embed(ev.embed(j, fa)))
def delay[A](a: => A): FastpathIO[A] = FF.liftF(Delay(() => a))
def handleErrorWith[A](fa: FastpathIO[A], f: Throwable => FastpathIO[A]): FastpathIO[A] = FF.liftF[FastpathOp, A](HandleErrorWith(fa, f))
def raiseError[A](err: Throwable): FastpathIO[A] = delay(throw err)
def async[A](k: (Either[Throwable, A] => Unit) => Unit): FastpathIO[A] = FF.liftF[FastpathOp, A](Async1(k))
// Smart constructors for Fastpath-specific operations.
def addFunction(a: String, b: Int): FastpathIO[Unit] = FF.liftF(AddFunction(a, b))
def addFunctions(a: ResultSet): FastpathIO[Unit] = FF.liftF(AddFunctions(a))
def fastpath(a: Int, b: Array[FastpathArg]): FastpathIO[Array[Byte]] = FF.liftF(Fastpath(a, b))
def fastpath(a: Int, b: Boolean, c: Array[FastpathArg]): FastpathIO[AnyRef] = FF.liftF(Fastpath1(a, b, c))
def fastpath(a: String, b: Array[FastpathArg]): FastpathIO[Array[Byte]] = FF.liftF(Fastpath2(a, b))
def fastpath(a: String, b: Boolean, c: Array[FastpathArg]): FastpathIO[AnyRef] = FF.liftF(Fastpath3(a, b, c))
def getData(a: String, b: Array[FastpathArg]): FastpathIO[Array[Byte]] = FF.liftF(GetData(a, b))
def getID(a: String): FastpathIO[Int] = FF.liftF(GetID(a))
def getInteger(a: String, b: Array[FastpathArg]): FastpathIO[Int] = FF.liftF(GetInteger(a, b))
def getLong(a: String, b: Array[FastpathArg]): FastpathIO[Long] = FF.liftF(GetLong(a, b))
def getOID(a: String, b: Array[FastpathArg]): FastpathIO[Long] = FF.liftF(GetOID(a, b))
// FastpathIO is an Async
implicit val AsyncFastpathIO: Async[FastpathIO] =
new Async[FastpathIO] {
val M = FF.catsFreeMonadForFree[FastpathOp]
def pure[A](x: A): FastpathIO[A] = M.pure(x)
def handleErrorWith[A](fa: FastpathIO[A])(f: Throwable => FastpathIO[A]): FastpathIO[A] = module.handleErrorWith(fa, f)
def raiseError[A](e: Throwable): FastpathIO[A] = module.raiseError(e)
def async[A](k: (Either[Throwable,A] => Unit) => Unit): FastpathIO[A] = module.async(k)
def flatMap[A, B](fa: FastpathIO[A])(f: A => FastpathIO[B]): FastpathIO[B] = M.flatMap(fa)(f)
def tailRecM[A, B](a: A)(f: A => FastpathIO[Either[A, B]]): FastpathIO[B] = M.tailRecM(a)(f)
def suspend[A](thunk: => FastpathIO[A]): FastpathIO[A] = M.flatten(module.delay(thunk))
}
}