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.classification.InterfaceAudience;
27  import org.apache.hadoop.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.Cell;
29  import org.apache.hadoop.hbase.KeyValue;
30  import org.apache.hadoop.hbase.KeyValueUtil;
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   * Note that this operation does not appear atomic to readers. Appends are done
39   * under a single row lock, so write operations to a row are synchronized, but
40   * readers do not take row locks so get and scan operations can see this
41   * operation partially completed.
42   * <p>
43   * To append to a set of columns of a row, instantiate an Append object with the
44   * row to append to. At least one column to append must be specified using the
45   * {@link #add(byte[], byte[], byte[])} method.
46   */
47  @InterfaceAudience.Public
48  @InterfaceStability.Stable
49  public class Append extends Mutation {
50    private static final String RETURN_RESULTS = "_rr_";
51    /**
52     * @param returnResults
53     *          True (default) if the append operation should return the results.
54     *          A client that is not interested in the result can save network
55     *          bandwidth setting this to false.
56     */
57    public Append setReturnResults(boolean returnResults) {
58      setAttribute(RETURN_RESULTS, Bytes.toBytes(returnResults));
59      return this;
60    }
61  
62    /**
63     * @return current setting for returnResults
64     */
65    public boolean isReturnResults() {
66      byte[] v = getAttribute(RETURN_RESULTS);
67      return v == null ? true : Bytes.toBoolean(v);
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    }
91  
92    /** Create a Append operation for the specified row.
93     * <p>
94     * At least one column must be appended to.
95     * @param rowArray Makes a copy out of this buffer.
96     * @param rowOffset
97     * @param rowLength
98     */
99    public Append(final byte [] rowArray, final int rowOffset, final int rowLength) {
100     checkRow(rowArray, rowOffset, rowLength);
101     this.row = Bytes.copy(rowArray, rowOffset, rowLength);
102   }
103 
104   /**
105    * Add the specified column and value to this Append operation.
106    * @param family family name
107    * @param qualifier column qualifier
108    * @param value value to append to specified column
109    * @return this
110    */
111   public Append add(byte [] family, byte [] qualifier, byte [] value) {
112     KeyValue kv = new KeyValue(this.row, family, qualifier, this.ts, KeyValue.Type.Put, value);
113     return add(kv);
114   }
115 
116   /**
117    * Add column and value to this Append operation.
118    * @param cell
119    * @return This instance
120    */
121   @SuppressWarnings("unchecked")
122   public Append add(final Cell cell) {
123     // Presume it is KeyValue for now.
124     KeyValue kv = KeyValueUtil.ensureKeyValue(cell);
125     byte [] family = kv.getFamily();
126     List<Cell> list = this.familyMap.get(family);
127     if (list == null) {
128       list  = new ArrayList<Cell>();
129     }
130     // find where the new entry should be placed in the List
131     list.add(kv);
132     this.familyMap.put(family, list);
133     return this;
134   }
135 
136   @Override
137   public Append setAttribute(String name, byte[] value) {
138     return (Append) super.setAttribute(name, value);
139   }
140 
141   @Override
142   public Append setId(String id) {
143     return (Append) super.setId(id);
144   }
145 
146   @Override
147   @Deprecated
148   public Append setWriteToWAL(boolean write) {
149     return (Append) super.setWriteToWAL(write);
150   }
151 
152   @Override
153   public Append setDurability(Durability d) {
154     return (Append) super.setDurability(d);
155   }
156 
157   @Override
158   public Append setFamilyCellMap(NavigableMap<byte[], List<Cell>> map) {
159     return (Append) super.setFamilyCellMap(map);
160   }
161 
162   @Override
163   @Deprecated
164   public Append setFamilyMap(NavigableMap<byte[], List<KeyValue>> map) {
165     return (Append) super.setFamilyMap(map);
166   }
167 
168   @Override
169   public Append setClusterIds(List<UUID> clusterIds) {
170     return (Append) super.setClusterIds(clusterIds);
171   }
172 
173   @Override
174   public Append setCellVisibility(CellVisibility expression) {
175     return (Append) super.setCellVisibility(expression);
176   }
177 
178   @Override
179   public Append setACL(String user, Permission perms) {
180     return (Append) super.setACL(user, perms);
181   }
182 
183   @Override
184   public Append setACL(Map<String, Permission> perms) {
185     return (Append) super.setACL(perms);
186   }
187 }