001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.client;
019
020import java.io.IOException;
021import java.util.List;
022import java.util.Map;
023import java.util.NavigableMap;
024import java.util.UUID;
025import org.apache.hadoop.hbase.Cell;
026import org.apache.hadoop.hbase.CellBuilder;
027import org.apache.hadoop.hbase.CellBuilderType;
028import org.apache.hadoop.hbase.ExtendedCell;
029import org.apache.hadoop.hbase.HConstants;
030import org.apache.hadoop.hbase.KeyValue;
031import org.apache.hadoop.hbase.security.access.Permission;
032import org.apache.hadoop.hbase.security.visibility.CellVisibility;
033import org.apache.hadoop.hbase.util.Bytes;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * Used to perform Delete operations on a single row.
038 * <p>
039 * To delete an entire row, instantiate a Delete object with the row to delete. To further define
040 * the scope of what to delete, perform additional methods as outlined below.
041 * <p>
042 * To delete specific families, execute {@link #addFamily(byte[]) deleteFamily} for each family to
043 * delete.
044 * <p>
045 * To delete multiple versions of specific columns, execute {@link #addColumns(byte[], byte[])
046 * deleteColumns} for each column to delete.
047 * <p>
048 * To delete specific versions of specific columns, execute {@link #addColumn(byte[], byte[], long)
049 * deleteColumn} for each column version to delete.
050 * <p>
051 * Specifying timestamps, deleteFamily and deleteColumns will delete all versions with a timestamp
052 * less than or equal to that passed. If no timestamp is specified, an entry is added with a
053 * timestamp of 'now' where 'now' is the servers's EnvironmentEdgeManager.currentTime(). Specifying
054 * a timestamp to the deleteColumn method will delete versions only with a timestamp equal to that
055 * specified. If no timestamp is passed to deleteColumn, internally, it figures the most recent
056 * cell's timestamp and adds a delete at that timestamp; i.e. it deletes the most recently added
057 * cell.
058 * <p>
059 * The timestamp passed to the constructor is used ONLY for delete of rows. For anything less -- a
060 * deleteColumn, deleteColumns or deleteFamily -- then you need to use the method overrides that
061 * take a timestamp. The constructor timestamp is not referenced.
062 */
063@InterfaceAudience.Public
064public class Delete extends Mutation {
065  /**
066   * Create a Delete operation for the specified row.
067   * <p>
068   * If no further operations are done, this will delete everything associated with the specified
069   * row (all versions of all columns in all families), with timestamp from current point in time to
070   * the past. Cells defining timestamp for a future point in time (timestamp > current time) will
071   * not be deleted.
072   * @param row row key
073   */
074  public Delete(byte[] row) {
075    this(row, HConstants.LATEST_TIMESTAMP);
076  }
077
078  /**
079   * Create a Delete operation for the specified row and timestamp.
080   * <p>
081   * If no further operations are done, this will delete all columns in all families of the
082   * specified row with a timestamp less than or equal to the specified timestamp.
083   * <p>
084   * This timestamp is ONLY used for a delete row operation. If specifying families or columns, you
085   * must specify each timestamp individually.
086   * @param row       row key
087   * @param timestamp maximum version timestamp (only for delete row)
088   */
089  public Delete(byte[] row, long timestamp) {
090    this(row, 0, row.length, timestamp);
091  }
092
093  /**
094   * Create a Delete operation for the specified row and timestamp.
095   * <p>
096   * If no further operations are done, this will delete all columns in all families of the
097   * specified row with a timestamp less than or equal to the specified timestamp.
098   * <p>
099   * This timestamp is ONLY used for a delete row operation. If specifying families or columns, you
100   * must specify each timestamp individually.
101   * @param row We make a local copy of this passed in row.
102   */
103  public Delete(final byte[] row, final int rowOffset, final int rowLength) {
104    this(row, rowOffset, rowLength, HConstants.LATEST_TIMESTAMP);
105  }
106
107  /**
108   * Create a Delete operation for the specified row and timestamp.
109   * <p>
110   * If no further operations are done, this will delete all columns in all families of the
111   * specified row with a timestamp less than or equal to the specified timestamp.
112   * <p>
113   * This timestamp is ONLY used for a delete row operation. If specifying families or columns, you
114   * must specify each timestamp individually.
115   */
116  public Delete(final byte[] row, final int rowOffset, final int rowLength, long timestamp) {
117    checkRow(row, rowOffset, rowLength);
118    this.row = Bytes.copy(row, rowOffset, rowLength);
119    setTimestamp(timestamp);
120  }
121
122  /**
123   * Create a Delete operation using another Delete as template.
124   * @param deleteToCopy delete to copy
125   */
126  public Delete(final Delete deleteToCopy) {
127    super(deleteToCopy);
128  }
129
130  /**
131   * Construct the Delete with user defined data. NOTED: 1) all cells in the familyMap must have the
132   * delete type. see {@link org.apache.hadoop.hbase.Cell.Type} 2) the row of each cell must be same
133   * with passed row.
134   * @param row       row. CAN'T be null
135   * @param ts        timestamp
136   * @param familyMap the map to collect all cells internally. CAN'T be null
137   */
138  public Delete(byte[] row, long ts, NavigableMap<byte[], List<Cell>> familyMap) {
139    super(row, ts, familyMap);
140  }
141
142  /**
143   * Add an existing delete marker to this Delete object.
144   * @param cell An existing cell of type "delete".
145   * @return this for invocation chaining
146   */
147  @Override
148  public Delete add(Cell cell) throws IOException {
149    super.add(cell);
150    return this;
151  }
152
153  /**
154   * Delete all versions of all columns of the specified family.
155   * <p>
156   * Overrides previous calls to deleteColumn and deleteColumns for the specified family.
157   * @param family family name
158   * @return this for invocation chaining
159   */
160  public Delete addFamily(final byte[] family) {
161    this.addFamily(family, this.ts);
162    return this;
163  }
164
165  /**
166   * Delete all columns of the specified family with a timestamp less than or equal to the specified
167   * timestamp.
168   * <p>
169   * Overrides previous calls to deleteColumn and deleteColumns for the specified family.
170   * @param family    family name
171   * @param timestamp maximum version timestamp
172   * @return this for invocation chaining
173   */
174  public Delete addFamily(final byte[] family, final long timestamp) {
175    checkTimestamp(timestamp);
176    List<ExtendedCell> list = getCellList(family);
177    if (!list.isEmpty()) {
178      list.clear();
179    }
180    KeyValue kv = new KeyValue(row, family, null, timestamp, KeyValue.Type.DeleteFamily);
181    list.add(kv);
182    return this;
183  }
184
185  /**
186   * Delete all columns of the specified family with a timestamp equal to the specified timestamp.
187   * @param family    family name
188   * @param timestamp version timestamp
189   * @return this for invocation chaining
190   */
191  public Delete addFamilyVersion(final byte[] family, final long timestamp) {
192    checkTimestamp(ts);
193    List<ExtendedCell> list = getCellList(family);
194    list.add(new KeyValue(row, family, null, timestamp, KeyValue.Type.DeleteFamilyVersion));
195    return this;
196  }
197
198  /**
199   * Delete all versions of the specified column.
200   * @param family    family name
201   * @param qualifier column qualifier
202   * @return this for invocation chaining
203   */
204  public Delete addColumns(final byte[] family, final byte[] qualifier) {
205    addColumns(family, qualifier, this.ts);
206    return this;
207  }
208
209  /**
210   * Delete all versions of the specified column with a timestamp less than or equal to the
211   * specified timestamp.
212   * @param family    family name
213   * @param qualifier column qualifier
214   * @param timestamp maximum version timestamp
215   * @return this for invocation chaining
216   */
217  public Delete addColumns(final byte[] family, final byte[] qualifier, final long timestamp) {
218    checkTimestamp(ts);
219    List<ExtendedCell> list = getCellList(family);
220    list.add(new KeyValue(this.row, family, qualifier, timestamp, KeyValue.Type.DeleteColumn));
221    return this;
222  }
223
224  /**
225   * Delete the latest version of the specified column. This is an expensive call in that on the
226   * server-side, it first does a get to find the latest versions timestamp. Then it adds a delete
227   * using the fetched cells timestamp.
228   * @param family    family name
229   * @param qualifier column qualifier
230   * @return this for invocation chaining
231   */
232  public Delete addColumn(final byte[] family, final byte[] qualifier) {
233    this.addColumn(family, qualifier, this.ts);
234    return this;
235  }
236
237  /**
238   * Delete the specified version of the specified column.
239   * @param family    family name
240   * @param qualifier column qualifier
241   * @param timestamp version timestamp
242   * @return this for invocation chaining
243   */
244  public Delete addColumn(byte[] family, byte[] qualifier, long timestamp) {
245    checkTimestamp(ts);
246    List<ExtendedCell> list = getCellList(family);
247    KeyValue kv = new KeyValue(this.row, family, qualifier, timestamp, KeyValue.Type.Delete);
248    list.add(kv);
249    return this;
250  }
251
252  @Override
253  public Delete setTimestamp(long timestamp) {
254    super.setTimestamp(timestamp);
255    return this;
256  }
257
258  @Override
259  public Delete setAttribute(String name, byte[] value) {
260    return (Delete) super.setAttribute(name, value);
261  }
262
263  @Override
264  public Delete setId(String id) {
265    return (Delete) super.setId(id);
266  }
267
268  @Override
269  public Delete setDurability(Durability d) {
270    return (Delete) super.setDurability(d);
271  }
272
273  @Override
274  public Delete setClusterIds(List<UUID> clusterIds) {
275    return (Delete) super.setClusterIds(clusterIds);
276  }
277
278  @Override
279  public Delete setCellVisibility(CellVisibility expression) {
280    return (Delete) super.setCellVisibility(expression);
281  }
282
283  @Override
284  public Delete setACL(String user, Permission perms) {
285    return (Delete) super.setACL(user, perms);
286  }
287
288  @Override
289  public Delete setACL(Map<String, Permission> perms) {
290    return (Delete) super.setACL(perms);
291  }
292
293  @Override
294  public Delete setTTL(long ttl) {
295    throw new UnsupportedOperationException("Setting TTLs on Deletes is not supported");
296  }
297
298  @Override
299  public Delete setPriority(int priority) {
300    return (Delete) super.setPriority(priority);
301  }
302
303  @Override
304  public CellBuilder getCellBuilder(CellBuilderType type) {
305    return getCellBuilder(type, Cell.Type.Delete);
306  }
307}