View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.client;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.NavigableMap;
24  import java.util.UUID;
25  
26  import org.apache.hadoop.hbase.classification.InterfaceAudience;
27  import org.apache.hadoop.hbase.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.CellUtil;
30  import org.apache.hadoop.hbase.KeyValue;
31  import org.apache.hadoop.hbase.security.access.Permission;
32  import org.apache.hadoop.hbase.security.visibility.CellVisibility;
33  import org.apache.hadoop.hbase.util.Bytes;
34  
35  /**
36   * Performs Append operations on a single row.
37   * <p>
38   * This operation ensures atomicty to readers. Appends are done
39   * under a single row lock, so write operations to a row are synchronized, and
40   * readers are guaranteed to see this operation fully completed.
41   * <p>
42   * To append to a set of columns of a row, instantiate an Append object with the
43   * row to append to. At least one column to append must be specified using the
44   * {@link #add(byte[], byte[], byte[])} method.
45   */
46  @InterfaceAudience.Public
47  @InterfaceStability.Stable
48  public class Append extends Mutation {
49    /**
50     * @param returnResults
51     *          True (default) if the append operation should return the results.
52     *          A client that is not interested in the result can save network
53     *          bandwidth setting this to false.
54     */
55    @Override
56    public Append setReturnResults(boolean returnResults) {
57      super.setReturnResults(returnResults);
58      return this;
59    }
60  
61    /**
62     * @return current setting for returnResults
63     */
64    // This method makes public the superclasses's protected method.
65    @Override
66    public boolean isReturnResults() {
67      return super.isReturnResults();
68    }
69  
70    /**
71     * Create a Append operation for the specified row.
72     * <p>
73     * At least one column must be appended to.
74     * @param row row key; makes a local copy of passed in array.
75     */
76    public Append(byte[] row) {
77      this(row, 0, row.length);
78    }
79    /**
80     * Copy constructor
81     * @param a
82     */
83    public Append(Append a) {
84      this.row = a.getRow();
85      this.ts = a.getTimeStamp();
86      this.familyMap.putAll(a.getFamilyCellMap());
87      for (Map.Entry<String, byte[]> entry : a.getAttributesMap().entrySet()) {
88        this.setAttribute(entry.getKey(), entry.getValue());
89      }
90      this.setPriority(a.getPriority());
91    }
92  
93    /** Create a Append operation for the specified row.
94     * <p>
95     * At least one column must be appended to.
96     * @param rowArray Makes a copy out of this buffer.
97     * @param rowOffset
98     * @param rowLength
99     */
100   public Append(final byte [] rowArray, final int rowOffset, final int rowLength) {
101     checkRow(rowArray, rowOffset, rowLength);
102     this.row = Bytes.copy(rowArray, rowOffset, rowLength);
103   }
104 
105   /**
106    * Add the specified column and value to this Append operation.
107    * @param family family name
108    * @param qualifier column qualifier
109    * @param value value to append to specified column
110    * @return this
111    */
112   public Append add(byte [] family, byte [] qualifier, byte [] value) {
113     KeyValue kv = new KeyValue(this.row, family, qualifier, this.ts, KeyValue.Type.Put, value);
114     return add(kv);
115   }
116 
117   /**
118    * Add column and value to this Append operation.
119    * @param cell
120    * @return This instance
121    */
122   @SuppressWarnings("unchecked")
123   public Append add(final Cell cell) {
124     // Presume it is KeyValue for now.
125     byte [] family = CellUtil.cloneFamily(cell);
126 
127     // Get cell list for the family
128     List<Cell> list = getCellList(family);
129 
130     // find where the new entry should be placed in the List
131     list.add(cell);
132     return this;
133   }
134 
135   @Override
136   public Append setAttribute(String name, byte[] value) {
137     return (Append) super.setAttribute(name, value);
138   }
139 
140   @Override
141   public Append setId(String id) {
142     return (Append) super.setId(id);
143   }
144 
145   @Override
146   @Deprecated
147   public Append setWriteToWAL(boolean write) {
148     return (Append) super.setWriteToWAL(write);
149   }
150 
151   @Override
152   public Append setDurability(Durability d) {
153     return (Append) super.setDurability(d);
154   }
155 
156   @Override
157   public Append setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
158     return (Append) super.setFamilyCellMap(map);
159   }
160 
161   @Override
162   @Deprecated
163   public Append setFamilyMap(NavigableMap<byte[], List<KeyValue>> map) {
164     return (Append) super.setFamilyMap(map);
165   }
166 
167   @Override
168   public Append setClusterIds(List<UUID> clusterIds) {
169     return (Append) super.setClusterIds(clusterIds);
170   }
171 
172   @Override
173   public Append setCellVisibility(CellVisibility expression) {
174     return (Append) super.setCellVisibility(expression);
175   }
176 
177   @Override
178   public Append setACL(String user, Permission perms) {
179     return (Append) super.setACL(user, perms);
180   }
181 
182   @Override
183   public Append setACL(Map<String, Permission> perms) {
184     return (Append) super.setACL(perms);
185   }
186 
187   @Override
188   public Append setPriority(int priority) {
189     return (Append) super.setPriority(priority);
190   }
191 
192   @Override
193   public Append setTTL(long ttl) {
194     return (Append) super.setTTL(ttl);
195   }
196 }