Skip to content

Instantly share code, notes, and snippets.

View bwmcadams's full-sized avatar

Brendan McAdams bwmcadams

View GitHub Profile
@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")
@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 / 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 / ast_parser_combinator.scala
Created October 13, 2011 15:26
AST Parsing Combinator
import scala.util.parsing.combinator._
trait RunParser {
this: RegexParsers =>
type RootType
def root: Parser[RootType]
def run(in: String): ParseResult[RootType] = parseAll(root, in)
}
case class Variable(name: String)
# note that I see *EXACT* same results as the original post with the original code.
# The fix below makes that not happen anymore (use snapshot mode)
# mongo.jar on my machine is the shipped version of 2.7.0, so same exact Java Driver of original test
# ( I was lead on the 2.7.0 release and did the build/release )
b-mac mongodb/mongo-java-driver ‹master*› » scala -cp mongo.jar Repro.scala
Inserting canary...
Inserting test data...
Paging through records...
Spotted the canary!
Updating canary object...