@Override public Document createFieldValue() { return new Document(this, (DocumentId) null); }
@SuppressWarnings("deprecation") public static void printDocumentXml(Document doc, XmlStream xml) { xml.addAttribute("documenttype", doc.getDataType().getName()); xml.addAttribute("documentid", doc.getId()); final java.lang.Long lastModified = doc.getLastModified(); if (lastModified != null) { xml.addAttribute("lastmodifiedtime", lastModified); } doc.getHeader().printXml(xml); doc.getBody().printXml(xml); }
@Override public void serialize(DocumentWriter writer) { doc.serialize(writer); }
/** * Creates a document that is a shallow copy of another. * * @param doc The document to copy. */ public Document(Document doc) { this(doc.getDataType(), doc.getId()); header = doc.header; body = doc.body; lastModified = doc.lastModified; }
public SimpleDocumentAdapter(Document input) { this(input, new Document(input.getDataType(), input.getId())); }
public void setId(DocumentId id) { internalSetId(id, getDataType()); } private void internalSetId(DocumentId id, DocumentType docType) {
@Override public DocumentId getId() { return doc.getId(); }
Document doc2 = doc1.clone(); doc2.setFieldValue("content", new StringFieldValue("hiho silver")); spi.createBucket(bucket); GetResult gr = spi.get(bucket, new AllFields(), doc1.getId());
final String attributeName = reader.getAttributeName(i).toString(); if ("documentid".equals(attributeName) || "id".equals(attributeName)) { document.setId(new DocumentId(reader.getAttributeValue(i))); } else if ("documenttype".equals(attributeName) || "type".equals(attributeName)) { typeName = reader.getAttributeValue(i); if (document.getId() != null) { if (field == null) { field = new FieldBase(document.getId().toString()); throw newDeserializeException(field, "Must specify an existing document type, not '" + typeName + "'"); } else { document.setDataType(doctype); document.setFieldValue(f, fv); skipToEnd(f.getName()); } else if (type == XMLStreamReader.END_ELEMENT) {
@Override public DocumentUpdate getOutput() { Document doc = adapter.getUpdatableOutput(); DocumentUpdate upd = new DocumentUpdate(doc.getDataType(), doc.getId()); for (Iterator<Map.Entry<Field, FieldValue>> it = doc.iterator(); it.hasNext();) { Map.Entry<Field, FieldValue> entry = it.next(); Field field = entry.getKey(); if (field.getName().equals("sddocname")) { continue; } FieldUpdate fieldUpd = FieldUpdate.create(field); fieldUpd.addValueUpdates(builder.build(entry.getValue())); if (!fieldUpd.isEmpty()) { upd.addFieldUpdate(fieldUpd); } } return upd.isEmpty() ? null : upd; }
@SuppressWarnings({ "unchecked" }) @Override public SimpleDocumentAdapter setOutputValue(Expression exp, String fieldName, FieldValue fieldValue) { Field field = output.getField(fieldName); if (field == null) { throw new IllegalArgumentException("Field '" + fieldName + "' not found in document type '" + output.getDataType().getName() + "'."); } output.setFieldValue(field, fieldValue); return this; } }
@Override public DocumentType getDataType() { return doc.getDataType(); }
public Document createRandomDocumentAtLocation(long location, long timestamp, int minSize, int maxSize) { Document document = new Document(getDocumentType("testdoctype1"), new DocumentId("userdoc:footype:" + location + ":" + timestamp)); document.setFieldValue("headerval", new IntegerFieldValue((int)timestamp)); StringBuffer value = new StringBuffer(); int length = (int)(Math.random() * (maxSize - minSize)) + minSize; for (int i = 0; i < length; ++i) { value.append("A"); } document.setFieldValue("content", new StringFieldValue(value.toString())); return document; } }
/** * Applies this field update. * * @param doc the document to apply the update to * @return a reference to itself */ public FieldUpdate applyTo(Document doc) { for (ValueUpdate vupd : valueUpdates) { DataType dataType = field.getDataType(); FieldValue oldValue = doc.getFieldValue(field); boolean existed = (oldValue != null); if (!existed) { oldValue = dataType.createFieldValue(); } FieldValue newValue = vupd.applyTo(oldValue); if (newValue == null) { if (existed) { doc.removeFieldValue(field); } } else { doc.setFieldValue(field, newValue); } } return this; }
@Override public void write(FieldBase field, Document value) { try { fieldNameIfNotNull(generator, field); generator.writeStartObject(); // this makes it impossible to refeed directly, not sure what's correct // perhaps just change to "put"? generator.writeStringField("id", value.getId().toString()); generator.writeObjectFieldStart(FIELDS); Iterator<Map.Entry<Field, FieldValue>> i = value.iterator(); while (i.hasNext()) { Map.Entry<Field, FieldValue> entry = i.next(); entry.getValue().serialize(entry.getKey(), this); } generator.writeEndObject(); generator.writeEndObject(); generator.flush(); } catch (IOException e) { throw new RuntimeException(e); } }
/** * Copies fields from one document to another based on whether the fields match the given * fieldset. */ public void copyFields(Document source, Document target, FieldSet fieldSet) { for (Iterator<Map.Entry<Field, FieldValue>> i = source.iterator(); i.hasNext();) { Map.Entry<Field, FieldValue> v = i.next(); if (fieldSet.contains(v.getKey())) { target.setFieldValue(v.getKey(), v.getValue()); } } }
DocumentUpdate update = new DocumentUpdate(docType, doc1.getId()); FieldUpdate fieldUpdate = FieldUpdate.create(docType.getField("headerval")); fieldUpdate.addValueUpdate(AssignValueUpdate.createAssign(new IntegerFieldValue(42))); GetResult result = spi.get(bucket, new AllFields(), doc1.getId()); assertEquals(new IntegerFieldValue(42), result.getDocument().getFieldValue("headerval")); spi.remove(bucket, 5, doc1.getId()); spi.flush(bucket); GetResult result = spi.get(bucket, new AllFields(), doc1.getId());
@Override public FieldValue setFieldValue(Field field, FieldValue fieldValue) { checkAccess(field); return doc.setFieldValue(field, fieldValue); }
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; }
@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; }