public FieldType getSingleType() { if (isArrayType()) { // GOTCHA: Wont' work if you add more types!!! return get((byte) (getId() % TYPES_COUNT)); } return this; }
private void validateType(PortablePosition position, FieldType expectedType) { FieldType returnedType = position.getType(); if (position.getIndex() >= 0) { returnedType = returnedType != null ? returnedType.getSingleType() : null; } if (expectedType != returnedType) { String name = returnedType != null ? returnedType.name() : null; throw new IllegalArgumentException("Wrong type read! Actual: " + name + " Expected: " + expectedType.name() + ". Did you use a correct read method? E.g. readInt() for int."); } } }
@Override public int hashCode() { int result = index; result = 31 * result + (fieldName != null ? fieldName.hashCode() : 0); result = 31 * result + (type != null ? type.hashCode() : 0); result = 31 * result + classId; result = 31 * result + factoryId; result = 31 * result + version; return result; }
private static PortablePosition createPositionForSingleCellPrimitiveArrayAccess( PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException { // assumes position.getIndex() >= 0 BufferObjectDataInput in = ctx.getIn(); in.position(getStreamPositionOfTheField(ctx)); // read the array length and ignore it, it has been already validated in.readInt(); int streamPosition; if (ctx.getCurrentFieldType() == FieldType.UTF || ctx.getCurrentFieldType() == FieldType.UTF_ARRAY) { // UTF arrays actually need iterating over all elements to read the length of each element // it's impossible to dead-reckon about a cell's position int currentIndex = 0; while (index > currentIndex) { int indexElementLen = in.readInt(); indexElementLen = indexElementLen < 0 ? 0 : indexElementLen; in.position(in.position() + indexElementLen); currentIndex++; } streamPosition = in.position(); } else { // in primitive non-utf arrays we can dead-reckon about a cell's position streamPosition = in.position() + index * ctx.getCurrentFieldType().getSingleType().getTypeSize(); } return PortablePositionFactory.createSinglePrimitivePosition(ctx.getCurrentFieldDefinition(), streamPosition, index, path.isLastToken()); }
/** * @param cd given classDefinition to validate against * @param fd given fieldDefinition to validate against * @param fullPath full path - just for output * @throws IllegalArgumentException if the current field definition is not of an array type */ static void validateArrayType(ClassDefinition cd, FieldDefinition fd, String fullPath) { if (!fd.getType().isArrayType()) { throw new IllegalArgumentException("Wrong use of array operator: '" + fullPath + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}"); } }
private FieldDefinition setPosition(String fieldName, FieldType fieldType) throws IOException { if (raw) { throw new HazelcastSerializationException("Cannot write Portable fields after getRawDataOutput() is called!"); } FieldDefinition fd = cd.getField(fieldName); if (fd == null) { throw new HazelcastSerializationException("Invalid field name: '" + fieldName + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}"); } if (writtenFields.add(fieldName)) { int pos = out.position(); int index = fd.getIndex(); out.writeInt(offset + index * INT_SIZE_IN_BYTES, pos); out.writeShort(fieldName.length()); out.writeBytes(fieldName); out.writeByte(fieldType.getId()); } else { throw new HazelcastSerializationException("Field '" + fieldName + "' has already been written!"); } return fd; }
FieldType type = FieldType.get(in.readByte()); String name = new String(chars); int fieldFactoryId = 0;
private static PortablePosition createPositionForSingleCellPrimitiveArrayAccess( PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException { // assumes position.getIndex() >= 0 BufferObjectDataInput in = ctx.getIn(); in.position(getStreamPositionOfTheField(ctx)); // read the array length and ignore it, it has been already validated in.readInt(); int streamPosition; if (ctx.getCurrentFieldType() == FieldType.UTF || ctx.getCurrentFieldType() == FieldType.UTF_ARRAY) { // UTF arrays actually need iterating over all elements to read the length of each element // it's impossible to dead-reckon about a cell's position int currentIndex = 0; while (index > currentIndex) { int indexElementLen = in.readInt(); indexElementLen = indexElementLen < 0 ? 0 : indexElementLen; in.position(in.position() + indexElementLen); currentIndex++; } streamPosition = in.position(); } else { // in primitive non-utf arrays we can dead-reckon about a cell's position streamPosition = in.position() + index * ctx.getCurrentFieldType().getSingleType().getTypeSize(); } return PortablePositionFactory.createSinglePrimitivePosition(ctx.getCurrentFieldDefinition(), streamPosition, index, path.isLastToken()); }
/** * @param cd given classDefinition to validate against * @param fd given fieldDefinition to validate against * @param fullPath full path - just for output * @throws IllegalArgumentException if the current field definition is not of an array type */ static void validateArrayType(ClassDefinition cd, FieldDefinition fd, String fullPath) { if (!fd.getType().isArrayType()) { throw new IllegalArgumentException("Wrong use of array operator: '" + fullPath + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}"); } }
private FieldDefinition setPosition(String fieldName, FieldType fieldType) throws IOException { if (raw) { throw new HazelcastSerializationException("Cannot write Portable fields after getRawDataOutput() is called!"); } FieldDefinition fd = cd.getField(fieldName); if (fd == null) { throw new HazelcastSerializationException("Invalid field name: '" + fieldName + "' for ClassDefinition {id: " + cd.getClassId() + ", version: " + cd.getVersion() + "}"); } if (writtenFields.add(fieldName)) { int pos = out.position(); int index = fd.getIndex(); out.writeInt(offset + index * INT_SIZE_IN_BYTES, pos); out.writeShort(fieldName.length()); out.writeBytes(fieldName); out.writeByte(fieldType.getId()); } else { throw new HazelcastSerializationException("Field '" + fieldName + "' has already been written!"); } return fd; }
FieldType type = FieldType.get(in.readByte()); String name = new String(chars); int fieldFactoryId = 0;
public FieldType getSingleType() { if (isArrayType()) { // GOTCHA: Wont' work if you add more types!!! return get((byte) (getId() % TYPES_COUNT)); } return this; }
private void validateType(PortablePosition position, FieldType expectedType) { FieldType returnedType = position.getType(); if (position.getIndex() >= 0) { returnedType = returnedType != null ? returnedType.getSingleType() : null; } if (expectedType != returnedType) { String name = returnedType != null ? returnedType.name() : null; throw new IllegalArgumentException("Wrong type read! Actual: " + name + " Expected: " + expectedType.name() + ". Did you use a correct read method? E.g. readInt() for int."); } } }
PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException { FieldType type = ctx.getCurrentFieldType(); if (type.isArrayType()) { if (type == FieldType.PORTABLE_ARRAY) { return createPositionForReadAccessInPortableArray(ctx, path, index);
@Override public int hashCode() { int result = index; result = 31 * result + (fieldName != null ? fieldName.hashCode() : 0); result = 31 * result + (type != null ? type.hashCode() : 0); result = 31 * result + classId; result = 31 * result + factoryId; result = 31 * result + version; return result; }
PortableNavigatorContext ctx, PortablePathCursor path, int index) throws IOException { FieldType type = ctx.getCurrentFieldType(); if (type.isArrayType()) { if (type == FieldType.PORTABLE_ARRAY) { return createPositionForReadAccessInPortableArray(ctx, path, index);