org.shaf.core.io.emulator.InternalWriter.java Source code

Java tutorial

Introduction

Here is the source code for org.shaf.core.io.emulator.InternalWriter.java

Source

/**
 * Copyright 2014-2015 SHAF-WORK
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.shaf.core.io.emulator;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;

/**
 * A {@link InternalWriter} writes key-value pairs to the internal buffer during
 * the {@code mapping} phase.
 * 
 * @author Mykola Galushka
 * 
 * @param <KEY>
 *            the writing key type.
 * @param <VALUE>
 *            the writing value type.
 */
public class InternalWriter<KEY, VALUE> extends AbstractWriter<KEY, VALUE> {

    /**
     * The memory buffer for writing key/value pairs.
     */
    private ListMultimap<KEY, VALUE> buffer;

    /**
     * Constructs a new memory writer.
     * 
     * @param config
     *            the writer configuration.
     */
    public InternalWriter(final Configuration config) {
        super(config);

        this.buffer = ArrayListMultimap.create();
    }

    @Override
    public void writeRecord(Record<KEY, VALUE> record) throws IOException {
        this.buffer.put(record.getKey(), record.getValue());
    }

    /**
     * Returns the memory buffer with accumulated key/value pairs.
     * 
     * @return the buffer with key/value pairs.
     */
    public final Multimap<KEY, VALUE> getBuffer() {
        return this.buffer;
    }

    @Override
    public void close() throws IOException {
        // Since internal writer is using memory for writing key/value pairs, it
        // doesn't have anything to close.
    }
}