HBASE-26921 Rewrite the counting cells part in TestMultiVersions (#4316)
[hbase.git] / hbase-client / src / main / java / org / apache / hadoop / hbase / client / OperationWithAttributes.java
blob7342e65bb31689e6c4ed96ef90a156e46a48db9c
1 /*
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 package org.apache.hadoop.hbase.client;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.TreeMap;
26 import java.util.stream.Collectors;
27 import org.apache.hadoop.hbase.HConstants;
28 import org.apache.hadoop.hbase.util.Bytes;
29 import org.apache.hadoop.hbase.util.ClassSize;
30 import org.apache.yetus.audience.InterfaceAudience;
32 @InterfaceAudience.Public
33 public abstract class OperationWithAttributes extends Operation implements Attributes {
34 // An opaque blob of attributes
35 private Map<String, byte[]> attributes;
37 // used for uniquely identifying an operation
38 public static final String ID_ATRIBUTE = "_operation.attributes.id";
39 private int priority = HConstants.PRIORITY_UNSET;
41 /**
42 * empty construction.
43 * We need this empty construction to keep binary compatibility.
45 protected OperationWithAttributes() {
48 protected OperationWithAttributes(OperationWithAttributes clone) {
49 this.attributes = clone.getAttributesMap() == null ? null :
50 clone.getAttributesMap().entrySet().stream()
51 .collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), (k, v) -> {
52 throw new RuntimeException("collisions!!!");
53 }, () -> new TreeMap<>()));
54 this.priority = clone.getPriority();
57 @Override
58 public OperationWithAttributes setAttribute(String name, byte[] value) {
59 if (attributes == null && value == null) {
60 return this;
63 if (attributes == null) {
64 attributes = new HashMap<>();
67 if (value == null) {
68 attributes.remove(name);
69 if (attributes.isEmpty()) {
70 this.attributes = null;
72 } else {
73 attributes.put(name, value);
75 return this;
78 @Override
79 public byte[] getAttribute(String name) {
80 if (attributes == null) {
81 return null;
84 return attributes.get(name);
87 @Override
88 public Map<String, byte[]> getAttributesMap() {
89 if (attributes == null) {
90 return Collections.emptyMap();
92 return Collections.unmodifiableMap(attributes);
95 protected long getAttributeSize() {
96 long size = 0;
97 if (attributes != null) {
98 size += ClassSize.align(this.attributes.size() * ClassSize.MAP_ENTRY);
99 for(Map.Entry<String, byte[]> entry : this.attributes.entrySet()) {
100 size += ClassSize.align(ClassSize.STRING + entry.getKey().length());
101 size += ClassSize.align(ClassSize.ARRAY + entry.getValue().length);
104 return size;
108 * This method allows you to set an identifier on an operation. The original
109 * motivation for this was to allow the identifier to be used in slow query
110 * logging, but this could obviously be useful in other places. One use of
111 * this could be to put a class.method identifier in here to see where the
112 * slow query is coming from.
113 * @param id
114 * id to set for the scan
116 public OperationWithAttributes setId(String id) {
117 setAttribute(ID_ATRIBUTE, Bytes.toBytes(id));
118 return this;
122 * This method allows you to retrieve the identifier for the operation if one
123 * was set.
124 * @return the id or null if not set
126 public String getId() {
127 byte[] attr = getAttribute(ID_ATRIBUTE);
128 return attr == null? null: Bytes.toString(attr);
131 public OperationWithAttributes setPriority(int priority) {
132 this.priority = priority;
133 return this;
136 public int getPriority() {
137 return priority;