aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/parallelai/spyglass/hbase/HBaseTableSplitRegional.java
blob: ad5f78b87bbe51be295ed4c72eda66fbd18cae04 (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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.Iterator;
import java.util.List;
import java.util.Vector;

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 HBaseTableSplitRegional extends HBaseTableSplitBase {

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

	private List<HBaseTableSplitGranular> splits = new Vector<HBaseTableSplitGranular>();

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

	}

	public HBaseTableSplitRegional(String regionLocation, String regionName) {
		this.m_regionLocation = regionLocation;
        this.m_regionName = regionName;
	}

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

        super.readFields(in);

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

		for (int i = 0; i < s; i++) {
			HBaseTableSplitGranular hbts = new HBaseTableSplitGranular();
            hbts.readFields(in);

			splits.add(hbts);
		}

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

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

        super.write(out);

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

		for (HBaseTableSplitGranular hbts : splits) {
			hbts.write(out);
		}

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

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

        str.append(super.toString());

        str.append(" REGIONAL => Region Location (" + m_regionLocation + ") Name (" + m_regionName + ")" );

        str.append(" GRANULAR = > ");

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

		return str.toString();
	}

	@Override
	public int compareTo(HBaseTableSplitBase o) {
        if( ! (o instanceof HBaseTableSplitRegional) ) return -1;

		return (splits.size() - ((HBaseTableSplitRegional)o).splits.size());
	}

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

	public void addSplit(HBaseTableSplitGranular hbt) throws IOException {
        LOG.debug("ADD Split : " + hbt);

		if (hbt.getRegionLocation().equals(m_regionLocation)) {
			splits.add(hbt);
            this.copy(hbt);
        } else
			throw new IOException("HBaseTableSplitGranular Region Location "
					+ hbt.getRegionLocation()
					+ " does NOT match MultiSplit Region Location " + m_regionLocation);
	}

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

    public boolean hasMoreSplits() {
        splitIterator = (splitIterator == null) ? splits.listIterator() : splitIterator;

        return splitIterator.hasNext();
    }

    private Iterator<HBaseTableSplitGranular> splitIterator = null;
    private int currSplitCount = 0;

    public HBaseTableSplitGranular getNextSplit() {
        splitIterator = (splitIterator == null) ? splits.listIterator() : splitIterator;

        if( splitIterator.hasNext() ) {
            currSplitCount ++;
            return splitIterator.next();
        } else {
            return null;
        }
    }

    public int getCurrSplitCount() {
        return currSplitCount;
    }
}