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