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