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.List;
26 import java.util.Map;
27
28 import org.apache.hadoop.hbase.KeyValue;
29 import org.apache.hadoop.hbase.util.Bytes;
30 import org.apache.hadoop.io.Writable;
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public class Append extends Mutation {
45 private static final String RETURN_RESULTS = "_rr_";
46 private static final byte APPEND_VERSION = (byte)1;
47
48
49
50
51
52
53
54 public void setReturnResults(boolean returnResults) {
55 setAttribute(RETURN_RESULTS, Bytes.toBytes(returnResults));
56 }
57
58
59
60
61 public boolean isReturnResults() {
62 byte[] v = getAttribute(RETURN_RESULTS);
63 return v == null ? true : Bytes.toBoolean(v);
64 }
65
66
67 public Append() {}
68
69
70
71
72
73
74
75 public Append(byte[] row) {
76 this.row = Arrays.copyOf(row, row.length);
77 }
78
79
80
81
82
83
84
85
86 public Append add(byte [] family, byte [] qualifier, byte [] value) {
87 List<KeyValue> list = familyMap.get(family);
88 if(list == null) {
89 list = new ArrayList<KeyValue>();
90 }
91 list.add(new KeyValue(
92 this.row, family, qualifier, this.ts, KeyValue.Type.Put, value));
93 familyMap.put(family, list);
94 return this;
95 }
96
97 @Override
98 public void readFields(final DataInput in)
99 throws IOException {
100 int version = in.readByte();
101 if (version > APPEND_VERSION) {
102 throw new IOException("version not supported: "+version);
103 }
104 this.row = Bytes.readByteArray(in);
105 this.ts = in.readLong();
106 this.lockId = in.readLong();
107 this.writeToWAL = in.readBoolean();
108 int numFamilies = in.readInt();
109 if (!this.familyMap.isEmpty()) this.familyMap.clear();
110 for(int i=0;i<numFamilies;i++) {
111 byte [] family = Bytes.readByteArray(in);
112 int numKeys = in.readInt();
113 List<KeyValue> keys = new ArrayList<KeyValue>(numKeys);
114 int totalLen = in.readInt();
115 byte [] buf = new byte[totalLen];
116 int offset = 0;
117 for (int j = 0; j < numKeys; j++) {
118 int keyLength = in.readInt();
119 in.readFully(buf, offset, keyLength);
120 keys.add(new KeyValue(buf, offset, keyLength));
121 offset += keyLength;
122 }
123 this.familyMap.put(family, keys);
124 }
125 readAttributes(in);
126 }
127
128 @Override
129 public void write(final DataOutput out)
130 throws IOException {
131 out.writeByte(APPEND_VERSION);
132 Bytes.writeByteArray(out, this.row);
133 out.writeLong(this.ts);
134 out.writeLong(this.lockId);
135 out.writeBoolean(this.writeToWAL);
136 out.writeInt(familyMap.size());
137 for (Map.Entry<byte [], List<KeyValue>> entry : familyMap.entrySet()) {
138 Bytes.writeByteArray(out, entry.getKey());
139 List<KeyValue> keys = entry.getValue();
140 out.writeInt(keys.size());
141 int totalLen = 0;
142 for(KeyValue kv : keys) {
143 totalLen += kv.getLength();
144 }
145 out.writeInt(totalLen);
146 for(KeyValue kv : keys) {
147 out.writeInt(kv.getLength());
148 out.write(kv.getBuffer(), kv.getOffset(), kv.getLength());
149 }
150 }
151 writeAttributes(out);
152 }
153 }