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.KeyValue;
31 import org.apache.hadoop.hbase.util.Bytes;
32 import org.apache.hadoop.hbase.util.ClassSize;
33 import org.apache.hadoop.io.Writable;
34
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 public class WALEdit implements Writable {
70
71 private final int VERSION_2 = -1;
72
73 private final ArrayList<KeyValue> kvs = new ArrayList<KeyValue>();
74 private NavigableMap<byte[], Integer> scopes;
75
76 public WALEdit() {
77 }
78
79 public void add(KeyValue kv) {
80 this.kvs.add(kv);
81 }
82
83 public boolean isEmpty() {
84 return kvs.isEmpty();
85 }
86
87 public int size() {
88 return kvs.size();
89 }
90
91 public List<KeyValue> getKeyValues() {
92 return kvs;
93 }
94
95 public NavigableMap<byte[], Integer> getScopes() {
96 return scopes;
97 }
98
99
100 public void setScopes (NavigableMap<byte[], Integer> scopes) {
101
102
103 this.scopes = scopes;
104 }
105
106 public void readFields(DataInput in) throws IOException {
107 kvs.clear();
108 if (scopes != null) {
109 scopes.clear();
110 }
111 int versionOrLength = in.readInt();
112 if (versionOrLength == VERSION_2) {
113
114 int numEdits = in.readInt();
115 for (int idx = 0; idx < numEdits; idx++) {
116 KeyValue kv = new KeyValue();
117 kv.readFields(in);
118 this.add(kv);
119 }
120 int numFamilies = in.readInt();
121 if (numFamilies > 0) {
122 if (scopes == null) {
123 scopes = new TreeMap<byte[], Integer>(Bytes.BYTES_COMPARATOR);
124 }
125 for (int i = 0; i < numFamilies; i++) {
126 byte[] fam = Bytes.readByteArray(in);
127 int scope = in.readInt();
128 scopes.put(fam, scope);
129 }
130 }
131 } else {
132
133
134 KeyValue kv = new KeyValue();
135 kv.readFields(versionOrLength, in);
136 this.add(kv);
137 }
138
139 }
140
141 public void write(DataOutput out) throws IOException {
142 out.writeInt(VERSION_2);
143 out.writeInt(kvs.size());
144
145 for (KeyValue kv : kvs) {
146 kv.write(out);
147 }
148 if (scopes == null) {
149 out.writeInt(0);
150 } else {
151 out.writeInt(scopes.size());
152 for (byte[] key : scopes.keySet()) {
153 Bytes.writeByteArray(out, key);
154 out.writeInt(scopes.get(key));
155 }
156 }
157
158 }
159
160 public String toString() {
161 StringBuilder sb = new StringBuilder();
162
163 sb.append("[#edits: " + kvs.size() + " = <");
164 for (KeyValue kv : kvs) {
165 sb.append(kv.toString());
166 sb.append("; ");
167 }
168 if (scopes != null) {
169 sb.append(" scopes: " + scopes.toString());
170 }
171 sb.append(">]");
172 return sb.toString();
173 }
174
175 }