1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver.wal;
20
21 import java.io.DataInput;
22 import java.io.DataOutput;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.NavigableMap;
27 import java.util.TreeMap;
28
29 import org.apache.hadoop.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.io.HeapSize;
31 import org.apache.hadoop.hbase.KeyValue;
32 import org.apache.hadoop.hbase.util.Bytes;
33 import org.apache.hadoop.hbase.util.ClassSize;
34 import org.apache.hadoop.io.Writable;
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 @InterfaceAudience.Private
71 public class WALEdit implements Writable, HeapSize {
72
73 private final int VERSION_2 = -1;
74
75 private final ArrayList<KeyValue> kvs = new ArrayList<KeyValue>();
76 private NavigableMap<byte[], Integer> scopes;
77
78 private CompressionContext compressionContext;
79
80 public WALEdit() {
81 }
82
83 public void setCompressionContext(final CompressionContext compressionContext) {
84 this.compressionContext = compressionContext;
85 }
86
87 public void add(KeyValue kv) {
88 this.kvs.add(kv);
89 }
90
91 public boolean isEmpty() {
92 return kvs.isEmpty();
93 }
94
95 public int size() {
96 return kvs.size();
97 }
98
99 public List<KeyValue> getKeyValues() {
100 return kvs;
101 }
102
103 public NavigableMap<byte[], Integer> getScopes() {
104 return scopes;
105 }
106
107
108 public void setScopes (NavigableMap<byte[], Integer> scopes) {
109
110
111 this.scopes = scopes;
112 }
113
114 public void readFields(DataInput in) throws IOException {
115 kvs.clear();
116 if (scopes != null) {
117 scopes.clear();
118 }
119 int versionOrLength = in.readInt();
120
121 if (versionOrLength == VERSION_2) {
122
123 int numEdits = in.readInt();
124 for (int idx = 0; idx < numEdits; idx++) {
125 if (compressionContext != null) {
126 this.add(KeyValueCompression.readKV(in, compressionContext));
127 } else {
128 this.add(KeyValue.create(in));
129 }
130 }
131 int numFamilies = in.readInt();
132 if (numFamilies > 0) {
133 if (scopes == null) {
134 scopes = new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
135 }
136 for (int i = 0; i < numFamilies; i++) {
137 byte[] fam = Bytes.readByteArray(in);
138 int scope = in.readInt();
139 scopes.put(fam, scope);
140 }
141 }
142 } else {
143
144
145 this.add(KeyValue.create(versionOrLength, in));
146 }
147
148 }
149
150 public void write(DataOutput out) throws IOException {
151 out.writeInt(VERSION_2);
152 out.writeInt(kvs.size());
153
154 for (KeyValue kv : kvs) {
155 if (compressionContext != null) {
156 KeyValueCompression.writeKV(out, kv, compressionContext);
157 } else{
158 KeyValue.write(kv, out);
159 }
160 }
161 if (scopes == null) {
162 out.writeInt(0);
163 } else {
164 out.writeInt(scopes.size());
165 for (byte[] key : scopes.keySet()) {
166 Bytes.writeByteArray(out, key);
167 out.writeInt(scopes.get(key));
168 }
169 }
170 }
171
172 public long heapSize() {
173 long ret = 0;
174 for (KeyValue kv : kvs) {
175 ret += kv.heapSize();
176 }
177 if (scopes != null) {
178 ret += ClassSize.TREEMAP;
179 ret += ClassSize.align(scopes.size() * ClassSize.MAP_ENTRY);
180
181 }
182 return ret;
183 }
184
185 public String toString() {
186 StringBuilder sb = new StringBuilder();
187
188 sb.append("[#edits: " + kvs.size() + " = <");
189 for (KeyValue kv : kvs) {
190 sb.append(kv.toString());
191 sb.append("; ");
192 }
193 if (scopes != null) {
194 sb.append(" scopes: " + scopes.toString());
195 }
196 sb.append(">]");
197 return sb.toString();
198 }
199
200 }