Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize config structure #36

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion core/src/main/scala/zio/nebula/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import zio.nebula.storage._
package object nebula {

type SessionClient = NebulaSessionClient
type Client = NebulaClient with NebulaSessionPoolConfig with NebulaPoolConfig
type Client = NebulaClient with NebulaPoolConfig
type Storage = NebulaStorageClient
type Meta = NebulaMetaClient

Expand Down
12 changes: 6 additions & 6 deletions core/src/test/scala/zio/nebula/NebulaSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ trait NebulaSpec extends ZIOSpecDefault {
(specLayered @@ beforeAll(
ZIO.serviceWithZIO[NebulaClient](_.init())
*> ZIO.serviceWithZIO[NebulaClient](
_.openSession().flatMap(_.execute(Stmt.str("""
|CREATE SPACE IF NOT EXISTS test(vid_type=fixed_string(20));
|USE test;
|CREATE TAG IF NOT EXISTS person(name string, age int);
|CREATE EDGE IF NOT EXISTS like(likeness double)
|""".stripMargin)))
_.openSession(false).flatMap(_.execute(Stmt.str("""
|CREATE SPACE IF NOT EXISTS test(vid_type=fixed_string(20));
|USE test;
|CREATE TAG IF NOT EXISTS person(name string, age int);
|CREATE EDGE IF NOT EXISTS like(likeness double)
|""".stripMargin)))
)
) @@ sequential @@ eventually)
.provideShared(
Expand Down
13 changes: 4 additions & 9 deletions core/src/test/scala/zio/nebula/ZioNebulaEnvironment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,10 @@ object ZioNebulaEnvironment {
maxConnsSize = 10,
intervalIdleMills = 100,
waitTimeMills = 100,
sslParam = None
)
) ++ ZLayer.fromZIO(
ZIO.attempt(
NebulaSessionPoolConfig(
List(NebulaHostAddress(host, port)),
NebulaAuth(defaultUser, defaultPwd),
defaultSpace
)
sslParam = None,
address = List(NebulaHostAddress(host, port)),
auth = NebulaAuth(defaultUser, defaultPwd),
spaceName = Some(defaultSpace)
)
)

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
Loading