/
syntax.scala
259 lines (205 loc) · 7.76 KB
/
syntax.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package oriented
import cats.data.{Coproduct, EitherT}
import cats.free.Free
import com.tinkerpop.blueprints.impls.orient.OrientBaseGraph
import oriented.free.dsl._
import oriented.free.interpreters._
import scala.concurrent.{ExecutionContext, Future}
import scala.util.{Failure, Success, Try}
import cats.instances.try_.catsStdInstancesForTry
import cats.instances.future.catsStdInstancesForFuture
import cats.{Id, ~>}
/**
* Syntax package for importing types and implicits in scope.
*/
package object syntax {
/**
* Type of an OrientProgram where each DSL is combined into one program.
*/
type OrientProgram[A] =
Coproduct[EdgeDSL, Coproduct[VertexDSL, Coproduct[ElementDSL, Coproduct[ClientDSL, SqlDSL, ?], ?], ?], A]
/**
* An OrientIO is a Free from the OrientProgram co product resulting in a type A.
*/
type OrientIO[A] = Free[OrientProgram, A]
type OrientRead[A] = Free[ReadDSL, A]
implicit class OrientIOInterpreter[A](orientIO: OrientIO[A]) {
/**
* Overloaded function of runUnsafe.
*/
def runGraphUnsafe(implicit orientClient: OrientClient): A =
runGraphUnsafe(enableTransactions = true)
/**
* Runs the OrientIO unsafely.
* Can throw errors and does not control side effects!
*/
def runGraphUnsafe(enableTransactions: Boolean)(implicit orientClient: OrientClient): A = {
implicit val graph: OrientBaseGraph =
if(enableTransactions) orientClient.graph
else orientClient.graphNoTransaction
val unsafeInterpreter: OrientProgram ~> Id =
UnsafeEdgeInterpreter or
(UnsafeVertexInterpreter or
(UnsafeElementInterpreter or
(UnsafeClientInterpreter() or
UnsafeSqlInterpreter())))
val result = orientIO.foldMap(unsafeInterpreter)
if(enableTransactions) graph.commit()
graph.shutdown()
result
}
/**
* Overloaded function of tryRun.
*/
def tryGraphRun(implicit orientClient: OrientClient): Try[A] = tryGraphRun(enableTransactions = true)
/**
* Runs the orientIO safely resulting in a Try[A].
*/
def tryGraphRun(enableTransactions: Boolean)(implicit orientClient: OrientClient): Try[A] = {
implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph
else orientClient.graphNoTransaction
val tryInterpreter: OrientProgram ~> Try =
TryEdgeInterpreter or
(TryVertexInterpreter or
(TryElementInterpreter or
(TryClientInterpreter() or
TrySqlInterpreter())))
val result = orientIO.foldMap(tryInterpreter)
if(result.isFailure && enableTransactions)
graph.rollback()
else if(enableTransactions) graph.commit()
graph.shutdown()
result
}
/**
* Overloaded function of runSafe.
*/
def runGraph(implicit orientClient: OrientClient): Either[Throwable, A] =
runGraph(enableTransactions = true)
/**
* Runs the orientIO safely resulting in either a Throwable or A, where A is the result of Free.
*/
def runGraph(enableTransactions: Boolean)(implicit orientClient: OrientClient): Either[Throwable, A] =
tryGraphRun(enableTransactions) match {
case Failure(exception) => Left(exception)
case Success(a) => Right(a)
}
/**
* Overloaded function of runAsyncUnsafe
*/
def runGraphAsyncUnsafe(implicit executionContext: ExecutionContext, orientClient: OrientClient): Future[A] =
runGraphAsyncUnsafe(enableTransactions = true)
/**
* Runs the orientIO resulting in Futures, note that this is expirimental since the OrientElements are not thread
* save.
*/
def runGraphAsyncUnsafe(enableTransactions: Boolean)
(implicit executionContext: ExecutionContext,
orientClient: OrientClient): Future[A] = {
implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph
else orientClient.graphNoTransaction
val asyncInterpreter: OrientProgram ~> Future =
AsyncEdgeInterpreter() or
(AsyncVertexInterpreter() or
(AsyncElementInterpreter() or
(AsyncClientInterpreter() or
AsyncSqlInterpreter())))
val result = orientIO.foldMap(asyncInterpreter)
result.onFailure(PartialFunction { _ =>
if(enableTransactions) graph.rollback()
graph.shutdown()
})
result.onSuccess(PartialFunction { _ =>
if(enableTransactions) graph.commit()
graph.shutdown()
})
result
}
/**
* Overloaded function of runAsyncSafe
*/
def runGraphAsync(implicit executionContext: ExecutionContext,
orientClient: OrientClient): EitherT[Future, Throwable, A] =
runGraphAsync(enableTransactions = true)
/**
* Runs the orientIO resulting in a Either Transformer of Future Either[Throwable, A]
*/
def runGraphAsync(enableTransactions: Boolean)
(implicit executionContext: ExecutionContext,
orientClient: OrientClient): EitherT[Future, Throwable, A] = {
implicit val graph: OrientBaseGraph = if(enableTransactions) orientClient.graph
else orientClient.graphNoTransaction
val interpreter: OrientProgram ~> EitherT[Future, Throwable, ?] =
SafeAsyncEdgeInterpreter() or
(SafeAsyncVertexInterpreter() or
(SafeAsyncElementInterpreter() or
(SafeAsyncClientInterpreter() or
SafeAsyncSqlInterpreter())))
val result = orientIO.foldMap(interpreter)
val isLeft = result.isLeft
isLeft.onFailure(PartialFunction { _ =>
if(enableTransactions) graph.rollback()
graph.shutdown()
})
isLeft.onSuccess(PartialFunction { failed =>
if(failed && enableTransactions) graph.rollback()
else if(enableTransactions) graph.commit()
graph.shutdown()
})
result
}
}
/**
* Implicit conversion for sql interpolation
*/
implicit class OrientSqlWrapper(val sql: StringContext) {
/**
* String interpolation method, currently using standardInterpolater passing to SqlStatement Class
*/
def sql(args: Any*): SQLStatement = {
SQLStatement(sql.standardInterpolator(identity, args))
}
}
/**
* TODO
*/
implicit class VertexToEdgeModels[V](val vertexModel: V)(implicit val orientFormat: OrientFormat[V]) {
/**
* TODO
*/
def --[E](edgeModel: E)(implicit orientFormatEdge: OrientFormat[E]): VertexToEdgeToVertexModels[V, E] =
new VertexToEdgeToVertexModels(vertexModel, edgeModel)(orientFormat, orientFormatEdge)
}
/**
* TODO
*/
class VertexToEdgeToVertexModels[V, E](val vertexModel: V,
val edgeModel: E)
(implicit val orientFormatV: OrientFormat[V],
val orientFormatE: OrientFormat[E]) {
/**
* TODO
*/
def -->[VT](vertexTwo: VT)
(implicit orientFormatVT: OrientFormat[VT],
client: OrientClient): OrientIO[(Vertex[V], Edge[E], Vertex[VT])] =
for {
vertex <- client.addVertex(vertexModel)
vertexTwo <- client.addVertex(vertexTwo)
edge <- vertex.addEdge(edgeModel, vertexTwo)
} yield (vertex, edge, vertexTwo)
}
/**
* TODO
*/
implicit class VertexToEdge[V](val vertex: Vertex[V]) {
def --[E](edge: E)(implicit orientFormat: OrientFormat[E]): VertexToEdgeToVertex[V, E] =
new VertexToEdgeToVertex(vertex, edge)
}
/**
* TODO
*/
class VertexToEdgeToVertex[V, E](val vertex: Vertex[V], val edge: E)(implicit val orientFormat: OrientFormat[E]) {
def -->[VT](vertexTwo: Vertex[VT]): OrientIO[Edge[E]] = vertex.addEdge(edge, vertexTwo)
}
}