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