protected void setSchema(Schema schema) { this.schema = schema; this.bytes = new byte[schema.getFixedSize()]; }
protected void setSchema(Schema schema) { this.schema = schema; this.bytes = new byte[schema.getFixedSize()]; }
private long maxPrecision(Schema schema) { if (schema.getType() == Schema.Type.BYTES) { // not bounded return Integer.MAX_VALUE; } else if (schema.getType() == Schema.Type.FIXED) { int size = schema.getFixedSize(); return Math.round( // convert double to long Math.floor(Math.log10( // number of base-10 digits Math.pow(2, 8 * size - 1) - 1) // max value stored )); } else { // not valid for any other type return 0; } }
private long maxPrecision(Schema schema) { if (schema.getType() == Schema.Type.BYTES) { // not bounded return Integer.MAX_VALUE; } else if (schema.getType() == Schema.Type.FIXED) { int size = schema.getFixedSize(); return Math.round( // convert double to long Math.floor(Math.log10( // number of base-10 digits Math.pow(2, 8 * size - 1) - 1) // max value stored )); } else { // not valid for any other type return 0; } }
private SchemaCompatibilityResult checkFixedSize(final Schema reader, final Schema writer, final Deque<String> location) { SchemaCompatibilityResult result = SchemaCompatibilityResult.compatible(); location.addFirst("size"); int actual = reader.getFixedSize(); int expected = writer.getFixedSize(); if (actual != expected) { String message = String.format("expected: %d, found: %d", expected, actual); result = SchemaCompatibilityResult.incompatible(SchemaIncompatibilityType.FIXED_SIZE_MISMATCH, reader, writer, message, asList(location)); } // POP "size" literal location.removeFirst(); return result; }
/* package private */void compareFixedTypes(Schema oldSchema, Schema newSchema, List<Message> messages, String name) { if(oldSchema == null || newSchema == null || oldSchema.getType() != Schema.Type.FIXED) { throw new IllegalArgumentException("Old schema must be FIXED type. Name=" + name + ". Type=" + oldSchema); } if(newSchema.getType() != Schema.Type.FIXED) { messages.add(new Message(Level.ERROR, "Illegal type change from " + oldSchema.getType() + " to " + newSchema.getType() + " for field " + name)); return; } if(newSchema.getFixedSize() != oldSchema.getFixedSize()) { messages.add(new Message(Level.ERROR, "Illegal size change for fixed type field " + name)); } }
/** * Processes fixed type. * * @param schemaNode schema for current type. * @return generated value for input record type. */ private Object processFixed(Schema schemaNode) { int size = schemaNode.getFixedSize(); byte[] bytes = new byte[size]; for (int i = 0; i < size; i++) { bytes[i] = (byte) 0; } GenericFixed result = new GenericData.Fixed(schemaNode, bytes); return result; }
/** Called to create an fixed value. May be overridden for alternate fixed * representations. By default, returns {@link GenericFixed}. */ public Object createFixed(Object old, byte[] bytes, Schema schema) { GenericFixed fixed = (GenericFixed)createFixed(old, schema); System.arraycopy(bytes, 0, fixed.bytes(), 0, schema.getFixedSize()); return fixed; }
/** Called to create an fixed value. May be overridden for alternate fixed * representations. By default, returns {@link GenericFixed}. */ public Object createFixed(Object old, byte[] bytes, Schema schema) { GenericFixed fixed = (GenericFixed)createFixed(old, schema); System.arraycopy(bytes, 0, fixed.bytes(), 0, schema.getFixedSize()); return fixed; }
@Override public GenericFixed toFixed(BigDecimal value, Schema schema, LogicalType type) { int scale = ((LogicalTypes.Decimal) type).getScale(); if (scale != value.scale()) { throw new AvroTypeException("Cannot encode decimal with scale " + value.scale() + " as scale " + scale); } byte fillByte = (byte) (value.signum() < 0 ? 0xFF : 0x00); byte[] unscaled = value.unscaledValue().toByteArray(); byte[] bytes = new byte[schema.getFixedSize()]; int offset = bytes.length - unscaled.length; // Fill the front of the array and copy remaining with unscaled values Arrays.fill(bytes, 0, offset, fillByte); System.arraycopy(unscaled, 0, bytes, offset, bytes.length - offset); return new GenericData.Fixed(schema, bytes); } }
/** Called to create an fixed value. May be overridden for alternate fixed * representations. By default, returns {@link GenericFixed}. */ public Object createFixed(Object old, Schema schema) { if ((old instanceof GenericFixed) && ((GenericFixed)old).bytes().length == schema.getFixedSize()) return old; return new GenericData.Fixed(schema); }
/** Called to write a fixed value. May be overridden for alternate fixed * representations.*/ protected void writeFixed(Schema schema, Object datum, Encoder out) throws IOException { out.writeFixed(((GenericFixed)datum).bytes(), 0, schema.getFixedSize()); }
/** Called to write a fixed value. May be overridden for alternate fixed * representations.*/ protected void writeFixed(Schema schema, Object datum, Encoder out) throws IOException { out.writeFixed(((GenericFixed)datum).bytes(), 0, schema.getFixedSize()); }
/** Called to create an fixed value. May be overridden for alternate fixed * representations. By default, returns {@link GenericFixed}. */ public Object createFixed(Object old, Schema schema) { if ((old instanceof GenericFixed) && ((GenericFixed)old).bytes().length == schema.getFixedSize()) return old; return new GenericData.Fixed(schema); }
public SpecificFixed() { bytes(new byte[getSchema().getFixedSize()]); }
public SpecificFixed() { bytes(new byte[getSchema().getFixedSize()]); }
@Override public void validate(Schema schema) { super.validate(schema); // validate the type if (schema.getType() != Schema.Type.FIXED && schema.getType() != Schema.Type.BYTES) { throw new IllegalArgumentException( "Logical type decimal must be backed by fixed or bytes"); } if (precision <= 0) { throw new IllegalArgumentException("Invalid decimal precision: " + precision + " (must be positive)"); } else if (precision > maxPrecision(schema)) { throw new IllegalArgumentException( "fixed(" + schema.getFixedSize() + ") cannot store " + precision + " digits (max " + maxPrecision(schema) + ")"); } if (scale < 0) { throw new IllegalArgumentException("Invalid decimal scale: " + scale + " (must be positive)"); } else if (scale > precision) { throw new IllegalArgumentException("Invalid decimal scale: " + scale + " (greater than precision: " + precision + ")"); } }
@Override public GenericFixed toFixed(BigDecimal value, Schema schema, LogicalType type) { int scale = ((LogicalTypes.Decimal) type).getScale(); if (scale != value.scale()) { throw new AvroTypeException("Cannot encode decimal with scale " + value.scale() + " as scale " + scale); } byte fillByte = (byte) (value.signum() < 0 ? 0xFF : 0x00); byte[] unscaled = value.unscaledValue().toByteArray(); byte[] bytes = new byte[schema.getFixedSize()]; int offset = bytes.length - unscaled.length; for (int i = 0; i < bytes.length; i += 1) { if (i < offset) { bytes[i] = fillByte; } else { bytes[i] = unscaled[i - offset]; } } return new GenericData.Fixed(schema, bytes); } }
/** Called to read a fixed value. May be overridden for alternate fixed * representations. By default, returns {@link GenericFixed}. */ protected Object readFixed(Object old, Schema expected, Decoder in) throws IOException { GenericFixed fixed = (GenericFixed)data.createFixed(old, expected); in.readFixed(fixed.bytes(), 0, expected.getFixedSize()); return fixed; }
/** Called to read a fixed value. May be overridden for alternate fixed * representations. By default, returns {@link GenericFixed}. */ protected Object readFixed(Object old, Schema expected, Decoder in) throws IOException { GenericFixed fixed = (GenericFixed)data.createFixed(old, expected); in.readFixed(fixed.bytes(), 0, expected.getFixedSize()); return fixed; }