Search in sources :

Example 1 with ListColumnDefn

use of org.apache.drill.exec.store.image.ImageBatchReader.ListColumnDefn in project drill by apache.

the class ImageDirectoryProcessor method processXmpDirectory.

protected static void processXmpDirectory(final MapColumnDefn writer, final XmpDirectory directory) {
    XMPMeta xmpMeta = directory.getXMPMeta();
    if (xmpMeta != null) {
        try {
            IteratorOptions iteratorOptions = new IteratorOptions().setJustLeafnodes(true);
            for (final XMPIterator i = xmpMeta.iterator(iteratorOptions); i.hasNext(); ) {
                try {
                    XMPPropertyInfo prop = (XMPPropertyInfo) i.next();
                    String path = prop.getPath();
                    String value = prop.getValue();
                    if (path != null && value != null) {
                        // handling lang-alt array items
                        if (prop.getOptions().getHasLanguage()) {
                            XMPPropertyInfo langProp = (XMPPropertyInfo) i.next();
                            if (langProp.getPath().endsWith("/xml:lang")) {
                                String lang = langProp.getValue();
                                path = path.replaceFirst("\\[\\d+\\]$", "") + (lang.equals("x-default") ? "" : "_" + lang);
                            }
                        }
                        ColumnDefn rootColumn = writer;
                        ColumnWriter subColumn = null;
                        String[] elements = path.replaceAll("/\\w+:", "/").split(":|/|(?=\\[)");
                        // 1. lookup and create nested structure
                        for (int j = 1; j < elements.length; j++) {
                            String parent = elements[j - 1];
                            boolean isList = elements[j].startsWith("[");
                            if (!parent.startsWith("[")) {
                                // skipped. such as parent is [1] but not the last element
                                final String formatName = ImageMetadataUtils.formatName(parent);
                                if (isList) {
                                    if (j + 1 == elements.length) {
                                        // for list
                                        subColumn = rootColumn.addList(formatName);
                                    } else {
                                        // for list-map
                                        subColumn = rootColumn.addListMap(formatName);
                                    }
                                    rootColumn = new ListColumnDefn(formatName).builder((ArrayWriter) subColumn);
                                } else {
                                    // for map
                                    subColumn = ((MapColumnDefn) rootColumn).addMap(formatName);
                                    // set up the current writer in nested structure
                                    rootColumn = new MapColumnDefn(formatName).builder((TupleWriter) subColumn);
                                }
                            }
                        }
                        // 2. set up the value for writer
                        String parent = elements[elements.length - 1];
                        if (parent.startsWith("[")) {
                            subColumn.setObject(new String[] { value });
                        } else {
                            rootColumn.addText(ImageMetadataUtils.formatName(parent)).setString(value);
                            if (subColumn instanceof ArrayWriter) {
                                ((ArrayWriter) subColumn).save();
                            }
                        }
                    }
                } catch (Exception skipped) {
                    // simply skip this property
                    logger.warn("Error in written xmp metadata : {}", skipped.getMessage());
                }
            }
        } catch (XMPException ignored) {
            logger.warn("Error in processing xmp directory : {}", ignored.getMessage());
        }
    }
}
Also used : XMPIterator(com.adobe.internal.xmp.XMPIterator) MapColumnDefn(org.apache.drill.exec.store.image.ImageBatchReader.MapColumnDefn) ColumnWriter(org.apache.drill.exec.vector.accessor.ColumnWriter) XMPPropertyInfo(com.adobe.internal.xmp.properties.XMPPropertyInfo) IteratorOptions(com.adobe.internal.xmp.options.IteratorOptions) XMPException(com.adobe.internal.xmp.XMPException) ListColumnDefn(org.apache.drill.exec.store.image.ImageBatchReader.ListColumnDefn) MapColumnDefn(org.apache.drill.exec.store.image.ImageBatchReader.MapColumnDefn) ColumnDefn(org.apache.drill.exec.store.image.ImageBatchReader.ColumnDefn) ListColumnDefn(org.apache.drill.exec.store.image.ImageBatchReader.ListColumnDefn) XMPMeta(com.adobe.internal.xmp.XMPMeta) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) XMPException(com.adobe.internal.xmp.XMPException) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter)

Example 2 with ListColumnDefn

use of org.apache.drill.exec.store.image.ImageBatchReader.ListColumnDefn in project drill by apache.

the class ImageDirectoryProcessor method processValue.

/**
 * Convert the value if necessary
 * @see org.apache.drill.exec.vector.accessor.writer.AbstractScalarWriter#setObject(Object)
 * @param writer MapColumnDefn
 * @param name Tag Name
 * @param value  Tag Value
 */
