-
Notifications
You must be signed in to change notification settings - Fork 5
WIP : begin working on gridfs implementation #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
6aed298
daff698
2711ffb
c6b1e2b
617ecd3
667e18e
954ad72
661d443
da6ee9a
7c29a2e
c656a44
2706535
fa86969
4a6e9d7
b6ee310
6b4aee1
bf33963
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| project/project | ||
| project/target | ||
| src/test/resources/local.conf | ||
| target |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| com.zengularity.benji.gridfs.GridFSScheme |
| 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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Une |
||
| } | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| } | ||
| 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 = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copy paste ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
| } | ||
| 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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| case error => Failure[GridFSTransport](new BenjiUnknownError(s"Fails to create the connection to $uri", Some(error))) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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) | ||
| } |
| 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 {} |
There was a problem hiding this comment.
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