use of com.yahoo.document.Field 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.Field 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.Field 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.Field in project vespa by vespa-engine.
the class FieldSetTestCase method testContains.
@Test
public void testContains() throws Exception {
Field headerField = testDocType.getField("intattr");
Field bodyField = testDocType.getField("rawattr");
assertFalse(headerField.contains(testDocType.getField("byteattr")));
assertTrue(headerField.contains(testDocType.getField("intattr")));
assertFalse(headerField.contains(bodyField));
assertTrue(headerField.contains(new DocIdOnly()));
assertTrue(headerField.contains(new NoFields()));
assertFalse(headerField.contains(new AllFields()));
assertFalse(headerField.contains(new HeaderFields()));
assertFalse(headerField.contains(new BodyFields()));
assertFalse(new NoFields().contains(headerField));
assertFalse(new NoFields().contains(new AllFields()));
assertFalse(new NoFields().contains(new DocIdOnly()));
assertTrue(new AllFields().contains(new HeaderFields()));
assertTrue(new AllFields().contains(headerField));
assertTrue(new AllFields().contains(bodyField));
assertTrue(new AllFields().contains(new BodyFields()));
assertTrue(new AllFields().contains(new DocIdOnly()));
assertTrue(new AllFields().contains(new NoFields()));
assertTrue(new AllFields().contains(new AllFields()));
assertTrue(new DocIdOnly().contains(new NoFields()));
assertTrue(new DocIdOnly().contains(new DocIdOnly()));
assertFalse(new DocIdOnly().contains(headerField));
assertTrue(new HeaderFields().contains(headerField));
assertFalse(new HeaderFields().contains(bodyField));
assertTrue(new HeaderFields().contains(new DocIdOnly()));
assertTrue(new HeaderFields().contains(new NoFields()));
assertContains("[body]", "testdoc:rawattr");
assertContains("[header]", "testdoc:intattr");
assertNotContains("[header]", "testdoc:rawattr");
assertContains("testdoc:rawattr,intattr", "testdoc:intattr");
assertNotContains("testdoc:intattr", "testdoc:rawattr,intattr");
assertContains("testdoc:intattr,rawattr", "testdoc:rawattr,intattr");
assertError("nodoctype");
assertError("unknowndoctype:foo");
assertError("testdoc:unknownfield");
assertError("[badid]");
}
use of com.yahoo.document.Field in project vespa by vespa-engine.
the class SimpleDocumentAdapter method tryOutputType.
@Override
public void tryOutputType(Expression exp, String fieldName, DataType valueType) {
Field field = output.getDataType().getField(fieldName);
if (field == null) {
throw new VerificationException(exp, "Field '" + fieldName + "' not found.");
}
DataType fieldType = field.getDataType();
if (!fieldType.isAssignableFrom(valueType)) {
throw new VerificationException(exp, "Can not assign " + valueType.getName() + " to field '" + fieldName + "' which is " + fieldType.getName() + ".");
}
}
Aggregations