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

fix reading properties without changing old configuration #119

Merged
merged 7 commits into from
Apr 27, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class MongodbPartitioner(config: Config) extends Partitioner[MongodbPartition] {
@transient private val ssloptions: Option[MongodbSSLOptions] =
config.get[MongodbSSLOptions](MongodbConfig.SSLOptions)

private val clientOptions = config.properties //config.properties.filterKeys(_.contains(MongodbConfig.ListMongoClientOptions)) // TODO review this Map. Can't filter keys
private val clientOptions = {
val lowerCaseOptions = MongodbConfig.ListMongoClientOptions.map(_.toLowerCase)
config.properties.filter(kv => kv._1.contains(lowerCaseOptions))
Copy link
Contributor

@pfcoperez pfcoperez Apr 27, 2016

Choose a reason for hiding this comment

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

The logic here is wrong: You are filtering those config properties which does not contain the whole option list.

I'd try something like:

lowerCaseOptions.flatMap(key => config.properties.get(key).map(key -> _)) toMap

Or, cleaner:

private val clientOptions = {
    val lowerCaseOptions = MongodbConfig.ListMongoClientOptions.map(_.toLowerCase).toSet
    config.properties.filterKeys(lowerCaseOptions contains _)
  }

Copy link
Contributor

@pfcoperez pfcoperez Apr 27, 2016

Choose a reason for hiding this comment

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

Looking at your commits I see you had already noticed that filterKeys isn't compatible with a map to be distributed to Spark workers as its return instance is not serializable. In any case, you should apply the logic I proposed:

 private val clientOptions = {
   val lowerCaseOptions = MongodbConfig.ListMongoClientOptions.map(_.toLowerCase).toSet
   config.properties.filter { case (k, _) => lowerCaseOptions contains k }
 }

}

private val databaseName: String = config(MongodbConfig.Database)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ abstract class ConfigBuilder[Builder<:ConfigBuilder[Builder] ](
*/
def build(): Config = new Config {

val properties = builder.properties
val properties = builder.properties.map(kv => kv.copy(_1 = kv._1.toLowerCase))
Copy link
Contributor

Choose a reason for hiding this comment

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

This approach is more readable:

val properties = builder.properties.map { case (k, v) => k.toLowerCase -> v }


require(
requiredProperties.forall(properties.isDefinedAt),
Expand Down Expand Up @@ -109,7 +109,7 @@ trait Config extends Serializable {
* @return the value associated with `key` if it exists,
* otherwise the result of the `default` computation.
*/
def getOrElse[T](key: Property, default: => T): T = properties.get(key) match {
def getOrElse[T](key: Property, default: => T): T = properties.get(key.toLowerCase) match {
case Some(v) => v.asInstanceOf[T]
case None => default
}
Expand All @@ -121,7 +121,7 @@ trait Config extends Serializable {
* @return An optional value of expected type
*/
def get[T: ClassTag](property: Property): Option[T] =
properties.get(property).map(_.asInstanceOf[T])
properties.get(property.toLowerCase).map(_.asInstanceOf[T])

/**
* Gets specified property from current configuration object.
Expand Down