Search in sources :

Example 1 with CollectionFieldValue

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

the class AddValueUpdate method applyTo.

@Override
public FieldValue applyTo(FieldValue val) {
    if (val instanceof WeightedSet) {
        WeightedSet wset = (WeightedSet) val;
        wset.put(value, weight);
    } else if (val instanceof CollectionFieldValue) {
        CollectionFieldValue fval = (CollectionFieldValue) val;
        fval.add(value);
    } else {
        throw new IllegalStateException("Cannot add " + value + " to field of type " + val.getClass().getName());
    }
    return val;
}
Also used : CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) WeightedSet(com.yahoo.document.datatypes.WeightedSet)

Example 2 with CollectionFieldValue

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

the class RemoveValueUpdate method applyTo.

@Override
public FieldValue applyTo(FieldValue fval) {
    if (fval instanceof CollectionFieldValue) {
        CollectionFieldValue val = (CollectionFieldValue) fval;
        val.removeValue(value);
    }
    return fval;
}
Also used : CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue)

Example 3 with CollectionFieldValue

use of com.yahoo.document.datatypes.CollectionFieldValue 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 4 with CollectionFieldValue

use of com.yahoo.document.datatypes.CollectionFieldValue 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 5 with CollectionFieldValue

use of com.yahoo.document.datatypes.CollectionFieldValue 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());
}
Also used : MapFieldValue(com.yahoo.document.datatypes.MapFieldValue) TensorFieldValue(com.yahoo.document.datatypes.TensorFieldValue) StructuredFieldValue(com.yahoo.document.datatypes.StructuredFieldValue) CollectionFieldValue(com.yahoo.document.datatypes.CollectionFieldValue) DataType(com.yahoo.document.DataType) JsonToken(com.fasterxml.jackson.core.JsonToken) WeightedSet(com.yahoo.document.datatypes.WeightedSet) WeightedSetReader.fillWeightedSet(com.yahoo.document.json.readers.WeightedSetReader.fillWeightedSet)

Aggregations

CollectionFieldValue (com.yahoo.document.datatypes.CollectionFieldValue)5 WeightedSet (com.yahoo.document.datatypes.WeightedSet)3 DataType (com.yahoo.document.DataType)2 FieldValue (com.yahoo.document.datatypes.FieldValue)2 MapFieldValue (com.yahoo.document.datatypes.MapFieldValue)2 JsonToken (com.fasterxml.jackson.core.JsonToken)1 IntegerFieldValue (com.yahoo.document.datatypes.IntegerFieldValue)1 StructuredFieldValue (com.yahoo.document.datatypes.StructuredFieldValue)1 TensorFieldValue (com.yahoo.document.datatypes.TensorFieldValue)1 TokenBuffer (com.yahoo.document.json.TokenBuffer)1 WeightedSetReader.fillWeightedSet (com.yahoo.document.json.readers.WeightedSetReader.fillWeightedSet)1 FieldUpdate (com.yahoo.document.update.FieldUpdate)1 ArrayList (java.util.ArrayList)1