Skip to content

Commit

Permalink
optimize config structure
Browse files Browse the repository at this point in the history
  • Loading branch information
jxnu-liguobin committed Jan 13, 2024
1 parent b2badd4 commit e970c0d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 43 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import zio.nebula._
final class NebulaSessionClientExample(sessionClient: NebulaSessionClient) {

def execute(stmt: String): ZIO[Any, Throwable, NebulaResultSet] = {
// Your business logic
// your custom logic
sessionClient.execute(stmt)
}
}
Expand All @@ -62,14 +62,15 @@ object NebulaSessionClientExample {
object NebulaSessionClientMain extends ZIOAppDefault {

override def run = (for {
_ <- ZIO.serviceWithZIO[NebulaSessionClient](_.init()) // since 0.1.1, no need to call it manually.
// since 0.1.1, no need to call it manually.
_ <- ZIO.serviceWithZIO[NebulaSessionClient](_.init())
_ <- ZIO.serviceWithZIO[NebulaSessionClientExample](
_.execute("""
|INSERT VERTEX person(name, age) VALUES
|'Bob':('Bob', 10),
|'Lily':('Lily', 9),'Tom':('Tom', 10),
|'Jerry':('Jerry', 13),
|'John':('John', 11);""".stripMargin).flatMap(r => ZIO.logInfo(r.toString))
|'John':('John', 11);""".stripMargin)
)
_ <- ZIO.serviceWithZIO[NebulaSessionClientExample](
_.execute("""
Expand All @@ -78,14 +79,13 @@ object NebulaSessionClientMain extends ZIOAppDefault {
|'Bob'->'Tom':(70.0),
|'Lily'->'Jerry':(84.0),
|'Tom'->'Jerry':(68.3),
|'Bob'->'John':(97.2);""".stripMargin).flatMap(r => ZIO.logInfo(r.toString))
|'Bob'->'John':(97.2);""".stripMargin)
)
_ <- ZIO.serviceWithZIO[NebulaSessionClientExample](
_.execute("""
|USE test;
|MATCH (p:person) RETURN p LIMIT 4;
|""".stripMargin)
.flatMap(r => ZIO.logInfo(r.rows.toString()))
)
} yield ())
.provide(
Expand All @@ -100,10 +100,10 @@ object NebulaSessionClientMain extends ZIOAppDefault {
## Configuration

Introduction for configuring keys:
- key `graph` for `NebulaSessionClient`
- key `meta` for `NebulaMetaClient`
- key `storage` for `NebulaStorageClient`
- key `pool` for `NebulaClient`
- key `graph` for `NebulaSessionClient`, For structure, please refer to `zio.nebula.NebulaSessionPoolConfig`
- key `meta` for `NebulaMetaClient`, For structure, please refer to `zio.nebula.NebulaMetaConfig`
- key `storage` for `NebulaStorageClient`, For structure, please refer to `zio.nebula.NebulaStorageConfig`
- key `pool` for `NebulaClient`, For structure, please refer to `zio.nebula.NebulaPoolConfig`

Sample Configuration:
```hocon
Expand Down
14 changes: 9 additions & 5 deletions core/src/main/scala/zio/nebula/NebulaConfig.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ final case class NebulaStorageConfig(
)

final case class NebulaPoolConfig(
address: List[NebulaHostAddress],
auth: NebulaAuth,
spaceName: Option[String],
minConnsSize: Int = 0,
maxConnsSize: Int = 10,
timeoutMills: Int = 0,
Expand All @@ -36,7 +39,8 @@ final case class NebulaPoolConfig(
waitTimeMills: Int = 0,
minClusterHealthRate: Double = 1d,
enableSsl: Boolean = false,
sslParam: Option[SSLParam]
sslParam: Option[SSLParam],
reconnect: Boolean = false
) {

def toJava: PoolConfig = {
Expand All @@ -55,9 +59,9 @@ final case class NebulaPoolConfig(
}

final case class NebulaSessionPoolConfig(
address: List[NebulaHostAddress], // both for NebulaClient and NebulaSessionClient
auth: NebulaAuth, // both for NebulaClient and NebulaSessionClient
spaceName: String, // both for NebulaClient and NebulaSessionClient
address: List[NebulaHostAddress],
auth: NebulaAuth,
spaceName: String,
maxSessionSize: Int = 10,
minSessionSize: Int = 1,
waitTimeMills: Int = 0,
Expand All @@ -66,7 +70,7 @@ final case class NebulaSessionPoolConfig(
intervalTimeMills: Int = 0,
healthCheckTimeSeconds: Int = 600,
cleanTimeSeconds: Int = 3600,
reconnect: Boolean = false, // both for NebulaClient and NebulaSessionClient
reconnect: Boolean = false,
useHttp2: Boolean = false
)

Expand Down
16 changes: 12 additions & 4 deletions core/src/main/scala/zio/nebula/net/NebulaClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package zio.nebula.net

import zio._
import zio.nebula._
import zio.nebula.NebulaPoolConfig

import com.vesoft.nebula.client.graph.{ NebulaPoolConfig => _ }
import com.vesoft.nebula.client.graph.net.{ NebulaPool => Pool }
Expand All @@ -14,13 +13,22 @@ import com.vesoft.nebula.client.graph.net.{ NebulaPool => Pool }
*/
trait NebulaClient {

def init(): ZIO[NebulaSessionPoolConfig & NebulaPoolConfig, Throwable, Boolean]
def init(): ZIO[NebulaPoolConfig, Throwable, Boolean]

/**
* close the client
*/
def close(): Task[Unit]

def openSession(): ZIO[NebulaSessionPoolConfig, Throwable, NebulaSession]
/**
* init the client and execute `USE spaceName` if exists
*/
def openSession(useSpace: Boolean): ZIO[NebulaPoolConfig, Throwable, NebulaSession]

def openSession(sessionPoolConfig: NebulaSessionPoolConfig): ZIO[Any, Throwable, NebulaSession]
/**
* init the client by using poolConfig
*/
def openSession(poolConfig: NebulaPoolConfig): ZIO[Any, Throwable, NebulaSession]

def activeConnNum: Task[Int]

Expand Down
44 changes: 21 additions & 23 deletions core/src/main/scala/zio/nebula/net/NebulaClientLive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import scala.jdk.CollectionConverters._

import zio._
import zio.nebula._
import zio.nebula.NebulaPoolConfig

import com.vesoft.nebula.client.graph.{ NebulaPoolConfig => _ }
import com.vesoft.nebula.client.graph.data.HostAddress
Expand All @@ -17,46 +16,45 @@ import com.vesoft.nebula.client.graph.net.{ NebulaPool => NebulaPl }
*/
private[nebula] final class NebulaClientLive(underlying: NebulaPl) extends NebulaClient {

def init(): ZIO[NebulaSessionPoolConfig & NebulaPoolConfig, Throwable, Boolean] =
def init(): ZIO[NebulaPoolConfig, Throwable, Boolean] =
for {
config <- ZIO.service[NebulaPoolConfig]
status <-
ZIO.serviceWithZIO[NebulaSessionPoolConfig](sessionConfig =>
ZIO.attempt(
underlying.init(sessionConfig.address.map(d => new HostAddress(d.host, d.port)).asJava, config.toJava)
)
ZIO.attempt(
underlying.init(config.address.map(d => new HostAddress(d.host, d.port)).asJava, config.toJava)
)
} yield status

def close(): Task[Unit] = ZIO.attempt(underlying.close())

def openSession(sessionPoolConfig: NebulaSessionPoolConfig): ZIO[Any, Throwable, NebulaSession] =
def openSession(poolConfig: NebulaPoolConfig): ZIO[Any, Throwable, NebulaSession] =
for {
session <- ZIO.attempt(
new NebulaSession(
underlying.getSession(
sessionPoolConfig.auth.username,
sessionPoolConfig.auth.password,
sessionPoolConfig.reconnect
poolConfig.auth.username,
poolConfig.auth.password,
poolConfig.reconnect
)
)
)
_ <- session.execute(Stmt.str(s"USE `${sessionPoolConfig.spaceName}`"))
} yield session

def openSession(): ZIO[NebulaSessionPoolConfig, Throwable, NebulaSession] =
def openSession(useSpace: Boolean): ZIO[NebulaPoolConfig, Throwable, NebulaSession] =
for {
sessionConfig <- ZIO.service[NebulaSessionPoolConfig]
session <- ZIO.attempt(
new NebulaSession(
underlying.getSession(
sessionConfig.auth.username,
sessionConfig.auth.password,
sessionConfig.reconnect
)
)
)
_ <- session.execute(Stmt.str(s"USE `${sessionConfig.spaceName}`"))
poolConfig <- ZIO.service[NebulaPoolConfig]
session <- ZIO.attempt(
new NebulaSession(
underlying.getSession(
poolConfig.auth.username,
poolConfig.auth.password,
poolConfig.reconnect
)
)
)
_ <- ZIO.when(useSpace && poolConfig.spaceName.nonEmpty) {
session.execute(Stmt.str(s"USE `${poolConfig.spaceName.orNull}`"))
}

} yield session

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import zio.nebula.net.{ NebulaClient, Stmt }

final class NebulaClientExample(nebulaClient: NebulaClient) {

def execute(stmt: String): ZIO[Scope with NebulaSessionPoolConfig, Throwable, NebulaResultSet] =
nebulaClient.openSession().flatMap(_.execute(Stmt.str(stmt)))
def execute(stmt: String): ZIO[Scope with NebulaPoolConfig, Throwable, NebulaResultSet] =
nebulaClient.openSession(false).flatMap(_.execute(Stmt.str(stmt)))
}

object NebulaClientExample {
Expand Down

0 comments on commit e970c0d

Please sign in to comment.