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    public Append setReturnResults(boolean returnResults) {
56      super.setReturnResults(returnResults);
57      return this;
58    }
59  
60    /**
61     * @return current setting for returnResults
62     */
63    // This method makes public the superclasses's protected method.
64    public boolean isReturnResults() {
65      return super.isReturnResults();
66    }
67  
68    /**
69     * Create a Append operation for the specified row.
70     * <p>
71     * At least one column must be appended to.
72     * @param row row key; makes a local copy of passed in array.
73     */
74    public Append(byte[] row) {
75      this(row, 0, row.length);
76    }
77    /**
78     * Copy constructor
79     * @param a
80     */
81    public Append(Append a) {
82      this.row = a.getRow();
83      this.ts = a.getTimeStamp();
84      this.familyMap.putAll(a.getFamilyCellMap());
85      for (Map.Entry<String, byte[]> entry : a.getAttributesMap().entrySet()) {
86        this.setAttribute(entry.getKey(), entry.getValue());
87      }
88    }
89  
90    /** Create a Append operation for the specified row.
91     * <p>
92     * At least one column must be appended to.
93     * @param rowArray Makes a copy out of this buffer.
94     * @param rowOffset
95     * @param rowLength
96     */
97    public Append(final byte [] rowArray, final int rowOffset, final int rowLength) {
98      checkRow(rowArray, rowOffset, rowLength);
99      this.row = Bytes.copy(rowArray, rowOffset, rowLength);
100   }
101 
102   /**
103    * Add the specified column and value to this Append operation.
104    * @param family family name
105    * @param qualifier column qualifier
106    * @param value value to append to specified column
107    * @return this
108    */
109   public Append add(byte [] family, byte [] qualifier, byte [] value) {
110     KeyValue kv = new KeyValue(this.row, family, qualifier, this.ts, KeyValue.Type.Put, value);
111     return add(kv);
112   }
113 
114   /**
115    * Add column and value to this Append operation.
116    * @param cell
117    * @return This instance
118    */
119   @SuppressWarnings("unchecked")
120   public Append add(final Cell cell) {
121     // Presume it is KeyValue for now.
122     byte [] family = CellUtil.cloneFamily(cell);
123     List<Cell> list = this.familyMap.get(family);
124     if (list == null) {
125       list  = new ArrayList<Cell>();
126     }
127     // find where the new entry should be placed in the List
128     list.add(cell);
129     this.familyMap.put(family, list);
130     return this;
131   }
132 
133   @Override
134   public Append setAttribute(String name, byte[] value) {
135     return (Append) super.setAttribute(name, value);
136   }
137 
138   @Override
139   public Append setId(String id) {
140     return (Append) super.setId(id);
141   }
142 
143   @Override
144   @Deprecated
145   public Append setWriteToWAL(boolean write) {
146     return (Append) super.setWriteToWAL(write);
147   }
148 
149   @Override
150   public Append setDurability(Durability d) {
151     return (Append) super.setDurability(d);
152   }
153 
154   @Override
155   public Append setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
156     return (Append) super.setFamilyCellMap(map);
157   }
158 
159   @Override
160   @Deprecated
161   public Append setFamilyMap(NavigableMap<byte[], List<KeyValue>> map) {
162     return (Append) super.setFamilyMap(map);
163   }
164 
165   @Override
166   public Append setClusterIds(List<UUID> clusterIds) {
167     return (Append) super.setClusterIds(clusterIds);
168   }
169 
170   @Override
171   public Append setCellVisibility(CellVisibility expression) {
172     return (Append) super.setCellVisibility(expression);
173   }
174 
175   @Override
176   public Append setACL(String user, Permission perms) {
177     return (Append) super.setACL(user, perms);
178   }
179 
180   @Override
181   public Append setACL(Map<String, Permission> perms) {
182     return (Append) super.setACL(perms);
183   }
184 
185   @Override
186   public Append setTTL(long ttl) {
187     return (Append) super.setTTL(ttl);
188   }
189 }