Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ lazy val vfs = project.in(file("vfs")).
"commons-io" % "commons-io" % "2.4" % Test)
).dependsOn(core % "test->test;compile->compile")

lazy val gridfs = project.in(file("gridfs")).
settings(Common.settings: _*).settings(
name := "benji-gridfs",
libraryDependencies ++= Seq(
Dependencies.slf4jApi,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

je ne pense pas que c'est nécessaire

"org.reactivemongo" %% "reactivemongo" % "0.17.0"
)
).dependsOn(core % "test->test;compile->compile")

lazy val play = project.in(file("play")).
settings(Common.settings ++ Seq(
name := "benji-play",
Expand Down Expand Up @@ -178,7 +187,7 @@ lazy val benji = (project in file(".")).
}
) ++ Publish.settings).
dependsOn(s3, google, vfs, play).
aggregate(core, s3, google, vfs, play)
aggregate(core, s3, google, vfs, play, gridfs)

publishTo in ThisBuild := Some {
import Resolver.mavenStylePatterns
Expand Down
4 changes: 4 additions & 0 deletions gridfs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
project/project
project/target
src/test/resources/local.conf
target
Empty file added gridfs/README.md
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.zengularity.benji.gridfs.GridFSScheme
40 changes: 40 additions & 0 deletions gridfs/src/main/scala/GridFSBucketRef.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018-2019 Zengularity SA (FaberNovel Technologies) <https://www.zengularity.com>
*/
package com.zengularity.benji.gridfs

import scala.concurrent.{ ExecutionContext, Future }

import com.zengularity.benji.{ BucketRef, BucketVersioning }
import com.zengularity.benji.exception.BucketAlreadyExistsException

final class GridFSBucketRef private[gridfs] (
storage: GridFSStorage,
val name: String) extends BucketRef with BucketVersioning {

def exists(implicit ec: ExecutionContext): Future[Boolean] = {
val filesAndChunksStats = for {
gridfs <- storage.transport.gridfs(name)
filesStats <- gridfs.files.stats
chunksStats <- gridfs.chunks.stats
} yield (filesStats, chunksStats)

filesAndChunksStats.transform {
case Success(_) => Success(true)
case Failure(_) => Success(false)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Une storage.transport.gridfs(name) ne devrait pas se retrouver ici

}
}

def create(failsIfExists: Boolean = false)(implicit ec: ExecutionContext): Future[Unit] = {
val isCreated = for {
gridfs <- storage.transport.gridfs(name)
isCreated <- gridfs.ensureIndex()
} yield isCreated

isCreated.transform {
case Success(false) if failsIfExists => Failure[Unit](BucketAlreadyExistsException("toto"))
case Success(_) => Success({})
case failed => failed
}
}
}
35 changes: 35 additions & 0 deletions gridfs/src/main/scala/GridFSFactory.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2018-2019 Zengularity SA (FaberNovel Technologies) <https://www.zengularity.com>
*/

package com.zengularity.benji.gridfs

import java.net.URI

import play.api.libs.ws.ahc.StandaloneAhcWSClient

import com.zengularity.benji.ObjectStorage

import com.zengularity.benji.spi.{ Injector, StorageFactory, StorageScheme }

/**
* This factory is using `javax.inject`
* to resolve `play.api.libs.ws.ahc.StandaloneAhcWSClient`.
*/
final class GridFSFactory extends StorageFactory {
@SuppressWarnings(Array("org.wartremover.warts.TryPartial"))
def apply(injector: Injector, uri: URI): ObjectStorage = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy paste ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, je ne me suis pas encore penché là dessus

@inline implicit def ws: StandaloneAhcWSClient =
injector.instanceOf(classOf[StandaloneAhcWSClient])

GridFS[URI](uri).get
}
}

/** Storage scheme for GridFS */
final class GridFSScheme extends StorageScheme {
val scheme = "gridfs"

@inline
def factoryClass: Class[_ <: StorageFactory] = classOf[GridFSFactory]
}
44 changes: 44 additions & 0 deletions gridfs/src/main/scala/GridFSTransport.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2018-2019 Zengularity SA (FaberNovel Technologies) <https://www.zengularity.com>
*/
package com.zengularity.benji.gridfs

import scala.concurrent.{ ExecutionContext, Future }
import scala.util.{ Failure, Try }

import reactivemongo.api.gridfs.GridFS
import reactivemongo.api.{ BSONSerializationPack, MongoConnection }

import com.zengularity.benji.exception.BenjiUnknownError

final class GridFSTransport(driver: reactivemongo.api.MongoDriver, connection: MongoConnection, mongoUri: MongoConnection.ParsedURI) {
def close(): Unit = {
driver.close()
}

def gridfs(prefix: String)(implicit ec: ExecutionContext): Future[GridFS[BSONSerializationPack.type]] = {
mongoUri.db match {
case Some(name) =>
connection.database(name).map(db => GridFS[BSONSerializationPack.type](db, prefix))

case None =>
Future.failed(new BenjiUnknownError(s"Fails to get the db from $mongoUri"))
}
}
}

object GridFSTransport {
def apply(uri: String): Try[GridFSTransport] = {
val driver = new reactivemongo.api.MongoDriver

val res = for {
mongoUri <- MongoConnection.parseURI(uri)
con <- driver.connection(mongoUri, strictUri = true)
} yield new GridFSTransport(driver, con, mongoUri)

res.recoverWith {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recoverWith vs recover

case error => Failure[GridFSTransport](new BenjiUnknownError(s"Fails to create the connection to $uri", Some(error)))
}
}
}

32 changes: 32 additions & 0 deletions gridfs/src/main/scala/GridsFSStorage.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (C) 2018-2019 Zengularity SA (FaberNovel Technologies) <https://www.zengularity.com>
*/

package com.zengularity.benji.gridfs

import com.zengularity.benji.{ Bucket, ObjectStorage }

class GridFSStorage(val transport: GridFSTransport, val requestTimeout: Option[Long]) extends ObjectStorage { self =>

def withRequestTimeout(timeout: Long) =
new GridFSStorage(transport, Some(timeout))

def bucket(name: String) = new GridFSBucketRef(this, name)

object buckets extends self.BucketsRequest {
def apply()(implicit m: Materializer): Source[Bucket, NotUsed] = {
implicit def ec: ExecutionContext = m.executionContext
val gridfs = transport.gridfsdb
}
}
}

object GridFSStorage {
/**
* Returns a client for GridsFS Object Storage.
*
* @param transport the GridFS transport
* @param requestTimeout the optional timeout for the prepared requests (none by default)
*/
def apply(transport: GridFSTransport, requestTimeout: Option[Long] = None): GridFSStorage = new GridFSStorage(transport, requestTimeout)
}
11 changes: 11 additions & 0 deletions gridfs/src/main/scala/package.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright (C) 2018-2019 Zengularity SA (FaberNovel Technologies) <https://www.zengularity.com>
*/

package com.zengularity.benji

/**
* Requires to add the appropriate dependencies in your build
* (see documentation).
*/
package object gridfs {}