protected static void processValue(final MapColumnDefn writer, final String name, final Object value) {
    if (value == null) {
        return;
    }
    if (value instanceof Boolean) {
        writer.addObject(name, MinorType.BIT).setObject(value);
    } else if (value instanceof Byte) {
        writer.addObject(name, MinorType.TINYINT).setObject(value);
    } else if (value instanceof Short) {
        writer.addObject(name, MinorType.SMALLINT).setObject(value);
    } else if (value instanceof Integer) {
        writer.addObject(name, MinorType.INT).setObject(value);
    } else if (value instanceof Long) {
        writer.addObject(name, MinorType.BIGINT).setObject(value);
    } else if (value instanceof Float) {
        writer.addObject(name, MinorType.FLOAT4).setObject(value);
    } else if (value instanceof Double) {
        writer.addObject(name, MinorType.FLOAT8).setObject(value);
    } else if (value instanceof Rational) {
        writer.addDouble(name).setDouble(((Rational) value).doubleValue());
    } else if (value instanceof StringValue) {
        writer.addText(name).setString(((StringValue) value).toString());
    } else if (value instanceof Date) {
        writer.addDate(name).setTimestamp(Instant.ofEpochMilli(((Date) value).getTime()));
    } else if (value instanceof String[]) {
        writer.addList(name).setObject(value);
    } else if (value instanceof byte[]) {
        writer.addListByte(name).setObject(value);
    } else if (value instanceof JpegComponent) {
        JpegComponent v = (JpegComponent) value;
        TupleWriter component = writer.addMap(name);
        writer.addIntToMap(component, TagName.JPEGCOMPONENT_CID).setInt(v.getComponentId());
        writer.addIntToMap(component, TagName.JPEGCOMPONENT_HSF).setInt(v.getHorizontalSamplingFactor());
        writer.addIntToMap(component, TagName.JPEGCOMPONENT_VSF).setInt(v.getVerticalSamplingFactor());
        writer.addIntToMap(component, TagName.JPEGCOMPONENT_QTN).setInt(v.getQuantizationTableNumber());
    } else if (value instanceof List<?>) {
        ArrayWriter listMap = writer.addListMap(name);
        ListColumnDefn list = new ListColumnDefn(name).builder(listMap);
        for (Object v : (List<?>) value) {
            if (v instanceof KeyValuePair) {
                list.addText(TagName.KEYVALUEPAIR_K).setString(((KeyValuePair) v).getKey());
                list.addText(TagName.KEYVALUEPAIR_V).setString(((KeyValuePair) v).getValue().toString());
            } else {
                list.addText(TagName.KEYVALUEPAIR_V).setString(v.toString());
            }
            listMap.save();
        }
    } else {
        writer.addText(name).setString(value.toString());
    }
}
Also used : Rational(com.drew.lang.Rational) KeyValuePair(com.drew.lang.KeyValuePair) Date(java.util.Date) ListColumnDefn(org.apache.drill.exec.store.image.ImageBatchReader.ListColumnDefn) JpegComponent(com.drew.metadata.jpeg.JpegComponent) TupleWriter(org.apache.drill.exec.vector.accessor.TupleWriter) StringValue(com.drew.metadata.StringValue) ArrayWriter(org.apache.drill.exec.vector.accessor.ArrayWriter)

Aggregations

ListColumnDefn (org.apache.drill.exec.store.image.ImageBatchReader.ListColumnDefn)2 ArrayWriter (org.apache.drill.exec.vector.accessor.ArrayWriter)2 TupleWriter (org.apache.drill.exec.vector.accessor.TupleWriter)2 XMPException (com.adobe.internal.xmp.XMPException)1 XMPIterator (com.adobe.internal.xmp.XMPIterator)1 XMPMeta (com.adobe.internal.xmp.XMPMeta)1 IteratorOptions (com.adobe.internal.xmp.options.IteratorOptions)1 XMPPropertyInfo (com.adobe.internal.xmp.properties.XMPPropertyInfo)1 KeyValuePair (com.drew.lang.KeyValuePair)1 Rational (com.drew.lang.Rational)1 StringValue (com.drew.metadata.StringValue)1 JpegComponent (com.drew.metadata.jpeg.JpegComponent)1 Date (java.util.Date)1 ColumnDefn (org.apache.drill.exec.store.image.ImageBatchReader.ColumnDefn)1 MapColumnDefn (org.apache.drill.exec.store.image.ImageBatchReader.MapColumnDefn)1 ColumnWriter (org.apache.drill.exec.vector.accessor.ColumnWriter)1