Search in sources :

Example 81 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class ToLongTestCase method requireThatValueIsConverted.

@Test
public void requireThatValueIsConverted() {
    ExecutionContext ctx = new ExecutionContext(new SimpleTestAdapter());
    ctx.setValue(new StringFieldValue("69")).execute(new ToLongExpression());
    FieldValue val = ctx.getValue();
    assertTrue(val instanceof LongFieldValue);
    assertEquals(69L, ((LongFieldValue) val).getLong());
}
Also used : SimpleTestAdapter(com.yahoo.vespa.indexinglanguage.SimpleTestAdapter) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) LongFieldValue(com.yahoo.document.datatypes.LongFieldValue) Test(org.junit.Test)

Example 82 with FieldValue

use of com.yahoo.document.datatypes.FieldValue 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);
}
Also used : FieldUpdate(com.yahoo.document.update.FieldUpdate) ArrayList(java.util.ArrayList) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) DataType(com.yahoo.document.DataType) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 83 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class MapReader method createMapUpdate.

@SuppressWarnings({ "rawtypes", "unchecked" })
public static ValueUpdate createMapUpdate(TokenBuffer buffer, DataType currentLevel, FieldValue keyParent, FieldValue topLevelKey) {
    TokenBuffer.Token element = buffer.prefetchScalar(UPDATE_ELEMENT);
    if (UPDATE_ELEMENT.equals(buffer.currentName())) {
        buffer.next();
    }
    FieldValue key = keyTypeForMapUpdate(element, currentLevel);
    if (keyParent != null) {
        ((CollectionFieldValue) keyParent).add(key);
    }
    // match will always have element, and either match or action
    if (!UPDATE_MATCH.equals(buffer.currentName())) {
        // we have reached an action...
        if (topLevelKey == null) {
            return ValueUpdate.createMap(key, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName()));
        } else {
            return ValueUpdate.createMap(topLevelKey, readSingleUpdate(buffer, valueTypeForMapUpdate(currentLevel), buffer.currentName()));
        }
    } else {
        // next level of matching
        if (topLevelKey == null) {
            return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, key);
        } else {
            return createMapUpdate(buffer, valueTypeForMapUpdate(currentLevel), key, topLevelKey);
        }
    }
}
Also used : TokenBuffer(com.yahoo.document.json.TokenBuffer) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue) IntegerFieldValue(com.yahoo.document.datatypes.IntegerFieldValue) MapFieldValue(com.yahoo.document.datatypes.MapFieldValue)

Example 84 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class StructReader method fillStruct.

public static void fillStruct(TokenBuffer buffer, StructuredFieldValue parent) {
    // do note the order of initializing initNesting and token is relevant for empty docs
    int initNesting = buffer.nesting();
    buffer.next();
    while (buffer.nesting() >= initNesting) {
        Field f = getField(buffer, parent);
        try {
            FieldValue v = readSingleValue(buffer, f.getDataType());
            parent.setFieldValue(f, v);
            buffer.next();
        } catch (IllegalArgumentException e) {
            throw new JsonReaderException(f, e);
        }
    }
}
Also used : Field(com.yahoo.document.Field) JsonReaderException(com.yahoo.document.json.JsonReaderException) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) FieldValue(com.yahoo.document.datatypes.FieldValue)

Example 85 with FieldValue

use of com.yahoo.document.datatypes.FieldValue in project vespa by vespa-engine.

the class WeightedSetReader method iterateThroughWeightedSet.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void iterateThroughWeightedSet(TokenBuffer buffer, int initNesting, DataType valueType, WeightedSet weightedSet) {
    while (buffer.nesting() >= initNesting) {
        // XXX the keys are defined in the spec to always be represented as strings
        FieldValue v = valueType.createFieldValue(buffer.currentName());
        weightedSet.put(v, Integer.valueOf(buffer.currentText()));
        buffer.next();
    }
}
Also used : FieldValue(com.yahoo.document.datatypes.FieldValue)

Aggregations

FieldValue (com.yahoo.document.datatypes.FieldValue)106 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)69 Test (org.junit.Test)52 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)45 SimpleTestAdapter (com.yahoo.vespa.indexinglanguage.SimpleTestAdapter)30 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)26 Array (com.yahoo.document.datatypes.Array)20 LongFieldValue (com.yahoo.document.datatypes.LongFieldValue)20 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)18 StructuredFieldValue (com.yahoo.document.datatypes.StructuredFieldValue)14 Struct (com.yahoo.document.datatypes.Struct)13 DataType (com.yahoo.document.DataType)12 Document (com.yahoo.document.Document)12 CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)12 DoubleFieldValue (com.yahoo.document.datatypes.DoubleFieldValue)12 ByteFieldValue (com.yahoo.document.datatypes.ByteFieldValue)11 FloatFieldValue (com.yahoo.document.datatypes.FloatFieldValue)11 ByteArrayInputStream (java.io.ByteArrayInputStream)11 InputStream (java.io.InputStream)11 DocumentPut (com.yahoo.document.DocumentPut)10