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.TreeMap;
025import java.util.UUID;
026import org.apache.hadoop.hbase.Cell;
027import org.apache.hadoop.hbase.CellUtil;
028import org.apache.hadoop.hbase.KeyValue;
029import org.apache.hadoop.hbase.io.TimeRange;
030import org.apache.hadoop.hbase.security.access.Permission;
031import org.apache.hadoop.hbase.security.visibility.CellVisibility;
032import org.apache.hadoop.hbase.util.Bytes;
033import org.apache.hadoop.hbase.util.ClassSize;
034import org.apache.yetus.audience.InterfaceAudience;
035
036/**
037 * Used to perform Increment operations on a single row.
038 * <p>
039 * This operation ensures atomicity to readers. Increments are done under a single row lock, so
040 * write operations to a row are synchronized, and readers are guaranteed to see this operation
041 * fully completed.
042 * <p>
043 * To increment columns of a row, instantiate an Increment object with the row to increment. At
044 * least one column to increment must be specified using the
045 * {@link #addColumn(byte[], byte[], long)} method.
046 */
047@InterfaceAudience.Public
048public class Increment extends Mutation {
049  private static final int HEAP_OVERHEAD = ClassSize.REFERENCE + ClassSize.TIMERANGE;
050  private TimeRange tr = TimeRange.allTime();
051
052  /**
053   * Create a Increment operation for the specified row.
054   * <p>
055   * At least one column must be incremented.
056   * @param row row key (we will make a copy of this).
057   */
058  public Increment(byte[] row) {
059    this(row, 0, row.length);
060  }
061
062  /**
063   * Create a Increment operation for the specified row.
064   * <p>
065   * At least one column must be incremented.
066   * @param row row key (we will make a copy of this).
067   */
068  public Increment(final byte[] row, final int offset, final int length) {
069    checkRow(row, offset, length);
070    this.row = Bytes.copy(row, offset, length);
071  }
072
073  /**
074   * Copy constructor
075   * @param incrementToCopy increment to copy
076   */
077  public Increment(Increment incrementToCopy) {
078    super(incrementToCopy);
079    this.tr = incrementToCopy.getTimeRange();
080  }
081
082  /**
083   * Construct the Increment with user defined data. NOTED: 1) all cells in the familyMap must have
084   * the Type.Put 2) the row of each cell must be same with passed row.
085   * @param row       row. CAN'T be null
086   * @param ts        timestamp
087   * @param familyMap the map to collect all cells internally. CAN'T be null
088   */
089  public Increment(byte[] row, long ts, NavigableMap<byte[], List<Cell>> familyMap) {
090    super(row, ts, familyMap);
091  }
092
093  /**
094   * Add the specified KeyValue to this operation.
095   * @param cell individual Cell
096   * @throws java.io.IOException e
097   */
098  public Increment add(Cell cell) throws IOException {
099    super.add(cell);
100    return this;
101  }
102
103  /**
104   * Increment the column from the specific family with the specified qualifier by the specified
105   * amount.
106   * <p>
107   * Overrides previous calls to addColumn for this family and qualifier.
108   * @param family    family name
109   * @param qualifier column qualifier
110   * @param amount    amount to increment by
111   * @return the Increment object
112   */
113  public Increment addColumn(byte[] family, byte[] qualifier, long amount) {
114    if (family == null) {
115      throw new IllegalArgumentException("family cannot be null");
116    }
117    List<Cell> list = getCellList(family);
118    KeyValue kv = createPutKeyValue(family, qualifier, ts, Bytes.toBytes(amount));
119    list.add(kv);
120    return this;
121  }
122
123  /**
124   * Gets the TimeRange used for this increment.
125   */
126  public TimeRange getTimeRange() {
127    return this.tr;
128  }
129
130  /**
131   * Sets the TimeRange to be used on the Get for this increment.
132   * <p>
133   * This is useful for when you have counters that only last for specific periods of time (ie.
134   * counters that are partitioned by time). By setting the range of valid times for this increment,
135   * you can potentially gain some performance with a more optimal Get operation. Be careful adding
136   * the time range to this class as you will update the old cell if the time range doesn't include
137   * the latest cells.
138   * <p>
139   * This range is used as [minStamp, maxStamp).
140   * @param minStamp minimum timestamp value, inclusive
141   * @param maxStamp maximum timestamp value, exclusive
142   * @throws IOException if invalid time range
143   */
144  public Increment setTimeRange(long minStamp, long maxStamp) throws IOException {
145    tr = new TimeRange(minStamp, maxStamp);
146    return this;
147  }
148
149  @Override
150  public Increment setTimestamp(long timestamp) {
151    super.setTimestamp(timestamp);
152    return this;
153  }
154
155  /**
156   * @param returnResults True (default) if the increment operation should return the results. A
157   *                      client that is not interested in the result can save network bandwidth
158   *                      setting this to false.
159   */
160  @Override
161  public Increment setReturnResults(boolean returnResults) {
162    super.setReturnResults(returnResults);
163    return this;
164  }
165
166  /** Returns current setting for returnResults */
167  // This method makes public the superclasses's protected method.
168  @Override
169  public boolean isReturnResults() {
170    return super.isReturnResults();
171  }
172
173  /**
174   * Method for retrieving the number of families to increment from
175   * @return number of families
176   */
177  @Override
178  public int numFamilies() {
179    return this.familyMap.size();
180  }
181
182  /**
183   * Method for checking if any families have been inserted into this Increment
184   * @return true if familyMap is non empty false otherwise
185   */
186  public boolean hasFamilies() {
187    return !this.familyMap.isEmpty();
188  }
189
190  /**
191   * Before 0.95, when you called Increment#getFamilyMap(), you got back a map of families to a list
192   * of Longs. Now, {@link #getFamilyCellMap()} returns families by list of Cells. This method has
193   * been added so you can have the old behavior.
194   * @return Map of families to a Map of qualifiers and their Long increments.
195   * @since 0.95.0
196   */
197  public Map<byte[], NavigableMap<byte[], Long>> getFamilyMapOfLongs() {
198    NavigableMap<byte[], List<Cell>> map = super.getFamilyCellMap();
199    Map<byte[], NavigableMap<byte[], Long>> results = new TreeMap<>(Bytes.BYTES_COMPARATOR);
200    for (Map.Entry<byte[], List<Cell>> entry : map.entrySet()) {
201      NavigableMap<byte[], Long> longs = new TreeMap<>(Bytes.BYTES_COMPARATOR);
202      for (Cell cell : entry.getValue()) {
203        longs.put(CellUtil.cloneQualifier(cell),
204          Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
205      }
206      results.put(entry.getKey(), longs);
207    }
208    return results;
209  }
210
211  /**
212   *   */
213  @Override
214  public String toString() {
215    StringBuilder sb = new StringBuilder();
216    sb.append("row=");
217    sb.append(Bytes.toStringBinary(this.row));
218    if (this.familyMap.isEmpty()) {
219      sb.append(", no columns set to be incremented");
220      return sb.toString();
221    }
222    sb.append(", families=");
223    boolean moreThanOne = false;
224    for (Map.Entry<byte[], List<Cell>> entry : this.familyMap.entrySet()) {
225      if (moreThanOne) {
226        sb.append("), ");
227      } else {
228        moreThanOne = true;
229        sb.append("{");
230      }
231      sb.append("(family=");
232      sb.append(Bytes.toString(entry.getKey()));
233      sb.append(", columns=");
234      if (entry.getValue() == null) {
235        sb.append("NONE");
236      } else {
237        sb.append("{");
238        boolean moreThanOneB = false;
239        for (Cell cell : entry.getValue()) {
240          if (moreThanOneB) {
241            sb.append(", ");
242          } else {
243            moreThanOneB = true;
244          }
245          sb.append(CellUtil.getCellKeyAsString(cell) + "+="
246            + Bytes.toLong(cell.getValueArray(), cell.getValueOffset(), cell.getValueLength()));
247        }
248        sb.append("}");
249      }
250    }
251    sb.append("}");
252    return sb.toString();
253  }
254
255  /**
256   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. No replacement.
257   */
258  @Deprecated
259  @Override
260  public int hashCode() {
261    // TODO: This is wrong. Can't have two gets the same just because on same row. But it
262    // matches how equals works currently and gets rid of the findbugs warning.
263    return Bytes.hashCode(this.getRow());
264  }
265
266  /**
267   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. Use
268   *             {@link Row#COMPARATOR} instead
269   */
270  @Deprecated
271  @Override
272  public boolean equals(Object obj) {
273    // TODO: This is wrong. Can't have two the same just because on same row.
274    if (this == obj) {
275      return true;
276    }
277    if (obj == null || getClass() != obj.getClass()) {
278      return false;
279    }
280    Row other = (Row) obj;
281    return compareTo(other) == 0;
282  }
283
284  @Override
285  protected long extraHeapSize() {
286    return HEAP_OVERHEAD;
287  }
288
289  @Override
290  public Increment setAttribute(String name, byte[] value) {
291    return (Increment) super.setAttribute(name, value);
292  }
293
294  @Override
295  public Increment setId(String id) {
296    return (Increment) super.setId(id);
297  }
298
299  @Override
300  public Increment setDurability(Durability d) {
301    return (Increment) super.setDurability(d);
302  }
303
304  /**
305   * Method for setting the Increment's familyMap
306   * @deprecated As of release 2.0.0, this will be removed in HBase 3.0.0. Use
307   *             {@link Increment#Increment(byte[], long, NavigableMap)} instead
308   */
309  @Deprecated
310  @Override
311  public Increment setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
312    return (Increment) super.setFamilyCellMap(map);
313  }
314
315  @Override
316  public Increment setClusterIds(List<UUID> clusterIds) {
317    return (Increment) super.setClusterIds(clusterIds);
318  }
319
320  @Override
321  public Increment setCellVisibility(CellVisibility expression) {
322    return (Increment) super.setCellVisibility(expression);
323  }
324
325  @Override
326  public Increment setACL(String user, Permission perms) {
327    return (Increment) super.setACL(user, perms);
328  }
329
330  @Override
331  public Increment setACL(Map<String, Permission> perms) {
332    return (Increment) super.setACL(perms);
333  }
334
335  @Override
336  public Increment setTTL(long ttl) {
337    return (Increment) super.setTTL(ttl);
338  }
339
340  @Override
341  public Increment setPriority(int priority) {
342    return (Increment) super.setPriority(priority);
343  }
344}