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 public WALEdit() {
78 }
79
80 public void add(KeyValue kv) {
81 this.kvs.add(kv);
82 }
83
84 public boolean isEmpty() {
85 return kvs.isEmpty();
86 }
87
88 public int size() {
89 return kvs.size();
90 }
91
92 public List<KeyValue> getKeyValues() {
93 return kvs;
94 }
95
96 public NavigableMap<byte[], Integer> getScopes() {
97 return scopes;
98 }
99
100
101 public void setScopes (NavigableMap<byte[], Integer> scopes) {
102
103
104 this.scopes = scopes;
105 }
106
107 public void readFields(DataInput in) throws IOException {
108 kvs.clear();
109 if (scopes != null) {
110 scopes.clear();
111 }
112 int versionOrLength = in.readInt();
113 if (versionOrLength == VERSION_2) {
114
115 int numEdits = in.readInt();
116 for (int idx = 0; idx < numEdits; idx++) {
117 KeyValue kv = new KeyValue();
118 kv.readFields(in);
119 this.add(kv);
120 }
121 int numFamilies = in.readInt();
122 if (numFamilies > 0) {
123 if (scopes == null) {
124 scopes = new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
125 }
126 for (int i = 0; i < numFamilies; i++) {
127 byte[] fam = Bytes.readByteArray(in);
128 int scope = in.readInt();
129 scopes.put(fam, scope);
130 }
131 }
132 } else {
133
134
135 KeyValue kv = new KeyValue();
136 kv.readFields(versionOrLength, in);
137 this.add(kv);
138 }
139
140 }
141
142 public void write(DataOutput out) throws IOException {
143 out.writeInt(VERSION_2);
144 out.writeInt(kvs.size());
145
146 for (KeyValue kv : kvs) {
147 kv.write(out);
148 }
149 if (scopes == null) {
150 out.writeInt(0);
151 } else {
152 out.writeInt(scopes.size());
153 for (byte[] key : scopes.keySet()) {
154 Bytes.writeByteArray(out, key);
155 out.writeInt(scopes.get(key));
156 }
157 }
158 }
159
160 public long heapSize() {
161 long ret = 0;
162 for (KeyValue kv : kvs) {
163 ret += kv.heapSize();
164 }
165 if (scopes != null) {
166 ret += ClassSize.TREEMAP;
167 ret += ClassSize.align(scopes.size() * ClassSize.MAP_ENTRY);
168
169 }
170 return ret;
171 }
172
173 public String toString() {
174 StringBuilder sb = new StringBuilder();
175
176 sb.append("[#edits: " + kvs.size() + " = <");
177 for (KeyValue kv : kvs) {
178 sb.append(kv.toString());
179 sb.append("; ");
180 }
181 if (scopes != null) {
182 sb.append(" scopes: " + scopes.toString());
183 }
184 sb.append(">]");
185 return sb.toString();
186 }
187
188 }