All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.oss.protocol.internal.response.Result Maven / Gradle / Ivy

Go to download

A set of Java types representing the frames and messages of the Apache Cassandra® native protocol, with the associated serialization and deserialization logic (this is a third-party implementation, not related to the Apache Cassandra project)

The newest version!
/*
 * Copyright DataStax, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.datastax.oss.protocol.internal.response;

import com.datastax.oss.protocol.internal.Message;
import com.datastax.oss.protocol.internal.PrimitiveCodec;
import com.datastax.oss.protocol.internal.PrimitiveSizes;
import com.datastax.oss.protocol.internal.ProtocolConstants;
import com.datastax.oss.protocol.internal.ProtocolErrors;
import com.datastax.oss.protocol.internal.response.result.DefaultRows;
import com.datastax.oss.protocol.internal.response.result.Prepared;
import com.datastax.oss.protocol.internal.response.result.SchemaChange;
import com.datastax.oss.protocol.internal.response.result.SetKeyspace;
import com.datastax.oss.protocol.internal.response.result.Void;
import com.datastax.oss.protocol.internal.util.IntMap;

public abstract class Result extends Message {

  /** @see ProtocolConstants.ResultKind */
  public final int kind;

  protected Result(int kind) {
    super(true, ProtocolConstants.Opcode.RESULT);
    this.kind = kind;
  }

  public static class Codec extends Message.Codec {
    private final IntMap subDecoders;

    public Codec(int protocolVersion, SubCodec... subCodecs) {
      super(ProtocolConstants.Opcode.RESULT, protocolVersion);
      IntMap.Builder builder = IntMap.builder();
      for (SubCodec subCodec : subCodecs) {
        builder.put(subCodec.kind, subCodec);
      }
      this.subDecoders = builder.build();
    }

    /** Creates an instance with subdecoders for the default kinds. */
    public Codec(int protocolVersion) {
      this(
          protocolVersion,
          new Void.SubCodec(protocolVersion),
          new DefaultRows.SubCodec(protocolVersion),
          new SetKeyspace.SubCodec(protocolVersion),
          new Prepared.SubCodec(protocolVersion),
          new SchemaChange.SubCodec(protocolVersion));
    }

    @Override
    public  void encode(B dest, Message message, PrimitiveCodec encoder) {
      Result result = (Result) message;
      encoder.writeInt(result.kind, dest);
      getSubCodec(result.kind).encode(dest, result, encoder);
    }

    @Override
    public int encodedSize(Message message) {
      Result result = (Result) message;
      return PrimitiveSizes.INT + getSubCodec(result.kind).encodedSize(result);
    }

    @Override
    public  Message decode(B source, PrimitiveCodec decoder) {
      int kind = decoder.readInt(source);
      return getSubCodec(kind).decode(source, decoder);
    }

    private SubCodec getSubCodec(int kind) {
      SubCodec subCodec = subDecoders.get(kind);
      ProtocolErrors.check(subCodec != null, "Unsupported result kind: %d", kind);
      return subCodec;
    }
  }

  public abstract static class SubCodec {
    public final int kind;
    public final int protocolVersion;

    protected SubCodec(int kind, int protocolVersion) {
      this.kind = kind;
      this.protocolVersion = protocolVersion;
    }

    public abstract  void encode(B dest, Message message, PrimitiveCodec encoder);

    public abstract int encodedSize(Message message);

    public abstract  Message decode(B source, PrimitiveCodec decoder);
  }
}