@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); } }
@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); } }
@Test public void testUnionWithFixed() { Schema s = new Schema.Parser().parse ("[\"null\", {\"type\":\"fixed\",\"name\":\"f\",\"size\":1}]"); Schema f = new Schema.Parser().parse("{\"type\":\"fixed\",\"name\":\"f\",\"size\":1}"); GenericData data = ReflectData.get(); assertEquals(1, data.resolveUnion(s, new GenericData.Fixed(f))); }
public static AvroBinaryDelta getComplexFieldDelta(Schema schema) { GenericRecord delta = new GenericData.Record(getDeltaSchemaByFullName(schema, "org.kaa.config.testT")); GenericEnumSymbol unchanged = new GenericData.EnumSymbol(getSchemaByFullName(delta.getSchema().getField("testField1").schema().getTypes(), "org.kaaproject.configuration.unchangedT"), "unchanged"); GenericRecord testField2 = new GenericData.Record(getSchemaByFullName(delta.getSchema().getField("testField2").schema().getTypes(), "org.kaa.config.testRecordT")); testField2.put("testField3", 456); byte[] rawUuid = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; GenericFixed uuid = new GenericData.Fixed(delta.getSchema().getField("__uuid").schema(), rawUuid); delta.put("testField1", unchanged); delta.put("testField2", testField2); delta.put("__uuid", uuid); AvroBinaryDelta deltaExpected = new AvroBinaryDelta(schema); deltaExpected.addDelta(delta); return deltaExpected; }
@Test public void testEcho() throws IOException { GenericRecord record = new GenericData.Record(PROTOCOL.getType("TestRecord")); record.put("name", new Utf8("foo")); record.put("kind", new GenericData.EnumSymbol (PROTOCOL.getType("Kind"), "BAR")); record.put("hash", new GenericData.Fixed (PROTOCOL.getType("MD5"), new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); GenericRecord params = new GenericData.Record(PROTOCOL.getMessages().get("echo").getRequest()); params.put("record", record); Object echoed = requestor.request("echo", params); assertEquals(record, echoed); }
/** * 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; }
@Override public Object createFixed(Object old, Schema schema) { // SpecificData will try to instantiate the type returned by getClass, but // that is the converted class and can't be constructed. LogicalType logicalType = schema.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return new GenericData.Fixed(schema); } } return super.createFixed(old, schema); }
@Override public Object createFixed(Object old, Schema schema) { // SpecificData will try to instantiate the type returned by getClass, but // that is the converted class and can't be constructed. LogicalType logicalType = schema.getLogicalType(); if (logicalType != null) { Conversion<?> conversion = getConversionFor(schema.getLogicalType()); if (conversion != null) { return new GenericData.Fixed(schema); } } return super.createFixed(old, schema); }
public static GenericData.Fixed generateUuidObject() { Schema avroSchema = Schema.createFixed(UUID_TYPE, null, KAA_NAMESPACE, UUID_SIZE); return new GenericData.Fixed(avroSchema, generateUuidBytes()); }
/** {@inheritDoc} */ @Override public GenericFixed convert(ByteWritable input) { return new GenericData.Fixed(mSchema, new byte[] { input.get() }); }
/** 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 static void fillArrayItemUpdateDelta(GenericRecord item) { byte[] rawItemUuid2 = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}; GenericFixed itemUuid2 = new GenericData.Fixed(item .getSchema().getField("__uuid").schema(), rawItemUuid2); item.put("__uuid", itemUuid2); item.put("testField2", 22); }
/** * Creates new GenericFixed from the given CommonFixed object. * * @param commonFixed CommonFixed object. * @return new GenericFixed object. */ public static GenericFixed createFixed(CommonFixed commonFixed) { return new GenericData.Fixed(commonFixed.getSchema(), commonFixed.getBytes()); }
public static void fillArrayItemRemoveDelta(GenericRecord delta) { GenericArray testField1 = new GenericData.Array(1, getArraySchema(delta, "testField1")); byte[] rawUuidToRemove = new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}; GenericFixed uuidToRemove = new GenericData.Fixed(getSchemaByFullName( testField1.getSchema().getElementType().getTypes(), "org.kaaproject.configuration.uuidT"), rawUuidToRemove); testField1.add(uuidToRemove); delta.put("testField1", testField1); }
public static void fillComplexFullResyncDelta(GenericRecord delta) { GenericRecord testField2 = new GenericData.Record(getSchemaByFullName( delta.getSchema().getField("testField2").schema().getTypes(), "org.kaa.config.testRecordT")); testField2.put("testField3", 456); byte[] rawUuid = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; GenericFixed uuid = new GenericData.Fixed(delta.getSchema() .getField("__uuid").schema(), rawUuid); delta.put("testField1", "abc"); delta.put("testField2", testField2); delta.put("__uuid", uuid); }
@Test public void testToStringFixed() throws Exception { GenericData data = GenericData.get(); assertEquals("[97, 10, 98]", data.toString(new GenericData.Fixed( Schema.createFixed("test", null, null, 3), new byte[] {'a', '\n', 'b'}))); }
@Test public void testConvertGenericToSpecific() { GenericRecord generic = new GenericData.Record(TestRecord.SCHEMA$); generic.put("name", "foo"); generic.put("kind", new GenericData.EnumSymbol(Kind.SCHEMA$, "BAR")); generic.put("hash", new GenericData.Fixed (MD5.SCHEMA$, new byte[]{0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5})); TestRecord specific = (TestRecord)SpecificData.get().deepCopy(TestRecord.SCHEMA$, generic); }
@Test public void testFixed() throws Exception { String json = "{\"type\": \"fixed\", \"name\":\"Test\", \"size\": 1}"; Schema schema = Schema.parse(json); check(new File(DIR.getRoot(), name.getMethodName()),json, "\"a\"", new GenericData.Fixed(schema, new byte[]{(byte)'a'}), false); checkParseError("{\"type\":\"fixed\"}"); // size required }
@Test public void testFixed() throws Exception { String json = "{\"type\": \"fixed\", \"name\":\"Test\", \"size\": 1}"; Schema schema = Schema.parse(json); check(json, new GenericData.Fixed(schema, new byte[]{(byte)'a'}), new GenericData.Fixed(schema, new byte[]{(byte)'b'})); }
/** 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); }