package com.datastax.oss.protocol.internal.response.error;
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.response.Error;
import com.datastax.oss.protocol.internal.util.collection.NullAllowingImmutableMap;
import java.net.InetAddress;
import java.util.Collections;
import java.util.Map;
public class ReadFailure extends Error {
public final int consistencyLevel;
public final int received;
public final int blockFor;
public final int numFailures;
public final Map<InetAddress, Integer> reasonMap;
public final boolean dataPresent;
public ReadFailure(
String message,
int consistencyLevel,
int received,
int blockFor,
int numFailures,
Map<InetAddress, Integer> reasonMap,
boolean dataPresent) {
super(ProtocolConstants.ErrorCode.READ_FAILURE, message);
this.consistencyLevel = consistencyLevel;
this.received = received;
this.blockFor = blockFor;
this.numFailures = numFailures;
this.reasonMap = reasonMap;
this.dataPresent = dataPresent;
}
public static class SubCodec extends Error.SubCodec {
public SubCodec(int protocolVersion) {
super(ProtocolConstants.ErrorCode.READ_FAILURE, protocolVersion);
}
@Override
public <B> void encode(B dest, Message message, PrimitiveCodec<B> encoder) {
ReadFailure readFailure = (ReadFailure) message;
encoder.writeString(readFailure.message, dest);
encoder.writeUnsignedShort(readFailure.consistencyLevel, dest);
encoder.writeInt(readFailure.received, dest);
encoder.writeInt(readFailure.blockFor, dest);
if (protocolVersion >= ProtocolConstants.Version.V5) {
writeReasonMap(readFailure.reasonMap, dest, encoder);
} else {
encoder.writeInt(readFailure.numFailures, dest);
}
encoder.writeByte((byte) (readFailure.dataPresent ? 1 : 0), dest);
}
@Override
public int encodedSize(Message message) {
ReadFailure readFailure = (ReadFailure) message;
int size =
PrimitiveSizes.sizeOfString(readFailure.message)
+ PrimitiveSizes.SHORT
+ PrimitiveSizes.INT
+ PrimitiveSizes.INT
+ PrimitiveSizes.BYTE;
if (protocolVersion >= ProtocolConstants.Version.V5) {
size += sizeOfReasonMap(readFailure.reasonMap);
} else {
size += PrimitiveSizes.INT;
}
return size;
}
@Override
public <B> Message decode(B source, PrimitiveCodec<B> decoder) {
String message = decoder.readString(source);
int consistencyLevel = decoder.readUnsignedShort(source);
int received = decoder.readInt(source);
int blockFor = decoder.readInt(source);
int numFailures;
Map<InetAddress, Integer> reasonMap;
if (protocolVersion >= ProtocolConstants.Version.V5) {
reasonMap = readReasonMap(source, decoder);
numFailures = reasonMap.size();
} else {
reasonMap = Collections.emptyMap();
numFailures = decoder.readInt(source);
}
boolean dataPresent = decoder.readByte(source) != 0;
return new ReadFailure(
message, consistencyLevel, received, blockFor, numFailures, reasonMap, dataPresent);
}
static <B> void writeReasonMap(Map<InetAddress, Integer> m, B dest, PrimitiveCodec<B> encoder) {
encoder.writeInt(m.size(), dest);
for (Map.Entry<InetAddress, Integer> entry : m.entrySet()) {
encoder.writeInetAddr(entry.getKey(), dest);
encoder.writeUnsignedShort(entry.getValue(), dest);
}
}
static int sizeOfReasonMap(Map<InetAddress, Integer> m) {
int size = PrimitiveSizes.INT;
for (Map.Entry<InetAddress, Integer> entry : m.entrySet()) {
size += PrimitiveSizes.sizeOfInetAddr(entry.getKey());
size += PrimitiveSizes.SHORT;
}
return size;
}
static <B> Map<InetAddress, Integer> readReasonMap(B source, PrimitiveCodec<B> decoder) {
int size = decoder.readInt(source);
if (size == 0) {
return Collections.emptyMap();
} else {
NullAllowingImmutableMap.Builder<InetAddress, Integer> builder =
NullAllowingImmutableMap.builder(size);
for (int i = 0; i < size; i++) {
InetAddress key = decoder.readInetAddr(source);
int value = decoder.readUnsignedShort(source);
builder.put(key, value);
}
return builder.build();
}
}
}
}