Skip to content

Commit 741d237

Browse files
committed
MongoSync
1 parent edd2fac commit 741d237

File tree

7 files changed

+22
-12
lines changed

7 files changed

+22
-12
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
### 2.0.2
77
* mongo-scala-driver 4.0.3
88
* dependency updates
9+
* Sync for collections added
910

1011
### 2.0.1
1112
* Mongo Java Server Support

src/main/scala/com/sfxcode/nosql/mongo/database/MongoConfig.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,13 @@ trait ConfigHelper {
8080

8181
def stringConfig(configPath: String, key: String, default: String = ""): Option[String] =
8282
if (conf.hasPath("%s.%s".format(configPath, key))) {
83-
Some(conf.getString("%s.%s".format(configPath, key)))
83+
val str = conf.getString("%s.%s".format(configPath, key))
84+
if (str.nonEmpty) {
85+
Some(str)
86+
}
87+
else {
88+
None
89+
}
8490
}
8591
else {
8692
if (default.nonEmpty) {

src/main/scala/com/sfxcode/nosql/mongo/server/LocalServer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ case class LocalServer(serverConfig: ServerConfig = ServerConfig()) {
1212
private val server: MongoServer = {
1313
if (ServerBackend.H2 == serverConfig.backend) {
1414
if (serverConfig.h2BackendConfig.isDefined && !serverConfig.h2BackendConfig.get.inMemory) {
15-
if (serverConfig.h2BackendConfig.get.path.nonEmpty) {
16-
h2Path = serverConfig.h2BackendConfig.get.path
15+
if (serverConfig.h2BackendConfig.get.path.isDefined) {
16+
h2Path = serverConfig.h2BackendConfig.get.path.get
1717
}
1818
else {
1919
h2Path = File.temporaryFile().get().path.toString

src/main/scala/com/sfxcode/nosql/mongo/server/ServerConfig.scala

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

14-
case class H2BackendConfig(inMemory: Boolean = true, path: String = "")
14+
case class H2BackendConfig(inMemory: Boolean = true, path: Option[String] = None)
1515

1616
object ServerBackend extends Enumeration {
1717
type ServerBackend = Value
@@ -46,7 +46,7 @@ object ServerConfig extends ConfigHelper {
4646
val h2BackendConfig: Option[H2BackendConfig] = {
4747
if (serverBackend == ServerBackend.H2 && conf.hasPath("%s.%s".format(configPath, "h2.inMemory"))) {
4848
val inMemory = booleanConfig(configPath, "h2.inMemory")
49-
val path = stringConfig(configPath, "h2.path").get
49+
val path = stringConfig(configPath, "h2.path")
5050
Some(H2BackendConfig(inMemory, path))
5151
}
5252
else {

src/main/scala/com/sfxcode/nosql/mongo/sync/MongoSyncOperation.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,13 @@ case class MongoSyncOperation(
7171
countBefore: Int,
7272
documentsToSync: Seq[Document]
7373
): MongoSyncResult = {
74-
val start = System.currentTimeMillis()
75-
val syncDate = new Date()
74+
val start = System.currentTimeMillis()
75+
val syncDate = new Date()
76+
var filteredDocumentsToSync = Seq[Document]()
7677
if (documentsToSync.nonEmpty) {
77-
val idSet: Set[ObjectId] = documentsToSync.map(doc => doc.getObjectId(idColumnName)).toSet
78-
val documentsToSync: Seq[Document] = left.dao(collectionName).find(valueFilter(idColumnName, idSet)).results()
79-
right.dao(collectionName).bulkWriteMany(documentsToSync).result()
78+
val idSet: Set[ObjectId] = documentsToSync.map(doc => doc.getObjectId(idColumnName)).toSet
79+
filteredDocumentsToSync = left.dao(collectionName).find(valueFilter(idColumnName, idSet)).results()
80+
right.dao(collectionName).bulkWriteMany(filteredDocumentsToSync).result()
8081
val update = combine(
8182
set(MongoSyncOperation.SyncColumnLastSync, syncDate),
8283
set(MongoSyncOperation.SyncColumnLastUpdate, syncDate)
@@ -89,7 +90,7 @@ case class MongoSyncOperation(
8990
collectionName,
9091
syncDate,
9192
true,
92-
documentsToSync.size,
93+
filteredDocumentsToSync.size,
9394
countBefore,
9495
countAfter,
9596
(System.currentTimeMillis() - start)

src/test/resources/application.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ unit.test.mongo.local {
3232
database = "simple-mongo-unit-test"
3333
host = "localhost"
3434
port = 28028
35+
userName = "testy"
36+
password = ""
3537
applicationName = "simple-mongo-config-test"
3638
}
3739

src/test/scala/com/sfxcode/nosql/mongo/server/ServerConfigSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class ServerConfigSpec extends Specification {
2121
config.port mustEqual 28028
2222
config.serverName mustEqual "local-unit-test-server"
2323
config.h2BackendConfig.get.inMemory must beFalse
24-
config.h2BackendConfig.get.path mustEqual ""
24+
config.h2BackendConfig.get.path must beNone
2525
}
2626
}
2727
}

0 commit comments

Comments
 (0)