aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/parallelai/spyglass/hbase/HBaseTableSplitBase.java
blob: 3b72a1d65c92ae43596119214d9d5dd5246514b7 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package parallelai.spyglass.hbase;

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;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.io.Serializable;
import java.util.TreeSet;

/**
 * Created with IntelliJ IDEA.
 * User: chand_000
 * Date: 29/08/13
 * Time: 16:18
 * To change this template use File | Settings | File Templates.
 */
public abstract class HBaseTableSplitBase implements InputSplit,
        Comparable<HBaseTableSplitBase>, Serializable {

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


    protected byte[] m_tableName = null;
    protected byte[] m_startRow = null;
    protected byte[] m_endRow = null;
    protected String m_regionLocation = null;
    protected String m_regionName = null;
    protected TreeSet<String> m_keyList = null;
    protected HBaseConstants.SourceMode m_sourceMode = HBaseConstants.SourceMode.EMPTY;
    protected boolean m_endRowInclusive = true;
    protected int m_versions = 1;
    protected boolean m_useSalt = false;
    protected long m_timestamp = -1L;


    /** @return table name */
    public byte[] getTableName() {
        return this.m_tableName;
    }

    /** @return starting row key */
    public byte[] getStartRow() {
        return this.m_startRow;
    }

    /** @return end row key */
    public byte[] getEndRow() {
        return this.m_endRow;
    }

    public boolean getEndRowInclusive() {
        return m_endRowInclusive;
    }

    public void setEndRowInclusive(boolean isInclusive) {
        m_endRowInclusive = isInclusive;
    }

    /** @return list of keys to get */
    public TreeSet<String> getKeyList() {
        return m_keyList;
    }

    public int getVersions() {
        return m_versions;
    }

    /** @return get the source mode */
    public HBaseConstants.SourceMode getSourceMode() {
        return m_sourceMode;
    }

    public boolean getUseSalt() {
        return m_useSalt;
    }

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

        return this.m_regionLocation;
    }

    public String[] getLocations() {
        LOG.debug("REGION ARRAY : " + m_regionLocation);

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

    public String getRegionName() {
        return this.m_regionName;
    }

    public long getTimestamp() {
        return m_timestamp;
    }

    public void setTimestamp(long m_timestamp) {
        this.m_timestamp = m_timestamp;
    }

    public void copy(HBaseTableSplitBase that) {
        this.m_endRow = that.m_endRow;
        this.m_endRowInclusive = that.m_endRowInclusive;
        this.m_keyList = that.m_keyList;
        this.m_sourceMode = that.m_sourceMode;
        this.m_startRow = that.m_startRow;
        this.m_tableName = that.m_tableName;
        this.m_useSalt = that.m_useSalt;
        this.m_versions = that.m_versions;
        this.m_regionLocation = that.m_regionLocation;
        this.m_regionName = that.m_regionName;
        this.m_timestamp = that.m_timestamp;
    }

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

        this.m_tableName = Bytes.readByteArray(in);
        this.m_regionLocation = Bytes.toString(Bytes.readByteArray(in));
        this.m_regionName = Bytes.toString(Bytes.readByteArray(in));
        this.m_sourceMode = HBaseConstants.SourceMode.valueOf(Bytes.toString(Bytes
                .readByteArray(in)));
        this.m_useSalt = Bytes.toBoolean(Bytes.readByteArray(in));

        switch (this.m_sourceMode) {
            case SCAN_RANGE:
                this.m_startRow = Bytes.readByteArray(in);
                this.m_endRow = Bytes.readByteArray(in);
                this.m_endRowInclusive = Bytes.toBoolean(Bytes.readByteArray(in));
                break;

            case GET_LIST:
                this.m_versions = Bytes.toInt(Bytes.readByteArray(in));
                this.m_keyList = new TreeSet<String>();

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

                for (int i = 0; i < m; i++) {
                    this.m_keyList.add(Bytes.toString(Bytes.readByteArray(in)));
                }
                break;
        }

        this.m_timestamp = Bytes.toLong(Bytes.readByteArray(in));

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

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

        Bytes.writeByteArray(out, this.m_tableName);
        Bytes.writeByteArray(out, Bytes.toBytes(this.m_regionLocation));
        Bytes.writeByteArray(out, Bytes.toBytes(this.m_regionName));
        Bytes.writeByteArray(out, Bytes.toBytes(this.m_sourceMode.name()));
        Bytes.writeByteArray(out, Bytes.toBytes(this.m_useSalt));

        switch (this.m_sourceMode) {
            case SCAN_RANGE:
                Bytes.writeByteArray(out, this.m_startRow);
                Bytes.writeByteArray(out, this.m_endRow);
                Bytes.writeByteArray(out, Bytes.toBytes(this.m_endRowInclusive));
                break;

            case GET_LIST:
                Bytes.writeByteArray(out, Bytes.toBytes(m_versions));
                Bytes.writeByteArray(out, Bytes.toBytes(this.m_keyList.size()));

                for (String k : this.m_keyList) {
                    Bytes.writeByteArray(out, Bytes.toBytes(k));
                }
                break;
        }

        Bytes.writeByteArray(out, Bytes.toBytes(m_timestamp));

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