Skip to content

Commit 4d18469

Browse files
committed
chore: Dependency Update and changes for mongo-scala-driver 5.0.0
1 parent d3be4e4 commit 4d18469

8 files changed

Lines changed: 78 additions & 114 deletions

File tree

build.sbt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ resolvers += "Sonatype OSS Snapshots".at("https://oss.sonatype.org/content/repos
6363

6464
libraryDependencies += "org.specs2" %% "specs2-core" % "4.20.5" % Test
6565

66-
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.5.0" % Test
66+
libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.5.3" % Test
6767

6868
libraryDependencies += "joda-time" % "joda-time" % "2.12.7" % Test
6969

@@ -75,7 +75,7 @@ libraryDependencies ++= Seq(
7575
"io.circe" %% "circe-parser"
7676
).map(_ % circeVersion % Test)
7777

78-
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "4.11.1"
78+
libraryDependencies += "org.mongodb.scala" %% "mongo-scala-driver" % "5.0.0"
7979

8080
libraryDependencies += "org.xerial.snappy" % "snappy-java" % "1.1.10.5" % Provided
8181

src/main/scala/dev/mongocamp/driver/mongodb/GridFSDAO.scala

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,56 @@
11
package dev.mongocamp.driver.mongodb
22

3-
import dev.mongocamp.driver.mongodb.database.{ ChangeObserver, CollectionStatus, DatabaseProvider }
3+
import dev.mongocamp.driver.mongodb.database.{ChangeObserver, CollectionStatus, DatabaseProvider}
44
import dev.mongocamp.driver.mongodb.gridfs.Metadata
55
import org.bson.types.ObjectId
66
import org.mongodb.scala.bson.conversions.Bson
7-
import org.mongodb.scala.gridfs.{ GridFSBucket, GridFSFile }
7+
import org.mongodb.scala.gridfs.{GridFSBucket, GridFSFile}
88
import org.mongodb.scala.model.CountOptions
9-
import org.mongodb.scala.{ Document, Observable, ReadConcern, ReadPreference, SingleObservable, WriteConcern }
9+
import org.mongodb.scala.{Document, Observable, ReadConcern, ReadPreference, SingleObservable, WriteConcern}
1010

1111
abstract class GridFSDAO(provider: DatabaseProvider, bucketName: String) extends Metadata(provider, bucketName) {
1212

13-
var bucket: GridFSBucket = provider.bucket(bucketName)
13+
protected var bucket: GridFSBucket = provider.bucket(bucketName)
1414

15-
val databaseName: String = provider.guessDatabaseName(bucketName)
15+
protected val databaseName: String = provider.guessDatabaseName(bucketName)
1616

17-
def addChangeObserver(observer: ChangeObserver[Document]): ChangeObserver[Document] =
17+
def addChangeObserver(observer: ChangeObserver[Document]): ChangeObserver[Document] = {
1818
Files.addChangeObserver(observer: ChangeObserver[Document])
19+
}
1920

2021
def fileCollectionStatus: Observable[CollectionStatus] = Files.collectionStatus
2122

2223
def chunkCollectionStats: Observable[CollectionStatus] = Chunks.collectionStatus
2324

2425
protected def gridfsBucket: GridFSBucket = bucket
2526

26-
def count(filter: Bson = Document(), options: CountOptions = CountOptions()): Observable[Long] =
27-
Files.count(filter, options)
27+
def count(filter: Bson = Document(), options: CountOptions = CountOptions()): Observable[Long] = Files.count(filter, options)
2828

29-
def createMetadataIndex(key: String, sortAscending: Boolean = true): SingleObservable[String] =
29+
def createMetadataIndex(key: String, sortAscending: Boolean = true): SingleObservable[String] = {
3030
Files.createIndexForField(createMetadataKey(key), sortAscending)
31+
}
3132

32-
def dropIndexForName(name: String): SingleObservable[Void] =
33-
Files.dropIndexForName(name)
33+
def dropIndexForName(name: String): SingleObservable[Unit] = Files.dropIndexForName(name)
3434

35-
def renameFile(id: ObjectId, newFilename: String): Observable[Void] =
36-
gridfsBucket.rename(id, newFilename)
35+
def renameFile(id: ObjectId, newFilename: String): Observable[Unit] = gridfsBucket.rename(id, newFilename)
3736

38-
def renameFile(file: GridFSFile, newFilename: String): Observable[Void] =
39-
gridfsBucket.rename(file.getId, newFilename)
37+
def renameFile(file: GridFSFile, newFilename: String): Observable[Unit] = gridfsBucket.rename(file.getId, newFilename)
4038

41-
def withReadConcern(readConcern: ReadConcern): Unit =
39+
def withReadConcern(readConcern: ReadConcern): Unit = {
4240
bucket = GridFSBucket(provider.database(), bucketName).withReadConcern(readConcern)
41+
}
4342

44-
def withWriteConcern(writeConcern: WriteConcern): Unit =
43+
def withWriteConcern(writeConcern: WriteConcern): Unit = {
4544
bucket = GridFSBucket(provider.database(), bucketName).withWriteConcern(writeConcern)
45+
}
4646

47-
def withChunkSizeBytes(chunkSizeBytes: Int): Unit =
47+
def withChunkSizeBytes(chunkSizeBytes: Int): Unit = {
4848
bucket = GridFSBucket(provider.database(), bucketName).withChunkSizeBytes(chunkSizeBytes)
49+
}
4950

50-
def withReadPreference(readPreference: ReadPreference): Unit =
51+
def withReadPreference(readPreference: ReadPreference): Unit = {
5152
bucket = GridFSBucket(provider.database(), bucketName).withReadPreference(readPreference)
53+
}
5254

5355
override def toString: String = "%s:%s@%s, %s".format(databaseName, bucketName, provider.config, super.toString)
5456

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

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

33
import dev.mongocamp.driver.mongodb._
44
import dev.mongocamp.driver.mongodb.bson.codecs.CustomCodecProvider
5-
import org.bson.codecs.configuration.CodecRegistries.{ fromProviders, fromRegistries }
5+
import org.bson.codecs.configuration.CodecRegistries.{fromProviders, fromRegistries}
66
import org.bson.codecs.configuration.CodecRegistry
77
import org.mongodb.scala.MongoClient.DEFAULT_CODEC_REGISTRY
88
import org.mongodb.scala._
@@ -12,8 +12,8 @@ import scala.collection.mutable
1212
import scala.reflect.ClassTag
1313

1414
class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) extends Serializable {
15-
private val cachedDatabaseMap = new mutable.HashMap[String, MongoDatabase]()
16-
private val cachedMongoDAOMap = new mutable.HashMap[String, MongoDAO[Document]]()
15+
private val cachedDatabaseMap = new mutable.HashMap[String, MongoDatabase]()
16+
private val cachedMongoDAOMap = new mutable.HashMap[String, MongoDAO[Document]]()
1717
private var cachedClient: Option[MongoClient] = None
1818

1919
val DefaultDatabaseName: String = config.database
@@ -40,7 +40,7 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
4040

4141
def databaseNames: List[String] = databaseInfos.map(info => info.name)
4242

43-
def dropDatabase(databaseName: String = DefaultDatabaseName): SingleObservable[Void] = database(databaseName).drop()
43+
def dropDatabase(databaseName: String = DefaultDatabaseName): SingleObservable[Unit] = database(databaseName).drop()
4444

4545
def compactDatabase(databaseName: String = DefaultDatabaseName, maxWaitPerCollection: Int = DefaultMaxWait): List[CompactResult] = {
4646
collectionNames(databaseName).flatMap(collectionName => dao(collectionName).compact.result(maxWaitPerCollection))
@@ -83,15 +83,13 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
8383
database(databaseName).runCommand(document)
8484
}
8585

86-
def collectionStatus(
87-
collectionName: String,
88-
databaseName: String = DefaultDatabaseName
89-
): Observable[CollectionStatus] =
86+
def collectionStatus(collectionName: String, databaseName: String = DefaultDatabaseName): Observable[CollectionStatus] = {
9087
runCommand(Map("collStats" -> collectionName), databaseName).map(document => CollectionStatus(document))
88+
}
9189

9290
def collection[A](collectionName: String)(implicit ct: ClassTag[A]): MongoCollection[A] =
9391
if (collectionName.contains(DatabaseProvider.CollectionSeparator)) {
94-
val newDatabaseName: String = guessDatabaseName(collectionName)
92+
val newDatabaseName: String = guessDatabaseName(collectionName)
9593
val newCollectionName: String = guessName(collectionName)
9694
database(newDatabaseName).getCollection[A](newCollectionName)
9795
}
@@ -120,7 +118,7 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
120118
def bucket(bucketName: String): GridFSBucket = {
121119
if (bucketName.contains(DatabaseProvider.CollectionSeparator)) {
122120
val newDatabaseName = guessDatabaseName(bucketName)
123-
val newBucketName = guessName(bucketName)
121+
val newBucketName = guessName(bucketName)
124122
GridFSBucket(database(newDatabaseName), newBucketName)
125123
}
126124
else {
@@ -144,7 +142,7 @@ class DatabaseProvider(val config: MongoConfig, val registry: CodecRegistry) ext
144142
}
145143

146144
object DatabaseProvider {
147-
val ObjectIdKey = "_id"
145+
val ObjectIdKey = "_id"
148146
val CollectionSeparator = ":"
149147

150148
private val CustomRegistry = fromProviders(CustomCodecProvider())

src/main/scala/dev/mongocamp/driver/mongodb/gridfs/Base.scala

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,22 @@ import dev.mongocamp.driver.mongodb.Converter
99
import dev.mongocamp.driver.mongodb.database.DatabaseProvider
1010
import com.typesafe.scalalogging.LazyLogging
1111
import org.mongodb.scala.bson.ObjectId
12-
import org.mongodb.scala.gridfs.{ GridFSBucket, GridFSDownloadObservable }
13-
import org.mongodb.scala.{ Document, Observable, ReadConcern, ReadPreference, WriteConcern }
12+
import org.mongodb.scala.gridfs.{GridFSBucket, GridFSDownloadObservable}
13+
import org.mongodb.scala.{Document, Observable, ReadConcern, ReadPreference, WriteConcern}
1414

1515
abstract class Base extends LazyLogging {
1616

1717
protected def gridfsBucket: GridFSBucket
1818

1919
def createMetadataKey(key: String): String = {
2020
var metadataKey = key
21-
if (!metadataKey.startsWith("metadata"))
21+
if (!metadataKey.startsWith("metadata")) {
2222
metadataKey = "%s.%s".format("metadata", key)
23+
}
2324
metadataKey
2425
}
2526

26-
def drop(): Observable[Void] = gridfsBucket.drop()
27+
def drop(): Observable[Unit] = gridfsBucket.drop()
2728

2829
def bucketName: String = gridfsBucket.bucketName
2930

@@ -35,49 +36,35 @@ abstract class Base extends LazyLogging {
3536

3637
def readConcern: ReadConcern = gridfsBucket.readConcern
3738

38-
def upload(
39-
fileName: String,
40-
source: Observable[ByteBuffer],
41-
metadata: AnyRef = Document(),
42-
chunkSizeBytes: Int = 1024 * 256
43-
): Observable[ObjectId] = {
39+
def upload(fileName: String, source: Observable[ByteBuffer], metadata: AnyRef = Document(), chunkSizeBytes: Int = 1024 * 256): Observable[ObjectId] = {
4440
val metadataDocument = {
4541
metadata match {
4642
case document: Document => document
47-
case _ => Converter.toDocument(metadata)
43+
case _ => Converter.toDocument(metadata)
4844
}
4945
}
50-
val options: GridFSUploadOptions = new GridFSUploadOptions()
51-
.chunkSizeBytes(chunkSizeBytes)
52-
.metadata(metadataDocument)
46+
val options: GridFSUploadOptions = new GridFSUploadOptions().chunkSizeBytes(chunkSizeBytes).metadata(metadataDocument)
5347
gridfsBucket.uploadFromObservable(fileName, source, options)
5448
}
5549

56-
def uploadFile(
57-
fileName: String,
58-
file: File,
59-
metadata: AnyRef = Document(),
60-
chunkSizeBytes: Int = 1204 * 256
61-
): Observable[ObjectId] =
50+
def uploadFile(fileName: String, file: File, metadata: AnyRef = Document(), chunkSizeBytes: Int = 1204 * 256): Observable[ObjectId] = {
6251
upload(fileName, GridFSStreamObservable(file.newInputStream, chunkSizeBytes), metadata, chunkSizeBytes)
52+
}
6353

64-
def download(oid: ObjectId): GridFSDownloadObservable =
65-
gridfsBucket.downloadToObservable(oid)
54+
def download(oid: ObjectId): GridFSDownloadObservable = gridfsBucket.downloadToObservable(oid)
6655

67-
def download(id: ObjectId, file: File): GridFSStreamObserver =
68-
download(id, file.newOutputStream)
56+
def download(id: ObjectId, file: File): GridFSStreamObserver = download(id, file.newOutputStream)
6957

7058
def downloadFileResult(id: ObjectId, file: File): Long = streamObserverResult(download(id, file))
7159

7260
def download(oid: ObjectId, outputStream: OutputStream): GridFSStreamObserver = {
7361
val observable: GridFSDownloadObservable = gridfsBucket.downloadToObservable(oid)
74-
val observer = GridFSStreamObserver(outputStream)
62+
val observer = GridFSStreamObserver(outputStream)
7563
observable.subscribe(observer)
7664
observer
7765
}
7866

79-
def downloadStreamResult(id: ObjectId, outputStream: OutputStream): Long =
80-
streamObserverResult(download(id, outputStream))
67+
def downloadStreamResult(id: ObjectId, outputStream: OutputStream): Long = streamObserverResult(download(id, outputStream))
8168

8269
protected def streamObserverResult(observer: GridFSStreamObserver): Long = {
8370
while (!observer.completed.get) {}

src/main/scala/dev/mongocamp/driver/mongodb/gridfs/Crud.scala

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@ package dev.mongocamp.driver.mongodb.gridfs
33
import java.io.InputStream
44

55
import org.mongodb.scala.bson.ObjectId
6-
import org.mongodb.scala.{ Document, Observable }
6+
import org.mongodb.scala.{Document, Observable}
77

88
abstract class Crud extends Search {
99

10-
def deleteOne(id: ObjectId): Observable[Void] = gridfsBucket.delete(id)
10+
def deleteOne(id: ObjectId): Observable[Unit] = gridfsBucket.delete(id)
1111

12-
def insertOne(
13-
fileName: String,
14-
stream: InputStream,
15-
metadata: AnyRef = Document(),
16-
chunkSizeBytes: Int = 1204 * 256
17-
): Observable[ObjectId] =
12+
def insertOne(fileName: String, stream: InputStream, metadata: AnyRef = Document(), chunkSizeBytes: Int = 1204 * 256): Observable[ObjectId] = {
1813
upload(fileName, GridFSStreamObservable(stream, chunkSizeBytes), metadata, chunkSizeBytes)
14+
}
1915

2016
}

src/main/scala/dev/mongocamp/driver/mongodb/operation/Base.scala

Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,22 @@ import com.typesafe.scalalogging.LazyLogging
44
import dev.mongocamp.driver.mongodb.database.MongoIndex
55
import org.mongodb.scala.bson.conversions.Bson
66
import org.mongodb.scala.model.Sorts._
7-
import org.mongodb.scala.model.{ CountOptions, DropIndexOptions, IndexOptions, Indexes }
8-
import org.mongodb.scala.{ Document, ListIndexesObservable, MongoCollection, Observable, SingleObservable }
7+
import org.mongodb.scala.model.{CountOptions, DropIndexOptions, IndexOptions, Indexes}
8+
import org.mongodb.scala.{Document, ListIndexesObservable, MongoCollection, Observable, SingleObservable}
99

1010
import scala.concurrent.duration.Duration
11-
import scala.reflect.ClassTag
1211

13-
abstract class Base[A]()(implicit ct: ClassTag[A]) extends LazyLogging {
12+
abstract class Base[A] extends LazyLogging {
1413

1514
protected def coll: MongoCollection[A]
1615

1716
def count(filter: Bson = Document(), options: CountOptions = CountOptions()): Observable[Long] = {
1817
coll.countDocuments(filter, options)
1918
}
2019

21-
def drop(): Observable[Void] = coll.drop()
20+
def drop(): Observable[Unit] = coll.drop()
2221

23-
def createIndexForField(
24-
fieldName: String,
25-
sortAscending: Boolean = true,
26-
options: IndexOptions = IndexOptions()
27-
): SingleObservable[String] = {
22+
def createIndexForField(fieldName: String, sortAscending: Boolean = true, options: IndexOptions = IndexOptions()): SingleObservable[String] = {
2823
if (sortAscending) {
2924
createIndex(ascending(fieldName), options)
3025
}
@@ -33,46 +28,35 @@ abstract class Base[A]()(implicit ct: ClassTag[A]) extends LazyLogging {
3328
}
3429
}
3530

36-
def createIndexForFieldWithName(
37-
fieldName: String,
38-
sortAscending: Boolean = true,
39-
name: String
40-
): SingleObservable[String] =
31+
def createIndexForFieldWithName(fieldName: String, sortAscending: Boolean = true, name: String): SingleObservable[String] = {
4132
createIndexForField(fieldName, sortAscending, MongoIndex.indexOptionsWithName(Some(name)))
33+
}
4234

43-
def createUniqueIndexForField(
44-
fieldName: String,
45-
sortAscending: Boolean = true,
46-
name: Option[String] = None
47-
): SingleObservable[String] =
35+
def createUniqueIndexForField(fieldName: String, sortAscending: Boolean = true, name: Option[String] = None): SingleObservable[String] = {
4836
createIndexForField(fieldName, sortAscending, MongoIndex.indexOptionsWithName(name).unique(true))
37+
}
4938

50-
def createHashedIndexForField(fieldName: String, options: IndexOptions = IndexOptions()): SingleObservable[String] =
39+
def createHashedIndexForField(fieldName: String, options: IndexOptions = IndexOptions()): SingleObservable[String] = {
5140
createIndex(Indexes.hashed(fieldName), options)
41+
}
5242

53-
def createTextIndexForField(fieldName: String, options: IndexOptions = IndexOptions()): SingleObservable[String] =
43+
def createTextIndexForField(fieldName: String, options: IndexOptions = IndexOptions()): SingleObservable[String] = {
5444
createIndex(Indexes.text(fieldName), options)
45+
}
5546

56-
def createExpiringIndexForField(
57-
fieldName: String,
58-
duration: Duration,
59-
sortAscending: Boolean = true,
60-
name: Option[String] = None
61-
): SingleObservable[String] =
62-
createIndexForField(
63-
fieldName,
64-
sortAscending,
65-
MongoIndex.indexOptionsWithName(name).expireAfter(duration._1, duration._2)
66-
)
67-
68-
def createIndex(key: Bson, options: IndexOptions = IndexOptions()): SingleObservable[String] =
69-
coll.createIndex(key, options)
70-
71-
def dropIndexForName(name: String, options: DropIndexOptions = new DropIndexOptions()): SingleObservable[Void] =
47+
def createExpiringIndexForField(fieldName: String, duration: Duration, sortAscending: Boolean = true, name: Option[String] = None): SingleObservable[String] = {
48+
createIndexForField(fieldName, sortAscending, MongoIndex.indexOptionsWithName(name).expireAfter(duration._1, duration._2))
49+
}
50+
51+
def createIndex(key: Bson, options: IndexOptions = IndexOptions()): SingleObservable[String] = coll.createIndex(key, options)
52+
53+
def dropIndexForName(name: String, options: DropIndexOptions = new DropIndexOptions()): SingleObservable[Unit] = {
7254
coll.dropIndex(name, options)
55+
}
7356

74-
def dropIndex(keys: Bson, options: DropIndexOptions = new DropIndexOptions()): SingleObservable[Void] =
57+
def dropIndex(keys: Bson, options: DropIndexOptions = new DropIndexOptions()): SingleObservable[Unit] = {
7558
coll.dropIndex(keys, options)
59+
}
7660

7761
def listIndexes: ListIndexesObservable[Map[String, Any]] = coll.listIndexes[Map[String, Any]]()
7862

src/test/scala/dev/mongocamp/driver/mongodb/gridfs/GridfsDatabaseFunctions.scala

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,13 @@ import org.mongodb.scala.result.UpdateResult
1010

1111
trait GridfsDatabaseFunctions extends MongoImplicits {
1212

13-
def createIndexOnImages(key: String): String =
14-
ImageFilesDAO.createMetadataIndex(key)
13+
def createIndexOnImages(key: String): String = ImageFilesDAO.createMetadataIndex(key)
1514

16-
def dropIndexOnImages(key: String): Void =
17-
ImageFilesDAO.dropIndexForName(key)
15+
def dropIndexOnImages(key: String): Unit = ImageFilesDAO.dropIndexForName(key)
1816

19-
def deleteImage(id: ObjectId): Void =
20-
ImageFilesDAO.deleteOne(id)
17+
def deleteImage(id: ObjectId): Unit = ImageFilesDAO.deleteOne(id)
2118

22-
def dropImages: Void = ImageFilesDAO.drop()
19+
def dropImages: Unit = ImageFilesDAO.drop()
2320

2421
def imagesCount: Long = ImageFilesDAO.count()
2522

src/test/scala/dev/mongocamp/driver/mongodb/operation/IndexSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class IndexSpec extends PersonSpecification {
2323
val index: MongoIndex = PersonDAO.indexForName("name_1").get
2424
index.expire must beFalse
2525

26-
val dropIndexResult: Void = PersonDAO.dropIndexForName(createIndexResult).result()
26+
val dropIndexResult: Unit = PersonDAO.dropIndexForName(createIndexResult).result()
2727

2828
PersonDAO.indexList must haveSize(1)
2929
}

0 commit comments

Comments
 (0)