Skip to content

Commit f150a08

Browse files
authored
Merge pull request #36 from picoded/mongodb-support
Mongodb support
2 parents b9cee07 + d86ca27 commit f150a08

3 files changed

Lines changed: 37 additions & 9 deletions

File tree

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public static String getFullConnectionURL(GenericConvertMap<String, Object> conf
5959
int port = config.getInt("port", 27017);
6060
String opts = config.getString("opt_str", "w=majority&retryWrites=true&retryReads=true"
6161
+ "&maxPoolSize=10&compressors=zstd");
62-
// "&readPreference=nearest&readConcernLevel=linearizable"
62+
// "&readPreference=master&readConcernLevel=majority"
63+
// "&readPreference=nearest&readConcernLevel=linearizable"
6364

6465
// Lets do a logging, for missing read concern if its not configured
6566
if (opts.indexOf("readConcernLevel") < 0) {
@@ -74,9 +75,10 @@ public static String getFullConnectionURL(GenericConvertMap<String, Object> conf
7475
// Unless you know what your doing from a performance standpoint, it is strongly recommended to use
7576
// `readConcernLevel=linearizable`
7677
//
77-
LOGGER.warning("MongoDB is configured without readConcernLevel, "
78-
+ "this is alright for a single node, but `readConcernLevel=linearizable`"
79-
+ "is highly recommended for replica clusters to ensure read after write consistency.");
78+
LOGGER.warning("MongoDB is configured without readConcernLevel, "+
79+
"this is alright for a single node, but `readConcernLevel=linearizable`"+
80+
"or `readPreference=master&readConcernLevel=majority`"+
81+
"is highly recommended for replica clusters to ensure read after write consistency.");
8082
}
8183

8284
// In the future we may want to support opt_map

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

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

33
// Java imports
4+
import java.util.regex.Pattern;
45
import java.util.HashMap;
56
import java.util.HashSet;
67
import java.util.List;
@@ -361,13 +362,14 @@ static protected Bson queryObjToBsonFilter(Query inQuery) {
361362
if (type == QueryType.LIKE) {
362363
// Because the LIKE operator does not natively exists,
363364
// we will generates its REGEX equivalent
364-
365365
String val = GenericConvert.toString(inQuery.defaultArgumentValue());
366-
val = val.replaceAll("*", "\\*");
367-
val = val.replaceAll("%", ".*");
368-
val = val.replaceAll("_", ".+");
366+
367+
// val = val.replaceAll("*", "*");
368+
val = val.replaceAll("\\*", "*");
369+
val = val.replaceAll("\\%", ".*");
370+
val = val.replaceAll("\\_", "[.]");
369371

370-
return Filters.regex(inQuery.fieldName(), val);
372+
return Filters.regex(inQuery.fieldName(), "^"+val+"$");
371373
}
372374
}
373375

src/test/java/picoded/dstack/struct/simple/StructSimple_DataObjectMap_test.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,30 @@ public void nestedDataTest() {
984984

985985
}
986986

987+
// Query where bug checks
988+
//-----------------------------------------------
989+
990+
@Test
991+
public void queryWithLikeClause() {
992+
993+
// Lets just rescycle old test for the names
994+
mtObj.newEntry(genNumStrObj(1, "this", 5));
995+
mtObj.newEntry(genNumStrObj(2, "is", 4));
996+
mtObj.newEntry(genNumStrObj(3, "hello", 3));
997+
mtObj.newEntry(genNumStrObj(4, "world", 2));
998+
mtObj.newEntry(genNumStrObj(5, "program", 1));
999+
mtObj.newEntry(genNumStrObj(6, "in", 6));
1000+
mtObj.newEntry(genNumStrObj(7, "this", 7));
1001+
1002+
// Query with like clause
1003+
DataObject[] qRes = mtObj.query("str_val LIKE ?", new String[] { "%his" });
1004+
assertEquals(qRes.length, 2);
1005+
1006+
qRes = mtObj.query("str_val LIKE ?", new String[] { "%ell%" });
1007+
assertEquals(qRes.length, 1);
1008+
1009+
}
1010+
9871011
// Random object, and iteration support
9881012
//-----------------------------------------------
9891013

0 commit comments

Comments
 (0)