@Override public int getFieldCount() { return getFields().size(); }
public Collection<Field> getAllFields() { Collection<Field> collection = new LinkedList<>(); for (NewDocumentType type : inherits.values()) { collection.addAll(type.getAllFields()); } collection.addAll(header.getFields()); collection.addAll(body.getFields()); return Collections.unmodifiableCollection(collection); }
/** * Returns an iterator over all fields in this documenttype * * @return An iterator for iterating the fields in this documenttype. */ public Iterator<Field> fieldIteratorThisTypeOnly() { return new Iterator<Field>() { Iterator<Field> headerIt = headerType.getFields().iterator(); Iterator<Field> bodyIt = bodyType.getFields().iterator(); public boolean hasNext() { if (headerIt != null) { if (headerIt.hasNext()) return true; headerIt = null; } return bodyIt.hasNext(); } public Field next() { return (headerIt != null ? headerIt.next() : bodyIt.next()); } public void remove() { if (headerIt != null) { headerIt.remove(); } else { bodyIt.remove(); } } }; }
public void inherit(StructDataType type) { if (superType != null) { throw new IllegalArgumentException("Already inherits type " + superType + ", multiple inheritance not currently supported."); } for (Field f : type.getFields()) { if (hasField(f)) { throw new IllegalArgumentException(f + " already present in " + type + ", " + this + " cannot inherit from it"); } } superType = type; }
@Override public Collection<Field> getFields() { if (superType == null) { return Collections.unmodifiableCollection(super.getFields()); } Collection<Field> fieldsBuilder = new ArrayList<>(); fieldsBuilder.addAll(super.getFields()); fieldsBuilder.addAll(superType.getFields()); return ImmutableList.copyOf(fieldsBuilder); }
private static String toString(StructDataType dataType) { StringBuilder builder = new StringBuilder(); builder.append(dataType.getName()).append(":{"); boolean first = true; for (Field field : dataType.getFields()) { if (!first) { builder.append(","); } if (field.getDataType() instanceof StructDataType) { builder.append(toString((StructDataType) field.getDataType())); } else { builder.append(field.getName() + ":" + field.getDataType().getName()); } first = false; } builder.append("}"); return builder.toString(); } }
/** * Data type of the header fields of this and all inherited document types * @return merged {@link StructDataType} */ public StructDataType allHeader() { StructDataType ret = new StructDataType(header.getName()); for (Field f : header.getFields()) { ret.addField(f); } for (NewDocumentType inherited : getInherited()) { for (Field f : ((StructDataType) inherited.getHeader()).getFields()) { ret.addField(f); } } return ret; }
/** * Data type of the body fields of this and all inherited document types * @return merged {@link StructDataType} */ public StructDataType allBody() { StructDataType ret = new StructDataType(body.getName()); for (Field f : body.getFields()) { ret.addField(f); } for (NewDocumentType inherited : getInherited()) { for (Field f : ((StructDataType) inherited.getBody()).getFields()) { ret.addField(f); } } return ret; }
private static SDDocumentType createSDDocumentType(StructDataType structType) { SDDocumentType docType = new SDDocumentType(structType.getName()); for (Field field : structType.getFields()) { docType.addField(new SDField(docType, field.getName(), field.getDataType())); } docType.setStruct(structType); return docType; }
@Override public int compareTo(FieldValue obj) { int cmp = super.compareTo(obj); if (cmp != 0) { return cmp; } Struct rhs = (Struct)obj; cmp = values.size() - rhs.values.size(); if (cmp != 0) { return cmp; } StructDataType type = getDataType(); for (Field field : type.getFields()) { FieldValue lhsField = getFieldValue(field); FieldValue rhsField = rhs.getFieldValue(field); if (lhsField != null && rhsField != null) { cmp = lhsField.compareTo(rhsField); if (cmp != 0) { return cmp; } } else if (lhsField != null || rhsField != null) { return (lhsField != null ? -1 : 1); } } return 0; }
private static boolean changedType(StructDataType currentType, StructDataType nextType) { for (Field currentField : currentType.getFields()) { Field nextField = nextType.getField(currentField.getName()); if (nextField != null) { if (areStructFields(currentField, nextField)) { if (changedType((StructDataType) currentField.getDataType(), (StructDataType) nextField.getDataType())) { return true; } } else { if (!currentField.getDataType().equals(nextField.getDataType())) { return true; } } } } return false; }
@SuppressWarnings("deprecation") private static void registerDocumentType(DocumentTypeManager manager, DocumentmanagerConfig.Datatype.Documenttype doc) { StructDataType header = (StructDataType) manager.getDataType(doc.headerstruct(), ""); StructDataType body = (StructDataType) manager.getDataType(doc.bodystruct(), ""); for (Field field : body.getFields()) { field.setHeader(false); } DocumentType type = new DocumentType(doc.name(), header, body); for (Object j : doc.inherits()) { DocumentmanagerConfig.Datatype.Documenttype.Inherits parent = (DocumentmanagerConfig.Datatype.Documenttype.Inherits) j; DataTypeName name = new DataTypeName(parent.name()); DocumentType parentType = manager.getDocumentType(name); if (parentType == null) { throw new IllegalArgumentException("Could not find document type '" + name.toString() + "'."); } type.inherit(parentType); } manager.register(type); }
} else if (type instanceof StructDataType) { StructDataType dt = (StructDataType) type; for (com.yahoo.document.Field field : dt.getFields()) { if (field.getDataType() != type) {
@Override protected void doVerify(VerificationContext context) { DataType input = context.getValue(); if (input instanceof ArrayDataType || input instanceof WeightedSetDataType) { context.setValue(((CollectionDataType)input).getNestedType()).execute(exp); if (input instanceof ArrayDataType) { context.setValue(DataType.getArray(context.getValue())); } else { WeightedSetDataType wset = (WeightedSetDataType)input; context.setValue(DataType.getWeightedSet(context.getValue(), wset.createIfNonExistent(), wset.removeIfZero())); } } else if (input instanceof StructDataType) { for (Field field : ((StructDataType)input).getFields()) { DataType fieldType = field.getDataType(); DataType valueType = context.setValue(fieldType).execute(exp).getValue(); if (!fieldType.isAssignableFrom(valueType)) { throw new VerificationException(this, "Expected " + fieldType.getName() + " output, got " + valueType.getName() + "."); } } context.setValue(input); } else { throw new VerificationException(this, "Expected Array, Struct or WeightedSet input, got " + input.getName() + "."); } }
private void buildConfig(StructDataType type, DocumenttypesConfig.Documenttype.Datatype.Builder dataTypeBuilder, DocumenttypesConfig.Documenttype.Builder documentBuilder, Set<Integer> built) { dataTypeBuilder.type(DocumenttypesConfig.Documenttype.Datatype.Type.Enum.STRUCT); DocumenttypesConfig.Documenttype.Datatype.Sstruct.Builder structBuilder = new DocumenttypesConfig.Documenttype.Datatype.Sstruct.Builder(); dataTypeBuilder.sstruct(structBuilder); structBuilder.name(type.getName()); if (type.getCompressionConfig().type.getCode() != 0) { structBuilder.compression(new DocumenttypesConfig.Documenttype.Datatype.Sstruct.Compression.Builder(). type(DocumenttypesConfig.Documenttype.Datatype.Sstruct.Compression.Type.Enum.valueOf(type.getCompressionConfig().type.toString())). level(type.getCompressionConfig().compressionLevel). threshold((int) type.getCompressionConfig().threshold). minsize((int) type.getCompressionConfig().minsize)); } for (com.yahoo.document.Field field : type.getFields()) { DocumenttypesConfig.Documenttype.Datatype.Sstruct.Field.Builder builder = new DocumenttypesConfig.Documenttype.Datatype.Sstruct.Field.Builder(); builder.name(field.getName()). id(field.getId()). id_v6(field.getIdV6()). datatype(field.getDataType().getId()); if (field.getDataType() instanceof TensorDataType) { builder.detailedtype(((TensorDataType) field.getDataType()).getTensorType().toString()); } structBuilder.field(builder); buildConfig(field.getDataType(), documentBuilder, built); } }