use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class StringTestCase method parseAnnotationType.
public void parseAnnotationType(AnnotationType t) {
System.out.println("Type Name: " + t.getName());
System.out.println("Type ID: " + t.getId());
DataType dt = t.getDataType();
String dataTypeStr;
if (dt == DataType.STRING) {
dataTypeStr = "String";
} else if (dt == DataType.INT) {
dataTypeStr = "Integer";
} else if (dt == DataType.URI) {
dataTypeStr = "URL";
} else {
dataTypeStr = "UNKNOWN";
}
System.out.println("Type DataType: " + dataTypeStr);
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class ArrayTestCase method testCreateIllegalArray.
public void testCreateIllegalArray() {
ArrayList<FieldValue> arrayList = new ArrayList<>();
arrayList.add(new StringFieldValue("foo"));
arrayList.add(new IntegerFieldValue(1000));
DataType stringType = new ArrayDataType(DataType.STRING);
try {
Array<FieldValue> illegalArray = new Array<>(stringType, arrayList);
fail("Expected an exception");
} catch (IllegalArgumentException e) {
assertEquals("FieldValue 1000 is not compatible with datatype " + "Array<string> (code: -1486737430).", e.getMessage());
}
DataType intType = new ArrayDataType(DataType.INT);
Array<IntegerFieldValue> intArray = new Array<>(intType);
intArray.add(new IntegerFieldValue(42));
Array<StringFieldValue> stringArray = new Array<>(stringType);
try {
stringArray.assign(intArray);
fail("Expected an exception");
} catch (IllegalArgumentException e) {
assertEquals("Incompatible data types. Got datatype int (code: 0)," + " expected datatype string (code: 2)", e.getMessage());
}
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class MapReader method fillMapFromArray.
@SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMapFromArray(TokenBuffer buffer, MapFieldValue parent) {
JsonToken token = buffer.currentToken();
int initNesting = buffer.nesting();
expectArrayStart(token);
token = buffer.next();
DataType keyType = parent.getDataType().getKeyType();
DataType valueType = parent.getDataType().getValueType();
while (buffer.nesting() >= initNesting) {
FieldValue key = null;
FieldValue value = null;
expectObjectStart(token);
token = buffer.next();
for (int i = 0; i < 2; ++i) {
if (MAP_KEY.equals(buffer.currentName())) {
key = readSingleValue(buffer, keyType);
} else if (MAP_VALUE.equals(buffer.currentName())) {
value = readSingleValue(buffer, valueType);
}
token = buffer.next();
}
Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
parent.put(key, value);
expectObjectEnd(token);
// array end or next entry
token = buffer.next();
}
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class MapReader method fillMapFromObject.
@SuppressWarnings({ "rawtypes", "cast", "unchecked" })
public static void fillMapFromObject(TokenBuffer buffer, MapFieldValue parent) {
JsonToken token = buffer.currentToken();
int initNesting = buffer.nesting();
expectObjectStart(token);
token = buffer.next();
DataType keyType = parent.getDataType().getKeyType();
DataType valueType = parent.getDataType().getValueType();
while (buffer.nesting() >= initNesting) {
FieldValue key = readAtomic(buffer.currentName(), keyType);
FieldValue value = readSingleValue(buffer, valueType);
Preconditions.checkState(key != null && value != null, "Missing key or value for map entry.");
parent.put(key, value);
token = buffer.next();
}
expectObjectEnd(token);
}
use of com.yahoo.document.DataType in project vespa by vespa-engine.
the class FieldUpdate method applyTo.
/**
* Applies this field update.
*
* @param doc the document to apply the update to
* @return a reference to itself
*/
public FieldUpdate applyTo(Document doc) {
for (ValueUpdate vupd : valueUpdates) {
DataType dataType = field.getDataType();
FieldValue oldValue = doc.getFieldValue(field);
boolean existed = (oldValue != null);
if (!existed) {
oldValue = dataType.createFieldValue();
}
FieldValue newValue = vupd.applyTo(oldValue);
if (newValue == null) {
if (existed) {
doc.removeFieldValue(field);
}
} else {
doc.setFieldValue(field, newValue);
}
}
return this;
}
Aggregations