Search in sources :

Example 6 with DocumentId

use of com.yahoo.document.DocumentId 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 DocumentId

use of com.yahoo.document.DocumentId 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 DocumentId

use of com.yahoo.document.DocumentId 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 DocumentId

use of com.yahoo.document.DocumentId 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 DocumentId

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

the class DocumentParser method handleIdentLevelOne.

private void handleIdentLevelOne(DocumentParseInfo documentParseInfo, boolean docIdAndOperationIsSetExternally) throws IOException {
    JsonToken currentToken = parser.getCurrentToken();
    if (currentToken == JsonToken.VALUE_TRUE || currentToken == JsonToken.VALUE_FALSE) {
        try {
            if (CREATE_IF_NON_EXISTENT.equals(parser.getCurrentName())) {
                documentParseInfo.create = Optional.ofNullable(parser.getBooleanValue());
                return;
            }
        } catch (IOException e) {
            throw new RuntimeException("Got IO exception while parsing document", e);
        }
    }
    if ((currentToken == JsonToken.VALUE_TRUE || currentToken == JsonToken.VALUE_FALSE) && CREATE_IF_NON_EXISTENT.equals(parser.getCurrentName())) {
        documentParseInfo.create = Optional.of(currentToken == JsonToken.VALUE_TRUE);
    } else if (currentToken == JsonToken.VALUE_STRING && CONDITION.equals(parser.getCurrentName())) {
        documentParseInfo.condition = Optional.of(parser.getText());
    } else if (currentToken == JsonToken.VALUE_STRING) {
        // as well.
        if (!docIdAndOperationIsSetExternally) {
            documentParseInfo.operationType = operationNameToOperationType(parser.getCurrentName());
            documentParseInfo.documentId = new DocumentId(parser.getText());
        }
    }
}
Also used : DocumentId(com.yahoo.document.DocumentId) JsonToken(com.fasterxml.jackson.core.JsonToken) IOException(java.io.IOException)

Aggregations

DocumentId (com.yahoo.document.DocumentId)61 Test (org.junit.Test)28 DocumentType (com.yahoo.document.DocumentType)20 Document (com.yahoo.document.Document)16 DocumentPut (com.yahoo.document.DocumentPut)12 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)9 BucketId (com.yahoo.document.BucketId)7 DocumentRemove (com.yahoo.document.DocumentRemove)5 DocumentUpdate (com.yahoo.document.DocumentUpdate)5 BucketIdFactory (com.yahoo.document.BucketIdFactory)4 FieldUpdate (com.yahoo.document.update.FieldUpdate)4 DocumentOperation (com.yahoo.document.DocumentOperation)3 FastHit (com.yahoo.prelude.fastsearch.FastHit)3 Hit (com.yahoo.search.result.Hit)3 HashMap (java.util.HashMap)3 TestDocumentProcessor1 (com.yahoo.docproc.DocumentProcessingAbstractTestCase.TestDocumentProcessor1)2 Field (com.yahoo.document.Field)2 GlobalId (com.yahoo.document.GlobalId)2 StructDataType (com.yahoo.document.StructDataType)2 Struct (com.yahoo.document.datatypes.Struct)2