Skip to content

Commit

Permalink
Merge pull request #77 from RADAR-base/release-0.13.0
Browse files Browse the repository at this point in the history
Release 0.13.0
  • Loading branch information
blootsvoets committed Jun 23, 2020
2 parents 3df512f + 8136c94 commit 25b4308
Show file tree
Hide file tree
Showing 18 changed files with 565 additions and 368 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ repositories {
}
dependencies {
implementation group: 'org.radarbase', name: 'radar-commons', version: '0.12.3'
implementation group: 'org.radarbase', name: 'radar-commons', version: '0.13.0'
}
```

Expand Down Expand Up @@ -69,7 +69,7 @@ repositories {
}
dependencies {
implementation group: 'org.radarbase', name: 'radar-commons-server', version: '0.12.3'
implementation group: 'org.radarbase', name: 'radar-commons-server', version: '0.13.0'
}
```

Expand All @@ -83,7 +83,7 @@ repositories {
}
dependencies {
testImplementation group: 'org.radarbase', name: 'radar-commons-testing', version: '0.12.3'
testImplementation group: 'org.radarbase', name: 'radar-commons-testing', version: '0.13.0'
}
```

Expand All @@ -96,7 +96,7 @@ repositories {
}
dependencies {
runtimeOnly group: 'org.radarbase', name: 'radar-commons-unsafe', version: '0.12.3'
runtimeOnly group: 'org.radarbase', name: 'radar-commons-unsafe', version: '0.13.0'
}
```

