/** * 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; } }
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(); } }
/** * 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; } }
/** * Initializes the document specific slots */ private void initializeSlots() { // Need to create a new slot for each array element for (ChildSlot blockSlot : block.getChildSlots()) { List<ChildSlot> childSlots = slots.get(blockSlot.getReference()); if (childSlots == null) { slots.put(blockSlot.getReference(), childSlots = new ArrayList<>()); } if (blockSlot.hasAnys()) { // Slot under array. Add one slot for each array element KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(blockSlot.getLocalContainerName()); while (cursor.hasNext()) { cursor.next(); ChildSlot newSlot = new ChildSlot(cursor.getCurrentKey(), blockSlot.getReference()); childSlots.add(newSlot); } } else { childSlots.add(blockSlot); } } }
/** * Construct a cursor at a given location in a tree * * @param p Path to the root node for the cursor. If docroot, empty path. * @param start The root node */ public AbstractTreeCursor(Path p, N start) { currentPath = new MutablePath(p); currentNode = start; if (!hasChildren(start)) { throw new IllegalArgumentException("Not iterable"); } currentCursor = new SingleElemCursor("", start); currentCursor.next(); }
/** * Returns a list of fields in the doc inaccessible to the current user * during insertion. If the returned list is empty, the user can insert the * doc. */ public List<Path> getInaccessibleFields_Insert(JsonDoc doc) { Set<Path> inaccessibleFields = getInaccessibleFields(Operation.insert); List<Path> ret = new ArrayList<>(inaccessibleFields.size()); for (Path x : inaccessibleFields) { KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(x); if (cursor.hasNext()) { ret.add(x); } } return ret; }
@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; } }
/** * Initializes the document specific slots */ private void initializeSlots() { // Need to create a new slot for each array element for (ChildSlot blockSlot : block.getChildSlots()) { List<ChildSlot> childSlots = slots.get(blockSlot.getReference()); if (childSlots == null) { slots.put(blockSlot.getReference(), childSlots = new ArrayList<>()); } if (blockSlot.hasAnys()) { // Slot under array. Add one slot for each array element KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(blockSlot.getLocalContainerName()); while (cursor.hasNext()) { cursor.next(); ChildSlot newSlot = new ChildSlot(cursor.getCurrentKey(), blockSlot.getReference()); childSlots.add(newSlot); } } else { childSlots.add(blockSlot); } } }
/** * Construct a cursor at a given location in a tree * * @param p Path to the root node for the cursor. If docroot, empty path. * @param start The root node */ public AbstractTreeCursor(Path p, N start) { currentPath = new MutablePath(p); currentNode = start; if (!hasChildren(start)) { throw new IllegalArgumentException("Not iterable"); } currentCursor = new SingleElemCursor("", start); currentCursor.next(); }
/** * Returns a list of fields in the doc inaccessible to the current user * during insertion. If the returned list is empty, the user can insert the * doc. */ public Set<Path> getInaccessibleFields_Insert(JsonDoc doc) { Set<Path> inaccessibleFields = getInaccessibleFields(Operation.insert); Set<Path> ret = new HashSet<>(inaccessibleFields.size()); for (Path x : inaccessibleFields) { KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(x); if (cursor.hasNext()) { ret.add(x); } } return ret; }
/** * 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; } }
@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; } }
/** * Construct a cursor at a given location in a tree * * @param p Path to the root node for the cursor. If docroot, empty path. * @param start The root node */ public AbstractTreeCursor(Path p, N start) { currentPath = new MutablePath(p); currentNode = start; if (!hasChildren(start)) { throw new IllegalArgumentException("Not iterable"); } currentCursor = new SingleElemCursor("", start); currentCursor.next(); }
/** * Returns a list of fields in the doc inaccessible to the current user * during insertion. If the returned list is empty, the user can insert the * doc. */ public Set<Path> getInaccessibleFields_Insert(JsonDoc doc) { Set<Path> inaccessibleFields = getInaccessibleFields(Operation.insert); Set<Path> ret = new HashSet<>(inaccessibleFields.size()); for (Path x : inaccessibleFields) { KeyValueCursor<Path, JsonNode> cursor = doc.getAllNodes(x); if (cursor.hasNext()) { ret.add(x); } } return ret; }
/** * 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; } }
@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 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 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 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 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(); } }