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.io.IOException;
21  import java.util.ArrayList;
22  import java.util.Arrays;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.classification.InterfaceStability;
28  import org.apache.hadoop.hbase.util.Bytes;
29  
30  /**
31   * Performs multiple mutations atomically on a single row.
32   * Currently {@link Put} and {@link Delete} are supported.
33   *
34   * The mutations are performed in the order in which they
35   * were added.
36   * 
37   * <p>We compare and equate mutations based off their row so be careful putting RowMutations
38   * into Sets or using them as keys in Maps.
39   */
40  @InterfaceAudience.Public
41  @InterfaceStability.Evolving
42  public class RowMutations implements Row {
43    private final List<Mutation> mutations = new ArrayList<Mutation>();
44    private byte [] row;
45  
46    /** Constructor for Writable. DO NOT USE */
47    public RowMutations() {}
48  
49    /**
50     * Create an atomic mutation for the specified row.
51     * @param row row key
52     */
53    public RowMutations(byte [] row) {
54      Mutation.checkRow(row);
55      this.row = Bytes.copy(row);
56    }
57  
58    /**
59     * Add a {@link Put} operation to the list of mutations
60     * @param p The {@link Put} to add
61     * @throws IOException
62     */
63    public void add(Put p) throws IOException {
64      internalAdd(p);
65    }
66  
67    /**
68     * Add a {@link Delete} operation to the list of mutations
69     * @param d The {@link Delete} to add
70     * @throws IOException
71     */
72    public void add(Delete d) throws IOException {
73      internalAdd(d);
74    }
75  
76    private void internalAdd(Mutation m) throws IOException {
77      int res = Bytes.compareTo(this.row, m.getRow());
78      if (res != 0) {
79        throw new WrongRowIOException("The row in the recently added Put/Delete <" +
80            Bytes.toStringBinary(m.getRow()) + "> doesn't match the original one <" +
81            Bytes.toStringBinary(this.row) + ">");
82      }
83      mutations.add(m);
84    }
85  
86    @Override
87    public int compareTo(Row i) {
88      return Bytes.compareTo(this.getRow(), i.getRow());
89    }
90  
91    @Override
92    public boolean equals(Object obj) {
93      if (obj == this) return true;
94      if (obj instanceof RowMutations) {
95        RowMutations other = (RowMutations)obj;
96        return compareTo(other) == 0;
97      }
98      return false;
99    }
100 
101   @Override
102   public int hashCode(){
103     return Arrays.hashCode(row);
104   }
105 
106   @Override
107   public byte[] getRow() {
108     return row;
109   }
110 
111   /**
112    * @return An unmodifiable list of the current mutations.
113    */
114   public List<Mutation> getMutations() {
115     return Collections.unmodifiableList(mutations);
116   }
117 }