Skip to content

Instantly share code, notes, and snippets.

View bwmcadams's full-sized avatar

Brendan McAdams bwmcadams

View GitHub Profile
@bwmcadams
bwmcadams / findAndRemoveSnippet.scala
Created July 5, 2011 20:45
FindAndRemove Snippet
def dequeue: MessageInvocation = withErrorHandling {
/**
* Retrieves first item in natural order (oldest first, assuming no modification/move)
* Waits 3 seconds for now for a message, else pops back out.
* TODO - How do we handle fetch, but sleep if nothing is in there cleanly?
* TODO - Should we have a specific query in place? Which way do we sort?
* TODO - Error handling version!
*/
val msgInvocation = new DefaultPromise[MessageInvocation](3000)
db.findAndRemove(collName)(Document.empty) { msg: MongoDurableMessage =>
@bwmcadams
bwmcadams / default_args_type_class.scala
Created June 9, 2011 15:20
Defaults + Type Classes
/**
* Attempting to factor this method (Which used to take a hard argument for Qry of BSONDocument
* to use a type class, where the type class object SerializableBSONObject knows how to encode/decode the
* Document represented in Qry
*
* I was ***hoping*** That the Scala compiler would be smart enough to extract the type class type
* in the case of a Default Argument but it seems not.
* The compiletime error is:
*
* could not find implicit value for evidence parameter of type org.bson.SerializableBSONObject[Nothing]
@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")
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 / 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>
@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)
#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 / 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>
@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 / 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