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