Skip to content

Commit ff0e03f

Browse files
committed
Handling of option map config
1 parent f7157bb commit ff0e03f

1 file changed

Lines changed: 46 additions & 7 deletions

File tree

src/main/java/picoded/dstack/mongodb/MongoDBStack.java

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package picoded.dstack.mongodb;
22

3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
36
import java.util.Map;
47
import java.util.logging.Level;
58
import java.util.logging.Logger;
69

10+
import picoded.core.conv.GenericConvert;
711
import picoded.core.struct.GenericConvertMap;
812
import picoded.dstack.core.*;
913

@@ -31,6 +35,41 @@ public class MongoDBStack extends CoreStack {
3135
protected MongoClient client_conn = null;
3236
protected MongoDatabase db_conn = null;
3337

38+
//-------------------------------------------------------------------------
39+
// Connector utilities
40+
//-------------------------------------------------------------------------
41+
42+
/// Default settings option JSON
43+
protected static String defaultOptJson = "{"+ //
44+
" \"w\":\"majority\","+ //
45+
" \"retryWrites\":\"true\","+ //
46+
" \"retryReads\":\"true\","+ //
47+
" \"maxPoolSize\":25,"+ //
48+
" \"compressors\":\"zstd\""+ //
49+
"}";
50+
// "&readPreference=master&readConcernLevel=majority"
51+
// "&readPreference=nearest&readConcernLevel=linearizable"
52+
53+
protected static String mapToOptStr(Map<String,Object> map) {
54+
// Get the sorted list of keys
55+
List<String> keys = (new ArrayList<>(map.keySet()));
56+
Collections.sort(keys);
57+
58+
// The ret stringbuilder
59+
StringBuilder ret = new StringBuilder();
60+
61+
// Lets loop the keys
62+
for(String key : keys) {
63+
if( ret.length() > 0 ) {
64+
ret.append("&");
65+
}
66+
ret.append(key+"="+GenericConvert.toString(map.get(key)));
67+
}
68+
69+
// And return the built str
70+
return ret.toString();
71+
}
72+
3473
//-------------------------------------------------------------------------
3574
// Database connection constructor
3675
//-------------------------------------------------------------------------
@@ -57,13 +96,13 @@ public static String getFullConnectionURL(GenericConvertMap<String, Object> conf
5796
String pass = config.getString("pass", null);
5897
String host = config.getString("host", "localhost");
5998
int port = config.getInt("port", 27017);
60-
String opts = config.getString("opt_str", "w=majority&retryWrites=true&retryReads=true"
61-
+ "&maxPoolSize=10&compressors=zstd");
62-
// "&readPreference=master&readConcernLevel=majority"
63-
// "&readPreference=nearest&readConcernLevel=linearizable"
99+
100+
// Hanlding of option string
101+
GenericConvertMap<String,Object> optMap = config.getGenericConvertStringMap("opt", defaultOptJson);
102+
String optStr = config.getString("opt_str", mapToOptStr(optMap));
64103

65104
// Lets do a logging, for missing read concern if its not configured
66-
if (opts.indexOf("readConcernLevel") < 0) {
105+
if (optStr.indexOf("readConcernLevel") < 0) {
67106
//
68107
// readConcernLevel is a complicated topic, do consider reading up
69108
// https://jepsen.io/analyses/mongodb-4.2.6
@@ -93,9 +132,9 @@ public static String getFullConnectionURL(GenericConvertMap<String, Object> conf
93132
// Return the full URL depending on the settings
94133
if (protocol.equals("mongodb+srv")) {
95134
// mongodb+srv does not support the port protocol
96-
return protocol + "://" + authStr + host + "/" + dbname + "?" + opts;
135+
return protocol + "://" + authStr + host + "/" + dbname + "?" + optStr;
97136
}
98-
return protocol + "://" + authStr + host + ":" + port + "/" + dbname + "?" + opts;
137+
return protocol + "://" + authStr + host + ":" + port + "/" + dbname + "?" + optStr;
99138
}
100139

101140
/**

0 commit comments

Comments
 (0)