Skip to content

Commit 0c8861a

Browse files
committed
refactor: added serverAddressList as connection option
1 parent 5f5c1ab commit 0c8861a

11 files changed

Lines changed: 128 additions & 119 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package dev.mongocamp.driver.mongodb.database
2+
3+
import com.typesafe.config.{ Config, ConfigFactory }
4+
5+
trait ConfigHelper {
6+
val conf: Config = ConfigFactory.load()
7+
8+
def stringConfig(configPath: String, key: String, default: String = ""): Option[String] = {
9+
if (conf.hasPath("%s.%s".format(configPath, key))) {
10+
val str = conf.getString("%s.%s".format(configPath, key))
11+
if (str.nonEmpty) {
12+
Some(str)
13+
}
14+
else {
15+
None
16+
}
17+
}
18+
else if (default.nonEmpty) {
19+
Some(default)
20+
}
21+
else {
22+
None
23+
}
24+
}
25+
26+
def intConfig(configPath: String, key: String, default: Int = 0): Int = {
27+
if (conf.hasPath("%s.%s".format(configPath, key))) {
28+
conf.getInt("%s.%s".format(configPath, key))
29+
}
30+
else {
31+
default
32+
}
33+
}
34+
35+
def booleanConfig(configPath: String, key: String, default: Boolean = false): Boolean = {
36+
if (conf.hasPath("%s.%s".format(configPath, key))) {
37+
conf.getBoolean("%s.%s".format(configPath, key))
38+
}
39+
else {
40+
default
41+
}
42+
}
43+
}

src/main/scala/dev/mongocamp/driver/mongodb/database/MongoConfig.scala

Lines changed: 27 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import java.util.concurrent.TimeUnit
44
import com.mongodb.MongoCompressor
55
import com.mongodb.MongoCredential.createCredential
66
import com.mongodb.event.{CommandListener, ConnectionPoolListener}
7-
import dev.mongocamp.driver.mongodb.database.MongoConfig._
87
import com.typesafe.config.{Config, ConfigFactory}
8+
import dev.mongocamp.driver.mongodb.database.MongoConfig.{CompressionSnappy, CompressionZlib, CompressionZstd, DefaultApplicationName, DefaultAuthenticationDatabaseName, DefaultHost, DefaultPort}
99
import org.mongodb.scala.connection._
1010
import org.mongodb.scala.{MongoClientSettings, MongoCredential, ServerAddress}
1111

