Example usage for org.apache.hadoop.io SortedMapWritable get

List of usage examples for org.apache.hadoop.io SortedMapWritable get

Introduction

In this page you can find the example usage for org.apache.hadoop.io SortedMapWritable get.

Prototype

@Override
    public Writable get(Object key) 

Source Link

Usage

From source file:org.huahinframework.core.lib.partition.SimpleSortComparator.java

License:Apache License

/**
 * {@inheritDoc}//  w ww. j av  a2s . c  om
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public int compare(WritableComparable a, WritableComparable b) {
    if (a instanceof Key && b instanceof Key) {
        Comparable oneIdentifier = Key.class.cast(a).identifier();
        Comparable otherIdentifier = Key.class.cast(b).identifier();

        int identifier = oneIdentifier.compareTo(otherIdentifier);
        if (identifier != 0) {
            return identifier;
        }

        SortedMapWritable oneSort = Key.class.cast(a).sort();
        SortedMapWritable otherSort = Key.class.cast(b).sort();
        if (oneSort.size() != otherSort.size()) {
            return -1;
        }

        for (Entry<WritableComparable, Writable> entry : oneSort.entrySet()) {
            IntWritable priority = ((IntWritable) entry.getKey());
            KeyDetail oneKeyDetail = (KeyDetail) entry.getValue();
            KeyDetail otherKeyDetail = (KeyDetail) otherSort.get(priority);

            WritableComparable oneKey = oneKeyDetail.getKey();
            WritableComparable otherKey = otherKeyDetail.getKey();
            if (ObjectUtil.typeCompareTo(oneKeyDetail.getKey(), otherKeyDetail.getKey()) != 0) {
                return -1;
            }

            int cmpare = oneKey.compareTo(otherKey);
            if (cmpare != 0) {
                if (oneKeyDetail.getSort() == Record.SORT_LOWER) {
                    return cmpare;
                } else {
                    return -cmpare;
                }
            }
        }

        return 0;
    }

    return super.compare(a, b);
}