Skip to content

Instantly share code, notes, and snippets.

@rktoomey
Created August 22, 2012 12:39
Show Gist options
  • Save rktoomey/3425185 to your computer and use it in GitHub Desktop.
Save rktoomey/3425185 to your computer and use it in GitHub Desktop.
package mongoverse
case class FooCursor(_grater: Grater[Foo],
underlying: DBCursor,
/* more input params */) extends TransformingCursor[Foo, Bar](_grater, underlying) {
def _newInstance(cursor: DBCursor) = FooCursor(_grater, cursor, /* params */)
def transform(a: Foo) = /* compose Foo instance with inputs to return a Bar instance */
}
package model._
case class Foo(x: String)
case class Bar(x: String, baz: Baz)(implicit config: SomeConfig)
// need a reference to the collection that contains the Foo documents and an instance of _grater for grater[Bar]
def cursor(ids: List[ObjectId], config: SomeConfig): Iterator[Bar] = {
val ref = MongoDBObject("_id" -> MongoDBObject("$in" -> ids))
val keys = MongoDBObject.empty
FooCursor(_grater,
collection.find(ref, keys).asInstanceOf[MongoCursorBase].underlying,
/* more params */)
}
package mongoverse
abstract class TransformingCursor[A <: AnyRef: Manifest, B <: AnyRef: Manifest](_grater: Grater[A],
underlying: DBCursor) extends TransformingCursorBase[A, B] with Iterator[B]
package mongoverse
import com.novus.salat.Grater
import com.mongodb.DBCursor
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.CursorExplanation
import com.novus.commons.util.Logging
trait TransformingCursorBase[A <: AnyRef, B <: AnyRef] extends Logging {
import scalaj.collection.Imports._
def _grater: Grater[A]
def transform(a: A): B
def underlying: DBCursor
def next() = transform(_grater.asObject(underlying.next))
def hasNext = underlying.hasNext
def sort[C <% DBObject](orderBy: C): this.type = {
// The Java code returns a copy of itself (via _this_) so no clone/_newInstance
underlying.sort(orderBy)
this
}
def count = underlying.count
def option_=(option: Int) {
underlying.addOption(option)
}
def option = underlying.getOptions
def resetOptions() {
underlying.resetOptions()
} // use parens because this side-effects
def options = underlying.getOptions
def options_=(opts: Int) {
underlying.setOptions(opts)
}
def hint[C <% DBObject](indexKeys: C): this.type = {
underlying.hint(indexKeys)
this
}
def hint(indexName: String): this.type = {
underlying.hint(indexName)
this
}
def snapshot(): this.type = {
// The Java code returns a copy of itself (via _this_) so no clone/_newInstance
underlying.snapshot() // parens for side-effecting
this
}
def explain = new CursorExplanation(underlying.explain)
def limit(n: Int): this.type = {
underlying.limit(n)
this
}
def skip(n: Int): this.type = {
underlying.skip(n)
this
}
def cursorId = underlying.getCursorId()
def close() {
underlying.close()
} // parens for side-effect
def slaveOk() = underlying.slaveOk() // parens for side-effect
def numGetMores = underlying.numGetMores
def numSeen = underlying.numSeen
def sizes = underlying.getSizes.asScala
def batchSize(n: Int) = {
underlying.batchSize(n)
this
}
def keysWanted = underlying.getKeysWanted
def query = underlying.getQuery
def addSpecial(name: String, o: Any): this.type = {
// The Java code returns a copy of itself (via _this_) so no clone/_newInstance
underlying.addSpecial(name, o.asInstanceOf[AnyRef])
this
}
def $returnKey(bool: Boolean = true): this.type = addSpecial("$returnKey", bool)
def $maxScan[D: Numeric](max: A): this.type = addSpecial("$maxScan", max)
def $query[C <% DBObject](q: C): this.type = addSpecial("$query", q)
def $orderby[C <% DBObject](obj: C): this.type = addSpecial("$orderby", obj)
def $explain(bool: Boolean = true): this.type = addSpecial("$explain", bool)
def $snapshot(bool: Boolean = true): this.type = addSpecial("$snapshot", bool)
def $min[C <% DBObject](obj: C): this.type = addSpecial("$min", obj)
def $max[C <% DBObject](obj: C): this.type = addSpecial("$max", obj)
def $showDiskLoc(bool: Boolean = true): this.type = addSpecial("$showDiskLoc", bool)
def $hint[C <% DBObject](obj: C): this.type = addSpecial("$hint", obj)
def _newInstance(cursor: DBCursor): TransformingCursorBase[A, B]
def copy(): TransformingCursorBase[A, B] = _newInstance(underlying.copy()) // parens for side-effects
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment