Skip to content
This repository has been archived by the owner on May 27, 2020. It is now read-only.

add readPreferenceTags config #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/src/site/sphinx/First_Steps.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ Configuration parameters
+-----------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
| readPreference | "nearest" | No |
+-----------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
| readPreferenceTags | "taga:1,tagb:2" | No |
+-----------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
| language | "en" | No |
+-----------------------------------------------+--------------------------------------------------------------------------------+-------------------------+
| connectTimeout | "10000" | No |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ object MongodbClientFactory {

val builder = new MongoClientOptions.Builder()
.readPreference(extractValue[String](clientOptions, ProviderReadPreference) match {
case Some(preference) => parseReadPreference(preference)
case Some(preference) => parseReadPreference(preference, extractValue[String](clientOptions, ReadPreferenceTags).getOrElse(DefaultReadPreferenceTags))
case None => DefaultReadPreference
})
.connectTimeout(extractValue[String](clientOptions, ConnectTimeout).map(_.toInt).getOrElse(DefaultConnectTimeout))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
package com.stratio.datasource.mongodb.config

import com.mongodb.casbah.Imports._
import com.mongodb.{MongoClientOptions => JavaMongoClientOptions}
import com.mongodb.{MongoClientOptions => JavaMongoClientOptions, TagSet, Tag}
import scala.collection.JavaConversions._
import com.stratio.datasource.util.Config._

/**
Expand All @@ -31,6 +32,7 @@ object MongodbConfig {
val Collection = "collection"
val SSLOptions = "sslOptions"
val ReadPreference = "readPreference"
val ReadPreferenceTags = "readPreferenceTags"
val ConnectTimeout = "connectTimeout"
val ConnectionsPerHost = "connectionsPerHost"
val MaxWaitTime = "maxWaitTime"
Expand All @@ -51,6 +53,7 @@ object MongodbConfig {
// List of parameters for mongoClientOptions
val ListMongoClientOptions = List(
ReadPreference,
ReadPreferenceTags,
ConnectionsPerHost,
ConnectTimeout,
MaxWaitTime,
Expand All @@ -68,6 +71,7 @@ object MongodbConfig {
// Default MongoDB values
val DefaultMongoClientOptions = new JavaMongoClientOptions.Builder().build()
val DefaultReadPreference = com.mongodb.casbah.ReadPreference.Nearest
val DefaultReadPreferenceTags = ""
val DefaultConnectTimeout = DefaultMongoClientOptions.getConnectTimeout
val DefaultConnectionsPerHost = DefaultMongoClientOptions.getConnectionsPerHost
val DefaultMaxWaitTime = DefaultMongoClientOptions.getMaxWaitTime
Expand Down Expand Up @@ -129,17 +133,26 @@ object MongodbConfig {
}
}

/**
* Parse one key to the associated readPreferenceTags
* @param readPreferenceTags string key for identify the correct object
* @return TagSet object
*/
def parseReadPreferenceTags(readPreferenceTags: String): TagSet = {
return new TagSet( readPreferenceTags.split(",").map(_.split(":")).filter(_.length == 2).map( kv => new Tag(kv(0),kv(1)) ).toList )
}

/**
* Parse one key to the associated readPreference
* @param readPreference string key for identify the correct object
* @return readPreference object
*/
def parseReadPreference(readPreference: String): ReadPreference = {
def parseReadPreference(readPreference: String, readPreferenceTags: String): ReadPreference = {
readPreference.toUpperCase match {
case "PRIMARY" => com.mongodb.casbah.ReadPreference.Primary
case "SECONDARY" => com.mongodb.casbah.ReadPreference.Secondary
case "NEAREST" => com.mongodb.casbah.ReadPreference.Nearest
case "PRIMARYPREFERRED" => com.mongodb.casbah.ReadPreference.primaryPreferred
case "SECONDARY" => com.mongodb.casbah.ReadPreference.secondary(parseReadPreferenceTags(readPreferenceTags))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you import these values so you don't have to repeat its package path 5 times?
Something like import com.mongodb.casbah.ReadPreference.{Primary, Secondary, SecondaryPreferred, Nearest}

case "NEAREST" => com.mongodb.casbah.ReadPreference.nearest(parseReadPreferenceTags(readPreferenceTags))
case "PRIMARYPREFERRED" => com.mongodb.casbah.ReadPreference.primaryPreferred(parseReadPreferenceTags(readPreferenceTags))
case "SECONDARYPREFERRED" => com.mongodb.casbah.ReadPreference.SecondaryPreferred
case _ => com.mongodb.casbah.ReadPreference.Nearest
}
Expand Down