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;
023import org.apache.hadoop.hbase.client.Delete;
024import org.apache.hadoop.hbase.client.Mutation;
025import org.apache.hadoop.hbase.client.Put;
026import org.apache.hadoop.io.serializer.Deserializer;
027import org.apache.hadoop.io.serializer.Serialization;
028import org.apache.hadoop.io.serializer.Serializer;
029import org.apache.yetus.audience.InterfaceAudience;
030
031import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.ClientProtos.MutationProto.MutationType;
034
035@InterfaceAudience.Public
036public class MutationSerialization implements Serialization<Mutation> {
037  @Override
038  public boolean accept(Class<?> c) {
039    return Mutation.class.isAssignableFrom(c);
040  }
041
042  @Override
043  public Deserializer<Mutation> getDeserializer(Class<Mutation> c) {
044    return new MutationDeserializer();
045  }
046
047  @Override
048  public Serializer<Mutation> getSerializer(Class<Mutation> c) {
049    return new MutationSerializer();
050  }
051
052  private static class MutationDeserializer implements Deserializer<Mutation> {
053    private InputStream in;
054
055    @Override
056    public void close() throws IOException {
057      in.close();
058    }
059
060    @Override
061    public Mutation deserialize(Mutation mutation) throws IOException {
062      MutationProto proto = MutationProto.parseDelimitedFrom(in);
063      return ProtobufUtil.toMutation(proto);
064    }
065
066    @Override
067    public void open(InputStream in) throws IOException {
068      this.in = in;
069    }
070
071  }
072
073  private static class MutationSerializer implements Serializer<Mutation> {
074    private OutputStream out;
075
076    @Override
077    public void close() throws IOException {
078      out.close();
079    }
080
081    @Override
082    public void open(OutputStream out) throws IOException {
083      this.out = out;
084    }
085
086    @Override
087    public void serialize(Mutation mutation) throws IOException {
088      MutationType type;
089      if (mutation instanceof Put) {
090        type = MutationType.PUT;
091      } else if (mutation instanceof Delete) {
092        type = MutationType.DELETE;
093      } else {
094        throw new IllegalArgumentException("Only Put and Delete are supported");
095      }
096      ProtobufUtil.toMutation(type, mutation).writeDelimitedTo(out);
097    }
098  }
099}