View Javadoc

1   /*
2    *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   */
19  
20  package org.apache.hadoop.hbase.client;
21  
22  import org.apache.hadoop.hbase.HConstants;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.util.Bytes;
26  import org.apache.hadoop.hbase.util.ClassSize;
27  
28  import java.util.Collections;
29  import java.util.HashMap;
30  import java.util.Map;
31  
32  @InterfaceAudience.Public
33  @InterfaceStability.Evolving
34  public abstract class OperationWithAttributes extends Operation implements Attributes {
35    // An opaque blob of attributes
36    private Map<String, byte[]> attributes;
37  
38    // used for uniquely identifying an operation
39    public static final String ID_ATRIBUTE = "_operation.attributes.id";
40    private int priority = HConstants.PRIORITY_UNSET;
41  
42    @Override
43    public OperationWithAttributes setAttribute(String name, byte[] value) {
44      if (attributes == null && value == null) {
45        return this;
46      }
47  
48      if (attributes == null) {
49        attributes = new HashMap<String, byte[]>();
50      }
51  
52      if (value == null) {
53        attributes.remove(name);
54        if (attributes.isEmpty()) {
55          this.attributes = null;
56        }
57      } else {
58        attributes.put(name, value);
59      }
60      return this;
61    }
62  
63    @Override
64    public byte[] getAttribute(String name) {
65      if (attributes == null) {
66        return null;
67      }
68  
69      return attributes.get(name);
70    }
71  
72    @Override
73    public Map<String, byte[]> getAttributesMap() {
74      if (attributes == null) {
75        return Collections.emptyMap();
76      }
77      return Collections.unmodifiableMap(attributes);
78    }
79  
80    protected long getAttributeSize() {
81      long size = 0;
82      if (attributes != null) {
83        size += ClassSize.align(this.attributes.size() * ClassSize.MAP_ENTRY);
84        for(Map.Entry<String, byte[]> entry : this.attributes.entrySet()) {
85          size += ClassSize.align(ClassSize.STRING + entry.getKey().length());
86          size += ClassSize.align(ClassSize.ARRAY + entry.getValue().length);
87        }
88      }
89      return size;
90    }
91  
92    /**
93     * This method allows you to set an identifier on an operation. The original
94     * motivation for this was to allow the identifier to be used in slow query
95     * logging, but this could obviously be useful in other places. One use of
96     * this could be to put a class.method identifier in here to see where the
97     * slow query is coming from.
98     * @param id
99     *          id to set for the scan
100    */
101   public OperationWithAttributes setId(String id) {
102     setAttribute(ID_ATRIBUTE, Bytes.toBytes(id));
103     return this;
104   }
105 
106   /**
107    * This method allows you to retrieve the identifier for the operation if one
108    * was set.
109    * @return the id or null if not set
110    */
111   public String getId() {
112     byte[] attr = getAttribute(ID_ATRIBUTE);
113     return attr == null? null: Bytes.toString(attr);
114   }
115 
116   public OperationWithAttributes setPriority(int priority) {
117     this.priority = priority;
118     return this;
119   }
120 
121   public int getPriority() {
122     return priority;
123   }
124 }