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));
}
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));
}
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());
}
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);
}
}
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;
}
Aggregations