aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/parallelai/spyglass/hbase/HBaseMultiInputSplit.java
blob: 02e7f7bfac354f6e174dbd7503363b0d0aedd130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package parallelai.spyglass.hbase;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.SerializationUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.mapred.InputSplit;

public class HBaseMultiInputSplit implements InputSplit,
		Comparable<HBaseMultiInputSplit>, Serializable {

	private final Log LOG = LogFactory.getLog(HBaseMultiInputSplit.class);

	private List<HBaseTableSplit> splits = new ArrayList<HBaseTableSplit>();

	private String regionLocation = null;

	/** default constructor */
	private HBaseMultiInputSplit() {

	}

	public HBaseMultiInputSplit(String regionLocation) {
		this.regionLocation = regionLocation;
	}

	/** @return the region's hostname */
	public String getRegionLocation() {
		LOG.debug("REGION GETTER : " + regionLocation);

		return this.regionLocation;
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		LOG.debug("READ ME : " + in.toString());

		int s = Bytes.toInt(Bytes.readByteArray(in));

		for (int i = 0; i < s; i++) {
			HBaseTableSplit hbts = (HBaseTableSplit) SerializationUtils
					.deserialize(Bytes.readByteArray(in));
			splits.add(hbts);
		}

		LOG.debug("READ and CREATED : " + this);
	}

	@Override
	public void write(DataOutput out) throws IOException {
		LOG.debug("WRITE : " + this);

		Bytes.writeByteArray(out, Bytes.toBytes(splits.size()));

		for (HBaseTableSplit hbts : splits) {
			Bytes.writeByteArray(out, SerializationUtils.serialize(hbts));
		}

		LOG.debug("WROTE : " + out.toString());
	}

	@Override
	public String toString() {
		StringBuffer str = new StringBuffer();
		str.append("HBaseMultiSplit : ");

		for (HBaseTableSplit hbt : splits) {
			str.append(" [" + hbt.toString() + "]");
		}

		return str.toString();
	}

	@Override
	public int compareTo(HBaseMultiInputSplit o) {
		// TODO: Make this comparison better
		return (splits.size() - o.splits.size());
	}

	@Override
	public long getLength() throws IOException {
		return splits.size();
	}

	@Override
	public String[] getLocations() throws IOException {
		LOG.debug("REGION ARRAY : " + regionLocation);

		return new String[] { this.regionLocation };
	}

	public void addSplit(HBaseTableSplit hbt) throws IOException {
		if (hbt.getRegionLocation().equals(regionLocation))
			splits.add(hbt);
		else
			throw new IOException("HBaseTableSplit Region Location "
					+ hbt.getRegionLocation()
					+ " does NOT match MultiSplit Region Location " + regionLocation);
	}

	public List<HBaseTableSplit> getSplits() {
		return splits;
	}
}