/
OrientClient.scala
123 lines (95 loc) · 3.58 KB
/
OrientClient.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
package oriented
import com.orientechnologies.orient.client.remote.OServerAdmin
import com.tinkerpop.blueprints.impls.orient.{OrientGraph, OrientGraphFactory, OrientGraphNoTx}
import oriented.free.dsl._
import oriented.syntax.{OrientIO, OrientProgram}
/**
* OrientClient provides communication to OrientDB, with either a InMemory, (P)Local or Remote database.
*/
sealed trait OrientClient {
/**
* Connection uri to OrientDB.
*/
def uri: String
/**
* Database name for connecting to the database.
*/
def db: String
/**
* User string for authentication.
*/
def user: String
/**
* User password for authentication.
*/
def password: String
/**
* Returns a graph instance that has transactions disabled.
*/
def graphNoTransaction: OrientGraphNoTx = factory.getNoTx
/**
* Returns a graph instances that has transactions enabled.
*/
def graph: OrientGraph = factory.getTx
/**
* Creates the server admin for type of connection to OrientDB.
*/
def serverAdmin: OServerAdmin
/**
* Factory for creation of the Graphs.
* Needs to be lazy for initializing in trait.
*/
lazy val factory: OrientGraphFactory = pool.map { case (min, max) =>
new OrientGraphFactory(uri).setupPool(min, max)
}.getOrElse(new OrientGraphFactory(uri))
/**
* Optional connection pool parameters, tuple represents minimal to maximal connections.
*/
def pool: Option[(Int, Int)]
val C: Clients[OrientProgram] = Clients.clients[OrientProgram]
/**
* Action on the client that creates a OrientIO for creating a VertexType of A.
*/
def createVertexType[A](implicit orientFormat: OrientFormat[A]): OrientIO[VertexType[A]] =
C.createVertexType[A](orientFormat)
/**
* Action on the client that creates a OrientIO for creating an EdgeType of A.
*/
def createEdgeType[A](implicit orientFormat: OrientFormat[A]): OrientIO[EdgeType[A]] =
C.createEdgeType[A](orientFormat)
/**
* Action on the client that creates an OrientIO which creates a Vertex in the database.
*/
def addVertex[A](vertexModel: A)(implicit orientFormat: OrientFormat[A]): OrientIO[Vertex[A]] =
C.addVertex[A](vertexModel, orientFormat)
/**
* Action on the client that creates an OrientIO which creates an Edge link between to vertices in the database.
*/
def addEdge[A, B, C](edgeModel: A,
inVertex: Vertex[B],
outVertex: Vertex[C])
(implicit orientFormat: OrientFormat[A]): OrientIO[Edge[A]] =
C.addEdge[A, B, C](edgeModel, inVertex, outVertex, orientFormat)
def shutdown(close: Boolean = false): OrientIO[Unit] =
C.shutdown(close)
}
/**
* Creates an InMemory Client for OrientDB.
*/
case class InMemoryClient(db: String, user: String = "root", password: String = "root", pool: Option[(Int, Int)] = None) extends OrientClient {
val uri: String = s"memory:$db"
val serverAdmin: OServerAdmin = new OServerAdmin(uri)
}
/**
* Creates an PLocal Client for OrientDB.
*/
case class PLocalClient(uri: String, db: String, user: String, password: String, pool: Option[(Int, Int)] = None) extends OrientClient {
val serverAdmin: OServerAdmin = new OServerAdmin(uri).connect(user, password)
if(!serverAdmin.existsDatabase()) serverAdmin.createDatabase(db, "document", "plocal")
}
/**
* Creates an Remote Client for OrientDB.
*/
case class RemoteClient(uri: String, db: String, user: String, password: String, pool: Option[(Int, Int)] = None) extends OrientClient {
val serverAdmin: OServerAdmin = new OServerAdmin(uri).connect(user, password)
}