1 /*
2 * Copyright 2010 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package org.apache.hadoop.hbase.client;
22
23 import org.apache.hadoop.hbase.HConstants;
24 import org.apache.hadoop.hbase.KeyValue;
25 import org.apache.hadoop.hbase.util.Bytes;
26 import org.apache.hadoop.io.Writable;
27
28 import java.io.DataInput;
29 import java.io.DataOutput;
30 import java.io.IOException;
31 import java.util.ArrayList;
32 import java.util.List;
33 import java.util.Map;
34
35 /**
36 * Used to perform Delete operations on a single row.
37 * <p>
38 * To delete an entire row, instantiate a Delete object with the row
39 * to delete. To further define the scope of what to delete, perform
40 * additional methods as outlined below.
41 * <p>
42 * To delete specific families, execute {@link #deleteFamily(byte[]) deleteFamily}
43 * for each family to delete.
44 * <p>
45 * To delete multiple versions of specific columns, execute
46 * {@link #deleteColumns(byte[], byte[]) deleteColumns}
47 * for each column to delete.
48 * <p>
49 * To delete specific versions of specific columns, execute
50 * {@link #deleteColumn(byte[], byte[], long) deleteColumn}
51 * for each column version to delete.
52 * <p>
53 * Specifying timestamps, deleteFamily and deleteColumns will delete all
54 * versions with a timestamp less than or equal to that passed. If no
55 * timestamp is specified, an entry is added with a timestamp of 'now'
56 * where 'now' is the servers's System.currentTimeMillis().
57 * Specifying a timestamp to the deleteColumn method will
58 * delete versions only with a timestamp equal to that specified.
59 * If no timestamp is passed to deleteColumn, internally, it figures the
60 * most recent cell's timestamp and adds a delete at that timestamp; i.e.
61 * it deletes the most recently added cell.
62 * <p>The timestamp passed to the constructor is used ONLY for delete of
63 * rows. For anything less -- a deleteColumn, deleteColumns or
64 * deleteFamily -- then you need to use the method overrides that take a
65 * timestamp. The constructor timestamp is not referenced.
66 */
67 public class Delete extends Mutation
68 implements Writable, Row, Comparable<Row> {
69 private static final byte DELETE_VERSION = (byte)3;
70
71 /** Constructor for Writable. DO NOT USE */
72 public Delete() {
73 this((byte [])null);
74 }
75
76 /**
77 * Create a Delete operation for the specified row.
78 * <p>
79 * If no further operations are done, this will delete everything
80 * associated with the specified row (all versions of all columns in all
81 * families).
82 * @param row row key
83 */
84 public Delete(byte [] row) {
85 this(row, HConstants.LATEST_TIMESTAMP, null);
86 }
87
88 /**
89 * Create a Delete operation for the specified row and timestamp, using
90 * an optional row lock.<p>
91 *
92 * If no further operations are done, this will delete all columns in all
93 * families of the specified row with a timestamp less than or equal to the
94 * specified timestamp.<p>
95 *
96 * This timestamp is ONLY used for a delete row operation. If specifying
97 * families or columns, you must specify each timestamp individually.
98 * @param row row key
99 * @param timestamp maximum version timestamp (only for delete row)
100 * @param rowLock previously acquired row lock, or null
101 */
102 public Delete(byte [] row, long timestamp, RowLock rowLock) {
103 this.row = row;
104 this.ts = timestamp;
105 if (rowLock != null) {
106 this.lockId = rowLock.getLockId();
107 }
108 }
109
110 /**
111 * @param d Delete to clone.
112 */
113 public Delete(final Delete d) {
114 this.row = d.getRow();
115 this.ts = d.getTimeStamp();
116 this.lockId = d.getLockId();
117 this.familyMap.putAll(d.getFamilyMap());
118 this.writeToWAL = d.writeToWAL;
119 }
120
121 /**
122 * Delete all versions of all columns of the specified family.
123 * <p>
124 * Overrides previous calls to deleteColumn and deleteColumns for the
125 * specified family.
126 * @param family family name
127 * @return this for invocation chaining
128 */
129 public Delete deleteFamily(byte [] family) {
130 this.deleteFamily(family, HConstants.LATEST_TIMESTAMP);
131 return this;
132 }
133
134 /**
135 * Delete all columns of the specified family with a timestamp less than
136 * or equal to the specified timestamp.
137 * <p>
138 * Overrides previous calls to deleteColumn and deleteColumns for the
139 * specified family.
140 * @param family family name
141 * @param timestamp maximum version timestamp
142 * @return this for invocation chaining
143 */
144 public Delete deleteFamily(byte [] family, long timestamp) {
145 List<KeyValue> list = familyMap.get(family);
146 if(list == null) {
147 list = new ArrayList<KeyValue>();
148 } else if(!list.isEmpty()) {
149 list.clear();
150 }
151 list.add(new KeyValue(row, family, null, timestamp, KeyValue.Type.DeleteFamily));
152 familyMap.put(family, list);
153 return this;
154 }
155
156 /**
157 * Delete all versions of the specified column.
158 * @param family family name
159 * @param qualifier column qualifier
160 * @return this for invocation chaining
161 */
162 public Delete deleteColumns(byte [] family, byte [] qualifier) {
163 this.deleteColumns(family, qualifier, HConstants.LATEST_TIMESTAMP);
164 return this;
165 }
166
167 /**
168 * Delete all versions of the specified column with a timestamp less than
169 * or equal to the specified timestamp.
170 * @param family family name
171 * @param qualifier column qualifier
172 * @param timestamp maximum version timestamp
173 * @return this for invocation chaining
174 */
175 public Delete deleteColumns(byte [] family, byte [] qualifier, long timestamp) {
176 List<KeyValue> list = familyMap.get(family);
177 if (list == null) {
178 list = new ArrayList<KeyValue>();
179 }
180 list.add(new KeyValue(this.row, family, qualifier, timestamp,
181 KeyValue.Type.DeleteColumn));
182 familyMap.put(family, list);
183 return this;
184 }
185
186 /**
187 * Delete the latest version of the specified column.
188 * This is an expensive call in that on the server-side, it first does a
189 * get to find the latest versions timestamp. Then it adds a delete using
190 * the fetched cells timestamp.
191 * @param family family name
192 * @param qualifier column qualifier
193 * @return this for invocation chaining
194 */
195 public Delete deleteColumn(byte [] family, byte [] qualifier) {
196 this.deleteColumn(family, qualifier, HConstants.LATEST_TIMESTAMP);
197 return this;
198 }
199
200 /**
201 * Delete the specified version of the specified column.
202 * @param family family name
203 * @param qualifier column qualifier
204 * @param timestamp version timestamp
205 * @return this for invocation chaining
206 */
207 public Delete deleteColumn(byte [] family, byte [] qualifier, long timestamp) {
208 List<KeyValue> list = familyMap.get(family);
209 if(list == null) {
210 list = new ArrayList<KeyValue>();
211 }
212 list.add(new KeyValue(
213 this.row, family, qualifier, timestamp, KeyValue.Type.Delete));
214 familyMap.put(family, list);
215 return this;
216 }
217
218 /**
219 * Set the timestamp of the delete.
220 *
221 * @param timestamp
222 */
223 public void setTimestamp(long timestamp) {
224 this.ts = timestamp;
225 }
226
227 @Override
228 public Map<String, Object> toMap(int maxCols) {
229 // we start with the fingerprint map and build on top of it.
230 Map<String, Object> map = super.toMap(maxCols);
231 // why is put not doing this?
232 map.put("ts", this.ts);
233 return map;
234 }
235
236 //Writable
237 public void readFields(final DataInput in) throws IOException {
238 int version = in.readByte();
239 if (version > DELETE_VERSION) {
240 throw new IOException("version not supported");
241 }
242 this.row = Bytes.readByteArray(in);
243 this.ts = in.readLong();
244 this.lockId = in.readLong();
245 if (version > 2) {
246 this.writeToWAL = in.readBoolean();
247 }
248 this.familyMap.clear();
249 int numFamilies = in.readInt();
250 for(int i=0;i<numFamilies;i++) {
251 byte [] family = Bytes.readByteArray(in);
252 int numColumns = in.readInt();
253 List<KeyValue> list = new ArrayList<KeyValue>(numColumns);
254 for(int j=0;j<numColumns;j++) {
255 KeyValue kv = new KeyValue();
256 kv.readFields(in);
257 list.add(kv);
258 }
259 this.familyMap.put(family, list);
260 }
261 if (version > 1) {
262 readAttributes(in);
263 }
264 }
265
266 public void write(final DataOutput out) throws IOException {
267 out.writeByte(DELETE_VERSION);
268 Bytes.writeByteArray(out, this.row);
269 out.writeLong(this.ts);
270 out.writeLong(this.lockId);
271 out.writeBoolean(this.writeToWAL);
272 out.writeInt(familyMap.size());
273 for(Map.Entry<byte [], List<KeyValue>> entry : familyMap.entrySet()) {
274 Bytes.writeByteArray(out, entry.getKey());
275 List<KeyValue> list = entry.getValue();
276 out.writeInt(list.size());
277 for(KeyValue kv : list) {
278 kv.write(out);
279 }
280 }
281 writeAttributes(out);
282 }
283 }