@@ -2,24 +2,26 @@ package com.sfxcode.nosql.mongo.database
22
33import java .util .concurrent .TimeUnit
44
5+ import com .mongodb .MongoCompressor
56import com .mongodb .MongoCredential .createCredential
67import com .sfxcode .nosql .mongo .database .MongoConfig ._
78import com .typesafe .config .{ Config , ConfigFactory }
89import org .mongodb .scala .connection ._
910import org .mongodb .scala .{ MongoClientSettings , MongoCredential , ServerAddress }
1011
1112import scala .collection .JavaConverters ._
12-
13- case class MongoConfig (
14- database : String ,
15- host : String = DefaultHost ,
16- port : Int = DefaultPort ,
17- applicationName : String = DefaultApplicationName ,
18- userName : Option [String ] = None ,
19- password : Option [String ] = None ,
20- authDatabase : String = DefaultAuthenticationDatabaseName ,
21- poolOptions : MongoPoolOptions = MongoPoolOptions (),
22- customClientSettings : Option [MongoClientSettings ] = None ) {
13+ import scala .collection .mutable .ArrayBuffer
14+
15+ case class MongoConfig (database : String ,
16+ host : String = DefaultHost ,
17+ port : Int = DefaultPort ,
18+ applicationName : String = DefaultApplicationName ,
19+ userName : Option [String ] = None ,
20+ password : Option [String ] = None ,
21+ authDatabase : String = DefaultAuthenticationDatabaseName ,
22+ poolOptions : MongoPoolOptions = MongoPoolOptions (),
23+ compressors : List [String ] = List (),
24+ customClientSettings : Option [MongoClientSettings ] = None ) {
2325
2426 val clientSettings : MongoClientSettings = {
2527 if (customClientSettings.isDefined) {
@@ -36,12 +38,25 @@ case class MongoConfig(
3638 .maintenanceInitialDelay(poolOptions.maintenanceInitialDelay, TimeUnit .SECONDS )
3739 .build()
3840
41+ val compressorList = new ArrayBuffer [MongoCompressor ]()
42+ compressors.foreach(compression => {
43+ if (ComressionSnappy .equalsIgnoreCase(compression)) {
44+ compressorList.+= (MongoCompressor .createSnappyCompressor())
45+ } else if (ComressionZlib .equalsIgnoreCase(compression)) {
46+ compressorList.+= (MongoCompressor .createZlibCompressor())
47+ } else if (ComressionZstd .equalsIgnoreCase(compression)) {
48+ compressorList.+= (MongoCompressor .createZstdCompressor())
49+ }
50+ })
51+
3952 val builder = MongoClientSettings
4053 .builder()
4154 .applicationName(applicationName)
4255 .applyToConnectionPoolSettings(
43- (b : com.mongodb.connection.ConnectionPoolSettings .Builder ) => b.applySettings(connectionPoolSettings))
56+ (b : com.mongodb.connection.ConnectionPoolSettings .Builder ) => b.applySettings(connectionPoolSettings)
57+ )
4458 .applyToClusterSettings((b : com.mongodb.connection.ClusterSettings .Builder ) => b.applySettings(clusterSettings))
59+ .compressorList(compressorList.asJava)
4560
4661 if (userName.isDefined && password.isDefined) {
4762 val credential : MongoCredential = createCredential(userName.get, authDatabase, password.get.toCharArray)
@@ -55,17 +70,21 @@ case class MongoConfig(
5570}
5671
5772object MongoConfig {
58- val DefaultHost = " 127.0.0.1"
59- val DefaultPort = 27017
73+ val DefaultHost = " 127.0.0.1"
74+ val DefaultPort = 27017
6075 val DefaultAuthenticationDatabaseName = " admin"
61- val DefaultApplicationName = " simple-mongo-app"
76+ val DefaultApplicationName = " simple-mongo-app"
6277
63- val DefaultPoolMaxConnectionIdleTime = 60
64- val DefaultPoolMaxSize = 50
65- val DefaultPoolMinSize = 0
66- val DefaultPoolMaxWaitQueueSize = 500
78+ val DefaultPoolMaxConnectionIdleTime = 60
79+ val DefaultPoolMaxSize = 50
80+ val DefaultPoolMinSize = 0
81+ val DefaultPoolMaxWaitQueueSize = 500
6782 val DefaultPoolMaintenanceInitialDelay = 0
6883
84+ val ComressionSnappy = " snappy"
85+ val ComressionZlib = " zlib"
86+ val ComressionZstd = " zstd"
87+
6988 val DefaultConfigPathPrefix = " mongo"
7089
7190 def fromPath (configPath : String = DefaultConfigPathPrefix ): MongoConfig = {
@@ -89,28 +108,35 @@ object MongoConfig {
89108 default
90109 }
91110
92- def portConfig : Int =
111+ val port : Int =
93112 if (conf.hasPath(" %s.port" .format(configPath))) {
94113 conf.getInt(" %s.port" .format(configPath))
95114 } else {
96115 DefaultPort
97116 }
98117
99- val host = stringConfig(" host" , DefaultHost ).get
100- val port = portConfig
101- val database = stringConfig(" database" ).get
102- val userName = stringConfig(" userName" )
103- val password = stringConfig(" password" )
104- val authDatabase = stringConfig(" authDatabase" , DefaultAuthenticationDatabaseName ).get
118+ val compressors : List [String ] =
119+ if (conf.hasPath(" %s.compressors" .format(configPath))) {
120+ conf.getStringList(" %s.compressors" .format(configPath)).asScala.toList
121+ } else {
122+ List ()
123+ }
124+
125+ val host = stringConfig(" host" , DefaultHost ).get
126+ val database = stringConfig(" database" ).get
127+ val userName = stringConfig(" userName" )
128+ val password = stringConfig(" password" )
129+ val authDatabase = stringConfig(" authDatabase" , DefaultAuthenticationDatabaseName ).get
105130 val applicationName = stringConfig(" applicationName" , DefaultApplicationName ).get
106131
107132 val poolOptions = MongoPoolOptions (
108133 poolOptionsConfig(" maxConnectionIdleTime" , DefaultPoolMaxConnectionIdleTime ),
109134 poolOptionsConfig(" maxSize" , DefaultPoolMaxSize ),
110135 poolOptionsConfig(" minSize" , DefaultPoolMinSize ),
111- poolOptionsConfig(" maintenanceInitialDelay" , DefaultPoolMaintenanceInitialDelay ))
136+ poolOptionsConfig(" maintenanceInitialDelay" , DefaultPoolMaintenanceInitialDelay )
137+ )
112138
113- MongoConfig (database, host, port, applicationName, userName, password, authDatabase, poolOptions)
139+ MongoConfig (database, host, port, applicationName, userName, password, authDatabase, poolOptions, compressors )
114140 }
115141
116142}
0 commit comments