/** * Get a reference to the hidden path's actual field. * * This does not guarantee the sub-path exists. * * @param path * @return */ public static Path getFieldForHidden(Path hiddenPath) { return hiddenPath.prefix(-2).mutableCopy().push(hiddenPath.getLast()); }
/** * Get a reference to the path's hidden sub-field. * * This does not guarantee the sub-path exists. * * @param path * @return */ public static Path getHiddenForField(Path path) { if (path.getLast().equals(Path.ANY)) { return path.prefix(-2).mutableCopy().push(HIDDEN_SUB_PATH).push(path.suffix(2)); } return path.prefix(-1).mutableCopy().push(HIDDEN_SUB_PATH).push(path.getLast()); }
/** * Returns a map where the key is the array field name, and value is a * List<Path> listing the identity fields for that array */ public Map<Path, List<Path>> getArrayIdentities() { FieldCursor cursor = getFieldCursor(); Map<Path, List<Path>> idMap = new HashMap<>(); while (cursor.next()) { FieldTreeNode fn = cursor.getCurrentNode(); if (fn instanceof SimpleField) { for (FieldConstraint fc : ((SimpleField) fn).getConstraints()) { if (fc instanceof ArrayElementIdConstraint) { Path fieldName = cursor.getCurrentPath(); int lastAny = findLastAnyIndex(fieldName); if (lastAny != -1) { Path arrayName = fieldName.prefix(lastAny); Path idName = fieldName.suffix(-(lastAny + 1)); List<Path> ids = idMap.get(arrayName); if (ids == null) { idMap.put(arrayName, ids = new ArrayList<>()); } ids.add(idName); } } } } } return idMap; }
/** * Returns if the field should be included based on the recursive pattern. * * @param field The field name * @param pattern The projection pattern * @param inclusion flag to return if the field matches the given pattern * @return Boolean value of <code>inclusion</code> if field is in subtree of * <code>pattern</code> else null */ public static Boolean impliedInclusion(Path field, Path pattern, boolean inclusion) { if (field.numSegments() > pattern.numSegments() && // If we're checking a field deeper than the pattern // And if we're checking a field under the subtree of the pattern field.prefix(pattern.numSegments()).matches(pattern)) { return inclusion ? Boolean.TRUE : Boolean.FALSE; } else { return null; } }
/** * Check if this path is a matching descendant of the pattern, that is: - * path matches pattern, or - a prefix of the path matches the pattern */ public boolean matchingDescendant(Path pattern) { int n = pattern.numSegments(); if (n < numSegments()) { return prefix(n).matches(pattern); } else if (n == numSegments()) { return matches(pattern); } return false; }
public ChildSlot(CompositeMetadata root, ResolvedReferenceField reference) { Path erfn = root.getEntityRelativeFieldName(reference); if (erfn.numSegments() == 1) { localContainerName = Path.EMPTY; referenceFieldName = erfn.head(0); slotFieldName = new Path(referenceFieldName); } else { localContainerName = erfn.prefix(-1); referenceFieldName = erfn.tail(0); slotFieldName = erfn; } this.reference = reference; }
/** * Check if this path is a matching descendant of the pattern, that is: - * path matches pattern, or - a prefix of the path matches the pattern */ public boolean matchingDescendant(Path pattern) { int n = pattern.numSegments(); if (n < numSegments()) { return prefix(n).matches(pattern); } else if (n == numSegments()) { return matches(pattern); } return false; }
/** * Returns if the field should be included based on the recursive pattern. * * @param field The field name * @param pattern The projection pattern * @param inclusion flag to return if the field matches the given pattern * @return Boolean value of <code>inclusion</code> if field is in subtree of * <code>pattern</code> else null */ public static Boolean impliedInclusion(Path field, Path pattern, boolean inclusion) { if (field.numSegments() > pattern.numSegments() && // If we're checking a field deeper than the pattern // And if we're checking a field under the subtree of the pattern field.prefix(pattern.numSegments()).matches(pattern)) { return inclusion ? Boolean.TRUE : Boolean.FALSE; } else { return null; } }
public ChildSlot(CompositeMetadata root, ResolvedReferenceField reference) { Path erfn = root.getEntityRelativeFieldName(reference); if (erfn.numSegments() == 1) { localContainerName = Path.EMPTY; referenceFieldName = erfn.head(0); slotFieldName = new Path(referenceFieldName); } else { localContainerName = erfn.prefix(-1); referenceFieldName = erfn.tail(0); slotFieldName = erfn; } this.reference = reference; }
/** * Check if this path is a matching descendant of the pattern, that is: - * path matches pattern, or - a prefix of the path matches the pattern */ public boolean matchingDescendant(Path pattern) { int n = pattern.numSegments(); if (n < numSegments()) { return prefix(n).matches(pattern); } else if (n == numSegments()) { return matches(pattern); } return false; }
/** * Modifies an existing node value * * @param root The root node * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public static JsonNode modify(JsonNode root, Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(root, parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Modifies an existing node value * * @param root The root node * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public static JsonNode modify(JsonNode root, Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(root, parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
Path parent = fieldMetadataPath.prefix(-1); String fieldName = fieldMetadataPath.tail(0); KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(parent);
/** * Modifies an existing node value * * @param p Path to modify * @param newValue new value to set. If null, path is removed from the doc. * @param createPath If true, creates all intermediate nodes if they don't * exist * * @return Old value */ public JsonNode modify(Path p, JsonNode newValue, boolean createPath) { int n = p.numSegments(); if (n == 0) { throw new IllegalArgumentException(UtilConstants.ERR_CANT_SET_EMPTY_PATH_VALUE); } Path parent = p.prefix(-1); // Parent must be a container node JsonNode parentNode = getParentNode(parent, createPath, p); JsonNode oldValue; String last = p.getLast(); if (parentNode instanceof ObjectNode) { oldValue = modifyObjectNode(parentNode, newValue, last, parent); } else { oldValue = modifyArrayNode((ArrayNode) parentNode, newValue, last, p); } return oldValue; }
/** * Check if this path is a matching prefix of the pattern, that is: - path * matches pattern, or - path matches a prefix of the pattern */ public boolean matchingPrefix(Path pattern) { int n = pattern.numSegments(); if (n > numSegments()) { return matches(pattern.prefix(numSegments())); } else if (n == numSegments()) { return matches(pattern); } return false; }
/** * Check if this path is a matching prefix of the pattern, that is: - path * matches pattern, or - path matches a prefix of the pattern */ public boolean matchingPrefix(Path pattern) { int n = pattern.numSegments(); if (n > numSegments()) { return matches(pattern.prefix(numSegments())); } else if (n == numSegments()) { return matches(pattern); } return false; }
/** * Check if this path is a matching prefix of the pattern, that is: - path * matches pattern, or - path matches a prefix of the pattern */ public boolean matchingPrefix(Path pattern) { int n = pattern.numSegments(); if (n > numSegments()) { return matches(pattern.prefix(numSegments())); } else if (n == numSegments()) { return matches(pattern); } return false; }
@Override public Projection.Inclusion project(Path p, QueryEvaluationContext ctx) { if (p.matchingPrefix(field)) { // If this is true, we're checking an ancestor of the // projection field, or the projection field itself, but // not a field that is a descendant of the projection // field if (include) { return Projection.Inclusion.explicit_inclusion; // Inclusion implies, because if we're going to // include a descendant of this field, this field // should also be included } else if (p.matches(field)) { return Projection.Inclusion.explicit_exclusion; // If this field is exclusively excluded, exclude it } // Otherwise, this projection does not tell anything about this particular field. } else if (recursive && // If this is a recursive projection p.numSegments() > field.numSegments() && // If we're checking a field deeper than our field p.prefix(field.numSegments()).matches(field) // And if we're checking a field under the subtree of our field ) { // This is an implied inclusion or exclusion, because the // projection is for an ancestor of this field. return include ? Projection.Inclusion.implicit_inclusion : Projection.Inclusion.implicit_exclusion; } return Projection.Inclusion.undecided; } }
/** * Remove the context prefix from the field. If the field is under an array elemMatch, and if the field * shares the same prefix as the array field, remove that prefix. This is only useful if the field * and the array are in the same entity */ private Path removeContext(CompositeMetadata fieldEntity,Path fieldName) { if(contextField!=null&&fieldEntity==contextField.getFieldEntity()) { Path prefix=new Path(contextField.getEntityRelativeFieldName(),Path.ANYPATH); if(fieldName.numSegments()>=prefix.numSegments()) { Path fieldPrefix=fieldName.prefix(prefix.numSegments()); // Also include the * if(fieldPrefix.equals(prefix)) { // Remove fieldPrefix Path relativeFieldName=fieldName.suffix(-prefix.numSegments()); if(relativeFieldName.isEmpty()) relativeFieldName=new Path(Path.THIS); return relativeFieldName; } } } return fieldName; }
/** * Remove the context prefix from the field. If the field is under an array elemMatch, and if the field * shares the same prefix as the array field, remove that prefix. This is only useful if the field * and the array are in the same entity */ private Path removeContext(CompositeMetadata fieldEntity,Path fieldName) { if(contextField!=null&&fieldEntity==contextField.getFieldEntity()) { Path prefix=new Path(contextField.getEntityRelativeFieldName(),Path.ANYPATH); if(fieldName.numSegments()>=prefix.numSegments()) { Path fieldPrefix=fieldName.prefix(prefix.numSegments()); // Also include the * if(fieldPrefix.equals(prefix)) { // Remove fieldPrefix Path relativeFieldName=fieldName.suffix(-prefix.numSegments()); if(relativeFieldName.isEmpty()) relativeFieldName=new Path(Path.THIS); return relativeFieldName; } } } return fieldName; }