@Override public boolean accept(FieldType fieldtype) { return fieldtype.getValueType().getDeepestValueType().getBaseName().equals("LINK"); } };
private static void extract(Object value, FieldType fieldType, LinkCollector collector, FieldType ctxField, RecordId ctxRecord, LRepository repository) throws RepositoryException, InterruptedException { ValueType valueType = fieldType.getValueType(); String baseType = valueType.getDeepestValueType().getBaseName(); if (baseType.equals("LINK") || baseType.equals("RECORD")) { extract(value, collector, ctxField, ctxRecord, repository); } }
private void copyUnspecifiedFields(FieldType userFieldType, FieldType latestFieldType) throws FieldTypeUpdateException { if (userFieldType.getScope() == null) { userFieldType.setScope(latestFieldType.getScope()); } if (userFieldType.getValueType() == null) { userFieldType.setValueType(latestFieldType.getValueType()); } }
private Follow processFieldDeref(Element fieldEl, String derefPart) throws IndexerConfException, InterruptedException, RepositoryException { FieldType followField = ConfUtil.getFieldType(derefPart, fieldEl, systemFields, typeManager); String type = followField.getValueType().getBaseName(); if (type.equals("LIST")) { type = followField.getValueType().getNestedValueType().getBaseName(); } if (type.equals("RECORD")) { return new RecordFieldFollow(followField); } else if (type.equals("LINK")) { return new LinkFieldFollow(followField); } else { throw new IndexerConfException("Dereferencing is not possible on field of type " + followField.getValueType().getName() + ". Field: '" + derefPart); } }
@Override public List<String> format(List<IndexValue> indexValues, LRepository repository) throws InterruptedException { List<String> result = new ArrayList<String>(); for (IndexValue indexValue : indexValues) { ValueType valueType = indexValue.fieldType.getValueType(); if (valueType.getBaseName().equals("LIST")) { // The values of the first list-level are supplied as individual IndexValues valueType = valueType.getNestedValueType(); } if (!valueType.getName().equals("STRING")) { throw new RuntimeException(this.getClass().getSimpleName() + " only supports string values, but got: " + valueType.getName()); } processPath((String)indexValue.value, result); } return result; }
@Override public String format(Object record, ValueType valueType, FormatContext formatCtx) throws InterruptedException { StringBuilder builder = new StringBuilder(); TypeManager typeManager = formatCtx.repository.getTypeManager(); for (Map.Entry<QName, Object> field : ((Record)record).getFields().entrySet()) { ValueType fieldValueType; try { fieldValueType = typeManager.getFieldTypeByName(field.getKey()).getValueType(); } catch (RepositoryException e) { // error loading field type: skip this field continue; } String result = formatCtx.format(field.getValue(), fieldValueType, formatCtx); if (result != null) { if (builder.length() > 0) { builder.append(" "); } builder.append(result); } } return returnBuilderResult(builder); } }
/** * Returns true if the given FieldType is a version tag. */ public static boolean isVersionTag(FieldType fieldType) { String namespace = fieldType.getName().getNamespace(); return (fieldType.getScope() == Scope.NON_VERSIONED && fieldType.getValueType().getBaseName().equals("LONG") && namespace != null && namespace.equals(NAMESPACE) /* namespace is typically the longest string, therefore compare it last */ && !fieldType.getName().getName().equals("last")); /* filter out 'last' vtag, it should not be custom assigned */ }
private void checkImmutableFieldsCorrespond(FieldType userFieldType, FieldType latestFieldType) throws FieldTypeUpdateException { if (!userFieldType.getValueType().equals(latestFieldType.getValueType())) { throw new FieldTypeUpdateException("Changing the valueType of a fieldType '" + latestFieldType.getId() + "' (current name: " + latestFieldType.getName() + ") is not allowed; old '" + latestFieldType.getValueType() + "' new '" + userFieldType.getValueType() + "'"); } if (!userFieldType.getScope().equals(latestFieldType.getScope())) { throw new FieldTypeUpdateException("Changing the scope of a fieldType '" + latestFieldType.getId() + "' (current name: " + latestFieldType.getName() + ") is not allowed; old '" + latestFieldType.getScope() + "' new '" + userFieldType.getScope() + "'"); } }
private void addField(FieldType type, Object oldValue, Object newValue, RecordEvent.IndexRecordFilterData idxSel) throws RepositoryException, InterruptedException { if (oldValue == null && newValue == null) { return; } if (oldValue != null) { oldValue = type.getValueType().toBytes(oldValue, new IdentityRecordStack()); } if (newValue != null) { newValue = type.getValueType().toBytes(newValue, new IdentityRecordStack()); } idxSel.addChangedField(type.getId(), (byte[])oldValue, (byte[])newValue); }
@Override public List<String> format(List<IndexValue> indexValues, LRepository repository) throws InterruptedException { List<String> results = new ArrayList<String>(); for (IndexValue value : filterValues(indexValues)) { FormatContext formatCtx = new FormatContext(repository); ValueType valueType = value.fieldType.getValueType(); if (valueType.getBaseName().equals("LIST")) { // The values of the first list-level are supplied as individual IndexValues valueType = valueType.getNestedValueType(); } String result = formatCtx.format(value.value, valueType, formatCtx); if (result != null) { results.add(result); } else { results.addAll(formatCtx.results); } } return results; }
/** * Direct 'evaluation' (content extraction, formatting) of a given field * from a record. Should only be called if the field is present in the * record. */ public List<String> format(String table, Record record, FieldType fieldType, boolean extractContent, String formatterName, LRepository repository) throws InterruptedException { Object value = record.getField(fieldType.getName()); List<IndexValue> indexValues; if (fieldType.getValueType().getBaseName().equals("LIST")) { List<Object> values = (List<Object>) value; indexValues = new ArrayList<IndexValue>(values.size()); for (int i = 0; i < values.size(); i++) { indexValues.add(new IndexValue(record, fieldType, i, values.get(i))); } } else { indexValues = Collections.singletonList(new IndexValue(record, fieldType, value)); } if (fieldType.getValueType().getDeepestValueType().getBaseName().equals("BLOB") && extractContent) { return extractContent(table, indexValues, repository); } Formatter formatter = conf.getFormatters().getFormatter(formatterName); return formatter.format(indexValues, repository); }
/** * Extracts the given field value from the record and transforms it into a flat (java) list, * regardless of the valueType of the field. * @param record * @param fieldType * @return */ public static List flatList(Record record, FieldType fieldType) { if (record != null && record.hasField(fieldType.getName())) { return flatList(record.getField(fieldType.getName()), fieldType.getValueType()); } else { return Collections.emptyList(); } }
@Override public BlobAccess getBlobAccess(Record record, QName fieldName, FieldType fieldType, int...indexes) throws BlobException { if (!(fieldType.getValueType().getDeepestValueType() instanceof BlobValueType)) { throw new BlobException("Cannot read a blob from a non-blob field type: " + fieldType.getName()); } Blob blob = getBlobFromRecord(record, fieldName, fieldType, indexes); return registry.getBlobAccess(blob); }
@Override public FieldType create() throws RepositoryException, InterruptedException { FieldType fieldType = buildFieldType(); // Apply defaults if (fieldType.getValueType() == null) { fieldType.setValueType(typeManager.getValueType("STRING")); } if (fieldType.getScope() == null) { fieldType.setScope(Scope.NON_VERSIONED); } return typeManager.createFieldType(fieldType); }
public static ObjectNode toJson(FieldType fieldType, Namespaces namespaces, boolean includeName) { ObjectNode fieldNode = JsonNodeFactory.instance.objectNode(); fieldNode.put("id", fieldType.getId().toString()); if (includeName) { fieldNode.put("name", QNameConverter.toJson(fieldType.getName(), namespaces)); } fieldNode.put("scope", fieldType.getScope().toString().toLowerCase()); fieldNode.put("valueType", ValueTypeNSConverter.toJson(fieldType.getValueType().getName(), namespaces)); return fieldNode; } }
@Override @SuppressWarnings("unchecked") public Record read(DataInput dataInput) throws RepositoryException, InterruptedException { Record record = new RecordImpl(); dataInput.readByte(); // Ignore, there is currently only one encoding : 1 int length = dataInput.readVInt(); byte[] recordTypeId = dataInput.readBytes(length); Long recordTypeVersion = dataInput.readLong(); RecordType recordType = typeManager.getRecordTypeById(new SchemaIdImpl(recordTypeId), recordTypeVersion); record.setRecordType(recordType.getName(), recordTypeVersion); Map<SchemaId, QName> idToQNameMapping = new HashMap<SchemaId, QName>(); List<FieldType> fieldTypes = getSortedFieldTypes(recordType); for (FieldType fieldType : fieldTypes) { byte readByte = dataInput.readByte(); if (DEFINED == readByte) { Object value = fieldType.getValueType().read(dataInput); record.setField(fieldType.getName(), value); idToQNameMapping.put(fieldType.getId(), fieldType.getName()); } } Map<Scope, SchemaId> recordTypeIds = new EnumMap<Scope, SchemaId>(Scope.class); recordTypeIds.put(Scope.NON_VERSIONED, recordType.getId()); return new IdRecordImpl(record, idToQNameMapping, recordTypeIds); }
private static void printFieldType(Pair<FieldTypeEntry, FieldType> pair, PrintStream out, int indent) { FieldTypeEntry fieldTypeEntry = pair.getV1(); FieldType fieldType = pair.getV2(); println(out, indent, "Name = " + fieldType.getName()); println(out, indent, "ID = " + fieldType.getId()); println(out, indent, "Mandatory = " + fieldTypeEntry.isMandatory()); try { println(out, indent, "ValueType = " + fieldType.getValueType().getName()); } catch (Throwable t) { // value type failed to load } }
public AvroFieldType convert(FieldType fieldType) { AvroFieldType avroFieldType = new AvroFieldType(); avroFieldType.setId(convert(fieldType.getId())); avroFieldType.setName(convert(fieldType.getName())); avroFieldType.setValueType(convert(fieldType.getValueType())); avroFieldType.setScope(convert(fieldType.getScope())); return avroFieldType; }
@Override public FieldValueFilter fromJson(JsonNode node, Namespaces namespaces, LRepository repository, RecordFilterJsonConverter<RecordFilter> converter) throws JsonFormatException, RepositoryException, InterruptedException { FieldValueFilter filter = new FieldValueFilter(); String field = JsonUtil.getString(node, "field", null); JsonNode fieldValue = node.get("fieldValue"); // field and fieldValue should be specified both, or not at all, for deserialization to work if ((field != null || fieldValue != null) && (field == null || fieldValue == null)) { throw new RuntimeException("FieldValueFilter deserialization: both field and fieldValue must be specified."); } if (field != null && fieldValue != null) { QName fieldQName = QNameConverter.fromJson(field, namespaces); filter.setField(fieldQName); ValueType valueType = repository.getTypeManager().getFieldTypeByName(fieldQName).getValueType(); Object value = RecordReader.INSTANCE.readValue( new RecordReader.ValueHandle(fieldValue, "fieldValue", valueType), new RecordReader.ReadContext(repository, new NamespacesImpl(), defaultLinkTransformer)); filter.setFieldValue(value); } String compareOp = JsonUtil.getString(node, "compareOp", null); if (compareOp != null) { filter.setCompareOp(CompareOp.valueOf(compareOp)); } filter.setFilterIfMissing(JsonUtil.getBoolean(node, "filterIfMissing", filter.getFilterIfMissing())); return filter; }
node.put("field", QNameConverter.toJson(filter.getField(), namespaces)); ValueType valueType = repository.getTypeManager().getFieldTypeByName(filter.getField()).getValueType(); JsonNode valueAsJson = RecordWriter.INSTANCE.valueToJson(filter.getFieldValue(), valueType, new WriteOptions(), namespaces, repository);