/** * Returns whether this type defines the given field name * * @param name The name of the field to check if it has * @return True if there is a field with the given name. */ public boolean hasField(String name) { return getField(name) != null; }
public String toString() { return "SD document type '" + docType.getName() + "'"; }
public TestDocMan() { DocumentType docType = new DocumentType("testdoctype1"); docType.addHeaderField("headerval", DataType.INT); docType.addField("content", DataType.STRING); registerDocumentType(docType); }
/** * Check if this document type has the given name, * or inherits from a type with that name. */ public boolean isA(String docTypeName) { if (getName().equalsIgnoreCase(docTypeName)) { return true; } for (DocumentType parent : inherits) { if (parent.isA(docTypeName)) { return true; } } return false; }
@SuppressWarnings("deprecation") private void setNewType(DocumentType type) { header = type.contentStruct().createFieldValue(); body = type.getBodyType().createFieldValue(); }
/** * Fail if the subtype changes the type of any equally named field. * * @param superType The supertype to verify against * TODO Add strict type checking no duplicate fields are allowed */ private void verifyTypeConsistency(DocumentType superType) { for (Field f : fieldSet()) { Field supField = superType.getField(f.getName()); if (supField != null) { if (!f.getDataType().equals(supField.getDataType())) { throw new IllegalArgumentException("Inheritance type mismatch: field \"" + f.getName() + "\" in datatype \"" + getName() + "\"" + " must have same datatype as in parent document type \"" + superType.getName() + "\""); } } } }
/** * Adds the given {@link FieldUpdate} to this DocumentUpdate. If this DocumentUpdate already contains a FieldUpdate * for the named field, the content of the given FieldUpdate is added to the existing one. * * @param update The FieldUpdate to add to this DocumentUpdate. * @return This, to allow chaining. * @throws IllegalArgumentException If the {@link DocumentType} of this DocumentUpdate does not have a corresponding * field. */ public DocumentUpdate addFieldUpdate(FieldUpdate update) { int fieldId = update.getField().getId(); if (documentType.getField(fieldId) == null) { throw new IllegalArgumentException("Document type '" + documentType.getName() + "' does not have field '" + update.getField().getName() + "'."); } FieldUpdate prevUpdate = getFieldUpdateById(fieldId); if (prevUpdate != update) { if (prevUpdate != null) { prevUpdate.addAll(update); } else { id2FieldUpdates.put(fieldId, update); } } return this; }
builder.documenttype(doc); doc. name(dt.getName()). headerstruct(dt.contentStruct().getId()). bodystruct(dt.getBodyType().getId()); for (DocumentType inherited : dt.getInheritedTypes()) { doc.inherits(new Datatype.Documenttype.Inherits.Builder().name(inherited.getName()));
@Override public Field getField(int id) { Field field = headerType.getField(id); if (field == null) { field = bodyType.getField(id); } if (field == null && !isRegistered()) { for (DocumentType inheritedType : inherits) { field = inheritedType.getField(id); if (field != null) break; } } return field; }
@Override public Field getField(String fieldName) { Field field = header.getField(fieldName); if (field == null) { field = body.getField(fieldName); } if (field == null) { for(DocumentType parent : getDataType().getInheritedTypes()) { field = parent.getField(fieldName); if (field != null) { break; } } } return field; }
/** * Creates a new document type. * The document type id will be generated as a hash from the document type name. * * @param name The name of the new document type * @param search check for type ID collisions in this search definition */ @SuppressWarnings("deprecation") public SDDocumentType(String name, Search search) { docType = new DocumentType(name); docType.contentStruct().setCompressionConfig(new CompressionConfig()); docType.getBodyType().setCompressionConfig(new CompressionConfig()); validateId(search); inherit(VESPA_DOCUMENT); }
if (docType.getInheritedTypes().size() == 0) { docType.inherit(documentTypes.get(new DataTypeName("document"))); documentTypes.put(docType.getDataTypeName(), docType);
private void visit(SDDocumentType docOrStruct) { int id; if (docOrStruct.isStruct()) { id = new StructDataType(docOrStruct.getName()).getId(); } else { id = new DocumentType(docOrStruct.getName()).getId(); } if (seenTypes.contains(id)) { return; } else { seenTypes.add((new StructDataType(docOrStruct.getName()).getId())); } for (Field field : docOrStruct.fieldSet()) { if (!seenTypes.contains(field.getDataType().getId())) { //we haven't seen this before, do it visit(field.getDataType()); } } processingOrder.add(docOrStruct); }
public void addField(Field field) { verifyInheritance(field); for (Iterator<Field> i = docType.fieldIteratorThisTypeOnly(); i.hasNext(); ) { if (field.getName().equalsIgnoreCase((i.next()).getName())) { throw new IllegalArgumentException("Duplicate (case insensitively) " + field + " in " + this); } } docType.addField(field); }
/** * Adds a document to the inherited document types of this. * If this type is already directly inherited, nothing is done * * @param type An already DocumentType object. */ public void inherit(DocumentType type) { //TODO: There is also a check like the following in SDDocumentType addField(), try to move that to this class' addField() to get it proper, // as this method is called only when the doc types are exported. verifyTypeConsistency(type); if (isRegistered()) { throw new IllegalStateException("You cannot add inheritance to a document type that is already registered."); } if (type == null) { throw new IllegalArgumentException("The document type cannot be null in inherit()"); } // If it inherits the exact same type if (inherits.contains(type)) return; // If we inherit a type, don't inherit the supertype if (inherits.size() == 1 && inherits.get(0).getDataTypeName().equals(new DataTypeName("document"))) { inherits.clear(); } inherits.add(type); }
/** * Sets the id of this field. Don't do this unless you know what you are doing * * @param newId the id - if this is less than 100 it will cause document to serialize * using just one byte for this field id. 100-127 are reserved values * @param owner the owning document, this is checked for collisions and notified * of the id change. It can not be null */ public void setId(int newId, DocumentType owner) { if (owner == null) { throw new NullPointerException("Can not assign an id of " + this + " without knowing the owner"); } validateId(newId, owner, Document.SERIALIZED_VERSION); owner.removeField(getName()); this.fieldId = newId; this.fieldIdV6 = newId; this.forcedId = true; owner.addField(this); }
private void registerDefaultDataTypes() { DocumentType superDocType = DataType.DOCUMENT; dataTypes.put(superDocType.getId(), superDocType); documentTypes.put(superDocType.getDataTypeName(), superDocType); Class<? extends DataType> dataTypeClass = DataType.class; for (java.lang.reflect.Field field : dataTypeClass.getFields()) { if (Modifier.isStatic(field.getModifiers()) && Modifier.isFinal(field.getModifiers())) { if (DataType.class.isAssignableFrom(field.getType())) { try { //these are all static final DataTypes listed in DataType: DataType type = (DataType) field.get(null); register(type); } catch (IllegalAccessException e) { //ignore } } } } for (AnnotationType type : AnnotationTypes.ALL_TYPES) { annotationTypeRegistry.register(type); } }
@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); }
public void setFieldPath(String fieldPath) { originalFieldPath = fieldPath; this.fieldPath = docType.buildFieldPath(fieldPath); }
public DataTypeName getDocumentName() { return docType.getDataTypeName(); } public DocumentType getDocumentType() { return docType; }