Search in sources :

Example 31 with StructDataType

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

the class StructTestCase method sortingOrderDependsOnTypeFieldOrderWhenNonEqual.

@Test
public void sortingOrderDependsOnTypeFieldOrderWhenNonEqual() {
    StructDataType type = new StructDataType("test");
    type.addField(new Field("int", DataType.INT));
    type.addField(new Field("intnotset", DataType.INT));
    type.addField(new Field("flt", DataType.FLOAT));
    type.addField(new Field("str", DataType.STRING));
    Struct a = new Struct(type);
    a.setFieldValue("int", new IntegerFieldValue(123));
    a.setFieldValue("flt", new DoubleFieldValue(45.6));
    Struct b = new Struct(type);
    b.setFieldValue("int", new IntegerFieldValue(123));
    b.setFieldValue("str", new StringFieldValue("hello world"));
    // a sorts before b as it has flt set which occurs before str in the type
    assertTrue(a.compareTo(b) < 0);
    assertTrue(b.compareTo(a) > 0);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 32 with StructDataType

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

the class StructTestCase method sortingFirstOrderedByNumberOfSetFields.

@Test
public void sortingFirstOrderedByNumberOfSetFields() {
    StructDataType type = new StructDataType("test");
    type.addField(new Field("int", DataType.INT));
    type.addField(new Field("flt", DataType.FLOAT));
    type.addField(new Field("str", DataType.STRING));
    Struct a = new Struct(type);
    a.setFieldValue("int", new IntegerFieldValue(123));
    Struct b = new Struct(type);
    assertTrue(b.compareTo(a) < 0);
    assertTrue(a.compareTo(b) > 0);
    assertEquals(0, b.compareTo(b));
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
    b.setFieldValue("int", new IntegerFieldValue(123));
    assertEquals(0, a.compareTo(b));
    assertEquals(0, b.compareTo(a));
    assertTrue(a.equals(b));
    assertTrue(b.equals(a));
    b.setFieldValue("str", new StringFieldValue("hello world"));
    assertTrue(b.compareTo(a) > 0);
    assertTrue(a.compareTo(b) < 0);
    assertFalse(a.equals(b));
    assertFalse(b.equals(a));
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 33 with StructDataType

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

the class StructTestCase method testCompareToDoesNotMutateStateBug6394548.

@Test
public void testCompareToDoesNotMutateStateBug6394548() {
    StructDataType type = new StructDataType("test");
    // NOTE: non-increasing ID order!
    type.addField(new Field("int", 2, DataType.INT, true));
    type.addField(new Field("flt", 1, DataType.FLOAT, true));
    type.addField(new Field("str", 0, DataType.STRING, true));
    Struct a = new Struct(type);
    a.setFieldValue("int", new IntegerFieldValue(123));
    a.setFieldValue("flt", new DoubleFieldValue(45.6));
    a.setFieldValue("str", new StringFieldValue("hello world"));
    Struct b = new Struct(type);
    b.setFieldValue("int", new IntegerFieldValue(100));
    b.setFieldValue("flt", new DoubleFieldValue(45.6));
    b.setFieldValue("str", new StringFieldValue("hello world"));
    String xmlBefore = a.toXml();
    int hashBefore = a.hashCode();
    assertEquals(1, a.compareTo(b));
    assertEquals(xmlBefore, a.toXml());
    assertEquals(hashBefore, a.hashCode());
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) Test(org.junit.Test)

Example 34 with StructDataType

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

the class StructTestCase method testSetGetAggregateTypes.

@Test
public void testSetGetAggregateTypes() throws Exception {
    StructDataType type = new StructDataType("teststr");
    type.addField(new Field("intarray", DataType.getArray(DataType.INT)));
    type.addField(new Field("strws", DataType.getWeightedSet(DataType.STRING)));
    Struct struct = new Struct(type);
    {
        // TEST USING OUR IMPLEMENTATION OF LIST
        Array integerArray = new Array(type.getField("intarray").getDataType());
        integerArray.add(new IntegerFieldValue(5));
        integerArray.add(new IntegerFieldValue(10));
        assertEquals(2, integerArray.size());
        struct.setFieldValue("intarray", integerArray);
        assertEquals(2, integerArray.size());
        List outList = (List) struct.getFieldValue("intarray");
        integerArray.add(new IntegerFieldValue(322));
        integerArray.add(new IntegerFieldValue(453));
        assertEquals(integerArray, outList);
        assertSame(integerArray, outList);
        assertEquals(4, integerArray.size());
        Array anotherArray = new Array(type.getField("intarray").getDataType());
        anotherArray.add(new IntegerFieldValue(5324));
        Object o = struct.setFieldValue("intarray", anotherArray);
        assertEquals(integerArray, o);
        assertSame(integerArray, o);
        outList = (List) struct.getFieldValue("intarray");
        assertFalse(integerArray.equals(outList));
        assertEquals(anotherArray, outList);
        assertSame(anotherArray, outList);
    }
    {
        WeightedSet<StringFieldValue> strWs = new WeightedSet<>(type.getField("strws").getDataType());
        strWs.put(new StringFieldValue("banana"), 10);
        strWs.add(new StringFieldValue("apple"));
        assertEquals(2, strWs.size());
        Object o = struct.setFieldValue("strws", strWs);
        assertNull(o);
        assertEquals(2, strWs.size());
        WeightedSet<StringFieldValue> outWs = (WeightedSet<StringFieldValue>) struct.getFieldValue("strws");
        strWs.add(new StringFieldValue("poison"));
        strWs.put(new StringFieldValue("pie"), 599);
        assertEquals(strWs, outWs);
        assertSame(strWs, outWs);
        assertEquals(4, strWs.size());
        WeightedSet anotherWs = new WeightedSet(type.getField("strws").getDataType());
        anotherWs.add(new StringFieldValue("be bop"));
        o = struct.setFieldValue("strws", anotherWs);
        assertEquals(strWs, o);
        assertSame(strWs, o);
        outWs = (WeightedSet<StringFieldValue>) struct.getFieldValue("strws");
        System.out.println("OutWS " + outWs);
        System.out.println("StrWS " + strWs);
        assertFalse(strWs.equals(outWs));
        assertEquals(anotherWs, outWs);
        assertSame(anotherWs, outWs);
    }
}
Also used : Field(com.yahoo.document.Field) StructDataType(com.yahoo.document.StructDataType) List(java.util.List) Test(org.junit.Test)

Example 35 with StructDataType

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

the class VespaDocumentDeserializer42 method read.

public void read(FieldBase fieldDef, Struct s) {
    s.setVersion(version);
    int startPos = position();
    if (version < 6) {
        throw new DeserializationException("Illegal document serialization version " + version);
    }
    int dataSize;
    if (version < 7) {
        long rSize = getInt2_4_8Bytes(null);
        // TODO: Look into how to support data segments larger than INT_MAX bytes
        if (rSize > Integer.MAX_VALUE) {
            throw new DeserializationException("Raw size of data block is too large.");
        }
        dataSize = (int) rSize;
    } else {
        dataSize = getInt(null);
    }
    byte comprCode = getByte(null);
    CompressionType compression = CompressionType.valueOf(comprCode);
    int uncompressedSize = 0;
    if (compression != CompressionType.NONE && compression != CompressionType.INCOMPRESSIBLE) {
        // uncompressedsize (full size of FIELDS only, after decompression)
        long pSize = getInt2_4_8Bytes(null);
        // TODO: Look into how to support data segments larger than INT_MAX bytes
        if (pSize > Integer.MAX_VALUE) {
            throw new DeserializationException("Uncompressed size of data block is too large.");
        }
        uncompressedSize = (int) pSize;
    }
    int numberOfFields = getInt1_4Bytes(null);
    List<Tuple2<Integer, Long>> fieldIdsAndLengths = new ArrayList<>(numberOfFields);
    for (int i = 0; i < numberOfFields; ++i) {
        // id, length (length only used for unknown fields
        fieldIdsAndLengths.add(new Tuple2<>(getInt1_4Bytes(null), getInt2_4_8Bytes(null)));
    }
    // save a reference to the big buffer we're reading from:
    GrowableByteBuffer bigBuf = buf;
    if (version < 7) {
        // In V6 and earlier, the length included the header.
        int headerSize = position() - startPos;
        dataSize -= headerSize;
    }
    byte[] destination = compressor.decompress(compression, getBuf().array(), position(), uncompressedSize, Optional.of(dataSize));
    // set position in original buffer to after data
    position(position() + dataSize);
    // for a while: deserialize from this buffer instead:
    buf = GrowableByteBuffer.wrap(destination);
    s.clear();
    StructDataType type = s.getDataType();
    for (int i = 0; i < numberOfFields; ++i) {
        Field structField = type.getField(fieldIdsAndLengths.get(i).first, version);
        if (structField == null) {
            // ignoring unknown field:
            position(position() + fieldIdsAndLengths.get(i).second.intValue());
        } else {
            int posBefore = position();
            FieldValue value = structField.getDataType().createFieldValue();
            value.deserialize(structField, this);
            s.setFieldValue(structField, value);
            // jump to beginning of next field:
            position(posBefore + fieldIdsAndLengths.get(i).second.intValue());
        }
    }
    // restore the original buffer
    buf = bigBuf;
}
Also used : ArrayList(java.util.ArrayList) GrowableByteBuffer(com.yahoo.io.GrowableByteBuffer) Field(com.yahoo.document.Field) Tuple2(com.yahoo.collections.Tuple2) StructDataType(com.yahoo.document.StructDataType) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) FloatFieldValue(com.yahoo.document.datatypes.FloatFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) ReferenceFieldValue(com.yahoo.document.datatypes.ReferenceFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) ByteFieldValue(com.yahoo.document.datatypes.ByteFieldValue) DoubleFieldValue(com.yahoo.document.datatypes.DoubleFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) PredicateFieldValue(com.yahoo.document.datatypes.PredicateFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) CompressionType(com.yahoo.compress.CompressionType)

Aggregations

StructDataType (com.yahoo.document.StructDataType)44 Field (com.yahoo.document.Field)37 Test (org.junit.Test)27 Struct (com.yahoo.document.datatypes.Struct)9 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)8 DocumentType (com.yahoo.document.DocumentType)6 Array (com.yahoo.document.datatypes.Array)5 FieldValue (com.yahoo.document.datatypes.FieldValue)5 MapDataType (com.yahoo.document.MapDataType)4 NewDocumentType (com.yahoo.documentmodel.NewDocumentType)4 DataType (com.yahoo.document.DataType)3 DocumentTypeManager (com.yahoo.document.DocumentTypeManager)3 WeightedSetDataType (com.yahoo.document.WeightedSetDataType)3 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)3 ArrayList (java.util.ArrayList)3 ArrayDataType (com.yahoo.document.ArrayDataType)2 CollectionDataType (com.yahoo.document.CollectionDataType)2 Document (com.yahoo.document.Document)2 DocumentId (com.yahoo.document.DocumentId)2 ReferenceDataType (com.yahoo.document.ReferenceDataType)2