PageRenderTime 1217ms CodeModel.GetById 32ms RepoModel.GetById 7ms app.codeStats 0ms

/casbah-gridfs/src/main/scala/JodaGridFS.scala

http://github.com/mongodb/casbah
Scala | 253 lines | 111 code | 42 blank | 100 comment | 0 complexity | e4b56791ad34a3cfe441bfdb89627039 MD5 | raw file
Possible License(s): Apache-2.0
  1. /**
  2. * Copyright (c) 2010 MongoDB, Inc. <http://mongodb.com>
  3. * Copyright (c) 2009, 2010 Novus Partners, Inc. <http://novus.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. * For questions and comments about this product, please see the project page at:
  18. *
  19. * http://github.com/mongodb/casbah
  20. *
  21. */
  22. package com.mongodb.casbah
  23. package gridfs
  24. import java.io.{ File, InputStream }
  25. import scala.beans.BeanInfo
  26. import com.mongodb.gridfs.{
  27. GridFS => MongoGridFS,
  28. GridFSDBFile => MongoGridFSDBFile,
  29. GridFSFile => MongoGridFSFile,
  30. GridFSInputFile => MongoGridFSInputFile
  31. }
  32. import com.mongodb.casbah.Imports._
  33. import com.mongodb.casbah.gridfs.Imports._
  34. import com.mongodb.casbah.commons.Logging
  35. import com.github.nscala_time.time.Imports.{ DateTime, LocalDateTime }
  36. /**
  37. * Companion object for GridFS.
  38. * Entry point for creation of GridFS Instances.
  39. *
  40. * @since 1.0
  41. */
  42. object JodaGridFS extends Logging {
  43. def apply(db: MongoDB): JodaGridFS = {
  44. log.info("Creating a new JodaGridFS Entry against DB '%s', using default bucket ('%s')", db.name, MongoGridFS.DEFAULT_BUCKET)
  45. new JodaGridFS(new MongoGridFS(db.underlying))
  46. }
  47. def apply(db: MongoDB, bucket: String): JodaGridFS = {
  48. log.info("Creating a new JodaGridFS Entry against DB '%s', using specific bucket ('%s')", db.name, bucket)
  49. new JodaGridFS(new MongoGridFS(db.underlying, bucket))
  50. }
  51. }
  52. class JodaGridFS protected[gridfs] (val underlying: MongoGridFS) extends GenericGridFS with Iterable[JodaGridFSDBFile] {
  53. type FileWriteOp = JodaGridFSInputFile => Unit
  54. def iterator: Iterator[JodaGridFSDBFile] = new Iterator[JodaGridFSDBFile] {
  55. val fileSet: MongoCursor = files
  56. def count(): Int = fileSet.count
  57. override def length: Int = fileSet.length
  58. def numSeen(): Int = fileSet.numSeen
  59. def curr: JodaGridFSDBFile = next()
  60. def explain(): CursorExplanation = fileSet.explain
  61. @SuppressWarnings(Array("deprecation"))
  62. def next(): JodaGridFSDBFile = {
  63. val gridfsfile = fileSet.next().asInstanceOf[GridFSDBFileSafeJoda]
  64. gridfsfile.setGridFS(underlying)
  65. new JodaGridFSDBFile(gridfsfile)
  66. }
  67. def hasNext: Boolean = fileSet.hasNext
  68. }
  69. /**
  70. * Create a new GridFS File from a scala.io.Source
  71. *
  72. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  73. * as a parameter.
  74. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  75. * If you don't want automatic saving/loaning please see the createFile method instead.
  76. * @see createFile
  77. * @return The ID of the created File (Option[AnyRef])
  78. */
  79. def apply(data: scala.io.Source)(op: FileWriteOp): Nothing = withNewFile(data)(op)
  80. /**
  81. * Create a new GridFS File from a Byte Array
  82. *
  83. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  84. * as a parameter.
  85. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  86. * If you don't want automatic saving/loaning please see the createFile method instead.
  87. * @see createFile
  88. * @return The ID of the created File (Option[AnyRef])
  89. */
  90. def apply(data: Array[Byte])(op: FileWriteOp): Option[AnyRef] = withNewFile(data)(op)
  91. /**
  92. * Create a new GridFS File from a java.io.File
  93. *
  94. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  95. * as a parameter.
  96. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  97. * If you don't want automatic saving/loaning please see the createFile method instead.
  98. * @see createFile
  99. * @return The ID of the created File (Option[AnyRef])
  100. */
  101. def apply(f: File)(op: FileWriteOp): Option[AnyRef] = withNewFile(f)(op)
  102. /**
  103. * Create a new GridFS File from a java.io.InputStream
  104. *
  105. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  106. * as a parameter.
  107. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  108. * If you don't want automatic saving/loaning please see the createFile method instead.
  109. * @see createFile
  110. * @return The ID of the created File (Option[AnyRef])
  111. */
  112. def apply(in: InputStream)(op: FileWriteOp): Option[AnyRef] = withNewFile(in)(op)
  113. /**
  114. * Create a new GridFS File from a java.io.InputStream and a specific filename
  115. *
  116. * Uses a loan pattern, so you need to pass a curried function which expects a GridFSInputFile
  117. * as a parameter.
  118. * It AUTOMATICALLY saves the GridFS file at it's end, so throw an exception if you want to fail.
  119. * If you don't want automatic saving/loaning please see the createFile method instead.
  120. * @see createFile
  121. * @return The ID of the created File (Option[AnyRef])
  122. */
  123. def apply(in: InputStream, filename: String)(op: FileWriteOp): Option[AnyRef] = withNewFile(in, filename)(op)
  124. /**
  125. * createFile
  126. *
  127. * Creates a new file in GridFS
  128. *
  129. * TODO - Should the curried versions give the option to not automatically save?
  130. */
  131. def createFile(data: scala.io.Source): JodaGridFSInputFile = throw new UnsupportedOperationException("Currently no support for scala.io.Source")
  132. def createFile(data: Array[Byte]): JodaGridFSInputFile = underlying.createFile(data)
  133. def createFile(f: File): JodaGridFSInputFile = underlying.createFile(f)
  134. def createFile(in: InputStream): JodaGridFSInputFile = underlying.createFile(in)
  135. def createFile(in: InputStream, filename: String): JodaGridFSInputFile = underlying.createFile(in, filename)
  136. def withNewFile(data: scala.io.Source)(op: FileWriteOp): Nothing = throw new UnsupportedOperationException("Currently no support for scala.io.Source")
  137. /**
  138. * Loan pattern style file creation.
  139. * @return The ID of the created File (Option[AnyRef])
  140. */
  141. def withNewFile(data: Array[Byte])(op: FileWriteOp): Option[AnyRef] =
  142. loan(createFile(data)) {
  143. fh =>
  144. op(fh)
  145. fh.save()
  146. fh.validate()
  147. Option(fh.id)
  148. }
  149. /**
  150. * Loan pattern style file creation.
  151. * @return The ID of the created File (Option[AnyRef])
  152. */
  153. def withNewFile(f: File)(op: FileWriteOp): Option[AnyRef] =
  154. loan(createFile(f)) {
  155. fh =>
  156. op(fh)
  157. fh.save()
  158. fh.validate()
  159. Option(fh.id)
  160. }
  161. /**
  162. * Loan pattern style file creation.
  163. * @return The ID of the created File (Option[AnyRef])
  164. */
  165. def withNewFile(in: InputStream)(op: FileWriteOp): Option[AnyRef] =
  166. loan(createFile(in)) {
  167. fh =>
  168. op(fh)
  169. fh.save()
  170. fh.validate()
  171. Option(fh.id)
  172. }
  173. /**
  174. * Loan pattern style file creation.
  175. * @return The ID of the created File (Option[AnyRef])
  176. */
  177. def withNewFile(in: InputStream, filename: String)(op: FileWriteOp): Option[AnyRef] =
  178. loan(createFile(in, filename)) {
  179. fh =>
  180. op(fh)
  181. fh.save()
  182. fh.validate()
  183. Option(fh.id)
  184. }
  185. def findOne[A <% DBObject](query: A): Option[JodaGridFSDBFile] = {
  186. filesCollection.findOne(query) match {
  187. case None => None
  188. case x => {
  189. val gridfsFile = x.get
  190. gridfsFile.setGridFS(underlying)
  191. Some(new JodaGridFSDBFile(gridfsFile))
  192. }
  193. }
  194. }
  195. def findOne(id: ObjectId): Option[JodaGridFSDBFile] = findOne(MongoDBObject("_id" -> id))
  196. def findOne(filename: String): Option[JodaGridFSDBFile] = findOne(MongoDBObject("filename" -> filename))
  197. }
  198. @BeanInfo
  199. class JodaGridFSDBFile(_underlying: MongoGridFSDBFile) extends GenericGridFSDBFile(_underlying) with ConvertToDateTime
  200. @BeanInfo
  201. class JodaGridFSFile(_underlying: MongoGridFSFile) extends GenericGridFSFile(_underlying) with ConvertToDateTime
  202. @BeanInfo
  203. class JodaGridFSInputFile(_underlying: MongoGridFSInputFile) extends GenericGridFSInputFile(_underlying) with ConvertToDateTime
  204. trait ConvertToDateTime {
  205. type DateType = DateTime
  206. def convertDate(in: AnyRef): DateType = in match {
  207. case d: java.util.Date => new DateTime(d)
  208. case j: DateTime => j
  209. case l: LocalDateTime => l.toDateTime
  210. }
  211. }