001/*
002 * Copyright 2010 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 */
020package org.apache.hadoop.hbase.client.coprocessor;
021
022import org.apache.hadoop.hbase.HConstants;
023import org.apache.hadoop.hbase.io.HbaseObjectWritable;
024import org.apache.hadoop.hbase.util.Bytes;
025import org.apache.hadoop.hbase.util.Classes;
026import org.apache.hadoop.io.Writable;
027
028import java.io.DataInput;
029import java.io.DataOutput;
030import java.io.IOException;
031import java.io.Serializable;
032
033/**
034 * Represents the return value from a
035 * {@link org.apache.hadoop.hbase.client.coprocessor.Exec} invocation.
036 * This simply wraps the value for easier
037 * {@link org.apache.hadoop.hbase.io.HbaseObjectWritable}
038 * serialization.
039 *
040 * <p>
041 * This class is used internally by the HBase client code to properly serialize
042 * responses from {@link org.apache.hadoop.hbase.ipc.CoprocessorProtocol}
043 * method invocations.  It should not be used directly by clients.
044 * </p>
045 *
046 * @see Exec
047 * @see org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call)
048 * @see org.apache.hadoop.hbase.client.HTable#coprocessorExec(Class, byte[], byte[], org.apache.hadoop.hbase.client.coprocessor.Batch.Call, org.apache.hadoop.hbase.client.coprocessor.Batch.Callback)
049 */
050public class ExecResult implements Writable {
051  private byte[] regionName;
052  private Object value;
053
054  public ExecResult() {
055  }
056
057  public ExecResult(Object value) {
058    this(HConstants.EMPTY_BYTE_ARRAY, value);
059  }
060  public ExecResult(byte[] region, Object value) {
061    this.regionName = region;
062    this.value = value;
063  }
064
065  public byte[] getRegionName() {
066    return regionName;
067  }
068
069  public Object getValue() {
070    return value;
071  }
072
073  @Override
074  public void write(DataOutput out) throws IOException {
075    Bytes.writeByteArray(out, regionName);
076    HbaseObjectWritable.writeObject(out, value,
077        value != null ? value.getClass() : Writable.class, null);
078  }
079
080  @Override
081  public void readFields(DataInput in) throws IOException {
082    regionName = Bytes.readByteArray(in);
083    value = HbaseObjectWritable.readObject(in, null);
084  }
085}