Skip to content

Commit 2d8e853

Browse files
committed
Initial Mongo Java Server Support
1 parent aa12e7f commit 2d8e853

File tree

10 files changed

+58
-11
lines changed

10 files changed

+58
-11
lines changed

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Versions
44

5+
### 2.0.1
6+
* Mongo Java Server Support
7+
58
### 2.0.0
69
* mongo package no longer extends MongoImplcits => Use MongoImplcits trait if needed
710

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ libraryDependencies += "org.xerial.snappy" % "snappy-java" % "1.1.7.3" % Provide
5959

6060
libraryDependencies += "com.github.luben" % "zstd-jni" % "1.4.4-9" % Provided
6161

62+
libraryDependencies += "de.bwaldvogel" % "mongo-java-server" % "1.28.0" % Provided
63+
6264
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.0.2"
6365

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

src/main/scala/com/sfxcode/nosql/mongo/operation/Search.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class Search[A]()(implicit ct: ClassTag[A]) extends Base[A] {
3838
def distinctResult[S <: Any](fieldName: String, filter: Bson = Document()): Seq[S] =
3939
distinct(fieldName, filter).resultList().map(v => fromBson(v).asInstanceOf[S])
4040

41-
def findAggregated(aggregator: Seq[Bson], allowDiskUse: Boolean = false): AggregateObservable[A] =
42-
coll.aggregate(aggregator).allowDiskUse(allowDiskUse)
41+
def findAggregated(pipeline: Seq[Bson], allowDiskUse: Boolean = false): AggregateObservable[A] =
42+
coll.aggregate(pipeline).allowDiskUse(allowDiskUse)
4343

4444
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.sfxcode.nosql.mongo.server
2+
3+
import de.bwaldvogel.mongo.MongoServer
4+
import de.bwaldvogel.mongo.backend.memory.MemoryBackend
5+
6+
case class LocalServer(serverConfig: ServerConfig = ServerConfig()) {
7+
private val server = new MongoServer(new MemoryBackend())
8+
9+
server.bind(serverConfig.host, serverConfig.port)
10+
11+
def name: String = serverConfig.name
12+
13+
def shutdown(): Unit = server.shutdown()
14+
15+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.sfxcode.nosql.mongo.server
2+
3+
import ServerConfig._
4+
5+
case class ServerConfig(name: String = "MongoLocal", host: String = DefaultHost, port: Int = DefaultPort)
6+
7+
object ServerConfig {
8+
val DefaultHost = "localhost"
9+
val DefaultPort = 28018
10+
}

src/test/resources/application.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ unit.test.mongo {
1212
compressors = ["snappy"]
1313
}
1414

15+
unit.test.mongo.local {
16+
database = "simple-mongo-unit-test"
17+
host = "localhost"
18+
port = 28018
19+
applicationName = "simple-mongo-config-test"
20+
pool {
21+
minSize = 5
22+
maxSize = 10
23+
}
24+
compressors = ["snappy"]
25+
}
26+
1527
config.test.mongo {
1628
database = "simple-mongo-unit-test"
1729
host = "localhost"

src/test/scala/com/sfxcode/nosql/mongo/dao/BookDAOSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ class BookDAOSpec extends Specification with BeforeAll {
4242
val dateFilter = dateInRangeFilter("publishedDate", From, new Date())
4343

4444
val filterStage = filter(and(categoryFilter, dateFilter))
45-
val aggregator = List(filterStage, projectStage)
45+
val pipeline = List(filterStage, projectStage)
4646

47-
val list = BookDAO.Raw.findAggregated(aggregator).resultList()
47+
val list = BookDAO.Raw.findAggregated(pipeline).resultList()
4848
list must haveSize(8)
4949
}
5050

@@ -53,9 +53,9 @@ class BookDAOSpec extends Specification with BeforeAll {
5353
val groupStage: Bson =
5454
group("$categories", Field.sumField("pageCount"), Field.minField("pageCount"), Field.maxField("pageCount"))
5555

56-
val aggregator = List(groupStage)
56+
val pipeline = List(groupStage)
5757

58-
val list = BookDAO.Raw.findAggregated(aggregator).resultList().map(doc => doc.asPlainMap)
58+
val list = BookDAO.Raw.findAggregated(pipeline).resultList().map(doc => doc.asPlainMap)
5959
list must haveSize(58)
6060
}
6161
}

src/test/scala/com/sfxcode/nosql/mongo/operation/AggregationSpec.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ class AggregationSpec extends PersonSpecification {
2525

2626
"support aggregation filter" in {
2727

28-
val aggregator = List(filterStage, sortStage)
28+
val pipeline = List(filterStage, sortStage)
2929

30-
val aggregated = PersonDAO.findAggregated(aggregator).resultList()
30+
val aggregated = PersonDAO.findAggregated(pipeline).resultList()
3131

3232
aggregated.size must be equalTo 98
3333

3434
}
3535

3636
"support aggregation filter and group" in {
3737
// #agg_execute
38-
val aggregator = List(filterStage, groupStage, sortStage)
38+
val pipeline = List(filterStage, groupStage, sortStage)
3939

40-
val aggregated = PersonDAO.Raw.findAggregated(aggregator).resultList()
40+
val aggregated = PersonDAO.Raw.findAggregated(pipeline).resultList()
4141

4242
// #agg_execute
4343

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.sfxcode.nosql.mongo.server
2+
3+
class LocalServerSpec {}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,22 @@ package com.sfxcode.nosql.mongo.test
22

33
import com.sfxcode.nosql.mongo.database.DatabaseProvider
44
import com.sfxcode.nosql.mongo.model._
5+
import com.sfxcode.nosql.mongo.server.LocalServer
56
import com.sfxcode.nosql.mongo.{GridFSDAO, MongoDAO}
67
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
78
import org.bson.codecs.configuration.CodecRegistry
89
import org.mongodb.scala.bson.codecs.Macros._
910

1011
object UniversityDatabase {
12+
val LocalTestServer = LocalServer()
1113

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

1517
private val registry: CodecRegistry = fromRegistries(universityRegistry)
1618

1719
// create provider
18-
val provider: DatabaseProvider = DatabaseProvider.fromPath(configPath = "unit.test.mongo", registry = registry)
20+
val provider: DatabaseProvider = DatabaseProvider.fromPath(configPath = "unit.test.mongo.local", registry = registry)
1921

2022
// setup DAO objects with mongo collection names
2123

0 commit comments

Comments
 (0)