private static Field resolveField(FieldPathUpdate update) { String orig = update.getOriginalFieldPath(); if (orig == null) { return null; } FieldPath path = update.getFieldPath(); if (path == null) { return null; } DataType type = path.getResultingDataType(); if (type == null) { return null; } return new Field(orig, type); } }
public void write(FieldPathUpdate update) { putByte(null, (byte)update.getUpdateType().getCode()); put(null, update.getOriginalFieldPath()); put(null, update.getOriginalWhereClause()); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; AssignFieldPathUpdate that = (AssignFieldPathUpdate) o; if (createMissingPath != that.createMissingPath) return false; if (removeIfZero != that.removeIfZero) return false; if (expression != null ? !expression.equals(that.expression) : that.expression != null) return false; if (fieldValue != null ? !fieldValue.equals(that.fieldValue) : that.fieldValue != null) return false; return true; }
@SuppressWarnings({ "unchecked", "rawtypes" }) private void createUpdatesAt(List<FieldPathEntry> path, FieldValue value, int idx, DocumentUpdate out) { FieldPath updatePath = update.getFieldPath(); if (idx < updatePath.size()) { FieldPathEntry pathEntry = updatePath.get(idx); value.getClass().getName() + "."); out.addFieldPathUpdate(new AddFieldPathUpdate(update.getDocumentType(), new FieldPath(path).toString(), update.getOriginalWhereClause(), (Array)value)); } else if (update instanceof AssignFieldPathUpdate) { out.addFieldPathUpdate(new AssignFieldPathUpdate(update.getDocumentType(), new FieldPath(path).toString(), update.getOriginalWhereClause(), value)); } else if (update instanceof RemoveFieldPathUpdate) { out.addFieldPathUpdate(new RemoveFieldPathUpdate(update.getDocumentType(), new FieldPath(path).toString(), update.getOriginalWhereClause()));
public static void applyUpdate(FieldPathUpdate update, Document doc) { if (update instanceof AddFieldPathUpdate) { update.applyTo(doc); } else if (update instanceof AssignFieldPathUpdate) { AssignFieldPathUpdate assign = (AssignFieldPathUpdate)update; boolean createMissingPath = assign.getCreateMissingPath(); boolean removeIfZero = assign.getRemoveIfZero(); assign.setCreateMissingPath(true); assign.setRemoveIfZero(false); assign.applyTo(doc); assign.setCreateMissingPath(createMissingPath); assign.setRemoveIfZero(removeIfZero); } else if (update instanceof RemoveFieldPathUpdate) { doc.iterateNested(update.getFieldPath(), 0, new MyHandler()); } }
public static boolean isComplete(FieldPathUpdate update) { if (!(update instanceof AssignFieldPathUpdate)) { return false; } // Only consider field path updates that touch a top-level field as 'complete', // as these may be converted to regular field value updates. return ((update.getFieldPath().size() == 1) && update.getFieldPath().get(0).getType() == FieldPathEntry.Type.STRUCT_FIELD); }
/** * Applies this document update. * * @param doc the document to apply the update to * @return a reference to itself * @throws IllegalArgumentException if the document does not have the same document type as this update */ public DocumentUpdate applyTo(Document doc) { verifyType(doc); for (FieldUpdate fieldUpdate : id2FieldUpdates.values()) { fieldUpdate.applyTo(doc); } for (FieldPathUpdate fieldPathUpdate : fieldPathUpdates) { fieldPathUpdate.applyTo(doc); } return this; }
@Override public int hashCode() { int result = super.hashCode(); result = 31 * result + handler.hashCode(); return result; }
public static Document newPartialDocument(DocumentId docId, FieldPathUpdate update) { Document doc = new Document(update.getDocumentType(), docId); applyUpdate(update, doc); return doc; }
private void write(FieldPath fieldPath, Collection<FieldPathUpdate> fieldPathUpdates, JsonGenerator generator) throws IOException { generator.writeObjectFieldStart(fieldPath.toString()); for (FieldPathUpdate update : fieldPathUpdates) { if (writeArithmeticFieldPathUpdate(update, generator)) continue; generator.writeFieldName(update.getUpdateType().name().toLowerCase()); if (update instanceof AssignFieldPathUpdate) { AssignFieldPathUpdate assignUp = (AssignFieldPathUpdate) update; if (assignUp.getExpression() != null) { throw new RuntimeException("Unable to parse expression: " + assignUp.getExpression()); } else { assignUp.getNewValue().serialize(null, this); } } else if (update instanceof AddFieldPathUpdate) { ((AddFieldPathUpdate) update).getNewValues().serialize(null, this); } else if (update instanceof RemoveFieldPathUpdate) { generator.writeNumber(0); } else { throw new RuntimeException("Unsupported fieldpath operation: " + update.getClass().getName()); } } generator.writeEndObject(); }
public void applyTo(Document doc) { if (selector == null) { FieldPathIteratorHandler handler = getIteratorHandler(doc); doc.iterateNested(fieldPath, 0, handler); } else { ResultList results = selector.getMatchingResultList(new DocumentPut(doc)); ListIterator<ResultList.ResultPair> resultIter = results.getResults().listIterator(results.getResults().size()); while (resultIter.hasPrevious()) { ResultList.ResultPair rp = resultIter.previous(); if (rp.getResult() == Result.TRUE) { FieldPathIteratorHandler handler = getIteratorHandler(doc); handler.getVariables().clear(); handler.getVariables().putAll(rp.getVariables()); doc.iterateNested(fieldPath, 0, handler); } } } }
public void read(DocumentUpdate update) { update.setId(new DocumentId(this)); update.setDocumentType(readDocumentType()); int size = getInt(null); for (int i = 0; i < size; i++) { update.addFieldUpdate(new FieldUpdate(this, update.getDocumentType(), 8)); } int sizeAndFlags = getInt(null); update.setCreateIfNonExistent(DocumentUpdateFlags.extractFlags(sizeAndFlags).getCreateIfNonExistent()); size = DocumentUpdateFlags.extractValue(sizeAndFlags); for (int i = 0; i < size; i++) { int type = getByte(null); update.addFieldPathUpdate(FieldPathUpdate.create(FieldPathUpdate.Type.valueOf(type), update.getDocumentType(), this)); } }
throw new IllegalArgumentException("Exception during handling of update '" + fieldUpd + "' to field '" + fieldUpd.getFieldPath() + "'", e);
@Override public int hashCode() { int result = super.hashCode(); result = 31 * result + (fieldValue != null ? fieldValue.hashCode() : 0); result = 31 * result + (expression != null ? expression.hashCode() : 0); result = 31 * result + (createMissingPath ? 1 : 0); result = 31 * result + (removeIfZero ? 1 : 0); return result; }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; if (!super.equals(o)) return false; AddFieldPathUpdate that = (AddFieldPathUpdate) o; if (!handler.equals(that.handler)) return false; return true; }