/** * Returns a node matching a path * * @param p The path * * The path cannot contain *. * * @returns The node, or null if the node cannot be found */ public JsonNode get(Path p) { return get(docRoot, p); }
/** * Returns a cursor that iterates all nodes of the document in a depth first * manner, but uses <code>p</code> as a prefix to all the paths during * iteration. This method is meant to be used for a JsonDoc rooted at an * intermediate node in a Json node tree. */ public JsonNodeCursor cursor(Path p) { return cursor(docRoot, p); }
public KeyValueCursor<Path, JsonNode> getNodes(Path relativePath) { return new JsonDoc(contextRoot).getAllNodes(new Path(relativePath)); }
private JsonNode setOrAdd(JsonDoc doc, Path contextPath, FieldData df, JsonNode newValueNode, Object newValue, Type newValueType) { JsonNode oldValueNode = null; Path fieldPath = new Path(contextPath, df.field); if (op == UpdateOperator._set) { LOGGER.debug("set fieldPath={}, newValue={}", fieldPath, newValueNode); oldValueNode = doc.modify(fieldPath, newValueNode, true); } else if (op == UpdateOperator._add) { oldValueNode = doc.get(fieldPath); if (newValueNode != null && oldValueNode != null) { newValueNode = df.fieldType.toJson(factory, Arith.add(df.fieldType.fromJson(oldValueNode), newValue, Arith.promote(df.fieldType, newValueType))); doc.modify(fieldPath, newValueNode, false); } } return oldValueNode; }
/** * Projects a document */ public JsonDoc project(JsonDoc doc, JsonNodeFactory factory) { JsonNodeCursor cursor = doc.cursor(); cursor.firstChild(); ObjectNode root = projectObject(this, factory, rootMdNode, rootMdPath, cursor, new QueryEvaluationContext(doc.getRoot())); return new JsonDoc(root); }
private void retrieveFragments(QueryPlanDoc doc, QueryPlanNodeExecutor exec) { // We only process child nodes. QueryPlanNode[] destinations=exec.getNode().getDestinations(); for(QueryPlanNode childNode:destinations) { QueryPlanNodeExecutor childExecutor=childNode.getProperty(QueryPlanNodeExecutor.class); CompositeMetadata childMd=childNode.getMetadata(); Path insertInto=childMd.getEntityPath(); JsonNode insertionNode=doc.getDoc().get(insertInto); if(insertionNode==null) doc.getDoc().modify(insertInto,insertionNode=factory.getNodeFactory().arrayNode(),true); List<QueryPlanDoc> children=doc.getChildren(childNode); if(children!=null) { for(QueryPlanDoc childDoc:children) { ((ArrayNode)insertionNode).add(childDoc.getDoc().getRoot()); retrieveFragments(doc,childExecutor); } } } }
public DocCtx(JsonDoc doc,ResultMetadata rmd) { super(doc.getRoot()); this.resultMetadata=rmd; }
JsonNodeCursor preCursor = processedDocument.getPreDoc().cursor(); JsonNodeCursor postCursor = processedDocument.getPostDoc().cursor(); JsonNode postNode = processedDocument.getPostDoc().get(path); String postValue = processedDocument.getOperation() == Operation.DELETE || postNode == null ? null : postNode.asText(); JsonNode preNode = processedDocument.getPreDoc().get(path); String preValue = preNode == null ? null : preNode.asText(); String postValue = node.asText(); JsonNode node = processedDocument.getPreDoc().get(p); node = processedDocument.getPostDoc().get(p);
/** * Returns a deep copy of the current document */ public JsonDoc copy() { return new JsonDoc(docRoot.deepCopy()); }
/** * Returns a query binder for a slot. The slot cannot have '*' in it (i.e. * there must be only one location for the slot in the document). */ public BindQuery getBindersForSlot(ChildSlot slot, AssociationQuery childAq) { List<Binder> binders = new ArrayList<>(); for (BoundObject bo : childAq.getFieldBindings()) { // Interpret field based on this slot Path field = bo.getFieldInfo().getEntityRelativeFieldNameWithContext(); Path fieldAtSlot = field.mutableCopy().rewriteIndexes(slot.getLocalContainerName()).immutableCopy(); if (fieldAtSlot.nAnys() > 0) { KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(fieldAtSlot); List<Value> value=new ArrayList<Value>(); while (cursor.hasNext()) { cursor.next(); ((List)value).add(getValue(bo.getFieldInfo().getFieldMd(),cursor.getCurrentValue())); } binders.add(new Binder(bo,value)); } else { binders.add(new Binder(bo,getValue(bo.getFieldInfo().getFieldMd(),doc.get(fieldAtSlot)))); } } return new BindQuery(binders); }
/** * Compares the pre- and post- documents after they are passed * through the watchProjector, and returns the delta */ private DocComparator.Difference<JsonNode> compareDocs(EntityMetadata metadata, JsonDoc preDoc, JsonDoc postDoc, Projector watchProjector) throws Exception { JsonDoc watchedPostDoc = watchProjector.project(postDoc, jsonNodeFactory); JsonDoc watchedPreDoc = preDoc == null ? new JsonDoc(jsonNodeFactory.objectNode()) : watchProjector.project(preDoc, jsonNodeFactory); // Compute diff JsonCompare cmp=metadata.getDocComparator(); LOGGER.debug("Array identities:{}",cmp.getArrayIdentities()); LOGGER.debug("Pre:{}, Post:{}",watchedPreDoc.getRoot(),watchedPostDoc.getRoot()); DocComparator.Difference<JsonNode> diff=cmp. compareNodes(watchedPreDoc.getRoot(),watchedPostDoc.getRoot()); LOGGER.debug("Diff: {}",diff); return diff; }
@Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { return doc.modify(contextPath, null, false) != null; } }
/** * Returns all nodes matching the path. The path can contain * * * @param p The path * * Returns a cursor iterating through all nodes of arrays, if any */ public KeyValueCursor<Path, JsonNode> getAllNodes(Path p) { return getAllNodes(p, false); }
private boolean updateDoc(EntityMetadata md, JsonDoc doc, Measure measure) { if (updater.update(doc, md.getFieldTreeRoot(), Path.EMPTY)) { // Remove any nulls from the document JsonDoc.filterNulls(doc.getRoot()); measure.begin("updateArraySizes"); PredefinedFields.updateArraySizes(md, nodeFactory, doc); measure.end("updateArraySizes"); return true; } else { return false; } }
request.getEntityVersion().getEntity(), factory, request instanceof DocRequest ? JsonDoc.docList(JsonDoc.filterNulls(((DocRequest) request).getEntityData())) : null, request.getExecution()); this.request = request;
/** * Recursively remove all null nodes in the given json subtree * * This method operates on the given root node, and returns the same root * instance. It does not create a new copy. */ public static JsonNode filterNulls(JsonNode root) { if (root instanceof ArrayNode) { for (JsonNode element : root) { filterNulls(element); } } else if (root instanceof ObjectNode) { ObjectNode o = (ObjectNode) root; for (Iterator<Map.Entry<String, JsonNode>> itr = o.fields(); itr.hasNext();) { Map.Entry<String, JsonNode> entry = itr.next(); JsonNode value = entry.getValue(); if (value == null || value instanceof NullNode) { itr.remove(); } else { filterNulls(value); } } } return root; }
private JsonNode setOrAdd(JsonDoc doc, Path contextPath, FieldData df, JsonNode newValueNode, Object newValue, Type newValueType) { JsonNode oldValueNode = null; Path fieldPath = new Path(contextPath, df.field); if (op == UpdateOperator._set) { LOGGER.debug("set fieldPath={}, newValue={}",fieldPath,newValueNode); oldValueNode = doc.modify(fieldPath, newValueNode, true); } else if (op == UpdateOperator._add) { oldValueNode = doc.get(fieldPath); if (newValueNode != null && oldValueNode != null) { newValueNode = df.fieldType.toJson(factory, Arith.add(df.fieldType.fromJson(oldValueNode), newValue, Arith.promote(df.fieldType, newValueType))); doc.modify(fieldPath, newValueNode, false); } } return oldValueNode; }
/** * Projects a document */ public JsonDoc project(JsonDoc doc, JsonNodeFactory factory) { JsonNodeCursor cursor = doc.cursor(); cursor.firstChild(); ObjectNode root = (ObjectNode) project(factory, rootMdPath, cursor, new QueryEvaluationContext(doc.getRoot()), false); if (root == null) { root = factory.objectNode(); } return new JsonDoc(root); }
public DocCtx(JsonDoc doc) { super(doc.getRoot()); }
preCursor = processedDocument.getPreDoc().cursor(); postCursor = processedDocument.getPostDoc().cursor(); if (processedDocument.getPostDoc() != null) { JsonNode postNode = processedDocument.getPostDoc().get(path); postValue = processedDocument.getCRUDOperation() == CRUDOperation.DELETE || postNode == null ? null : postNode.asText(); if (processedDocument.getPreDoc() != null) { JsonNode preNode = processedDocument.getPreDoc().get(path); preValue = preNode == null ? null : preNode.asText(); node = processedDocument.getPreDoc().get(p); node = processedDocument.getPostDoc().get(p);