001/*
002 * Copyright 2011 The Apache Software Foundation
003 *
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *     http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020
021package org.apache.hadoop.hbase.client;
022
023import java.io.DataInput;
024import java.io.DataOutput;
025import java.io.IOException;
026import java.util.Collections;
027import java.util.HashMap;
028import java.util.Map;
029
030import org.apache.hadoop.hbase.util.Bytes;
031import org.apache.hadoop.hbase.util.ClassSize;
032import org.apache.hadoop.io.WritableUtils;
033
034public abstract class OperationWithAttributes extends Operation implements Attributes {
035  // a opaque blob of attributes
036  private Map<String, byte[]> attributes;
037
038  // used for uniquely identifying an operation
039  static public String ID_ATRIBUTE = "_operation.attributes.id";
040
041  public void setAttribute(String name, byte[] value) {
042    if (attributes == null && value == null) {
043      return;
044    }
045
046    if (attributes == null) {
047      attributes = new HashMap<String, byte[]>();
048    }
049
050    if (value == null) {
051      attributes.remove(name);
052      if (attributes.isEmpty()) {
053        this.attributes = null;
054      }
055    } else {
056      attributes.put(name, value);
057    }
058  }
059
060  public byte[] getAttribute(String name) {
061    if (attributes == null) {
062      return null;
063    }
064
065    return attributes.get(name);
066  }
067
068  public Map<String, byte[]> getAttributesMap() {
069    if (attributes == null) {
070      return Collections.emptyMap();
071    }
072    return Collections.unmodifiableMap(attributes);
073  }
074
075  protected long getAttributeSize() {
076    long size = 0;
077    if (attributes != null) {
078      size += ClassSize.align(this.attributes.size() * ClassSize.MAP_ENTRY);
079      for(Map.Entry<String, byte[]> entry : this.attributes.entrySet()) {
080        size += ClassSize.align(ClassSize.STRING + entry.getKey().length());
081        size += ClassSize.align(ClassSize.ARRAY + entry.getValue().length);
082      }
083    }
084    return size;
085  }
086
087  protected void writeAttributes(final DataOutput out) throws IOException {
088    if (this.attributes == null) {
089      out.writeInt(0);
090    } else {
091      out.writeInt(this.attributes.size());
092      for (Map.Entry<String, byte[]> attr : this.attributes.entrySet()) {
093        WritableUtils.writeString(out, attr.getKey());
094        Bytes.writeByteArray(out, attr.getValue());
095      }
096    }
097  }
098  
099  protected void readAttributes(final DataInput in) throws IOException {
100    int numAttributes = in.readInt();
101    if (numAttributes > 0) {
102      this.attributes = new HashMap<String, byte[]>(numAttributes);
103      for(int i=0; i<numAttributes; i++) {
104        String name = WritableUtils.readString(in);
105        byte[] value = Bytes.readByteArray(in);
106        this.attributes.put(name, value);
107      }
108    }
109  }
110
111  /**
112   * This method allows you to set an identifier on an operation. The original
113   * motivation for this was to allow the identifier to be used in slow query
114   * logging, but this could obviously be useful in other places. One use of
115   * this could be to put a class.method identifier in here to see where the
116   * slow query is coming from.
117   * @param id
118   *          id to set for the scan
119   */
120  public void setId(String id) {
121    setAttribute(ID_ATRIBUTE, Bytes.toBytes(id));
122  }
123
124  /**
125   * This method allows you to retrieve the identifier for the operation if one
126   * was set.
127   * @return the id or null if not set
128   */
129  public String getId() {
130    byte[] attr = getAttribute(ID_ATRIBUTE);
131    return attr == null? null: Bytes.toString(attr);
132  }
133}