Skip to content

Instantly share code, notes, and snippets.

View bwmcadams's full-sized avatar

Brendan McAdams bwmcadams

View GitHub Profile
@bwmcadams
bwmcadams / Oops.scala
Created December 22, 2010 19:39
When a one line change produces an error like this you may have a MINOR issue.
[error] /Users/bwmcadams/code/mongodb/casbah/casbah-query/src/test/scala/DSLCoreOperatorsSpec.scala:1271: type mismatch;
[error] found : ((some other)$anon(in value <local DSLCoreOperatorsSpec>) forSome { type (some other)$anon(in value <local DSLCoreOperatorsSpec>) <: java.lang.Object with com.mongodb.casbah.commons.MongoDBObject{def $maxDistance[T](radius: T)(implicit evidence$15: Numeric[T]): (some other)$anon(in value <local DSLCoreOperatorsSpec>)} })
[error] required: $anon(in value <local DSLCoreOperatorsSpec>) where type $anon(in value <local DSLCoreOperatorsSpec>) <: java.lang.Object with com.mongodb.casbah.commons.MongoDBObject{def $maxDistance[T](radius: T)(implicit evidence$15: Numeric[T]): $anon(in value <local DSLCoreOperatorsSpec>)}
[error] near must notBeNull
[error] ^
((some other)$anon(in value <local DSLCoreOperatorsSpec>) forSome { type (some other)$anon(in value <local DSLCoreOperatorsSpec>) <: java.lang.Object with com.mongodb.casbah.commons.MongoDBObject{def $maxDista
@bwmcadams
bwmcadams / Casbah_Field_Limits.scala
Created January 3, 2011 20:06
Select a limited set of fields with Casbah
import com.mongodb.casbah.Imports._
val conn = MongoConnection()
val db = conn("test")
val coll = db("foo")
for (x <- coll.find(MongoDBObject("_id" -> new ObjectId("4cd2d357c575954934c5a975")),
MongoDBObject("x" -> 1))) println(x)
@bwmcadams
bwmcadams / PimpMyMap.scala
Created January 18, 2011 18:04
Adding some Casbah object methods to Scala Maps
implicit def pimpMyMap(underlying: Map[_,_]) = new {
/** Lazy utility method to allow typing without conflicting with Map's required get() method and causing ambiguity */
def getAs[A <: Any : Manifest](key: Any): Option[A] = {
require(manifest[A] != manifest[scala.Nothing],
"Type inference failed; getAs[A]() requires an explicit type argument " +
"(e.g. mapObject.getAs[<ReturnType>](somegetAKey) ) to function correctly.")
underlying.get(key) match {
case null => None
@bwmcadams
bwmcadams / CallableMap.scala
Created January 26, 2011 21:34
Quick and dirty version of Map in Scala that can be 'called' like Python dicts where unknown methods are pulled out as Map keys. Uses the 2.9 dynamic trait.
class CallableMap extends scala.collection.mutable.HashMap[String, Any] with Dynamic {
def invokeDynamic(name: String)(args: Any*) = {
println("Invoke Dynamic: (name = %s)(args: %s)".format(name, args))
get(name)
}
def typed[T]: T = {
asInstanceOf[T]
}
@bwmcadams
bwmcadams / org.mongodb.mongod.plist
Created January 27, 2011 07:15
MongoDB plist for Mac, with durability enabled and keepalive set to false
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.mongodb.mongod</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mongodb/bin/mongod</string>
<string>run</string>
#ruby
[1,2,3,4].select{ |x| x.even? }
#python
[x for x in [1,2,3,4] if not x%2]
#or, more norvingly
filter(lambda x: not x%2, [1,2,3,4])
#clojure
(filter #(even? % ) [1 2 3 4])
@bwmcadams
bwmcadams / tail_profile.py
Created February 26, 2011 14:32
Tail the Slow Query Log on MongoDB
#!/usr/bin/python
# Connects to localhost, 27017 by default
import sys
import pymongo
import time
if len(sys.argv) < 2:
print >> sys.stderr, "Usage: ./tail_profile.py <dbName> [hostname] [port]"
sys.exit(-1)
@bwmcadams
bwmcadams / org.mongodb.mongod.plist
Created March 2, 2011 23:59
Suggested Plist for Mac MongoDB 1.8+, enables durability and clean shutdown
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>org.mongodb.mongod</string>
<key>ProgramArguments</key>
<array>
<string>/usr/local/mongodb/bin/mongod</string>
<string>run</string>
scala> val x: PartialFunction[String, Unit] = { case "foo" => println("Foo") }
x: PartialFunction[String,Unit] = <function1>
scala> x("foo")
Foo
scala> x("bar")
scala.MatchError: bar
scala> val y: PartialFunction[String, Unit] = { case "bar" => println("Bar") }
@bwmcadams
bwmcadams / dynamicCasbah.scala
Created May 17, 2011 20:22
Dynamic Casbah Example
import com.mongodb.casbah.Imports._
import com.mongodb.casbah.dynamic._
val d = DynamicDBObject("_id" -> new ObjectId, "name" -> "Brendan McAdams",
"address" -> MongoDBObject("street" -> "134 5th Ave Fl 3",
"city" -> "New York",
"state" -> "NY",
"zip" -> 10011),
"email" -> "brendan@10gen.com")