001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.mapreduce;
019
020import java.io.IOException;
021import java.io.InputStream;
022import java.io.OutputStream;
023
024import org.apache.hadoop.hbase.client.Delete;
025import org.apache.hadoop.hbase.client.Mutation;
026import org.apache.hadoop.hbase.client.Put;
027import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
028import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto;
029import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto.MutationType;
030import org.apache.hadoop.io.serializer.Deserializer;
031import org.apache.hadoop.io.serializer.Serialization;
032import org.apache.hadoop.io.serializer.Serializer;
033
034public class MutationSerialization implements Serialization<Mutation> {
035  @Override
036  public boolean accept(Class<?> c) {
037    return Mutation.class.isAssignableFrom(c);
038  }
039
040  @Override
041  public Deserializer<Mutation> getDeserializer(Class<Mutation> c) {
042    return new MutationDeserializer();
043  }
044
045  @Override
046  public Serializer<Mutation> getSerializer(Class<Mutation> c) {
047    return new MutationSerializer();
048  }
049
050  private static class MutationDeserializer implements Deserializer<Mutation> {
051    private InputStream in;
052
053    @Override
054    public void close() throws IOException {
055      in.close();
056    }
057
058    @Override
059    public Mutation deserialize(Mutation mutation) throws IOException {
060      MutationProto proto = MutationProto.parseDelimitedFrom(in);
061      return ProtobufUtil.toMutation(proto);
062    }
063
064    @Override
065    public void open(InputStream in) throws IOException {
066      this.in = in;
067    }
068    
069  }
070  private static class MutationSerializer implements Serializer<Mutation> {
071    private OutputStream out;
072
073    @Override
074    public void close() throws IOException {
075      out.close();
076    }
077
078    @Override
079    public void open(OutputStream out) throws IOException {
080      this.out = out;
081    }
082
083    @Override
084    public void serialize(Mutation mutation) throws IOException {
085      MutationType type;
086      if (mutation instanceof Put) {
087        type = MutationType.PUT;
088      } else if (mutation instanceof Delete) {
089        type = MutationType.DELETE;
090      } else {
091        throw new IllegalArgumentException("Only Put and Delete are supported");
092      }
093      ProtobufUtil.toMutation(type, mutation).writeDelimitedTo(out);
094    }
095  }
096}