use of com.yahoo.document.Document in project vespa by vespa-engine.
the class AbstractDocumentApiTestCase method requireThatAsyncHandlerWorks.
@Test
public void requireThatAsyncHandlerWorks() throws InterruptedException {
MyHandler handler = new MyHandler();
AsyncSession session = access().createAsyncSession(new AsyncParameters().setResponseHandler(handler));
DocumentType type = access().getDocumentTypeManager().getDocumentType("music");
Document doc1 = new Document(type, new DocumentId("doc:music:1"));
assertTrue(session.put(doc1).isSuccess());
assertTrue(handler.latch.await(60, TimeUnit.SECONDS));
assertNotNull(handler.response);
session.destroy();
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class VespaXMLFeedReader method read.
/* (non-Javadoc)
* @see com.yahoo.vespaxmlparser.FeedReader#read(com.yahoo.vespaxmlparser.VespaXMLFeedReader.Operation)
*/
@Override
public void read(Operation operation) throws Exception {
String startTag = null;
operation.setInvalid();
try {
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamReader.START_ELEMENT) {
startTag = reader.getName().toString();
if ("document".equals(startTag)) {
VespaXMLDocumentReader documentReader = new VespaXMLDocumentReader(reader, docTypeManager);
Document document = new Document(documentReader);
operation.setDocument(document);
operation.setCondition(TestAndSetCondition.fromConditionString(documentReader.getCondition()));
return;
} else if ("update".equals(startTag)) {
VespaXMLUpdateReader updateReader = new VespaXMLUpdateReader(reader, docTypeManager);
DocumentUpdate update = new DocumentUpdate(updateReader);
operation.setDocumentUpdate(update);
operation.setCondition(TestAndSetCondition.fromConditionString(updateReader.getCondition()));
return;
} else if ("remove".equals(startTag)) {
boolean documentIdFound = false;
Optional<String> condition = Optional.empty();
for (int i = 0; i < reader.getAttributeCount(); i++) {
final String attributeName = reader.getAttributeName(i).toString();
if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
operation.setRemove(new DocumentId(reader.getAttributeValue(i)));
documentIdFound = true;
} else if ("condition".equals(attributeName)) {
condition = Optional.of(reader.getAttributeValue(i));
}
}
if (!documentIdFound) {
throw newDeserializeException("Missing \"documentid\" attribute for remove operation");
}
operation.setCondition(TestAndSetCondition.fromConditionString(condition));
return;
} else {
throw newDeserializeException("Element \"" + startTag + "\" not allowed in this context");
}
}
}
} catch (XMLStreamException e) {
throw (e);
// Skip to end of current tag with other exceptions.
} catch (Exception e) {
try {
if (startTag != null) {
skipToEnd(startTag);
}
} catch (Exception ignore) {
}
throw (e);
}
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class ApplicationTest method client.
@Test
public void client() throws Exception {
try (ApplicationFacade app = new ApplicationFacade(Application.fromBuilder(new Application.Builder().documentType("test", IOUtils.toString(this.getClass().getResourceAsStream("/test.sd"))).container("default", new Application.Builder.Container().client("mbus://*/*", MockClient.class).documentProcessor(MockDispatchDocproc.class))))) {
Map<String, DocumentType> typeMap = app.application().getJDisc("jdisc").documentProcessing().getDocumentTypes();
assertNotNull(typeMap);
DocumentType docType = typeMap.get("test");
Document doc = new Document(docType, "id:foo:test::bar");
doc.setFieldValue("title", "hello");
assertEquals(DocumentProcessor.Progress.DONE, app.process(new DocumentPut(doc)));
MockClient client = (MockClient) app.getClientById(MockClient.class.getName());
assertNotNull(client);
assertEquals(1, client.getCounter());
MockDispatchDocproc docproc = (MockDispatchDocproc) app.getComponentById(MockDispatchDocproc.class.getName() + "@default");
assertNotNull(docproc);
assertEquals(1, docproc.getResponses().size());
assertEquals(200, docproc.getResponses().get(0).getStatus());
}
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class JDiscContainerDocprocTest method requireThatBasicDocumentProcessingWorks.
@Test
public void requireThatBasicDocumentProcessingWorks() throws Exception {
try (Application app = new ApplicationBuilder().servicesXml(getXML(CHAIN_NAME, Rot13DocumentProcessor.class.getCanonicalName())).documentType("music", DOCUMENT).build()) {
JDisc container = app.getJDisc("container");
DocumentProcessing docProc = container.documentProcessing();
DocumentType type = docProc.getDocumentTypes().get("music");
ChainRegistry<DocumentProcessor> chains = docProc.getChains();
assertTrue(chains.allComponentsById().containsKey(new ComponentId(CHAIN_NAME)));
Document doc = new Document(type, "doc:this:is:a:great:album");
doc.setFieldValue("title", "Great Album!");
com.yahoo.docproc.Processing processing;
DocumentProcessor.Progress progress;
DocumentPut put = new DocumentPut(doc);
processing = com.yahoo.docproc.Processing.of(put);
progress = docProc.process(ComponentSpecification.fromString(CHAIN_NAME), processing);
assertThat(progress, sameInstance(DocumentProcessor.Progress.DONE));
assertThat(doc.getFieldValue("title").toString(), equalTo("Terng Nyohz!"));
processing = com.yahoo.docproc.Processing.of(put);
progress = docProc.process(ComponentSpecification.fromString(CHAIN_NAME), processing);
assertThat(progress, sameInstance(DocumentProcessor.Progress.DONE));
assertThat(doc.getFieldValue("title").toString(), equalTo("Great Album!"));
}
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class GetSearcher method handleFieldFiltering.
private void handleFieldFiltering(GetResponse response, Result result, String fieldName, String contentType, boolean headersOnly) {
if (response.getDocumentHits().isEmpty()) {
result.hits().addError(ErrorMessage.createNotFound("Document not found, could not return field '" + fieldName + "'"));
return;
}
if (result.hits().getErrorHit() == null) {
Document doc = response.getDocumentHits().get(0).getDocument();
Field field = doc.getDataType().getField(fieldName);
boolean wrapXml = false;
if (field == null) {
result.hits().addError(ErrorMessage.createIllegalQuery("Field '" + fieldName + "' not found in document type"));
return;
}
FieldValue value = doc.getFieldValue(field);
// content will be null. We treat this as an error.
if (value == null) {
if (!field.isHeader() && headersOnly) {
// TODO(vekterli): make this work with field sets as well.
result.hits().addError(ErrorMessage.createInvalidQueryParameter("Field '" + fieldName + "' is located in document body, but headersonly " + "prevents it from being retrieved in " + doc.getId().toString()));
} else {
result.hits().addError(ErrorMessage.createNotFound("Field '" + fieldName + "' found in document type, but had " + "no content in " + doc.getId().toString()));
}
return;
}
String encoding = null;
if (field.getDataType() == DataType.RAW) {
if (contentType == null) {
contentType = "application/octet-stream";
}
encoding = "ISO-8859-1";
} else {
// By default, return field wrapped in a blanket of vespa XML
contentType = "text/xml";
wrapXml = true;
}
if (encoding == null) {
// Encoding doesn't matter for binary content, since we're always
// writing directly to the byte buffer and not through a charset
// encoder. Presumably, the client is intelligent enough to not
// attempt to UTF-8 decode binary data.
encoding = "UTF-8";
}
// Add hit now that we know there aren't any field errors. Otherwise,
// there would be both an error hit and a document hit in the result
response.addHitsToResult(result, false);
// Override Vespa XML template
result.getTemplating().setTemplates(new DocumentFieldTemplate(field, contentType, encoding, wrapXml));
}
// else: return with error hit, invoking regular Vespa XML error template
}
Aggregations