@@ -21,18 +21,20 @@ case class MongoConfig(
2121
password: Option[String] = None,
2222
authDatabase: String = DefaultAuthenticationDatabaseName,
2323
poolOptions: MongoPoolOptions = MongoPoolOptions(),
24-
compressors: List[String] = List(),
25-
connectionPoolListener: List[ConnectionPoolListener] = List(),
26-
commandListener: List[CommandListener] = List(),
27-
customClientSettings: Option[MongoClientSettings] = None
24+
compressors: List[String] = List.empty,
25+
connectionPoolListener: List[ConnectionPoolListener] = List.empty,
26+
commandListener: List[CommandListener] = List.empty,
27+
customClientSettings: Option[MongoClientSettings] = None,
28+
serverAddressList: List[ServerAddress] = List.empty
2829
) {
2930

3031
val clientSettings: MongoClientSettings = {
3132
if (customClientSettings.isDefined) {
3233
customClientSettings.get
3334
}
3435
else {
35-
val clusterSettings: ClusterSettings = ClusterSettings.builder().hosts(List(new ServerAddress(host, port)).asJava).build()
36+
val internalServerAddressList = if (serverAddressList.nonEmpty) serverAddressList else List(new ServerAddress(host, port))
37+
val clusterSettings: ClusterSettings = ClusterSettings.builder().hosts(internalServerAddressList.asJava).build()
3638

3739
val connectionPoolSettingsBuilder = ConnectionPoolSettings
3840
.builder()
@@ -47,13 +49,13 @@ case class MongoConfig(
4749

4850
val compressorList = new ArrayBuffer[MongoCompressor]()
4951
compressors.foreach(compression => {
50-
if (ComressionSnappy.equalsIgnoreCase(compression)) {
52+
if (CompressionSnappy.equalsIgnoreCase(compression)) {
5153
compressorList.+=(MongoCompressor.createSnappyCompressor())
5254
}
53-
else if (ComressionZlib.equalsIgnoreCase(compression)) {
55+
else if (CompressionZlib.equalsIgnoreCase(compression)) {
5456
compressorList.+=(MongoCompressor.createZlibCompressor())
5557
}
56-
else if (ComressionZstd.equalsIgnoreCase(compression)) {
58+
else if (CompressionZstd.equalsIgnoreCase(compression)) {
5759
compressorList.+=(MongoCompressor.createZstdCompressor())
5860
}
5961
})
@@ -78,61 +80,21 @@ case class MongoConfig(
7880
}
7981
}
8082

81-
trait ConfigHelper {
82-
val conf: Config = ConfigFactory.load()
83-
84-
def stringConfig(configPath: String, key: String, default: String = ""): Option[String] = {
85-
if (conf.hasPath("%s.%s".format(configPath, key))) {
86-
val str = conf.getString("%s.%s".format(configPath, key))
87-
if (str.nonEmpty) {
88-
Some(str)
89-
}
90-
else {
91-
None
92-
}
93-
}
94-
else if (default.nonEmpty) {
95-
Some(default)
96-
}
97-
else {
98-
None
99-
}
100-
}
101-
102-
def intConfig(configPath: String, key: String, default: Int = 0): Int = {
103-
if (conf.hasPath("%s.%s".format(configPath, key))) {
104-
conf.getInt("%s.%s".format(configPath, key))
105-
}
106-
else {
107-
default
108-
}
109-
}
110-
111-
def booleanConfig(configPath: String, key: String, default: Boolean = false): Boolean = {
112-
if (conf.hasPath("%s.%s".format(configPath, key))) {
113-
conf.getBoolean("%s.%s".format(configPath, key))
114-
}
115-
else {
116-
default
117-
}
118-
}
119-
}
120-
12183
object MongoConfig extends ConfigHelper {
122-
val DefaultHost = "127.0.0.1"
123-
val DefaultPort = 27017
84+
val DefaultHost = "127.0.0.1"
85+
val DefaultPort = 27017
12486
val DefaultAuthenticationDatabaseName = "admin"
125-
val DefaultApplicationName = "mongocampdb-app"
87+
val DefaultApplicationName = "mongocampdb-app"
12688

127-
val DefaultPoolMaxConnectionIdleTime = 60
128-
val DefaultPoolMaxSize = 50
129-
val DefaultPoolMinSize = 0
130-
val DefaultPoolMaxWaitQueueSize = 500
89+
val DefaultPoolMaxConnectionIdleTime = 60
90+
val DefaultPoolMaxSize = 50
91+
val DefaultPoolMinSize = 0
92+
val DefaultPoolMaxWaitQueueSize = 500
13193
val DefaultPoolMaintenanceInitialDelay = 0
13294

133-
val ComressionSnappy = "snappy"
134-
val ComressionZlib = "zlib"
135-
val ComressionZstd = "zstd"
95+
val CompressionSnappy = "snappy"
96+
val CompressionZlib = "zlib"
97+
val CompressionZstd = "zstd"
13698

13799
val DefaultConfigPathPrefix = "mongodb"
138100

@@ -158,11 +120,11 @@ object MongoConfig extends ConfigHelper {
158120
}
159121
}
160122

161-
val host = stringConfig(configPath, "host", DefaultHost).get
162-
val database = stringConfig(configPath, "database").get
163-
val userName = stringConfig(configPath, "userName")
164-
val password = stringConfig(configPath, "password")
165-
val authDatabase = stringConfig(configPath, "authDatabase", DefaultAuthenticationDatabaseName).get
123+
val host = stringConfig(configPath, "host", DefaultHost).get
124+
val database = stringConfig(configPath, "database").get
125+
val userName = stringConfig(configPath, "userName")
126+
val password = stringConfig(configPath, "password")
127+
val authDatabase = stringConfig(configPath, "authDatabase", DefaultAuthenticationDatabaseName).get
166128
val applicationName = stringConfig(configPath, "applicationName", DefaultApplicationName).get
167129

168130
val poolOptions = MongoPoolOptions(
@@ -175,4 +137,5 @@ object MongoConfig extends ConfigHelper {
175137
MongoConfig(database, host, port, applicationName, userName, password, authDatabase, poolOptions, compressors)
176138
}
177139

140+
178141
}

src/main/scala/dev/mongocamp/driver/mongodb/pagination/MongoPagination.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
package dev.mongocamp.driver.mongodb.pagination
2+
23
import dev.mongocamp.driver.mongodb.database.ConfigHelper
34

45
trait MongoPagination[A <: Any] extends ConfigHelper {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package dev.mongocamp.driver.mongodb.server
2+
3+
case class H2BackendConfig(inMemory: Boolean = true, path: Option[String] = None)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dev.mongocamp.driver.mongodb.server
2+
3+
object ServerBackend extends Enumeration {
4+
type ServerBackend = Value
5+
val Memory, H2 = Value
6+
}

src/main/scala/dev/mongocamp/driver/mongodb/server/ServerConfig.scala

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ case class ServerConfig(
1111
h2BackendConfig: Option[H2BackendConfig] = None
1212
)
1313

14-
case class H2BackendConfig(inMemory: Boolean = true, path: Option[String] = None)
15-
16-
object ServerBackend extends Enumeration {
17-
type ServerBackend = Value
18-
val Memory, H2 = Value
19-
}
20-
2114
object ServerConfig extends ConfigHelper {
2215
val DefaultServerConfigPathPrefix = "local.mongodb.server"
2316

@@ -26,10 +19,12 @@ object ServerConfig extends ConfigHelper {
2619
val DefaultPort = 28018
2720

2821
def serverBackendFromString(backendName: String): ServerBackend.Value =
29-
if (ServerBackend.H2.toString.toLowerCase.equals(backendName.toLowerCase))
22+
if (ServerBackend.H2.toString.toLowerCase.equals(backendName.toLowerCase)) {
3023
ServerBackend.H2
31-
else
24+
}
25+
else {
3226
ServerBackend.Memory
27+
}
3328

3429
def fromPath(configPath: String = DefaultServerConfigPathPrefix): ServerConfig = {
3530

@@ -47,11 +42,12 @@ object ServerConfig extends ConfigHelper {
4742
val path = stringConfig(configPath, "h2.path")
4843
Some(H2BackendConfig(inMemory, path))
4944
}
50-
else
45+
else {
5146
None
47+
}
5248
}
5349

5450
ServerConfig(name, host, port, serverBackend, h2BackendConfig)
55-
5651
}
52+
5753
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package dev.mongocamp.driver.mongodb.sync
2+
3+
case class MongoSyncException(message: String) extends Exception(message)

src/main/scala/dev/mongocamp/driver/mongodb/sync/MongoSyncOperation.scala

Lines changed: 12 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dev.mongocamp.driver.mongodb.sync
22

33
import com.typesafe.scalalogging.LazyLogging
44
import dev.mongocamp.driver.mongodb._
5-
import dev.mongocamp.driver.mongodb.database.{ ConfigHelper, DatabaseProvider }
5+
import dev.mongocamp.driver.mongodb.database.{ConfigHelper, DatabaseProvider}
66
import dev.mongocamp.driver.mongodb.sync.SyncDirection.SyncDirection
77
import dev.mongocamp.driver.mongodb.sync.SyncStrategy.SyncStrategy
88
import org.mongodb.scala.Document
@@ -12,18 +12,6 @@ import org.mongodb.scala.model.Updates._
1212

1313
import java.util.Date
1414

15-
object SyncStrategy extends Enumeration {
16-
type SyncStrategy = Value
17-
val SyncAll = Value
18-
}
19-
20-
object SyncDirection extends Enumeration {
21-
type SyncDirection = Value
22-
val SourceToTarget, TargetToSource, TwoWay = Value
23-
}
24-
25-
case class MongoSyncException(message: String) extends Exception(message)
26-
2715
case class MongoSyncOperation(
2816
collectionName: String,
2917
syncDirection: SyncDirection = SyncDirection.SourceToTarget,
@@ -35,10 +23,8 @@ case class MongoSyncOperation(
3523

3624
def excecute(source: DatabaseProvider, target: DatabaseProvider): List[MongoSyncResult] =
3725
try {
38-
val sourceInfos: Seq[Document] =
39-
source.dao(collectionName).find().projection(includes).results(MongoSyncOperation.MaxWait)
40-
val targetInfos: Seq[Document] =
41-
target.dao(collectionName).find().projection(includes).results(MongoSyncOperation.MaxWait)
26+
val sourceInfos: Seq[Document] = source.dao(collectionName).find().projection(includes).results(MongoSyncOperation.MaxWait)
27+
val targetInfos: Seq[Document] = target.dao(collectionName).find().projection(includes).results(MongoSyncOperation.MaxWait)
4228

4329
if (SyncDirection.SourceToTarget == syncDirection) {
4430
val diff = sourceInfos.diff(targetInfos)
@@ -48,13 +34,15 @@ case class MongoSyncOperation(
4834
val diff = targetInfos.diff(sourceInfos)
4935
List(syncInternal(target, source, sourceInfos.size, diff))
5036
}
51-
else if (SyncDirection.TwoWay == syncDirection)
37+
else if (SyncDirection.TwoWay == syncDirection) {
5238
List(
5339
syncInternal(source, target, targetInfos.size, sourceInfos.diff(targetInfos)),
5440
syncInternal(target, source, sourceInfos.size, targetInfos.diff(sourceInfos))
5541
)
56-
else
42+
}
43+
else {
5744
List(MongoSyncResult(collectionName))
45+
}
5846
}
5947
catch {
6048
case e: Exception =>
@@ -87,7 +75,7 @@ case class MongoSyncOperation(
8775
MongoSyncResult(
8876
collectionName,
8977
syncDate,
90-
true,
78+
acknowleged = true,
9179
filteredDocumentsToSync.size,
9280
countBefore,
9381
countAfter,
@@ -100,29 +88,9 @@ object MongoSyncOperation extends ConfigHelper {
10088
val MaxWaitDefault = 600
10189
val MaxWait: Int = intConfig(configPath = "dev.mongocamp.mongodb.sync", key = "maxWait", default = MaxWaitDefault)
10290

103-
val SyncColumnLastSync: String =
104-
stringConfig(configPath = "dev.mongocamp.mongodb.sync", key = "syncColumnLastSync", default = "_lastSync").get
105-
val SyncColumnLastUpdate: String =
106-
stringConfig(configPath = "dev.mongocamp.mongodb.sync", key = "syncColumnLastUpdate", default = "_lastUpdate").get
91+
val SyncColumnLastSync: String = stringConfig(configPath = "dev.mongocamp.mongodb.sync", key = "syncColumnLastSync", default = "_lastSync").get
92+
val SyncColumnLastUpdate: String = stringConfig(configPath = "dev.mongocamp.mongodb.sync", key = "syncColumnLastUpdate", default = "_lastUpdate").get
10793

108-
val WriteSyncLogOnMaster = booleanConfig(configPath = "dev.mongocamp.mongodb.sync", key = "writeSyncLogOnMaster")
109-
val SyncLogTableName: String =
110-
stringConfig(
111-
configPath = "dev.mongocamp.mongodb.sync",
112-
key = "syncLogTableName",
113-
default = "mongodb-sync-log"
114-
).get
94+
val WriteSyncLogOnMaster = booleanConfig(configPath = "dev.mongocamp.mongodb.sync", key = "writeSyncLogOnMaster")
95+
val SyncLogTableName: String = stringConfig(configPath = "dev.mongocamp.mongodb.sync", key = "syncLogTableName", default = "mongodb-sync-log").get
11596
}
116-
117-
//case class MongoSyncInfo(id: Any = new ObjectId(), syncDate: Date = new Date(), updateDate: Date = new Date())
118-
119-
case class MongoSyncResult(
120-
collectionName: String,
121-
syncDate: Date = new Date(),
122-
acknowleged: Boolean = false,
123-
synced: Int = -1,
124-
countBefore: Int = -1,
125-
countAfter: Int = -1,
126-
syncTime: Long = -1,
127-
exception: Option[Exception] = None
128-
)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package dev.mongocamp.driver.mongodb.sync
2+
3+
import java.util.Date
4+
5+
case class MongoSyncResult(
6+
collectionName: String,
7+
syncDate: Date = new Date(),
8+
acknowleged: Boolean = false,
9+
synced: Int = -1,
10+
countBefore: Int = -1,
11+
countAfter: Int = -1,
12+
syncTime: Long = -1,
13+
exception: Option[Exception] = None
14+
)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package dev.mongocamp.driver.mongodb.sync
2+
3+
object SyncDirection extends Enumeration {
4+
type SyncDirection = Value
5+
val SourceToTarget, TargetToSource, TwoWay = Value
6+
}

0 commit comments

Comments
 (0)