use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class JsonReaderTestCase method testAssignToWeightedSet.
@Test
public final void testAssignToWeightedSet() throws IOException {
DocumentUpdate doc = parseUpdate("{\"update\": \"id:unittest:testset::whee\"," + " \"fields\": { " + "\"actualset\": {" + " \"assign\": {" + " \"person\": 37," + " \"another person\": 41}}}}");
FieldUpdate x = doc.getFieldUpdate("actualset");
assertEquals(1, x.size());
AssignValueUpdate assign = (AssignValueUpdate) x.getValueUpdate(0);
WeightedSet<?> w = (WeightedSet<?>) assign.getValue();
assertEquals(2, w.size());
assertEquals(new Integer(37), w.get(new StringFieldValue("person")));
assertEquals(new Integer(41), w.get(new StringFieldValue("another person")));
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class ForEachExpression method doExecute.
@Override
protected void doExecute(final ExecutionContext ctx) {
FieldValue input = ctx.getValue();
if (input instanceof Array || input instanceof WeightedSet) {
FieldValue next = new MyConverter(ctx, exp).convert(input);
if (next == null) {
VerificationContext vctx = new VerificationContext(ctx);
vctx.setValue(input.getDataType()).execute(this);
next = vctx.getValue().createFieldValue();
}
ctx.setValue(next);
} else if (input instanceof Struct) {
ctx.setValue(new MyConverter(ctx, exp).convert(input));
} else {
throw new IllegalArgumentException("Expected Array, Struct or WeightedSet input, got " + input.getDataType().getName() + ".");
}
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class AddRemoveCreator method createAddsOrRemoves.
// yes, this suppresswarnings ugliness is by intention, the code relies on
// the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes", "unchecked" })
private static void createAddsOrRemoves(TokenBuffer buffer, Field field, FieldUpdate update, boolean isRemove) {
FieldValue container = field.getDataType().createFieldValue();
FieldUpdate singleUpdate;
int initNesting = buffer.nesting();
Preconditions.checkState(buffer.currentToken().isStructStart(), "Expected start of composite, got %s", buffer.currentToken());
if (container instanceof CollectionFieldValue) {
buffer.next();
DataType valueType = ((CollectionFieldValue) container).getDataType().getNestedType();
if (container instanceof WeightedSet) {
// these are objects with string keys (which are the nested
// types) and values which are the weight
WeightedSet weightedSet = (WeightedSet) container;
fillWeightedSetUpdate(buffer, initNesting, valueType, weightedSet);
if (isRemove) {
singleUpdate = FieldUpdate.createRemoveAll(field, weightedSet);
} else {
singleUpdate = FieldUpdate.createAddAll(field, weightedSet);
}
} else {
List<FieldValue> arrayContents = new ArrayList<>();
ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents);
if (buffer.currentToken() != JsonToken.END_ARRAY) {
throw new IllegalStateException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'.");
}
if (isRemove) {
singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents);
} else {
singleUpdate = FieldUpdate.createAddAll(field, arrayContents);
}
}
} else {
throw new UnsupportedOperationException("Trying to add or remove from a field of a type the reader does not know how to handle: " + container.getClass().getName());
}
expectCompositeEnd(buffer.currentToken());
update.addAll(singleUpdate);
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class CompositeReader method populateComposite.
// TODO createComposite is extremely similar to add/remove, refactor
// yes, this suppresswarnings ugliness is by intention, the code relies on the contracts in the builders
@SuppressWarnings({ "cast", "rawtypes" })
public static void populateComposite(TokenBuffer buffer, FieldValue fieldValue) {
JsonToken token = buffer.currentToken();
if ((token != JsonToken.START_OBJECT) && (token != JsonToken.START_ARRAY)) {
throw new IllegalArgumentException("Expected '[' or '{'. Got '" + token + "'.");
}
if (fieldValue instanceof CollectionFieldValue) {
DataType valueType = ((CollectionFieldValue) fieldValue).getDataType().getNestedType();
if (fieldValue instanceof WeightedSet) {
fillWeightedSet(buffer, valueType, (WeightedSet) fieldValue);
} else {
fillArray(buffer, (CollectionFieldValue) fieldValue, valueType);
}
} else if (fieldValue instanceof MapFieldValue) {
MapReader.fillMap(buffer, (MapFieldValue) fieldValue);
} else if (fieldValue instanceof StructuredFieldValue) {
StructReader.fillStruct(buffer, (StructuredFieldValue) fieldValue);
} else if (fieldValue instanceof TensorFieldValue) {
TensorReader.fillTensor(buffer, (TensorFieldValue) fieldValue);
} else {
throw new IllegalStateException("Has created a composite field" + " value the reader does not know how to handle: " + fieldValue.getClass().getName() + " This is a bug. token = " + token);
}
expectCompositeEnd(buffer.currentToken());
}
use of com.yahoo.document.datatypes.WeightedSet in project vespa by vespa-engine.
the class DocumentTestCase method validateCppDocNotMap.
@SuppressWarnings("unchecked")
public void validateCppDocNotMap(Document doc) throws IOException {
// in practice to validate v6 serialization
assertEquals("doc:serializetest:http://test.doc.id/", doc.getId().toString());
assertEquals(new IntegerFieldValue(5), doc.getFieldValue("intfield"));
assertEquals(new FloatFieldValue((float) -9.23), doc.getFieldValue("floatfield"));
assertEquals(new StringFieldValue("This is a string."), doc.getFieldValue("stringfield"));
assertEquals(new LongFieldValue(398420092938472983L), doc.getFieldValue("longfield"));
assertEquals(new DoubleFieldValue(98374532.398820d), doc.getFieldValue("doublefield"));
assertEquals(new StringFieldValue("http://this.is.a.test/"), doc.getFieldValue("urifield"));
// NOTE: The value really is unsigned 254, which becomes signed -2:
assertEquals(new ByteFieldValue(-2), doc.getFieldValue("bytefield"));
ByteBuffer raw = ByteBuffer.wrap("RAW DATA".getBytes());
assertEquals(new Raw(raw), doc.getFieldValue("rawfield"));
Document docindoc = (Document) doc.getFieldValue("docfield");
assertEquals(docMan.getDocumentType("docindoc"), docindoc.getDataType());
assertEquals(new DocumentId("doc:docindoc:http://embedded"), docindoc.getId());
Array<FloatFieldValue> array = (Array<FloatFieldValue>) doc.getFieldValue("arrayoffloatfield");
assertEquals(new FloatFieldValue(1.0f), array.get(0));
assertEquals(new FloatFieldValue(2.0f), array.get(1));
WeightedSet<StringFieldValue> wset = (WeightedSet<StringFieldValue>) doc.getFieldValue("wsfield");
assertEquals(new Integer(50), wset.get(new StringFieldValue("Weighted 0")));
assertEquals(new Integer(199), wset.get(new StringFieldValue("Weighted 1")));
}
Aggregations