View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import java.util.Map;
26  import java.util.NavigableMap;
27  import java.util.UUID;
28  
29  import org.apache.hadoop.hbase.Cell;
30  import org.apache.hadoop.hbase.CellUtil;
31  import org.apache.hadoop.hbase.HConstants;
32  import org.apache.hadoop.hbase.KeyValue;
33  import org.apache.hadoop.hbase.classification.InterfaceAudience;
34  import org.apache.hadoop.hbase.classification.InterfaceStability;
35  import org.apache.hadoop.hbase.security.access.Permission;
36  import org.apache.hadoop.hbase.security.visibility.CellVisibility;
37  import org.apache.hadoop.hbase.util.Bytes;
38  
39  /**
40   * Used to perform Delete operations on a single row.
41   * <p>
42   * To delete an entire row, instantiate a Delete object with the row
43   * to delete.  To further define the scope of what to delete, perform
44   * additional methods as outlined below.
45   * <p>
46   * To delete specific families, execute {@link #addFamily(byte[]) deleteFamily}
47   * for each family to delete.
48   * <p>
49   * To delete multiple versions of specific columns, execute
50   * {@link #addColumns(byte[], byte[]) deleteColumns}
51   * for each column to delete.
52   * <p>
53   * To delete specific versions of specific columns, execute
54   * {@link #addColumn(byte[], byte[], long) deleteColumn}
55   * for each column version to delete.
56   * <p>
57   * Specifying timestamps, deleteFamily and deleteColumns will delete all
58   * versions with a timestamp less than or equal to that passed.  If no
59   * timestamp is specified, an entry is added with a timestamp of 'now'
60   * where 'now' is the servers's System.currentTimeMillis().
61   * Specifying a timestamp to the deleteColumn method will
62   * delete versions only with a timestamp equal to that specified.
63   * If no timestamp is passed to deleteColumn, internally, it figures the
64   * most recent cell's timestamp and adds a delete at that timestamp; i.e.
65   * it deletes the most recently added cell.
66   * <p>The timestamp passed to the constructor is used ONLY for delete of
67   * rows.  For anything less -- a deleteColumn, deleteColumns or
68   * deleteFamily -- then you need to use the method overrides that take a
69   * timestamp.  The constructor timestamp is not referenced.
70   */
71  @InterfaceAudience.Public
72  @InterfaceStability.Stable
73  public class Delete extends Mutation implements Comparable<Row> {
74    /**
75     * Create a Delete operation for the specified row.
76     * <p>
77     * If no further operations are done, this will delete everything
78     * associated with the specified row (all versions of all columns in all
79     * families).
80     * @param row row key
81     */
82    public Delete(byte [] row) {
83      this(row, HConstants.LATEST_TIMESTAMP);
84    }
85  
86    /**
87     * Create a Delete operation for the specified row and timestamp.<p>
88     *
89     * If no further operations are done, this will delete all columns in all
90     * families of the specified row with a timestamp less than or equal to the
91     * specified timestamp.<p>
92     *
93     * This timestamp is ONLY used for a delete row operation.  If specifying
94     * families or columns, you must specify each timestamp individually.
95     * @param row row key
96     * @param timestamp maximum version timestamp (only for delete row)
97     */
98    public Delete(byte [] row, long timestamp) {
99      this(row, 0, row.length, timestamp);
100   }
101 
102   /**
103    * Create a Delete operation for the specified row and timestamp.<p>
104    *
105    * If no further operations are done, this will delete all columns in all
106    * families of the specified row with a timestamp less than or equal to the
107    * specified timestamp.<p>
108    *
109    * This timestamp is ONLY used for a delete row operation.  If specifying
110    * families or columns, you must specify each timestamp individually.
111    * @param rowArray We make a local copy of this passed in row.
112    * @param rowOffset
113    * @param rowLength
114    */
115   public Delete(final byte [] rowArray, final int rowOffset, final int rowLength) {
116     this(rowArray, rowOffset, rowLength, HConstants.LATEST_TIMESTAMP);
117   }
118 
119   /**
120    * Create a Delete operation for the specified row and timestamp.<p>
121    *
122    * If no further operations are done, this will delete all columns in all
123    * families of the specified row with a timestamp less than or equal to the
124    * specified timestamp.<p>
125    *
126    * This timestamp is ONLY used for a delete row operation.  If specifying
127    * families or columns, you must specify each timestamp individually.
128    * @param rowArray We make a local copy of this passed in row.
129    * @param rowOffset
130    * @param rowLength
131    * @param ts maximum version timestamp (only for delete row)
132    */
133   public Delete(final byte [] rowArray, final int rowOffset, final int rowLength, long ts) {
134     checkRow(rowArray, rowOffset, rowLength);
135     this.row = Bytes.copy(rowArray, rowOffset, rowLength);
136     setTimestamp(ts);
137   }
138 
139   /**
140    * @param d Delete to clone.
141    */
142   public Delete(final Delete d) {
143     this.row = d.getRow();
144     this.ts = d.getTimeStamp();
145     this.familyMap.putAll(d.getFamilyCellMap());
146     this.durability = d.durability;
147     for (Map.Entry<String, byte[]> entry : d.getAttributesMap().entrySet()) {
148       this.setAttribute(entry.getKey(), entry.getValue());
149     }
150   }
151 
152   /**
153    * Advanced use only.
154    * Add an existing delete marker to this Delete object.
155    * @param kv An existing KeyValue of type "delete".
156    * @return this for invocation chaining
157    * @throws IOException
158    */
159   @SuppressWarnings("unchecked")
160   public Delete addDeleteMarker(Cell kv) throws IOException {
161     // TODO: Deprecate and rename 'add' so it matches how we add KVs to Puts.
162     if (!CellUtil.isDelete(kv)) {
163       throw new IOException("The recently added KeyValue is not of type "
164           + "delete. Rowkey: " + Bytes.toStringBinary(this.row));
165     }
166     if (Bytes.compareTo(this.row, 0, row.length, kv.getRowArray(),
167         kv.getRowOffset(), kv.getRowLength()) != 0) {
168       throw new WrongRowIOException("The row in " + kv.toString() +
169         " doesn't match the original one " +  Bytes.toStringBinary(this.row));
170     }
171     byte [] family = CellUtil.cloneFamily(kv);
172     List<Cell> list = familyMap.get(family);
173     if (list == null) {
174       list = new ArrayList<Cell>();
175     }
176     list.add(kv);
177     familyMap.put(family, list);
178     return this;
179   }
180 
181 
182   /**
183    * Delete all versions of all columns of the specified family.
184    * <p>
185    * Overrides previous calls to deleteColumn and deleteColumns for the
186    * specified family.
187    * @param family family name
188    * @return this for invocation chaining
189    */
190   public Delete addFamily(final byte [] family) {
191     this.addFamily(family, this.ts);
192     return this;
193   }
194 
195   /**
196    * Delete all columns of the specified family with a timestamp less than
197    * or equal to the specified timestamp.
198    * <p>
199    * Overrides previous calls to deleteColumn and deleteColumns for the
200    * specified family.
201    * @param family family name
202    * @param timestamp maximum version timestamp
203    * @return this for invocation chaining
204    */
205   public Delete addFamily(final byte [] family, final long timestamp) {
206     if (timestamp < 0) {
207       throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
208     }
209     List<Cell> list = familyMap.get(family);
210     if(list == null) {
211       list = new ArrayList<Cell>();
212     } else if(!list.isEmpty()) {
213       list.clear();
214     }
215     KeyValue kv = new KeyValue(row, family, null, timestamp, KeyValue.Type.DeleteFamily);
216     list.add(kv);
217     familyMap.put(family, list);
218     return this;
219   }
220 
221   /**
222    * Delete all columns of the specified family with a timestamp equal to
223    * the specified timestamp.
224    * @param family family name
225    * @param timestamp version timestamp
226    * @return this for invocation chaining
227    */
228   public Delete addFamilyVersion(final byte [] family, final long timestamp) {
229     List<Cell> list = familyMap.get(family);
230     if(list == null) {
231       list = new ArrayList<Cell>();
232     }
233     list.add(new KeyValue(row, family, null, timestamp,
234           KeyValue.Type.DeleteFamilyVersion));
235     familyMap.put(family, list);
236     return this;
237   }
238 
239   /**
240    * Delete all versions of the specified column.
241    * @param family family name
242    * @param qualifier column qualifier
243    * @return this for invocation chaining
244    */
245   public Delete addColumns(final byte [] family, final byte [] qualifier) {
246     addColumns(family, qualifier, this.ts);
247     return this;
248   }
249 
250   /**
251    * Delete all versions of the specified column with a timestamp less than
252    * or equal to the specified timestamp.
253    * @param family family name
254    * @param qualifier column qualifier
255    * @param timestamp maximum version timestamp
256    * @return this for invocation chaining
257    */
258   public Delete addColumns(final byte [] family, final byte [] qualifier, final long timestamp) {
259     if (timestamp < 0) {
260       throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
261     }
262     List<Cell> list = familyMap.get(family);
263     if (list == null) {
264       list = new ArrayList<Cell>();
265     }
266     list.add(new KeyValue(this.row, family, qualifier, timestamp,
267         KeyValue.Type.DeleteColumn));
268     familyMap.put(family, list);
269     return this;
270   }
271 
272   /**
273    * Delete the latest version of the specified column.
274    * This is an expensive call in that on the server-side, it first does a
275    * get to find the latest versions timestamp.  Then it adds a delete using
276    * the fetched cells timestamp.
277    * @param family family name
278    * @param qualifier column qualifier
279    * @return this for invocation chaining
280    */
281   public Delete addColumn(final byte [] family, final byte [] qualifier) {
282     this.addColumn(family, qualifier, this.ts);
283     return this;
284   }
285 
286   /**
287    * Delete the specified version of the specified column.
288    * @param family family name
289    * @param qualifier column qualifier
290    * @param timestamp version timestamp
291    * @return this for invocation chaining
292    */
293   public Delete addColumn(byte [] family, byte [] qualifier, long timestamp) {
294     if (timestamp < 0) {
295       throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
296     }
297     List<Cell> list = familyMap.get(family);
298     if(list == null) {
299       list = new ArrayList<Cell>();
300     }
301     KeyValue kv = new KeyValue(this.row, family, qualifier, timestamp, KeyValue.Type.Delete);
302     list.add(kv);
303     familyMap.put(family, list);
304     return this;
305   }
306 
307   /**
308    * Set the timestamp of the delete.
309    *
310    * @param timestamp
311    */
312   public Delete setTimestamp(long timestamp) {
313     if (timestamp < 0) {
314       throw new IllegalArgumentException("Timestamp cannot be negative. ts=" + timestamp);
315     }
316     this.ts = timestamp;
317     return this;
318   }
319 
320   @Override
321   public Map<String, Object> toMap(int maxCols) {
322     // we start with the fingerprint map and build on top of it.
323     Map<String, Object> map = super.toMap(maxCols);
324     // why is put not doing this?
325     map.put("ts", this.ts);
326     return map;
327   }
328 
329   @Override
330   public Delete setAttribute(String name, byte[] value) {
331     return (Delete) super.setAttribute(name, value);
332   }
333 
334   @Override
335   public Delete setId(String id) {
336     return (Delete) super.setId(id);
337   }
338 
339   @Override
340   public Delete setDurability(Durability d) {
341     return (Delete) super.setDurability(d);
342   }
343 
344   @Override
345   public Delete setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
346     return (Delete) super.setFamilyCellMap(map);
347   }
348 
349   @Override
350   public Delete setClusterIds(List<UUID> clusterIds) {
351     return (Delete) super.setClusterIds(clusterIds);
352   }
353 
354   @Override
355   public Delete setCellVisibility(CellVisibility expression) {
356     return (Delete) super.setCellVisibility(expression);
357   }
358 
359   @Override
360   public Delete setACL(String user, Permission perms) {
361     return (Delete) super.setACL(user, perms);
362   }
363 
364   @Override
365   public Delete setACL(Map<String, Permission> perms) {
366     return (Delete) super.setACL(perms);
367   }
368 
369   @Override
370   public Delete setTTL(long ttl) {
371     throw new UnsupportedOperationException("Setting TTLs on Deletes is not supported");
372   }
373 }