Search in sources :

Example 6 with DocumentType

use of com.yahoo.document.DocumentType 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");
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.yahoo.document.Document) Field(com.yahoo.docproc.Accesses.Field)

Example 7 with DocumentType

use of com.yahoo.document.DocumentType 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;
}
Also used : DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) Document(com.yahoo.document.Document) AnnotationType(com.yahoo.document.annotation.AnnotationType) Annotation(com.yahoo.document.annotation.Annotation) Struct(com.yahoo.document.datatypes.Struct) Array(com.yahoo.document.datatypes.Array) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType) SpanTree(com.yahoo.document.annotation.SpanTree)

Example 8 with DocumentType

use of com.yahoo.document.DocumentType in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappedDocStruct.

public void testMappedDocStruct() {
    StructDataType materialsStructType = new StructDataType("materialstype");
    materialsStructType.addField(new com.yahoo.document.Field("ceiling", DataType.STRING));
    materialsStructType.addField(new com.yahoo.document.Field("walls", DataType.STRING));
    DocumentType docType = new DocumentType("album");
    docType.addField("title", DataType.STRING);
    docType.addField("artist", DataType.STRING);
    StructDataType storeStructType = new StructDataType("storetype");
    storeStructType.addField(new com.yahoo.document.Field("name", DataType.STRING));
    storeStructType.addField(new com.yahoo.document.Field("city", DataType.STRING));
    storeStructType.addField(new com.yahoo.document.Field("materials", materialsStructType));
    docType.addField("store", storeStructType);
    Document doc = new Document(docType, new DocumentId("doc:map:test:1"));
    doc.setFieldValue("title", new StringFieldValue("Black Rock"));
    doc.setFieldValue("artist", new StringFieldValue("Joe Bonamassa"));
    Struct material = new Struct(materialsStructType);
    material.setFieldValue("ceiling", new StringFieldValue("wood"));
    material.setFieldValue("walls", new StringFieldValue("brick"));
    Struct store = new Struct(storeStructType);
    store.setFieldValue("name", new StringFieldValue("Platekompaniet"));
    store.setFieldValue("city", new StringFieldValue("Trondheim"));
    store.setFieldValue(storeStructType.getField("materials"), material);
    doc.setFieldValue(docType.getField("store"), store);
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("c", "store.city");
    fieldMap.put("w", "store.materials.walls");
    ProxyDocument mapped = new ProxyDocument(new TestDocumentProcessor1(), doc, fieldMap);
    assertEquals(new StringFieldValue("Trondheim"), mapped.getFieldValue("c"));
    assertEquals(new StringFieldValue("Black Rock"), mapped.getFieldValue("t"));
    assertEquals(new StringFieldValue("brick"), mapped.getFieldValue("w"));
    assertEquals(new StringFieldValue("brick"), material.getFieldValue("walls"));
    mapped.setFieldValue("c", new StringFieldValue("Steinkjer"));
    mapped.setFieldValue("w", new StringFieldValue("plaster"));
    assertEquals(new StringFieldValue("plaster"), mapped.getFieldValue("w"));
    assertEquals(new StringFieldValue("plaster"), material.getFieldValue("walls"));
    assertEquals(new StringFieldValue("Steinkjer"), store.getFieldValue("city"));
    assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
    assertEquals(new StringFieldValue("Steinkjer"), mapped.getFieldValue("c"));
    mapped.setFieldValue("c", new StringFieldValue("Levanger"));
    assertEquals(new StringFieldValue("Levanger"), store.getFieldValue("city"));
    assertEquals(new StringFieldValue("Levanger"), mapped.getFieldValue("c"));
    mapped.setFieldValue("c", mapped.getFieldValue("c") + "Kommune");
    assertEquals(new StringFieldValue("LevangerKommune"), mapped.getFieldValue("c"));
// mapped.set(mapped.getField("c"), mapped.get("c")+"Styre");
// assertEquals(new StringFieldValue("LevangerKommuneStyre"), mapped.getFieldValue("c"));
}
Also used : HashMap(java.util.HashMap) TestDocumentProcessor1(com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StructDataType(com.yahoo.document.StructDataType)

Example 9 with DocumentType

use of com.yahoo.document.DocumentType in project vespa by vespa-engine.

the class SchemaMappingAndAccessesTest method testMappedDocUpdateAPI.

public void testMappedDocUpdateAPI() {
    Document doc = getDoc();
    DocumentType type = doc.getDataType();
    DocumentUpdate dud = new DocumentUpdate(type, new DocumentId("doc:map:test:1"));
    FieldUpdate assignSingle = FieldUpdate.createAssign(type.getField("title"), new StringFieldValue("something"));
    Map<String, String> fieldMap = new HashMap<>();
    fieldMap.put("t", "title");
    fieldMap.put("a", "artist");
    ProxyDocumentUpdate pup = new ProxyDocumentUpdate(dud, fieldMap);
    pup.addFieldUpdate(assignSingle);
    assertEquals(pup.getFieldUpdates(), dud.getFieldUpdates());
    assertEquals(pup.getDocumentType(), dud.getDocumentType());
    assertEquals(pup.getFieldUpdate(new com.yahoo.document.Field("title")).size(), 1);
    assertEquals(pup.getFieldUpdate(0), dud.getFieldUpdate(0));
    assertEquals(pup.getFieldUpdate("title"), dud.getFieldUpdate("title"));
    assertEquals(pup.getId(), dud.getId());
    assertEquals(pup.getType(), dud.getType());
    assertEquals(pup.applyTo(doc), dud);
    assertEquals(doc.getFieldValue("title").getWrappedValue(), "something");
    assertEquals(pup, dud);
    assertEquals(pup.hashCode(), dud.hashCode());
    assertEquals(pup.toString(), dud.toString());
    assertEquals(pup.size(), dud.size());
    assertEquals(pup.getWrappedDocumentOperation().getId().toString(), "doc:map:test:1");
}
Also used : HashMap(java.util.HashMap) DocumentId(com.yahoo.document.DocumentId) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) DocumentUpdate(com.yahoo.document.DocumentUpdate) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FieldUpdate(com.yahoo.document.update.FieldUpdate)

Example 10 with DocumentType

use of com.yahoo.document.DocumentType in project vespa by vespa-engine.

the class DocumentScriptTestCase method newDocumentType.

private static DocumentType newDocumentType() {
    DocumentType type = new DocumentType("documentType");
    type.addField("documentField", DataType.STRING);
    type.addField("extraField", DataType.STRING);
    return type;
}
Also used : DocumentType(com.yahoo.document.DocumentType)

Aggregations

DocumentType (com.yahoo.document.DocumentType)98 Test (org.junit.Test)45 Document (com.yahoo.document.Document)41 DocumentPut (com.yahoo.document.DocumentPut)35 Field (com.yahoo.document.Field)24 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)24 DocumentId (com.yahoo.document.DocumentId)20 ByteArrayInputStream (java.io.ByteArrayInputStream)19 InputStream (java.io.InputStream)19 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)17 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)17 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)17 DocumentUpdate (com.yahoo.document.DocumentUpdate)15 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)15 StructDataType (com.yahoo.document.StructDataType)14 TensorDataType (com.yahoo.document.TensorDataType)12 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)11 ArrayDataType (com.yahoo.document.ArrayDataType)10 MapDataType (com.yahoo.document.MapDataType)10 ReferenceDataType (com.yahoo.document.ReferenceDataType)10