Search in sources :

Example 36 with Document

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

the class VespaDocumentSerializerTestCase method incompressable_structs_are_serialized_without_buffer_size_overhead_bug.

@Test
public void incompressable_structs_are_serialized_without_buffer_size_overhead_bug() {
    CompressionFixture fixture = new CompressionFixture();
    Document doc = new Document(fixture.docType, "id:foo:map_of_structs::flarn");
    Struct nested = new Struct(fixture.nestedType);
    nested.setFieldValue("str", new StringFieldValue(CompressionFixture.COMPRESSABLE_STRING));
    MapFieldValue<StringFieldValue, Struct> map = new MapFieldValue<StringFieldValue, Struct>(fixture.mapType);
    // Only 1 struct added. Not enough redundant information that header struct containing map itself
    // can be compressed.
    map.put(new StringFieldValue("foo"), nested);
    doc.setFieldValue("map", map);
    GrowableByteBuffer buf = CompressionFixture.asSerialized(doc);
    // Explanation of arbitrary value: buffer copy bug meant that incompressable structs were all serialized
    // rounded up to 4096 bytes.
    assertTrue(buf.remaining() < 4096);
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Document(com.yahoo.document.Document) Struct(com.yahoo.document.datatypes.Struct) Test(org.junit.Test)

Example 37 with Document

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

the class JsonWriterTestCase method stringToArrayOfIntMapTest.

@Test
public final void stringToArrayOfIntMapTest() throws IOException {
    String docId = "id:unittest:testMapStringToArrayOfInt::whee";
    String fields = "{ \"actualMapStringToArrayOfInt\": { \"bamse\": [1, 2, 3] }}";
    Document doc = readDocumentFromJson(docId, fields);
    ObjectMapper m = new ObjectMapper();
    Map<?, ?> generated = m.readValue(JsonWriter.toByteArray(doc), Map.class);
    assertEquals(docId, generated.get("id"));
    // and from here on down there will be lots of unchecked casting and
    // other fun. This is OK here, because if the casts fail, the should and
    // will fail anyway
    Map<?, ?> inputMap = (Map<?, ?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualMapStringToArrayOfInt");
    Map<?, ?> generatedMap = (Map<?, ?>) ((Map<?, ?>) generated.get("fields")).get("actualMapStringToArrayOfInt");
    assertEquals(inputMap, generatedMap);
}
Also used : Document(com.yahoo.document.Document) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 38 with Document

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

the class JsonWriterTestCase method mapTest.

@Test
public void mapTest() throws IOException {
    String fields = "{ \"actualmap\": { \"nalle\": \"kalle\", \"tralle\": \"skalle\" }}";
    String docId = "id:unittest:testmap::whee";
    Document doc = readDocumentFromJson(docId, fields);
    ObjectMapper m = new ObjectMapper();
    Map<?, ?> generated = m.readValue(JsonWriter.toByteArray(doc), Map.class);
    assertEquals(docId, generated.get("id"));
    // and from here on down there will be lots of unchecked casting and
    // other fun. This is OK here, because if the casts fail, the should and
    // will fail anyway
    Map<?, ?> inputMap = (Map<?, ?>) m.readValue(Utf8.toBytes(fields), Map.class).get("actualmap");
    Map<?, ?> generatedMap = (Map<?, ?>) ((Map<?, ?>) generated.get("fields")).get("actualmap");
    assertEquals(inputMap, generatedMap);
}
Also used : Document(com.yahoo.document.Document) HashMap(java.util.HashMap) Map(java.util.Map) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 39 with Document

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

the class JsonWriterTestCase method readDocumentFromJson.

private Document readDocumentFromJson(String docId, String fields) throws IOException {
    InputStream rawDoc = new ByteArrayInputStream(asFeed(docId, fields));
    JsonReader r = new JsonReader(types, rawDoc, parserFactory);
    DocumentParseInfo raw = r.parseDocument().get();
    DocumentType docType = r.readDocumentType(raw.documentId);
    DocumentPut put = new DocumentPut(new Document(docType, raw.documentId));
    new VespaJsonDocumentReader().readPut(raw.fieldsBuffer, put);
    return put.getDocument();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) DocumentPut(com.yahoo.document.DocumentPut) DocumentType(com.yahoo.document.DocumentType) Document(com.yahoo.document.Document) DocumentParseInfo(com.yahoo.document.json.readers.DocumentParseInfo) VespaJsonDocumentReader(com.yahoo.document.json.readers.VespaJsonDocumentReader)

Example 40 with Document

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

the class JsonWriterTestCase method roundTripEquality.

private void roundTripEquality(final String docId, final String fields) throws IOException {
    Document doc = readDocumentFromJson(docId, fields);
    assertEqualJson(asDocument(docId, fields), JsonWriter.toByteArray(doc));
}
Also used : Document(com.yahoo.document.Document)

Aggregations

Document (com.yahoo.document.Document)109 Test (org.junit.Test)63 DocumentType (com.yahoo.document.DocumentType)41 DocumentPut (com.yahoo.document.DocumentPut)40 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)33 ByteArrayInputStream (java.io.ByteArrayInputStream)23 InputStream (java.io.InputStream)18 DocumentId (com.yahoo.document.DocumentId)16 DocumentParseInfo (com.yahoo.document.json.readers.DocumentParseInfo)16 VespaJsonDocumentReader (com.yahoo.document.json.readers.VespaJsonDocumentReader)16 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)13 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)13 HashMap (java.util.HashMap)13 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)12 FieldValue (com.yahoo.document.datatypes.FieldValue)12 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)12 DocumentOperation (com.yahoo.document.DocumentOperation)10 GrowableByteBuffer (com.yahoo.io.GrowableByteBuffer)10 DocumentUpdate (com.yahoo.document.DocumentUpdate)8 Array (com.yahoo.document.datatypes.Array)8