/** * Copy constructor * @param other DocumentPut to copy */ public DocumentPut(DocumentPut other) { super(other); this.document = new Document(other.getDocument()); }
@Override public void put(DocumentPut documentPut, DocumentProtocol.Priority priority) { access.documents.put(documentPut.getId(), documentPut.getDocument()); }
private void unwrapSchemaMapping(Processing processing) { final List<DocumentOperation> documentOperations = processing.getDocumentOperations(); for (int i = 0; i < documentOperations.size(); i++) { DocumentOperation documentOperation = documentOperations.get(i); if (documentOperation instanceof DocumentPut) { DocumentPut putOperation = (DocumentPut) documentOperation; if (putOperation.getDocument() instanceof DocumentOperationWrapper) { DocumentOperationWrapper proxy = (DocumentOperationWrapper) putOperation.getDocument(); documentOperations.set(i, new DocumentPut(putOperation, ((DocumentPut)proxy.getWrappedDocumentOperation()).getDocument())); } } } }
static boolean doProcessOuterDocument(Object o, String documentTypeName) { if ( ! (o instanceof DocumentOperation)) { if (log.isLoggable(LogLevel.DEBUG)) { log.log(LogLevel.DEBUG, o + " is not a DocumentOperation."); } return false; } DocumentOperation outerDocOp = (DocumentOperation)o; if ( ! (outerDocOp instanceof DocumentPut)) { //this is not a put, return if (log.isLoggable(LogLevel.DEBUG)) { log.log(LogLevel.DEBUG, "Given DocumentOperation is not a DocumentPut, returning. (Was given " + outerDocOp + ")."); } return false; } Document outerDoc = ((DocumentPut) outerDocOp).getDocument(); DocumentType type = outerDoc.getDataType(); if (!type.getName().equalsIgnoreCase(documentTypeName)) { //this is not the right document type if (log.isLoggable(LogLevel.DEBUG)) { log.log(LogLevel.DEBUG, "Given Document is of wrong type, returning. (Was given " + outerDoc + ")."); } return false; } return true; }
@Override public void put(DocumentPut documentPut) { if (documentPut.getCondition().isPresent()) { throw new UnsupportedOperationException("test-and-set is not supported."); } access.documents.put(documentPut.getId(), documentPut.getDocument()); }
private void logProgress(Processing processing, DocumentProcessor.Progress progress, Call call) { StringBuilder message = new StringBuilder(); boolean first = true; message.append(call.getDocumentProcessorId()).append(" of class ") .append(call.getDocumentProcessor().getClass().getSimpleName()).append(" returned ").append(progress) .append(" for the documents: ["); for (DocumentOperation op : processing.getDocumentOperations()) { if (first) { first = false; } else { message.append(", "); } if (op instanceof DocumentPut) { message.append(Utf8.toString(JsonWriter.toByteArray(((DocumentPut) op).getDocument()))); } else { message.append(op.toString()); } } message.append("]"); log.log(LogLevel.SPAM, message.toString()); }
public void readPut(TokenBuffer buffer, DocumentPut put) { try { if (buffer.isEmpty()) // no "fields" map throw new IllegalArgumentException(put + " is missing a 'fields' map"); populateComposite(buffer, put.getDocument()); } catch (JsonReaderException e) { throw JsonReaderException.addDocId(e, put.getId()); } }
@Override public int getApproxSize() { if (buffer != null) { return buffer.getBuf().remaining(); } return put.getDocument().getApproxSize(); }
public Object evaluate(DocumentOperation op) { DocumentType doct; if (op instanceof DocumentPut) { doct = ((DocumentPut)op).getDocument().getDataType(); } else if (op instanceof DocumentUpdate) { doct = ((DocumentUpdate)op).getDocumentType(); } else if (op instanceof DocumentRemove) { DocumentRemove removeOp = (DocumentRemove)op; return (removeOp.getId().getDocType().equals(type) ? op : Boolean.FALSE); } else if (op instanceof DocumentGet) { DocumentGet getOp = (DocumentGet)op; return (getOp.getId().getDocType().equals(type) ? op : Boolean.FALSE); } else { throw new IllegalStateException("Document class '" + op.getClass().getName() + "' is not supported."); } return doct.isA(this.type) ? op : Boolean.FALSE; }
@Override public void onMessage(Message m, AckToken token) { if (m instanceof PutDocumentMessage) { PutDocumentMessage pm = (PutDocumentMessage)m; onDocument(pm.getDocumentPut().getDocument(), pm.getTimestamp()); } else if (m instanceof RemoveDocumentMessage) { RemoveDocumentMessage rm = (RemoveDocumentMessage)m; onRemove(rm.getDocumentId()); } else { throw new UnsupportedOperationException("Received unsupported message " + m.toString() + " to dump visitor data handler. This handler only accepts Put and Remove"); } ack(token); }
@Override public Progress process(Processing processing) { if (processing.getDocumentOperations().size() != 1) { //we were given more than one document, return log.log(LogLevel.DEBUG, "More than one document given, returning. (Was given " + processing.getDocumentOperations().size() + " documents)."); return Progress.DONE; } if (!doProcessOuterDocument(processing.getDocumentOperations().get(0), documentTypeName)) { return Progress.DONE; } Document outerDoc = ((DocumentPut)processing.getDocumentOperations().get(0)).getDocument();; @SuppressWarnings("unchecked") Array<Document> innerDocuments = (Array<Document>) outerDoc.getFieldValue(arrayFieldName); if (innerDocuments == null) { //the document does not have the field, return log.log(LogLevel.DEBUG, "The given Document does not have a field value for field " + arrayFieldName + ", returning. (Was given " + outerDoc + ")."); return Progress.DONE; } if (innerDocuments.size() == 0) { //the array is empty, return log.log(LogLevel.DEBUG, "The given Document does not have any elements in array field " + arrayFieldName + ", returning. (Was given " + outerDoc + ")."); return Progress.DONE; } split(processing, innerDocuments); return Progress.DONE; }
@Override public void read(Operation operation) throws Exception { DocumentOperation documentOperation = reader.next(); if (documentOperation == null) { stream.close(); operation.setInvalid(); return; } if (documentOperation instanceof DocumentUpdate) { operation.setDocumentUpdate((DocumentUpdate) documentOperation); } else if (documentOperation instanceof DocumentRemove) { operation.setRemove(documentOperation.getId()); } else if (documentOperation instanceof DocumentPut) { operation.setDocument(((DocumentPut) documentOperation).getDocument()); } else { throw new IllegalStateException("Got unknown class from JSON reader: " + documentOperation.getClass().getName()); } operation.setCondition(documentOperation.getCondition()); }
private Document createPutDocument(PutDocumentMessage msg) { Document document = msg.getDocumentPut().getDocument(); String typeName = document.getDataType().getName(); ContainerDocumentConfig.Doctype typeConfig = getDocumentConfig(typeName); if (typeConfig == null) { return document; } return createConcreteDocument(document, typeConfig); }
@Override public Progress process(Processing processing) { if ( ! doProcessOuterDocument(processing.getVariable(contextFieldName), documentTypeName)) { return Progress.DONE; } DocumentPut outerDoc = (DocumentPut)processing.getVariable(contextFieldName); @SuppressWarnings("unchecked") Array<Document> innerDocuments = (Array<Document>) outerDoc.getDocument().getFieldValue(arrayFieldName); if (innerDocuments == null) { @SuppressWarnings("unchecked") Array<Document> empty = (Array<Document>) outerDoc.getDocument().getDataType().getField(arrayFieldName).getDataType().createFieldValue(); innerDocuments = empty; } for (DocumentOperation op : processing.getDocumentOperations()) { if (op instanceof DocumentPut) { innerDocuments.add(((DocumentPut)op).getDocument()); } else { log.log(LogLevel.DEBUG, "Skipping: " + op); } } processing.getDocumentOperations().clear(); processing.getDocumentOperations().add(outerDoc); processing.removeVariable(contextFieldName); return Progress.DONE; }
private VespaXMLFeedReader.Operation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation) { final JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory); final DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId); VespaXMLFeedReader.Operation operation = new VespaXMLFeedReader.Operation(); try { inputStream.close(); } catch (IOException e) { throw new RuntimeException(e); } if (supportedOperation == DocumentParser.SupportedOperation.PUT) { operation.setDocument(((DocumentPut) documentOperation).getDocument()); } else { operation.setDocumentUpdate((DocumentUpdate) documentOperation); } // (A potentially empty) test-and-set condition is always set by JsonReader operation.setCondition(documentOperation.getCondition()); return operation; } }
private static Object evaluateFieldPath(String fieldPth, Object value) { if (value instanceof DocumentPut) { final Document doc = ((DocumentPut) value).getDocument(); FieldPath fieldPath = doc.getDataType().buildFieldPath(fieldPth); IteratorHandler handler = new IteratorHandler(); doc.iterateNested(fieldPath, 0, handler); if (handler.values.isEmpty()) { return null; } return handler.values; } else if (value instanceof DocumentUpdate) { return Result.INVALID; } else if (value instanceof DocumentRemove) { return Result.INVALID; } else if (value instanceof DocumentGet) { return Result.INVALID; } return Result.FALSE; }
/** * The Document object the proc should work on. Normally the one in arguments, but could be a proxy object * if schema mapping or @Accesses is in effect. * * <p> * public for testing */ public DocumentPut configDoc(DocumentProcessor docProc, DocumentPut documentPut) { if (!docProc.getFieldMap().isEmpty() || docProc.hasAnnotations()) { Document document = documentPut.getDocument(); document = new ProxyDocument(docProc, document, docProc.getDocMap(document.getDataType().getName())); DocumentPut newDocumentPut = new DocumentPut(document); newDocumentPut.setCondition(documentPut.getCondition()); documentPut = newDocumentPut; } return documentPut; }
@Override protected boolean doEncode(DocumentMessage obj, DocumentSerializer buf) { PutDocumentMessage msg = (PutDocumentMessage)obj; if (msg.getSerializedBuffer() != null) { buf.put(null, msg.getSerializedBuffer()); } else { msg.getDocumentPut().getDocument().serialize(buf); buf.putLong(null, msg.getTimestamp()); encodeTasCondition(buf, (TestAndSetMessage) obj); } return true; } }
@SuppressWarnings("deprecation") private BucketId getBucketId(Message msg) { switch (msg.getType()) { case DocumentProtocol.MESSAGE_PUTDOCUMENT: return factory.getBucketId(((PutDocumentMessage)msg).getDocumentPut().getDocument().getId()); case DocumentProtocol.MESSAGE_GETDOCUMENT: return factory.getBucketId(((GetDocumentMessage)msg).getDocumentId()); case DocumentProtocol.MESSAGE_REMOVEDOCUMENT: return factory.getBucketId(((RemoveDocumentMessage)msg).getDocumentId()); case DocumentProtocol.MESSAGE_UPDATEDOCUMENT: return factory.getBucketId(((UpdateDocumentMessage)msg).getDocumentUpdate().getId()); case DocumentProtocol.MESSAGE_GETBUCKETLIST: return ((GetBucketListMessage)msg).getBucketId(); case DocumentProtocol.MESSAGE_STATBUCKET: return ((StatBucketMessage)msg).getBucketId(); case DocumentProtocol.MESSAGE_CREATEVISITOR: return ((CreateVisitorMessage)msg).getBuckets().get(0); case DocumentProtocol.MESSAGE_REMOVELOCATION: return ((RemoveLocationMessage)msg).getBucketId(); default: log.log(LogLevel.ERROR, "Message type '" + msg.getType() + "' not supported."); return null; } }
private static Response toError(Reply reply, long reqId) { Message msg = reply.getMessage(); String err = getErrorMessage(reply); switch (msg.getType()) { case DocumentProtocol.MESSAGE_PUTDOCUMENT: return new DocumentResponse(reqId, ((PutDocumentMessage)msg).getDocumentPut().getDocument(), err, false); case DocumentProtocol.MESSAGE_UPDATEDOCUMENT: return new DocumentUpdateResponse(reqId, ((UpdateDocumentMessage)msg).getDocumentUpdate(), err, false); case DocumentProtocol.MESSAGE_REMOVEDOCUMENT: return new DocumentIdResponse(reqId, ((RemoveDocumentMessage)msg).getDocumentId(), err, false); case DocumentProtocol.MESSAGE_GETDOCUMENT: return new DocumentIdResponse(reqId, ((GetDocumentMessage)msg).getDocumentId(), err, false); default: return new Response(reqId, err, false); } }