@Override public Set<Key> extract(JsonDoc doc,Set<Key> set) { if(set==null) set=new HashSet<>(); KeyValueCursor<Path,JsonNode> cursor=doc.getAllNodes(fullName); boolean hasData=false; while(cursor.hasNext()) { cursor.next(); set.add(new SimpleKey(type.fromJson(cursor.getCurrentValue()))); hasData=true; } if(!hasData) { // No value in doc: insert null set.add(SimpleKey.NULL_KEY); } return set; } }
@Override public Set<Key> extract(JsonDoc doc,Set<Key> set) { if(set==null) set=new HashSet<>(); KeyValueCursor<Path,JsonNode> cursor=doc.getAllNodes(fullName); boolean hasData=false; while(cursor.hasNext()) { cursor.next(); set.add(new SimpleKey(type.fromJson(cursor.getCurrentValue()))); hasData=true; } if(!hasData) { // No value in doc: insert null set.add(SimpleKey.NULL_KEY); } return set; } }
@Override public boolean evaluate(QueryEvaluationContext ctx) { LOGGER.debug("evaluate {} {}", relativePath, regex); KeyValueCursor<Path, JsonNode> cursor = ctx.getNodes(relativePath); boolean ret = false; while (cursor.hasNext()) { cursor.next(); JsonNode valueNode = cursor.getCurrentValue(); Object docValue; if (valueNode != null) { docValue = fieldMd.getType().fromJson(valueNode); } else { docValue = null; } LOGGER.debug(" value={}", valueNode); if (docValue != null) { ret = regex.matcher(docValue.toString()).matches(); } if (ret) { break; } } ctx.setResult(ret); return ret; } }
@Override public boolean evaluate(QueryEvaluationContext ctx) { LOGGER.debug("evaluate {} {}", relativePath, regex); KeyValueCursor<Path, JsonNode> cursor = ctx.getNodes(relativePath); boolean ret = false; while (cursor.hasNext()) { cursor.next(); JsonNode valueNode = cursor.getCurrentValue(); Object docValue; if (valueNode != null) { docValue = fieldMd.getType().fromJson(valueNode); } else { docValue = null; } LOGGER.debug(" value={}", valueNode); if (docValue != null) { ret = regex.matcher(docValue.toString()).matches(); } if (ret) { break; } } ctx.setResult(ret); return ret; } }
private boolean different(KeyValueCursor<Path, JsonNode> c1, KeyValueCursor<Path, JsonNode> c2) { while (c1.hasNext()) { if (c2.hasNext()) { c1.next(); c2.next(); JsonNode v1 = c1.getCurrentValue(); JsonNode v2 = c2.getCurrentValue(); if (!v1.equals(v2)) { return true; } } else { return true; } } return c2.hasNext(); } }
@Override public boolean evaluate(QueryEvaluationContext ctx) { LOGGER.debug("evaluate {} {} {}", field, operator, values); KeyValueCursor<Path, JsonNode> cursor = ctx.getNodes(field, true); boolean ret = false; while (cursor.hasNext()) { cursor.next(); JsonNode valueNode = cursor.getCurrentValue(); Object docValue = fieldMd.getType().fromJson(valueNode); LOGGER.debug(" value={}", valueNode); boolean in = values.contains(docValue); LOGGER.debug(" result={}", in); if (in) { ret = true; break; } } ctx.setResult(operator.apply(ret)); return ctx.getResult(); } }
@Override public boolean evaluate(QueryEvaluationContext ctx) { LOGGER.debug("evaluate {} {} {}", field, operator, values); KeyValueCursor<Path, JsonNode> cursor = ctx.getNodes(field, true); boolean ret = false; while (cursor.hasNext()) { cursor.next(); JsonNode valueNode = cursor.getCurrentValue(); Object docValue = fieldMd.getType().fromJson(valueNode); LOGGER.debug(" value={}", valueNode); boolean in = values.contains(docValue); LOGGER.debug(" result={}", in); if (in) { ret = true; break; } } ctx.setResult(operator.apply(ret)); return ctx.getResult(); } }
/** * Attempt to seek the cursor to the next sibling of the current node * * @return If the current node is the last node of its parent, returns * false, and the cursor still points to the same node. Otherwise, cursor * points to the next sibling and true is returned. */ public boolean nextSibling() { if (currentCursor.hasNext()) { currentCursor.next(); currentNode = currentCursor.getCurrentValue(); currentPath.setLast(currentCursor.getCurrentKey()); return true; } else { return false; } }
/** * Attempt to seek the cursor to the next sibling of the current node * * @return If the current node is the last node of its parent, returns * false, and the cursor still points to the same node. Otherwise, cursor * points to the next sibling and true is returned. */ public boolean nextSibling() { if (currentCursor.hasNext()) { currentCursor.next(); currentNode = currentCursor.getCurrentValue(); currentPath.setLast(currentCursor.getCurrentKey()); return true; } else { return false; } }
/** * Attempt to seek the cursor to the next sibling of the current node * * @return If the current node is the last node of its parent, returns * false, and the cursor still points to the same node. Otherwise, cursor * points to the next sibling and true is returned. */ public boolean nextSibling() { if (currentCursor.hasNext()) { currentCursor.next(); currentNode = currentCursor.getCurrentValue(); currentPath.setLast(currentCursor.getCurrentKey()); return true; } else { return false; } }
public static void initializeUIDFields(JsonNodeFactory factory, EntityMetadata md, JsonDoc doc) { FieldCursor cursor = md.getFieldCursor(); while (cursor.next()) { FieldTreeNode node = cursor.getCurrentNode(); // Process all UID fields if (node.getType().equals(UIDType.TYPE)) { Path p = cursor.getCurrentPath(); LOGGER.debug("Processing UID field {}", p); if (node instanceof Field && required((Field) node)) { LOGGER.debug("Field {} is required", p); setRequiredField(factory, doc, p, 1, null); } else { // Here, node could be a field or an array LOGGER.debug("Field {} is not required", p); KeyValueCursor<Path, JsonNode> nodeCursor = doc.getAllNodes(p); while (nodeCursor.hasNext()) { nodeCursor.next(); JsonNode valueNode = nodeCursor.getCurrentValue(); if (valueNode.isNull() || valueNode.asText().length() == 0) { String value = UIDType.newValue(); LOGGER.debug("Setting {} to {}", nodeCursor.getCurrentKey(), value); doc.modify(nodeCursor.getCurrentKey(), factory.textNode(value), true); } } } } } }
@Override public boolean evaluate(QueryEvaluationContext ctx) { boolean ret = false; KeyValueCursor<Path, JsonNode> cursor = ctx.getNodes(field); while(cursor.hasNext()&&!ret) { cursor.next(); JsonNode node=cursor.getCurrentValue(); if (node instanceof ArrayNode) { ArrayNode array = (ArrayNode) node; int index = 0; QueryEvaluationContext nestedCtx = null; for (Iterator<JsonNode> itr = array.elements(); itr.hasNext();) { JsonNode arrayElem = itr.next(); if (index == 0) { nestedCtx = ctx.firstElementNestedContext(arrayElem, field); } else { nestedCtx.elementNestedContext(arrayElem, index); } if (ev.evaluate(nestedCtx)) { ret = true; } index++; } } } ctx.setResult(ret); return ret; } }
@Override public boolean evaluate(QueryEvaluationContext ctx) { Object value=rvalue.getValue(); LOGGER.debug("evaluate {} {} {}", field, operator, value); KeyValueCursor<Path, JsonNode> cursor = ctx.getNodes(field); while (cursor.hasNext()) { cursor.next(); JsonNode valueNode = cursor.getCurrentValue(); Object docValue; if (valueNode != null) { docValue = fieldMd.getType().fromJson(valueNode); } else { docValue = null; } LOGGER.debug(" value={}", valueNode); int result = fieldMd.getType().compare(docValue, value); LOGGER.debug(" result={}", result); ctx.setResult(operator.apply(result)); if (ctx.getResult()) { break; } } return ctx.getResult(); } }
/** * Attempt to seek the cursor to the first child of the current node * * @return If the current node does not have any children, returns false, * and cursor still points to the node it was pointing to before the call. * If the current node has children, cursor points to the first child of the * node, and true is returned. */ public boolean firstChild() { // push current state to stack, and get the first child of TOS if (hasChildren(currentNode)) { push(); currentCursor = getCursor(currentNode); currentCursor.next(); currentNode = currentCursor.getCurrentValue(); currentPath.push(currentCursor.getCurrentKey()); return true; } else { return false; } }
/** * 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); }
/** * 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); }
/** * Attempt to seek the cursor to the first child of the current node * * @return If the current node does not have any children, returns false, * and cursor still points to the node it was pointing to before the call. * If the current node has children, cursor points to the first child of the * node, and true is returned. */ public boolean firstChild() { // push current state to stack, and get the first child of TOS if (hasChildren(currentNode)) { push(); currentCursor = getCursor(currentNode); if (!currentCursor.hasNext()) { pop(); return false; } currentCursor.next(); currentNode = currentCursor.getCurrentValue(); currentPath.push(currentCursor.getCurrentKey()); return true; } else { return false; } }
/** * Attempt to seek the cursor to the first child of the current node * * @return If the current node does not have any children, returns false, * and cursor still points to the node it was pointing to before the call. * If the current node has children, cursor points to the first child of the * node, and true is returned. */ public boolean firstChild() { // push current state to stack, and get the first child of TOS if (hasChildren(currentNode)) { push(); currentCursor = getCursor(currentNode); if (!currentCursor.hasNext()) { pop(); return false; } currentCursor.next(); currentNode = currentCursor.getCurrentValue(); currentPath.push(currentCursor.getCurrentKey()); return true; } else { return false; } }
@Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; if (numAny > 0) { KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(memento.expr.getField()); boolean b = cursor.hasNext(); while (b) { cursor.next(); Path currentKey = cursor.getCurrentKey(); JsonNode currentValue = cursor.getCurrentValue(); UpdateInfo updateInfoInstance = generateProcessedInfo(memento.context, memento.expr, currentKey, this.updateInfo.updater); if (update(doc, contextPath, updateInfoInstance)) { ret = true; } b = cursor.hasNext(); } return ret; } else { ret = update(doc, contextPath, this.updateInfo); return ret; } }
@Override public boolean update(JsonDoc doc, FieldTreeNode contextMd, Path contextPath) { boolean ret = false; if (numAny > 0) { KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(memento.expr.getField()); boolean b = cursor.hasNext(); while (b) { cursor.next(); Path currentKey = cursor.getCurrentKey(); JsonNode currentValue = cursor.getCurrentValue(); UpdateInfo updateInfoInstance = generateProcessedInfo(memento.context, memento.expr, currentKey, this.updateInfo.updater); if (update(doc, contextPath, updateInfoInstance)) { ret = true; } b = cursor.hasNext(); } return ret; } else { ret = update(doc, contextPath, this.updateInfo); return ret; } }