Skip to content
Browse files

Updated ReadPreference in line with Java driver.

You now can choose from several different ReadPreferences:
    Primary - Reads come only from the primary (default)
    Secondary - Reads come from Secondary servers
    SecondaryPreferred - Reads come from Secondary if available, otherwise Primary
    Nearest - Reads come from nearest nodes

Each of these options can also be paired with tagging, using similar
named methods
  • Loading branch information...
1 parent 28b732e commit 6af83fa316222fef09ef5d5bf92ea19e1dfbd1b2 @bwmcadams bwmcadams committed Jul 24, 2012
Showing with 36 additions and 27 deletions.
  1. +36 −27 casbah-core/src/main/scala/ReadPreference.scala
View
63 casbah-core/src/main/scala/ReadPreference.scala
@@ -17,6 +17,7 @@
package com.mongodb.casbah
import scalaj.collection.Imports._
+import com.mongodb.DBObject
/**
* Helper class for creating ReadPreference instances
@@ -30,45 +31,53 @@ object ReadPreference {
/**
* Reads come only through the Primary
*/
- val Primary = com.mongodb.ReadPreference.PRIMARY
+ val Primary = com.mongodb.ReadPreference.primary()
/**
* Reads come from Secondary servers (equiv of old SlaveOK)
*/
- val Secondary = com.mongodb.ReadPreference.SECONDARY
+ val Secondary = com.mongodb.ReadPreference.secondary()
/**
- * Read by particular tags.
- * Note that you should provide an *ordered* Map for tags,
- * as the driver looks in order for matching servers.
- * (DBObjects are automatically order preserving)
- *
- * I.E. {dc: "london", type: "backup", foo: "bar"}
- *
- * Tries first to find a secondary with 'dc: "London"',
- * if it doesn't find one then 'type: "backup"', and finally
- * 'foo: "bar"'.
+ * Reads come from secondary if available, otherwise from primary
*/
- /*
- *def apply(tags: DBObject) =
- * com.mongodb.ReadPreference.withTags(tags)
+ val SecondaryPreferred = com.mongodb.ReadPreference.secondaryPreferred()
+
+ /**
+ * Reads come from nearest node.
*/
+ val Nearest = com.mongodb.ReadPreference.nearest()
/**
- * Read by particular tags.
- * Note that you should provide an *ordered* Map for tags,
- * as the driver looks in order for matching servers.
- * (DBObjects are automatically order preserving)
- *
- * I.E. {dc: "london", type: "backup", foo: "bar"}
*
- * Tries first to find a secondary with 'dc: "London"',
- * if it doesn't find one then 'type: "backup"', and finally
- * 'foo: "bar"'.
+ * @return ReadPreference with reads primary if available
*/
- /*
- *def apply(tags: Map[String, String]) =
- * com.mongodb.ReadPreference.withTags(tags.asJava)
+ def primaryPreferred = com.mongodb.ReadPreference.primaryPreferred()
+
+ /**
+ * @return ReadPreference which reads primary if available, otherwise a secondary respective of tags.
*/
+ def primaryPreferred(firstTagSet: DBObject, remainingTagSets: DBObject*) =
+ com.mongodb.ReadPreference.primaryPreferred(firstTagSet, remainingTagSets: _*)
+
+ /**
+ * @return ReadPreference which returns secondary respective of tags
+ */
+ def secondary(firstTagSet: DBObject, remainingTagSets: DBObject*) =
+ com.mongodb.ReadPreference.secondary(firstTagSet, remainingTagSets: _*)
+
+ /**
+ * @return ReadPreference which reads secondary if available respective of tags,
+ * otherwise from primary irresepective of tags
+ */
+ def secondaryPreferred(firstTagSet: DBObject, remainingTagSets: DBObject*) =
+ com.mongodb.ReadPreference.secondaryPreferred(firstTagSet, remainingTagSets: _*)
+
+ /**
+ * @return ReadPreference which reads nearest node respective of tags
+ */
+ def nearest(firstTagSet: DBObject, remainingTagSets: DBObject*) =
+ com.mongodb.ReadPreference.nearest(firstTagSet, remainingTagSets: _*)
+
}

0 comments on commit 6af83fa

Please sign in to comment.
Something went wrong with that request. Please try again.