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
;
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
;
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();
58 public OperationWithAttributes
setAttribute(String name
, byte[] value
) {
59 if (attributes
== null && value
== null) {
63 if (attributes
== null) {
64 attributes
= new HashMap
<>();
68 attributes
.remove(name
);
69 if (attributes
.isEmpty()) {
70 this.attributes
= null;
73 attributes
.put(name
, value
);
79 public byte[] getAttribute(String name
) {
80 if (attributes
== null) {
84 return attributes
.get(name
);
88 public Map
<String
, byte[]> getAttributesMap() {
89 if (attributes
== null) {
90 return Collections
.emptyMap();
92 return Collections
.unmodifiableMap(attributes
);
95 protected long getAttributeSize() {
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
);
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.
114 * id to set for the scan
116 public OperationWithAttributes
setId(String id
) {
117 setAttribute(ID_ATRIBUTE
, Bytes
.toBytes(id
));
122 * This method allows you to retrieve the identifier for the operation if one
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
;
136 public int getPriority() {