Example usage for org.apache.spark.unsafe KVIterator KVIterator

List of usage examples for org.apache.spark.unsafe KVIterator KVIterator

Introduction

In this page you can find the example usage for org.apache.spark.unsafe KVIterator KVIterator.

Prototype

KVIterator

Source Link

Usage

From source file:edu.ucla.cs.wis.bigdatalog.spark.storage.map.UnsafeFixedWidthMonotonicAggregationMap.java

License:Apache License

/**
 * Returns an iterator over the keys and values in this map. This uses destructive iterator of
 * BytesToBytesMap. So it is illegal to call any other method on this map after `iterator()` has
 * been called./*from w  w w  . j a  v  a  2 s.  com*/
 * <p>
 * For efficiency, each call returns the same object.
 */
public KVIterator<UnsafeRow, UnsafeRow> iterator() {
    return new KVIterator<UnsafeRow, UnsafeRow>() {

        private final edu.ucla.cs.wis.bigdatalog.spark.storage.map.BytesToBytesMap.MapIterator mapLocationIterator = map
                .iterator();
        private final UnsafeRow key = new UnsafeRow();
        private final UnsafeRow value = new UnsafeRow();

        @Override
        public boolean next() {
            if (mapLocationIterator.hasNext()) {
                final edu.ucla.cs.wis.bigdatalog.spark.storage.map.BytesToBytesMap.Location loc = mapLocationIterator
                        .next();
                final MemoryLocation keyAddress = loc.getKeyAddress();
                final MemoryLocation valueAddress = loc.getValueAddress();
                key.pointTo(keyAddress.getBaseObject(), keyAddress.getBaseOffset(), groupingKeySchema.length(),
                        loc.getKeyLength());
                value.pointTo(valueAddress.getBaseObject(), valueAddress.getBaseOffset(),
                        aggregationBufferSchema.length(), loc.getValueLength());
                return true;
            } else {
                return false;
            }
        }

        @Override
        public UnsafeRow getKey() {
            return key;
        }

        @Override
        public UnsafeRow getValue() {
            return value;
        }

        @Override
        public void close() {
            // Do nothing.
        }
    };
}