1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import java.io.DataInput;
21 import java.io.DataOutput;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.List;
27
28 import org.apache.hadoop.hbase.HConstants;
29 import org.apache.hadoop.hbase.io.HbaseObjectWritable;
30 import org.apache.hadoop.hbase.util.Bytes;
31
32
33
34
35
36
37
38
39 public class RowMutations implements Row {
40 private List<Mutation> mutations = new ArrayList<Mutation>();
41 private byte [] row;
42 private static final byte VERSION = (byte)0;
43
44
45 public RowMutations() {}
46
47
48
49
50
51 public RowMutations(byte [] row) {
52 if(row == null || row.length > HConstants.MAX_ROW_LENGTH) {
53 throw new IllegalArgumentException("Row key is invalid");
54 }
55 this.row = Arrays.copyOf(row, row.length);
56 }
57
58
59
60
61
62
63 public void add(Put p) throws IOException {
64 internalAdd(p);
65 }
66
67
68
69
70
71
72 public void add(Delete d) throws IOException {
73 internalAdd(d);
74 }
75
76 private void internalAdd(Mutation m) throws IOException {
77 int res = Bytes.compareTo(this.row, m.getRow());
78 if(res != 0) {
79 throw new IOException("The row in the recently added Put/Delete " +
80 Bytes.toStringBinary(m.getRow()) + " doesn't match the original one " +
81 Bytes.toStringBinary(this.row));
82 }
83 mutations.add(m);
84 }
85
86 @Override
87 public void readFields(final DataInput in) throws IOException {
88 int version = in.readByte();
89 if (version > VERSION) {
90 throw new IOException("version not supported");
91 }
92 this.row = Bytes.readByteArray(in);
93 int numMutations = in.readInt();
94 mutations.clear();
95 for(int i = 0; i < numMutations; i++) {
96 mutations.add((Mutation) HbaseObjectWritable.readObject(in, null));
97 }
98 }
99
100 @Override
101 public void write(final DataOutput out) throws IOException {
102 out.writeByte(VERSION);
103 Bytes.writeByteArray(out, this.row);
104 out.writeInt(mutations.size());
105 for (Mutation m : mutations) {
106 HbaseObjectWritable.writeObject(out, m, m.getClass(), null);
107 }
108 }
109
110 @Override
111 public int compareTo(Row i) {
112 return Bytes.compareTo(this.getRow(), i.getRow());
113 }
114
115 @Override
116 public byte[] getRow() {
117 return row;
118 }
119
120
121
122
123 public List<Mutation> getMutations() {
124 return Collections.unmodifiableList(mutations);
125 }
126 }