Expand All @@ -121,7 +121,7 @@ configurations.all {
}
dependencies {
compile group: 'org.radarbase', name: 'radar-commons', version: '0.12.4-SNAPSHOT'
compile group: 'org.radarbase', name: 'radar-commons', version: '0.13.1-SNAPSHOT'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ subprojects {
// Configuration //
//---------------------------------------------------------------------------//

version = '0.12.3'
version = '0.13.0'
group = 'org.radarbase'
ext.githubRepoName = 'RADAR-base/radar-commons'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ protected DatumReader<?> getDatumReader(Schema writerSchema, Schema readerSchema
}

/**
* Normalizes the reader schema, puts the resolved schema into the cache.
* Normalizes the reader schema, puts the resolved schema into the cache.
* <li>
* <ul>if the reader schema is provided, use the provided one</ul>
* <ul>if the reader schema is cached for the writer schema full name, use the cached value</ul>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static org.apache.avro.JsonProperties.NULL_VALUE;

import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
Expand Down Expand Up @@ -103,19 +102,9 @@ public AvroDataMapper createMapper(Schema from, Schema to, final Object defaultV
} catch (SchemaValidationException ex) {
if (defaultVal != null) {
if (defaultVal == NULL_VALUE) {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return null;
}
};
return obj -> null;
} else {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return defaultVal;
}
};
return obj -> defaultVal;
}
} else {
throw ex;
Expand All @@ -142,12 +131,7 @@ private static AvroDataMapper mapEnum(Schema from, final Schema to, Object defau
"Cannot map enum from non-string or enum type"));
}
if (containsAll) {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return new GenericData.EnumSymbol(to, obj.toString());
}
};
return obj -> new GenericData.EnumSymbol(to, obj.toString());
} else {
String defaultString = (String) defaultVal;
if (defaultString == null && to.hasEnumSymbol("UNKNOWN")) {
Expand All @@ -157,27 +141,19 @@ public Object convertAvro(Object obj) {
throw new SchemaValidationException(to, from, new IllegalArgumentException(
"Cannot map enum symbols without default value"));
} else {
final GenericEnumSymbol symbol = new GenericData.EnumSymbol(to, defaultString);
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
String value = obj.toString();
if (to.hasEnumSymbol(value)) {
return new GenericData.EnumSymbol(to, value);
} else {
return symbol;
}
GenericEnumSymbol<?> symbol = new GenericData.EnumSymbol(to, defaultString);
return obj -> {
String value = obj.toString();
if (to.hasEnumSymbol(value)) {
return new GenericData.EnumSymbol(to, value);
} else {
return symbol;
}
};
}
}
} else if (from.getType() == Schema.Type.ENUM && to.getType() == Schema.Type.STRING) {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return obj.toString();
}
};
return Object::toString;
} else {
throw new SchemaValidationException(to, from, new IllegalArgumentException(
"Cannot map unknown type with enum."));
Expand Down Expand Up @@ -244,40 +220,15 @@ public Number stringToNumber(String obj) {
} else {
switch (to.getType()) {
case INT:
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return ((Number) obj).intValue();
}
};
return obj -> ((Number) obj).intValue();
case LONG:
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return ((Number) obj).longValue();
}
};
return obj -> ((Number) obj).longValue();
case DOUBLE:
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return Double.valueOf(obj.toString());
}
};
return obj -> Double.valueOf(obj.toString());
case FLOAT:
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return ((Number) obj).floatValue();
}
};
return obj -> ((Number) obj).floatValue();
case STRING:
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
return obj.toString();
}
};
return Object::toString;
default:
throw new SchemaValidationException(to, from, new IllegalArgumentException(
"Cannot map numeric type with non-numeric type"));
Expand Down Expand Up @@ -318,14 +269,11 @@ private AvroDataMapper mapUnion(Schema from, Schema to, Object defaultVal)
if (defaultVal != null) {
final Object actualDefault = getDefaultValue(defaultVal, to);
final AvroDataMapper subMapper = createMapper(resolvedFrom, to, defaultVal);
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
if (obj == null) {
return actualDefault;
} else {
return subMapper.convertAvro(obj);
}
return obj -> {
if (obj == null) {
return actualDefault;
} else {
return subMapper.convertAvro(obj);
}
};
} else {
Expand All @@ -335,14 +283,11 @@ public Object convertAvro(Object obj) {
} else {
Schema toNonNull = nonNullUnionSchema(to);
final AvroDataMapper unionMapper = createMapper(resolvedFrom, toNonNull, defaultVal);
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
if (obj == null) {
return null;
} else {
return unionMapper.convertAvro(obj);
}
return obj -> {
if (obj == null) {
return null;
} else {
return unionMapper.convertAvro(obj);
}
};
}
Expand All @@ -357,16 +302,13 @@ private AvroDataMapper mapArray(Schema from, Schema to)
}
final AvroDataMapper subMapper = createMapper(from.getElementType(), to.getElementType(),
null);
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
List array = (List) obj;
List<Object> toArray = new ArrayList<>(array.size());
for (Object val : array) {
toArray.add(subMapper.convertAvro(val));
}
return toArray;
return obj -> {
List<?> array = (List<?>) obj;
List<Object> toArray = new ArrayList<>(array.size());
for (Object val : array) {
toArray.add(subMapper.convertAvro(val));
}
return toArray;
};
}

Expand All @@ -378,17 +320,14 @@ private AvroDataMapper mapMap(Schema from, Schema to) throws SchemaValidationExc
}
final AvroDataMapper subMapper = createMapper(from.getValueType(), to.getValueType(),
null);
return new AvroDataMapper() {
@Override
public Object convertAvro(Object obj) {
@SuppressWarnings("unchecked")
Map<? extends CharSequence, ?> map = (Map<? extends CharSequence, ?>) obj;
Map<String, Object> toMap = new HashMap<>(map.size() * 4 / 3 + 1);
for (Map.Entry<? extends CharSequence, ?> entry : map.entrySet()) {
toMap.put(entry.getKey().toString(), subMapper.convertAvro(entry.getValue()));
}
return toMap;
return obj -> {
@SuppressWarnings("unchecked")
Map<? extends CharSequence, ?> map = (Map<? extends CharSequence, ?>) obj;
Map<String, Object> toMap = new HashMap<>(map.size() * 4 / 3 + 1);
for (Map.Entry<? extends CharSequence, ?> entry : map.entrySet()) {
toMap.put(entry.getKey().toString(), subMapper.convertAvro(entry.getValue()));
}
return toMap;
};
}

Expand All @@ -399,46 +338,26 @@ private AvroDataMapper mapBytes(Schema from, final Schema to, final Object defau
|| (from.getType() == Type.FIXED && from.getFixedSize() == to.getFixedSize()))) {
return IDENTITY_MAPPER;
} else if (from.getType() == Type.FIXED && to.getType() == Schema.Type.BYTES) {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object object) {
return ByteBuffer.wrap(((Fixed)object).bytes());
}
};
return object -> ByteBuffer.wrap(((Fixed)object).bytes());
} else if (from.getType() == Type.BYTES && to.getType() == Type.FIXED) {
if (defaultVal == null) {
throw new SchemaValidationException(to, from, new IllegalArgumentException(
"Cannot map bytes to fixed without default value"));
}
return new AvroDataMapper() {
@Override
public Object convertAvro(Object object) {
byte[] bytes = ((ByteBuffer) object).array();
if (bytes.length == to.getFixedSize()) {
return GenericData.get().createFixed(null, bytes, to);
} else {
return GenericData.get().createFixed(null, (byte[]) defaultVal, to);
}
return object -> {
byte[] bytes = ((ByteBuffer) object).array();
if (bytes.length == to.getFixedSize()) {
return GenericData.get().createFixed(null, bytes, to);
} else {
return GenericData.get().createFixed(null, (byte[]) defaultVal, to);
}
};
} else if (to.getType() == Type.STRING) {
final Encoder encoder = Base64.getEncoder();
if (from.getType() == Type.FIXED) {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object object) {
return new String(encoder.encode(((Fixed) object).bytes()),
StandardCharsets.UTF_8);
}
};
return object -> encoder.encode(((Fixed) object).bytes());
} else {
return new AvroDataMapper() {
@Override
public Object convertAvro(Object object) {
return new String(encoder.encode(((ByteBuffer) object).array()),
StandardCharsets.UTF_8);
}
};
return object -> encoder.encode(((ByteBuffer) object).array());
}
} else {
throw new SchemaValidationException(to, from,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class BinaryRecordRequest<K, V> implements RecordRequest<K, V> {
private RecordData<K, V> records;
private BinaryEncoder binaryEncoder;
private final AvroWriter<V> valueEncoder;
private int sourceIdPos;
private final int sourceIdPos;

/**
* Binary record request for given topic.
Expand Down
Loading

0 comments on commit 25b4308

Please sign in to comment.