This document summarizes a presentation about Rogue, a Scala DSL for MongoDB. Some key points:
- Rogue allows type-safe querying of MongoDB with features like filters, pagination, and awareness of indexes.
- It uses phantom types to prevent issues like multiple selects or limits.
- Queries can be logged and validated.
- Future goals include iteratees for cursors, compile-time checking, and generating JavaScript for map-reduce.
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
1. rogue:
a scala dsl for mongodb
ScalaDays 2011 - 6/2/2011
Jason Liszka (@jliszka)
Jorge Ortiz (@jorgeortiz85)
2. what is foursquare?
location-based social network - “check-in” to bars,
restaurants, museums, parks, etc
friend-finder (where are my friends right now?)
virtual game (badges, points, mayorships)
city guide (local, personalized recommendations)
location diary + stats engine (where was I a year ago?)
specials (get rewards at your favorite restaurant)
4. foursquare: the tech
Nginx, HAProxy
Scala, Lift
MongoDB, PostgreSQL (legacy)
(Kestrel, Munin, Ganglia, Python, Memcache, ...)
All on EC2
5. what is mongodb?
fast, schema-less document store
indexes & rich queries on any field
sharding, auto-balancing
replication
geo-indexes
6. mongodb: our numbers
8 clusters
some sharded, some not
some master/slave, some replica set
~40 machines (68.4GB, m2.4xl on EC2)
2.3 billion records
~15k QPS
10. mongodb: query example
val query =
(BasicDBOBjectBuilder
.start
.push(“mayorid”)
.add(“$lte”, 100)
.pop
.add(“veneuname”, “Starbucks”)
...
.get)
11. rogue: a scala dsl for mongo
type-safe
all mongo query features
logging & validation hooks
pagination
index-aware
cursors
http://github.com/foursquare/rogue
13. rogue: code example
val vs: List[Venue] =
(Venue where (_.mayorid <= 100)
and (_.venuename eqs “Starbucks”)
and (_.tags contains “wifi”)
and (_.latlng near
(39.0, -74.0, Degrees(0.2))
orderDesc (_._id)
fetch (5))
21. rogue: more limit problems
(Venue where (_.venuename eqs “Starbucks”)
orderAsc (_.total_checkins)
limit (5)
bulkDelete_!!)
// How many Venues got deleted?
22. rogue: phantom types
Sel =:= Selected, Unselected
Ord =:= Ordered, Unordered
Lim =:= Limited, Unlimited
Sk =:= Skipped, Unskipped
http://james-iry.blogspot.com/2010/10/phantom-types-in-haskell-and-scala.html
23. rogue: phantom types
abstract sealed class Ordered
abstract sealed class Unordered
abstract sealed class Selected
abstract sealed class Unselected
abstract sealed class Limited
abstract sealed class Unlimited
abstract sealed class Skipped
abstract sealed class Unskipped
24. rogue: initializing phantoms
class BaseQuery[M <: MongoRecord[M], R,
Ord, Sel, Lim, Sk](...) {
...
}
def dfltQuery[M <: MongoRecord[M]](rec: M) =
new BaseQuery[M, M,
Unordered,
Unselected,
Unlimited,
Unskipped](...)