@Override public FieldPath buildFieldPath(String remainFieldName) { return MapDataType.buildFieldPath(remainFieldName, getNestedType(), DataType.INT); }
private void replaceTemporaryTypesInWeightedSet(WeightedSetDataType weightedSetDataType, List<DataType> seenStructs) { if (weightedSetDataType.getNestedType() instanceof TemporaryDataType) { weightedSetDataType.setNestedType(getDataType(weightedSetDataType.getNestedType().getCode(), "")); } else { replaceTemporaryTypes(weightedSetDataType.getNestedType(), seenStructs); } }
private static FieldValue keyTypeForMapUpdate(TokenBuffer.Token element, DataType expectedType) { FieldValue v; if (expectedType instanceof ArrayDataType) { v = new IntegerFieldValue(Integer.valueOf(element.text)); } else if (expectedType instanceof WeightedSetDataType) { v = ((WeightedSetDataType) expectedType).getNestedType().createFieldValue(element.text); } else if (expectedType instanceof MapDataType) { v = ((MapDataType) expectedType).getKeyType().createFieldValue(element.text); } else { throw new IllegalArgumentException("Container type " + expectedType + " not supported for match update."); } return v; } }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> value) { try { while (reader.hasNext()) { int type = reader.next(); if (type == XMLStreamReader.START_ELEMENT) { if ("item".equals(reader.getName().toString())) { FieldValue fv = value.getDataType().getNestedType().createFieldValue(); int weight = 1; for (int i = 0; i < reader.getAttributeCount(); i++) { if ("weight".equals(reader.getAttributeName(i).toString())) { weight = Integer.parseInt(reader.getAttributeValue(i)); } } deserializeFieldValue(field, fv); // noinspection unchecked value.put((T)fv, weight); skipToEnd("item"); } else { throw newDeserializeException(field, "Illegal tag " + reader.getName() + " expected 'item'"); } } else if (type == XMLStreamReader.END_ELEMENT) { return; } } } catch (XMLStreamException e) { throw newException(field, e); } }
@Override protected void checkCompatibility(DataType fieldType) { if (fieldType instanceof ArrayDataType) { if (!(value instanceof IntegerFieldValue)) { throw new IllegalArgumentException("Expected integer, got " + value.getClass().getName() + "."); } update.checkCompatibility(((ArrayDataType)fieldType).getNestedType()); } else if (fieldType instanceof WeightedSetDataType) { ((WeightedSetDataType)fieldType).getNestedType().createFieldValue().assign(value); update.checkCompatibility(DataType.INT); } else if (fieldType instanceof StructuredDataType) { if (!(value instanceof StringFieldValue)) { throw new IllegalArgumentException("Expected string, got " + value.getClass().getName() + "."); } Field field = ((StructuredDataType)fieldType).getField(((StringFieldValue)value).getString()); if (field == null) { throw new IllegalArgumentException("Field '" + value + "' not found."); } update.checkCompatibility(field.getDataType()); } else { throw new UnsupportedOperationException("Field type " + fieldType.getName() + " not supported."); } }
/** * Reserve space for this amount of keys in order to avoid resizing */ public void clearAndReserve(int count) { map = new MapFieldValue<>(new MapDataType(getDataType().getNestedType(), DataType.INT), count); }
private void assertConsistentTypes(SummaryField existing, SummaryField seen) { if (existing.getDataType() instanceof WeightedSetDataType && seen.getDataType() instanceof WeightedSetDataType && ((WeightedSetDataType)existing.getDataType()).getNestedType().equals(((WeightedSetDataType)seen.getDataType()).getNestedType())) return; // Disregard create-if-nonexistent and create-if-zero distinction if ( ! compatibleTypes(seen.getDataType(), existing.getDataType())) throw new IllegalArgumentException(existing.toLocateString() + " is inconsistent with " + seen.toLocateString() + ": All declarations of the same summary field must have the same type"); }
private void buildConfig(WeightedSetDataType type, DocumenttypesConfig.Documenttype.Datatype.Builder dataTypeBuilder, DocumenttypesConfig.Documenttype.Builder documentBuilder, Set<Integer> built) { dataTypeBuilder.type(DocumenttypesConfig.Documenttype.Datatype.Type.Enum.WSET). wset(new DocumenttypesConfig.Documenttype.Datatype.Wset.Builder(). key(new DocumenttypesConfig.Documenttype.Datatype.Wset.Key.Builder(). id(type.getNestedType().getId())). createifnonexistent(type.createIfNonExistent()). removeifzero(type.removeIfZero())); buildConfig(type.getNestedType(), documentBuilder, built); }
if ("key".equals(reader.getName().toString())) { if (f.getDataType() instanceof WeightedSetDataType) { DataType nestedType = ((WeightedSetDataType)f.getDataType()).getNestedType(); key = nestedType.createFieldValue(); key.deserialize(this);
public void apply(SDField field) { WeightedSetDataType ctype = (WeightedSetDataType) field.getDataType(); if (createIfNonExistent != null) { field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), createIfNonExistent, ctype.removeIfZero())); } ctype = (WeightedSetDataType) field.getDataType(); if (removeIfZero != null) { field.setDataType(DataType.getWeightedSet(ctype.getNestedType(), ctype.createIfNonExistent(), removeIfZero)); } ctype = (WeightedSetDataType) field.getDataType(); for (Object o : field.getAttributes().values()) { Attribute attribute = (Attribute) o; attribute.setRemoveIfZero(ctype.removeIfZero()); attribute.setCreateIfNonExistent(ctype.createIfNonExistent()); } }
private void checkFieldType(SDField field, DataType dataType) { if (dataType instanceof ArrayDataType) { DataType nestedType = ((ArrayDataType) dataType).getNestedType(); checkFieldType(field, nestedType); } else if (dataType instanceof WeightedSetDataType) { DataType nestedType = ((WeightedSetDataType) dataType).getNestedType(); if ( ! (nestedType instanceof PrimitiveDataType)) { fail(search, field, "Weighted set must have a primitive key type."); } } else if (dataType instanceof MapDataType) { DataType keyType = ((MapDataType) dataType).getKeyType(); if ( ! (keyType instanceof PrimitiveDataType)) { fail(search, field, "Map key type must be a primitive type."); } checkFieldType(field, ((MapDataType) dataType).getValueType()); } }
private static DataType createCompatType(DataType origType) { if (origType instanceof ArrayDataType) { return DataType.getArray(createCompatType(((ArrayDataType)origType).getNestedType())); } else if (origType instanceof MapDataType) { MapDataType mapType = (MapDataType)origType; return DataType.getMap(createCompatType(mapType.getKeyType()), createCompatType(mapType.getValueType())); } else if (origType instanceof WeightedSetDataType) { return DataType.getWeightedSet(createCompatType(((WeightedSetDataType)origType).getNestedType())); } else if (origType == PositionDataType.INSTANCE) { return DataType.LONG; } else { return origType; } } }
/** * Write out the value of weighted set field * * @param field - field description (name and data type) * @param ws - field value */ public <T extends FieldValue> void write(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); putInt(null, type.getNestedType().getId()); putInt(null, ws.size()); Iterator<T> it = ws.fieldValueIterator(); while (it.hasNext()) { FieldValue key = it.next(); java.lang.Integer value = ws.get(key); int sizePos = buf.position(); putInt(null, 0); int startPos = buf.position(); key.serialize(this); putInt(null, value); int finalPos = buf.position(); int size = finalPos - startPos; buf.position(sizePos); putInt(null, size); buf.position(finalPos); } }
/** * Write out the value of weighted set field * * @param field - field description (name and data type) * @param ws - field value */ public <T extends FieldValue> void write(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); putInt(null, type.getNestedType().getId()); putInt(null, ws.size()); Iterator<T> it = ws.fieldValueIterator(); while (it.hasNext()) { FieldValue key = it.next(); java.lang.Integer value = ws.get(key); int sizePos = buf.position(); putInt(null, 0); int startPos = buf.position(); key.serialize(this); putInt(null, value); int finalPos = buf.position(); int size = finalPos - startPos; buf.position(sizePos); putInt(null, size); buf.position(finalPos); } }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); getInt(null); // Have no need for type int numElements = getInt(null); if (numElements < 0) { throw new DeserializationException("Bad number of weighted set elements, " + numElements); } ws.clearAndReserve(numElements * 2); // Avoid resizing for (int i = 0; i < numElements; i++) { int size = getInt(null); FieldValue value = type.getNestedType().createFieldValue(); value.deserialize(null, this); IntegerFieldValue weight = new IntegerFieldValue(getInt(null)); ws.putUnChecked((T) value, weight); } }
public <T extends FieldValue> void read(FieldBase field, WeightedSet<T> ws) { WeightedSetDataType type = ws.getDataType(); getInt(null); // Have no need for type int numElements = getInt(null); if (numElements < 0) { throw new DeserializationException("Bad number of weighted set elements, " + numElements); } ws.clearAndReserve(numElements * 2); // Avoid resizing for (int i = 0; i < numElements; i++) { int size = getInt(null); FieldValue value = type.getNestedType().createFieldValue(); value.deserialize(null, this); IntegerFieldValue weight = new IntegerFieldValue(getInt(null)); ws.putUnChecked((T) value, weight); } }
} else if (type instanceof WeightedSetDataType) { WeightedSetDataType wType = (WeightedSetDataType) type; visit(wType.getNestedType()); } else if (type instanceof CollectionDataType) { CollectionDataType cType = (CollectionDataType) type;
WeightedSetDataType dt = (WeightedSetDataType) type; builder.weightedsettype(new Datatype.Weightedsettype.Builder(). datatype(dt.getNestedType().getId()). createifnonexistant(dt.createIfNonExistent()). removeifzero(dt.removeIfZero()));