Skip to content

Commit 2668cda

Browse files
committed
Mongo Java Server Support
1 parent 2d8e853 commit 2668cda

File tree

8 files changed

+157
-38
lines changed

8 files changed

+157
-38
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### 2.0.1
66
* Mongo Java Server Support
7+
* LocalServer, ServerConfig added
78

89
### 2.0.0
910
* mongo package no longer extends MongoImplcits => Use MongoImplcits trait if needed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Support MongoDB 2.6 to 4.2.
1717
* Implicit conversions for Document, Bson, ObjectID ...
1818
* Reactive Streams Support
1919
* Json loading from File, Conversion to plain Json
20+
* Local Server Support [mongo-java-server](https://github.com/bwaldvogel/mongo-java-server)
2021
* ...
2122

2223
## Documentation

build.sbt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ libraryDependencies += "joda-time" % "joda-time" % "2.10.5" % Test
5555

5656
libraryDependencies += "org.json4s" %% "json4s-native" % "3.6.7" % Test
5757

58+
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.0.2"
59+
5860
libraryDependencies += "org.xerial.snappy" % "snappy-java" % "1.1.7.3" % Provided
5961

6062
libraryDependencies += "com.github.luben" % "zstd-jni" % "1.4.4-9" % Provided
6163

6264
libraryDependencies += "de.bwaldvogel" % "mongo-java-server" % "1.28.0" % Provided
6365

64-
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.0.2"
66+
libraryDependencies += "de.bwaldvogel" % "mongo-java-server-h2-backend" % "1.28.0" % Provided
6567

6668
libraryDependencies += "com.github.pathikrit" %% "better-files" % "3.8.0"
6769

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

Lines changed: 41 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,40 @@ case class MongoConfig(
7575
}
7676
}
7777

