Skip to content

Commit 24815fc

Browse files
committed
gridfs handling
1 parent bc489f1 commit 24815fc

File tree

12 files changed

+49
-41
lines changed

12 files changed

+49
-41
lines changed

CHANGES.md

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

33
## Versions
44
* mongo-scala-driver 4.0.2
5+
* maxWaitQueueSize is removed since 4.0.0
6+
*
57

68
### 1.7.1
79
* [mongo-scala-driver 2.9.0](https://mongodb.github.io/mongo-scala-driver/2.9/changelog/)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ case class MongoConfig(
3333
.maxConnectionIdleTime(poolOptions.maxConnectionIdleTime, TimeUnit.SECONDS)
3434
.maxSize(poolOptions.maxSize)
3535
.minSize(poolOptions.minSize)
36-
.maxWaitQueueSize(poolOptions.maxWaitQueueSize)
3736
.maintenanceInitialDelay(poolOptions.maintenanceInitialDelay, TimeUnit.SECONDS)
3837
.build()
3938

@@ -107,7 +106,6 @@ object MongoConfig {
107106
poolOptionsConfig("maxConnectionIdleTime", DefaultPoolMaxConnectionIdleTime),
108107
poolOptionsConfig("maxSize", DefaultPoolMaxSize),
109108
poolOptionsConfig("minSize", DefaultPoolMinSize),
110-
poolOptionsConfig("maxWaitQueueSize", DefaultPoolMaxWaitQueueSize),
111109
poolOptionsConfig("maintenanceInitialDelay", DefaultPoolMaintenanceInitialDelay))
112110

113111
MongoConfig(database, host, port, applicationName, userName, password, authDatabase, poolOptions)

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ case class MongoPoolOptions(
66
maxConnectionIdleTime: Int = DefaultMaxConnectionIdleTime,
77
maxSize: Int = DefaultMaxSize,
88
minSize: Int = DefaultMinSize,
9-
maxWaitQueueSize: Int = DefaultMaxWaitQueueSize,
109
maintenanceInitialDelay: Int = DefaultMaintenanceInitialDelay) {}
1110

1211
object MongoPoolOptions {
1312
val DefaultMaxConnectionIdleTime = 60
1413
val DefaultMaxSize = 50
1514
val DefaultMinSize = 0
16-
val DefaultMaxWaitQueueSize = 500
1715
val DefaultMaintenanceInitialDelay = 0
1816
}

src/main/scala/com/sfxcode/nosql/mongo/gridfs/Base.scala

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.sfxcode.nosql.mongo.gridfs
22

33
import java.nio.channels.AsynchronousFileChannel
4-
import java.nio.file.{ OpenOption, Path, StandardOpenOption }
4+
import java.nio.file.{OpenOption, Path, StandardOpenOption}
55

66
import com.typesafe.scalalogging.LazyLogging
77
import org.mongodb.scala.bson.ObjectId
8-
import org.mongodb.scala.gridfs.GridFSBucket
9-
import org.mongodb.scala.gridfs.helpers.AsynchronousChannelHelper.channelToOutputStream
10-
import org.mongodb.scala.{ Observable, ReadConcern, ReadPreference, WriteConcern }
8+
import org.mongodb.scala.gridfs.{GridFSBucket, GridFSDownloadObservable}
9+
import org.mongodb.scala.{Observable, ReadConcern, ReadPreference, WriteConcern}
1110

1211
abstract class Base extends LazyLogging {
1312

@@ -21,13 +20,8 @@ abstract class Base extends LazyLogging {
2120
metadataKey
2221
}
2322

24-
def downloadToStream(
25-
oid: ObjectId,
26-
outputPath: Path,
27-
openOptions: Seq[OpenOption] = List(StandardOpenOption.CREATE, StandardOpenOption.WRITE)): Observable[Long] = {
28-
val streamToDownloadTo: AsynchronousFileChannel =
29-
AsynchronousFileChannel.open(outputPath, openOptions: _*)
30-
gridfsBucket.downloadToStream(oid, channelToOutputStream(streamToDownloadTo))
23+
def download(oid: ObjectId):GridFSDownloadObservable = {
24+
gridfsBucket.downloadToObservable(oid)
3125
}
3226

3327
def drop(): Observable[Void] = gridfsBucket.drop()
Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,38 @@
11
package com.sfxcode.nosql.mongo.gridfs
22

3-
import java.io.InputStream
3+
import java.nio.ByteBuffer
44

55
import com.mongodb.client.gridfs.model.GridFSUploadOptions
6-
import com.mongodb.internal.async.client.gridfs.AsyncInputStream
76
import com.sfxcode.nosql.mongo.Converter
87
import org.bson.types.ObjectId
9-
import org.mongodb.scala.gridfs.AsyncInputStream
10-
import org.mongodb.scala.gridfs.AsyncStreamHelper.toAsyncInputStream
11-
import org.mongodb.scala.{Document, Observable}
8+
import org.mongodb.scala.{ Document, Observable }
129

1310
abstract class Crud extends Search {
1411

1512
def deleteOne(id: ObjectId): Observable[Void] = gridfsBucket.delete(id)
1613

17-
def insertOne(
18-
fileName: String,
19-
stream: InputStream,
20-
metadata: AnyRef = Document(),
21-
chunkSizeBytes: Int = chunkSizeBytes): Observable[ObjectId] = {
22-
val streamToUploadFrom: AsyncInputStream = toAsyncInputStream(stream)
14+
def insertOne(fileName: String,
15+
source: Observable[ByteBuffer],
16+
metadata: AnyRef = Document(),
17+
chunkSizeBytes: Int = 1024 * 1204): Observable[ObjectId] = {
2318
val metadataDocument = {
2419
metadata match {
2520
case document: Document => document
26-
case _ => Converter.toDocument(metadata)
21+
case _ => Converter.toDocument(metadata)
2722
}
2823
}
2924
val options: GridFSUploadOptions = new GridFSUploadOptions()
30-
.chunkSizeBytes(1024 * 1204)
25+
.chunkSizeBytes(chunkSizeBytes)
3126
.metadata(metadataDocument)
32-
gridfsBucket.uploadFromStream(fileName, streamToUploadFrom, options)
27+
gridfsBucket.uploadFromObservable(fileName, source, options)
3328
}
3429

30+
def upload(fileName: String,
31+
source: Observable[ByteBuffer],
32+
metadata: AnyRef = Document(),
33+
chunkSizeBytes: Int = 1024 * 1204): Observable[ObjectId] =
34+
insertOne(fileName, source, metadata, chunkSizeBytes)
35+
36+
37+
3538
}
-31.5 KB
Loading

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.sfxcode.nosql.mongo.database.DatabaseProvider
44
import com.sfxcode.nosql.mongo.model._
55
import com.sfxcode.nosql.mongo.operation.CrudObserver
66
import org.mongodb.scala._
7+
import org.mongodb.scala.result.InsertManyResult
78

89
object TestDatabase extends ObservableImplicits {
910

@@ -30,7 +31,7 @@ object TestDatabase extends ObservableImplicits {
3031

3132
val persons: List[Person] = Person.personList
3233

33-
val insertResult: Void = PersonDAO.insertMany(persons)
34+
val insertResult: InsertManyResult = PersonDAO.insertMany(persons)
3435

3536
def printDatabaseStatus(): Unit = {
3637
val count: Long = PersonDAO.count()

src/test/scala/com/sfxcode/nosql/mongo/demo/tour/QuickTour.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.sfxcode.nosql.mongo.demo.tour
33
import com.sfxcode.nosql.mongo._
44
import com.sfxcode.nosql.mongo.model._
55
import TestDatabase._
6-
import org.mongodb.scala.Void
76
import org.mongodb.scala.result.{InsertManyResult, InsertOneResult}
87

98
object QuickTour extends App {

src/test/scala/com/sfxcode/nosql/mongo/demo/tour/TestTour.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.sfxcode.nosql.mongo.demo.tour
33
import com.sfxcode.nosql.mongo._
44
import com.sfxcode.nosql.mongo.model._
55
import TestDatabase._
6-
import org.mongodb.scala.Void
6+
import org.mongodb.scala.result.InsertOneResult
77

88
object TestTour extends App {
99

@@ -14,7 +14,7 @@ object TestTour extends App {
1414

1515
val scalaBook = Book(Some(1), "Programming In Scala", 852, Author("Martin Odersky"), List(2, 4, 10))
1616

17-
val completed: Void = BookDAO.insertOne(scalaBook)
17+
val completed: InsertOneResult = BookDAO.insertOne(scalaBook)
1818

1919
assert(BookDAO.count().headResult() == 1)
2020

src/test/scala/com/sfxcode/nosql/mongo/gridfs/GridfsDatabaseApp.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.sfxcode.nosql.mongo.gridfs
22

3+
import java.io.FileOutputStream
4+
import java.nio.ByteBuffer
5+
36
import com.sfxcode.nosql.mongo._
47
import org.bson.types.ObjectId
58
import com.sfxcode.nosql.mongo.gridfs.GridfsDatabase._

0 commit comments

Comments
 (0)