use of com.yahoo.document.Document in project vespa by vespa-engine.
the class OperationHandlerImpl method get.
@Override
public Optional<String> get(RestUri restUri, Optional<String> fieldSet) throws RestApiException {
SyncSession syncSession = syncSessions.alloc();
setRoute(syncSession, Optional.empty());
try {
DocumentId id = new DocumentId(restUri.generateFullId());
final Document document = syncSession.get(id, fieldSet.orElse(restUri.getDocumentType() + ":[document]"), DocumentProtocol.Priority.NORMAL_1);
if (document == null) {
return Optional.empty();
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonWriter jsonWriter = new JsonWriter(outputStream);
jsonWriter.write(document);
return Optional.of(outputStream.toString(StandardCharsets.UTF_8.name()));
} catch (Exception e) {
throw new RestApiException(Response.createErrorResponse(500, ExceptionUtils.getStackTrace(e), restUri, RestUri.apiErrorCodes.UNSPECIFIED));
} finally {
syncSessions.free(syncSession);
}
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class DummyVisitorSession method addDocumentReply.
public void addDocumentReply(String docId) {
Document replyDoc = new Document(documentType, docId);
autoReplyMessages.add(new PutDocumentMessage(new DocumentPut(replyDoc)));
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class MockReader method read.
@Override
public void read(Operation operation) throws Exception {
if (finished) {
return;
}
byte whatToDo = stream.getNextOperation();
DocumentId id = new DocumentId("id:banana:banana::doc1");
DocumentType docType = new DocumentType("banana");
switch(whatToDo) {
case 0:
finished = true;
break;
case 1:
Document doc = new Document(docType, id);
operation.setDocument(doc);
break;
case 2:
operation.setRemove(id);
break;
case 3:
operation.setDocumentUpdate(new DocumentUpdate(docType, id));
break;
case 4:
throw new RuntimeException("boom");
}
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method testMappedDocAPI.
public void testMappedDocAPI() {
Document doc = getDoc();
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("t", "title");
fieldMap.put("a", "artist");
ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
assertEquals(mapped.getFieldValue("title"), doc.getFieldValue("title"));
assertEquals(mapped.getFieldValue(new com.yahoo.document.Field("title")), doc.getFieldValue((new com.yahoo.document.Field("title"))));
mapped.setFieldValue("title", "foo");
assertEquals(doc.getFieldValue("title").getWrappedValue(), "foo");
assertEquals(mapped.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
assertEquals(doc, mapped);
assertEquals(doc.toString(), mapped.toString());
assertEquals(doc.hashCode(), mapped.hashCode());
assertEquals(doc.clone(), mapped.clone());
assertEquals(doc.iterator().hasNext(), mapped.iterator().hasNext());
assertEquals(doc.getId(), mapped.getId());
assertEquals(doc.getDataType(), mapped.getDataType());
mapped.setLastModified(56l);
assertEquals(doc.getLastModified(), (Long) 56l);
assertEquals(mapped.getLastModified(), (Long) 56l);
mapped.setId(new DocumentId("doc:map:test:2"));
assertEquals(mapped.getId().toString(), "doc:map:test:2");
assertEquals(doc.getId().toString(), "doc:map:test:2");
assertEquals(doc.getHeader(), mapped.getHeader());
assertEquals(doc.getBody(), mapped.getBody());
assertEquals(doc.getSerializedSize(), mapped.getSerializedSize());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayOutputStream bos2 = new ByteArrayOutputStream();
mapped.serialize(bos);
doc.serialize(bos2);
assertEquals(bos.toString(), bos2.toString());
assertEquals(mapped.toXml(), doc.toXml());
assertEquals(mapped.getFieldCount(), doc.getFieldCount());
assertTrue(mapped.getDocument() == doc);
mapped.clear();
assertNull(mapped.getFieldValue("title"));
assertNull(doc.getFieldValue("title"));
mapped.setDataType(new DocumentType("newType"));
assertEquals(doc.getDataType().getName(), "newType");
}
use of com.yahoo.document.Document in project vespa by vespa-engine.
the class SchemaMappingAndAccessesTest method getDoc.
private Document getDoc() {
DocumentType type = new DocumentType("album");
AnnotationType personType = new AnnotationType("person");
Annotation person = new Annotation(personType);
type.addField("title", DataType.STRING);
type.addField("artist", DataType.STRING);
type.addField("guitarist", DataType.STRING);
type.addField("year", DataType.INT);
type.addField("labels", DataType.getArray(DataType.STRING));
Document doc = new Document(type, new DocumentId("doc:map:test:1"));
doc.setFieldValue("title", new StringFieldValue("Black Rock"));
StringFieldValue joe = new StringFieldValue("Joe Bonamassa");
joe.setSpanTree(new SpanTree("mytree").annotate(person));
doc.setFieldValue("artist", joe);
doc.setFieldValue("year", new IntegerFieldValue(2010));
Array<StringFieldValue> labels = new Array<>(type.getField("labels").getDataType());
labels.add(new StringFieldValue("audun"));
labels.add(new StringFieldValue("tylden"));
doc.setFieldValue("labels", labels);
StructDataType personStructType = new StructDataType("artist");
personStructType.addField(new com.yahoo.document.Field("firstname", DataType.STRING));
personStructType.addField(new com.yahoo.document.Field("lastname", DataType.STRING));
type.addField("listeners", DataType.getArray(personStructType));
Array<Struct> listeners = new Array<>(type.getField("listeners").getDataType());
Struct listenerOne = new Struct(personStructType);
listenerOne.setFieldValue("firstname", new StringFieldValue("per"));
listenerOne.setFieldValue("lastname", new StringFieldValue("olsen"));
Struct listenerTwo = new Struct(personStructType);
listenerTwo.setFieldValue("firstname", new StringFieldValue("anders"));
listenerTwo.setFieldValue("lastname", new StringFieldValue("and"));
listeners.add(listenerOne);
listeners.add(listenerTwo);
doc.setFieldValue("listeners", listeners);
return doc;
}
Aggregations