78-
object MongoConfig {
78+
trait ConfigHelper {
79+
val conf: Config = ConfigFactory.load()
80+
81+
def stringConfig(configPath: String, key: String, default: String = ""): Option[String] =
82+
if (conf.hasPath("%s.%s".format(configPath, key))) {
83+
Some(conf.getString("%s.%s".format(configPath, key)))
84+
}
85+
else {
86+
if (default.nonEmpty) {
87+
Some(default)
88+
}
89+
else {
90+
None
91+
}
92+
}
93+
94+
def intConfig(configPath: String, key: String, default: Int = 0): Int =
95+
if (conf.hasPath("%s.%s".format(configPath, key))) {
96+
conf.getInt("%s.%s".format(configPath, key))
97+
}
98+
else {
99+
default
100+
}
101+
102+
def booleanConfig(configPath: String, key: String, default: Boolean = false): Boolean =
103+
if (conf.hasPath("%s.%s".format(configPath, key))) {
104+
conf.getBoolean("%s.%s".format(configPath, key))
105+
}
106+
else {
107+
default
108+
}
109+
}
110+
111+
object MongoConfig extends ConfigHelper {
79112
val DefaultHost = "127.0.0.1"
80113
val DefaultPort = 27017
81114
val DefaultAuthenticationDatabaseName = "admin"
@@ -94,20 +127,6 @@ object MongoConfig {
94127
val DefaultConfigPathPrefix = "mongo"
95128

96129
def fromPath(configPath: String = DefaultConfigPathPrefix): MongoConfig = {
97-
val conf: Config = ConfigFactory.load()
98-
99-
def stringConfig(key: String, default: String = ""): Option[String] =
100-
if (conf.hasPath("%s.%s".format(configPath, key))) {
101-
Some(conf.getString("%s.%s".format(configPath, key)))
102-
}
103-
else {
104-
if (default.nonEmpty) {
105-
Some(default)
106-
}
107-
else {
108-
None
109-
}
110-
}
111130

112131
def poolOptionsConfig(key: String, default: Int): Int =
113132
if (conf.hasPath("%s.pool.%s".format(configPath, key))) {
@@ -117,13 +136,7 @@ object MongoConfig {
117136
default
118137
}
119138

120-
val port: Int =
121-
if (conf.hasPath("%s.port".format(configPath))) {
122-
conf.getInt("%s.port".format(configPath))
123-
}
124-
else {
125-
DefaultPort
126-
}
139+
val port: Int = intConfig(configPath, "port", DefaultPort)
127140

128141
val compressors: List[String] =
129142
if (conf.hasPath("%s.compressors".format(configPath))) {
@@ -133,12 +146,12 @@ object MongoConfig {
133146
List()
134147
}
135148

136-
val host = stringConfig("host", DefaultHost).get
137-
val database = stringConfig("database").get
138-
val userName = stringConfig("userName")
139-
val password = stringConfig("password")
140-
val authDatabase = stringConfig("authDatabase", DefaultAuthenticationDatabaseName).get
141-
val applicationName = stringConfig("applicationName", DefaultApplicationName).get
149+
val host = stringConfig(configPath, "host", DefaultHost).get
150+
val database = stringConfig(configPath, "database").get
151+
val userName = stringConfig(configPath, "userName")
152+
val password = stringConfig(configPath, "password")
153+
val authDatabase = stringConfig(configPath, "authDatabase", DefaultAuthenticationDatabaseName).get
154+
val applicationName = stringConfig(configPath, "applicationName", DefaultApplicationName).get
142155

143156
val poolOptions = MongoPoolOptions(
144157
poolOptionsConfig("maxConnectionIdleTime", DefaultPoolMaxConnectionIdleTime),
Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,55 @@
11
package com.sfxcode.nosql.mongo.server
22

3+
import better.files.File
4+
import com.sfxcode.nosql.mongo.server.ServerConfig.DefaultServerConfigPathPrefix
35
import de.bwaldvogel.mongo.MongoServer
6+
import de.bwaldvogel.mongo.backend.h2.H2Backend
47
import de.bwaldvogel.mongo.backend.memory.MemoryBackend
58

69
case class LocalServer(serverConfig: ServerConfig = ServerConfig()) {
7-
private val server = new MongoServer(new MemoryBackend())
10+
private var h2Path = "undefined"
11+
12+
private val server: MongoServer = {
13+
if (ServerBackend.H2 == serverConfig.backend) {
14+
if (serverConfig.h2BackendConfig.isDefined && !serverConfig.h2BackendConfig.get.inMemory) {
15+
if (serverConfig.h2BackendConfig.get.path.nonEmpty) {
16+
h2Path = serverConfig.h2BackendConfig.get.path
17+
}
18+
else {
19+
h2Path = File.temporaryFile().get().path.toString
20+
}
21+
createH2Server(h2Path)
22+
}
23+
else {
24+
createH2InMemoryServer
25+
}
26+
}
27+
else {
28+
createInMemoryServer
29+
}
30+
}
831

932
server.bind(serverConfig.host, serverConfig.port)
1033

11-
def name: String = serverConfig.name
34+
def name: String = serverConfig.serverName
35+
36+
def h2FilePath: String = h2Path
1237

1338
def shutdown(): Unit = server.shutdown()
1439

40+
private def createInMemoryServer: MongoServer =
41+
new MongoServer(new MemoryBackend())
42+
43+
private def createH2InMemoryServer: MongoServer =
44+
new MongoServer(H2Backend.inMemory())
45+
46+
private def createH2Server(path: String): MongoServer =
47+
new MongoServer(new H2Backend(path))
48+
49+
}
50+
51+
object LocalServer {
52+
53+
def fromPath(configPath: String = DefaultServerConfigPathPrefix): LocalServer =
54+
LocalServer(ServerConfig.fromPath(configPath))
1555
}
Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,60 @@
11
package com.sfxcode.nosql.mongo.server
22

3-
import ServerConfig._
3+
import com.sfxcode.nosql.mongo.database.ConfigHelper
4+
import com.sfxcode.nosql.mongo.server.ServerBackend.ServerBackend
45

5-
case class ServerConfig(name: String = "MongoLocal", host: String = DefaultHost, port: Int = DefaultPort)
6+
case class ServerConfig(
7+
serverName: String = ServerConfig.DefaultServerName,
8+
host: String = ServerConfig.DefaultHost,
9+
port: Int = ServerConfig.DefaultPort,
10+
backend: ServerBackend = ServerBackend.Memory,
11+
h2BackendConfig: Option[H2BackendConfig] = None
12+
)
613

7-
object ServerConfig {
8-
val DefaultHost = "localhost"
9-
val DefaultPort = 28018
14+
case class H2BackendConfig(inMemory: Boolean = true, path: String = "")
15+
16+
object ServerBackend extends Enumeration {
17+
type ServerBackend = Value
18+
val Memory, H2 = Value
19+
}
20+
21+
object ServerConfig extends ConfigHelper {
22+
val DefaultServerConfigPathPrefix = "local.mongo.server"
23+
24+
val DefaultServerName = "local-mongo-server"
25+
val DefaultHost = "localhost"
26+
val DefaultPort = 28018
27+
28+
def serverBackendFromString(backendName: String): ServerBackend.Value =
29+
if (ServerBackend.H2.toString.toLowerCase.equals(backendName.toLowerCase)) {
30+
ServerBackend.H2
31+
}
32+
else {
33+
ServerBackend.Memory
34+
}
35+
36+
def fromPath(configPath: String = DefaultServerConfigPathPrefix): ServerConfig = {
37+
38+
val name = stringConfig(configPath, "serverName", DefaultServerName).get
39+
val host = stringConfig(configPath, "host", DefaultHost).get
40+
val port = intConfig(configPath, "port", DefaultPort)
41+
42+
val backend = stringConfig(configPath, "backend").get
43+
44+
val serverBackend: ServerBackend = serverBackendFromString(backend)
45+
46+
val h2BackendConfig: Option[H2BackendConfig] = {
47+
if (serverBackend == ServerBackend.H2 && conf.hasPath("%s.%s".format(configPath, "h2.inMemory"))) {
48+
val inMemory = booleanConfig(configPath, "h2.inMemory")
49+
val path = stringConfig(configPath, "h2.path").get
50+
Some(H2BackendConfig(inMemory, path))
51+
}
52+
else {
53+
None
54+
}
55+
}
56+
57+
ServerConfig(name, host, port, serverBackend, h2BackendConfig)
58+
59+
}
1060
}

src/test/resources/application.conf

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ unit.test.mongo {
1212
compressors = ["snappy"]
1313
}
1414

15+
unit.test.local.mongo.server {
16+
host = "localhost"
17+
port = 28028
18+
serverName = "local-unit-test-server"
19+
backend = "h2"
20+
h2 {
21+
inMemory = false
22+
path = "/tmp/local-unit-test-server.mv"
23+
}
24+
}
25+
1526
unit.test.mongo.local {
1627
database = "simple-mongo-unit-test"
1728
host = "localhost"
18-
port = 28018
29+
port = 28028
1930
applicationName = "simple-mongo-config-test"
2031
pool {
2132
minSize = 5

src/test/scala/com/sfxcode/nosql/mongo/test/UniversityDatabase.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import org.bson.codecs.configuration.CodecRegistry
99
import org.mongodb.scala.bson.codecs.Macros._
1010

1111
object UniversityDatabase {
12-
val LocalTestServer = LocalServer()
12+
// create local test server (mongo-java-server)
13+
val LocalTestServer = LocalServer.fromPath("unit.test.local.mongo.server")
1314

1415
// create codecs for custom classes
1516
private val universityRegistry: CodecRegistry = fromProviders(classOf[Student], classOf[Score], classOf[Grade])

0 commit comments

Comments
 (0)