use of com.yahoo.document.Field in project vespa by vespa-engine.
the class StructTestCase method sortingOrderIndependentOfValueInsertionOrder.
@Test
public void sortingOrderIndependentOfValueInsertionOrder() {
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));
a.setFieldValue("flt", new DoubleFieldValue(45.6));
a.setFieldValue("str", new StringFieldValue("hello world"));
Struct b = new Struct(type);
b.setFieldValue("str", new StringFieldValue("hello world"));
b.setFieldValue("flt", new DoubleFieldValue(45.6));
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("int", new IntegerFieldValue(122));
assertTrue(a.compareTo(b) > 0);
assertTrue(b.compareTo(a) < 0);
assertFalse(a.equals(b));
assertFalse(b.equals(a));
}
use of com.yahoo.document.Field in project vespa by vespa-engine.
the class VespaDocumentDeserializer42 method read.
public void read(FieldBase field, Document doc) {
// Verify that we have correct version
version = getShort(null);
if (version < 6 || version > Document.SERIALIZED_VERSION) {
throw new DeserializationException("Unknown version " + version + ", expected " + Document.SERIALIZED_VERSION + ".");
}
int dataLength = 0;
int dataPos = 0;
if (version < 7) {
// Total document size.. Ignore
getInt2_4_8Bytes(null);
} else {
dataLength = getInt(null);
dataPos = position();
}
doc.setId(readDocumentId());
Byte content = getByte(null);
doc.setDataType(readDocumentType());
if ((content & 0x2) != 0) {
doc.getHeader().deserialize(new Field("header"), this);
}
if ((content & 0x4) != 0) {
doc.getBody().deserialize(new Field("body"), this);
} else if (body != null) {
GrowableByteBuffer header = getBuf();
setBuf(body);
body = null;
doc.getBody().deserialize(new Field("body"), this);
body = getBuf();
setBuf(header);
}
if (version < 8) {
int crcVal = getInt(null);
}
if (version > 6) {
if (dataLength != (position() - dataPos)) {
throw new DeserializationException("Length mismatch");
}
}
}
use of com.yahoo.document.Field in project vespa by vespa-engine.
the class VespaXMLFieldReader method read.
public void read(FieldBase field, Struct value) {
try {
boolean base64 = isBase64EncodedElement(reader);
boolean foundField = false;
StringBuilder positionBuilder = null;
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamReader.START_ELEMENT) {
Field structField = value.getField(reader.getName().toString());
if (structField == null) {
throw newDeserializeException(field, "Field " + reader.getName() + " not found.");
}
FieldValue fieldValue = structField.getDataType().createFieldValue();
fieldValue.deserialize(structField, this);
value.setFieldValue(structField, fieldValue);
skipToEnd(structField.getName());
foundField = true;
} else if (type == XMLStreamReader.CHARACTERS) {
if (foundField) {
continue;
}
// The text of an XML element may be output using 1-n CHARACTERS
// events, so we have to buffer up until the end of the element to
// ensure we get everything.
String chars = reader.getText();
if (positionBuilder == null) {
positionBuilder = new StringBuilder(chars);
} else {
positionBuilder.append(chars);
}
} else if (type == XMLStreamReader.END_ELEMENT) {
if (positionBuilder != null) {
assignPositionFieldFromStringIfNonEmpty(value, positionBuilder.toString(), base64);
}
break;
}
}
} catch (XMLStreamException e) {
throw newException(field, e);
}
}
use of com.yahoo.document.Field in project vespa by vespa-engine.
the class VespaXMLFieldReader method read.
public void read(FieldBase field, Document document) {
try {
// workaround for documents inside array <item>
if (reader.getEventType() != XMLStreamReader.START_ELEMENT || !"document".equals(reader.getName().toString())) {
while (reader.hasNext()) {
if (reader.getEventType() == XMLStreamReader.START_ELEMENT && "document".equals(reader.getName().toString())) {
break;
}
reader.next();
}
}
// First fetch attributes.
String typeName = null;
for (int i = 0; i < reader.getAttributeCount(); i++) {
final String attributeName = reader.getAttributeName(i).toString();
if ("documentid".equals(attributeName) || "id".equals(attributeName)) {
document.setId(new DocumentId(reader.getAttributeValue(i)));
} else if ("documenttype".equals(attributeName) || "type".equals(attributeName)) {
typeName = reader.getAttributeValue(i);
} else if ("condition".equals(attributeName)) {
condition = Optional.of(reader.getAttributeValue(i));
}
}
if (document.getId() != null) {
if (field == null) {
field = new FieldBase(document.getId().toString());
}
}
DocumentType doctype = docTypeManager.getDocumentType(typeName);
if (doctype == null) {
throw newDeserializeException(field, "Must specify an existing document type, not '" + typeName + "'");
} else {
document.setDataType(doctype);
}
// Then fetch fields
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamReader.START_ELEMENT) {
Field f = doctype.getField(reader.getName().toString());
if (f == null) {
throw newDeserializeException(field, "Field " + reader.getName() + " not found.");
}
FieldValue fv = f.getDataType().createFieldValue();
fv.deserialize(f, this);
document.setFieldValue(f, fv);
skipToEnd(f.getName());
} else if (type == XMLStreamReader.END_ELEMENT) {
return;
}
}
} catch (XMLStreamException e) {
throw newException(field, e);
}
}
use of com.yahoo.document.Field in project vespa by vespa-engine.
the class VespaJsonDocumentReader method addFieldUpdates.
private void addFieldUpdates(DocumentUpdate update, TokenBuffer buffer, String fieldName) {
Field field = update.getType().getField(fieldName);
int localNesting = buffer.nesting();
FieldUpdate fieldUpdate = FieldUpdate.create(field);
buffer.next();
while (localNesting <= buffer.nesting()) {
switch(buffer.currentName()) {
case UPDATE_REMOVE:
createRemoves(buffer, field, fieldUpdate);
break;
case UPDATE_ADD:
createAdds(buffer, field, fieldUpdate);
break;
case UPDATE_MATCH:
fieldUpdate.addValueUpdate(createMapUpdate(buffer, field));
break;
default:
String action = buffer.currentName();
fieldUpdate.addValueUpdate(readSingleUpdate(buffer, field.getDataType(), action));
}
buffer.next();
}
update.addFieldUpdate(fieldUpdate);
}
Aggregations