-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handled dollar prefixed values for "createOrReplace" Mongo implementation #207
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,10 @@ | |
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.node.ArrayNode; | ||
import com.fasterxml.jackson.databind.node.JsonNodeFactory; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fasterxml.jackson.databind.node.TextNode; | ||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.client.model.ReturnDocument; | ||
import java.io.IOException; | ||
|
@@ -35,6 +37,7 @@ | |
public final class MongoUtils { | ||
public static final String FIELD_SEPARATOR = "."; | ||
public static final String PREFIX = "$"; | ||
private static final String UNICODE_FOR_FIELD_PREFIX = "\\u0024"; | ||
private static final String LITERAL = PREFIX + "literal"; | ||
private static final String UNSUPPORTED_OPERATION = "No MongoDB support available for: '%s'"; | ||
private static final ObjectMapper MAPPER = new ObjectMapper(); | ||
|
@@ -54,15 +57,19 @@ | |
return null; | ||
} | ||
|
||
return key.replace("\\", "\\\\").replace(PREFIX, "\\u0024").replace(FIELD_SEPARATOR, "\\u002e"); | ||
return key.replace("\\", "\\\\") | ||
.replace(PREFIX, UNICODE_FOR_FIELD_PREFIX) | ||
.replace(FIELD_SEPARATOR, "\\u002e"); | ||
} | ||
|
||
public static String decodeKey(final String key) { | ||
if (key == null) { | ||
return null; | ||
} | ||
|
||
return key.replace("\\u002e", FIELD_SEPARATOR).replace("\\u0024", PREFIX).replace("\\\\", "\\"); | ||
return key.replace("\\u002e", FIELD_SEPARATOR) | ||
.replace(UNICODE_FOR_FIELD_PREFIX, PREFIX) | ||
.replace("\\\\", "\\"); | ||
} | ||
|
||
public static String getLastField(final String fieldPath) { | ||
|
@@ -99,6 +106,10 @@ | |
JsonNode src, | ||
UnaryOperator<String> function, | ||
final UnaryOperator<ObjectNode> emptyObjectConverter) { | ||
if (src.isTextual()) { | ||
return new TextNode(function.apply(src.asText())); | ||
Check warning on line 110 in document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java Codecov / codecov/patchdocument-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java#L110
|
||
} | ||
|
||
if (!src.isObject()) { | ||
return src; | ||
} | ||
|
@@ -113,6 +124,17 @@ | |
if (value.isObject()) { | ||
newValue = recursiveClone(value, function, emptyObjectConverter); | ||
} | ||
if (value.isArray()) { | ||
newValue = new ArrayNode(JsonNodeFactory.instance); | ||
Check warning on line 128 in document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java Codecov / codecov/patchdocument-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java#L128
|
||
for (int i = 0; i < value.size(); i++) { | ||
final JsonNode elementValue = | ||
recursiveClone(value.get(i), function, emptyObjectConverter); | ||
((ArrayNode) newValue).add(elementValue); | ||
Check warning on line 132 in document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java Codecov / codecov/patchdocument-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java#L130-L132
|
||
} | ||
} | ||
if (newValue.isTextual()) { | ||
newValue = new TextNode(function.apply(newValue.asText())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this, we have already applied transformation to each value? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry my mistake. I read it as array node. |
||
} | ||
tgt.set(newFieldName, newValue); | ||
} | ||
return tgt.isEmpty() ? emptyObjectConverter.apply(tgt) : tgt; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically in this kind of function you'd have object/array/primitives as sibling code paths, with recursion inside object and array. Here it looks like we handle primitives, else assume it's an object and only treat array differently inside the processing of object fields? Am I reading that right and if so, is there a reason to do it that way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are treating "primitive values" differently.
Also, we are treating "object keys" differently.
Both needs encoding so that a
$
prefix is (treated literally and) not treated as the special field lookup operator.We are still doing similar thing. Nested inside objects and arrays and handling primitives differently. Because, we also want object keys to be encoded, this method turned out this way.
In this PR, I'm just handling the missing case of "primitive values" (especially when they are strings). So, just piggy bagging onto the existing method.