Skip to content

Commit

Permalink
KAA-325: Rename CommonUtils class to AvroUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
Yaroslav Zeygerman committed Feb 23, 2015
1 parent 308d24d commit b9fbc33
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.apache.avro.generic.GenericData;
import org.codehaus.jackson.JsonNode;

public class CommonUtils {
public class AvroUtils {


/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.codehaus.jackson.JsonNode;
import org.codehaus.jackson.map.JsonMappingException;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
import org.kaaproject.kaa.server.common.core.algorithms.CommonUtils;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;
import org.kaaproject.kaa.server.common.core.configuration.KaaData;
import org.kaaproject.kaa.server.common.core.configuration.KaaDataFactory;
import org.kaaproject.kaa.server.common.core.schema.KaaSchema;
Expand Down Expand Up @@ -95,7 +95,7 @@ public DefaultRecordGenerationAlgorithmImpl(U kaaSchema, KaaDataFactory<U, T> fa
*/
private Object processType(Schema schemaNode, JsonNode byDefault) throws ConfigurationGenerationException {
if (byDefault != null && !byDefault.isNull()) {
if (byDefault.isArray() && CommonUtils.getSchemaByType(schemaNode, Type.BYTES) != null) {
if (byDefault.isArray() && AvroUtils.getSchemaByType(schemaNode, Type.BYTES) != null) {
// if this is a 'bytes' type then convert json bytes array to
// avro 'bytes' representation or
// if this is a named type - look for already processed types
Expand All @@ -107,28 +107,28 @@ private Object processType(Schema schemaNode, JsonNode byDefault) throws Configu
byteBuffer.flip();
return byteBuffer;
}
if (byDefault.isBoolean() && CommonUtils.getSchemaByType(schemaNode, Type.BOOLEAN) != null) {
if (byDefault.isBoolean() && AvroUtils.getSchemaByType(schemaNode, Type.BOOLEAN) != null) {
return byDefault.asBoolean();
}
if (byDefault.isDouble()) {
if (CommonUtils.getSchemaByType(schemaNode, Type.DOUBLE) != null) {
if (AvroUtils.getSchemaByType(schemaNode, Type.DOUBLE) != null) {
return byDefault.asDouble();
} else if (CommonUtils.getSchemaByType(schemaNode, Type.FLOAT) != null) {
} else if (AvroUtils.getSchemaByType(schemaNode, Type.FLOAT) != null) {
return (float) byDefault.asDouble();
}
}
if (byDefault.isInt() && CommonUtils.getSchemaByType(schemaNode, Type.INT) != null) {
if (byDefault.isInt() && AvroUtils.getSchemaByType(schemaNode, Type.INT) != null) {
return byDefault.asInt();
}
if (byDefault.isLong() && CommonUtils.getSchemaByType(schemaNode, Type.LONG) != null) {
if (byDefault.isLong() && AvroUtils.getSchemaByType(schemaNode, Type.LONG) != null) {
return byDefault.asLong();
}
if (byDefault.isTextual() && CommonUtils.getSchemaByType(schemaNode, Type.STRING) != null) {
if (byDefault.isTextual() && AvroUtils.getSchemaByType(schemaNode, Type.STRING) != null) {
return byDefault.asText();
}
throw new ConfigurationGenerationException("Default value " + byDefault.toString() + " is not applicable for the field");
}
if (CommonUtils.getSchemaByType(schemaNode, Type.NULL) != null) {
if (AvroUtils.getSchemaByType(schemaNode, Type.NULL) != null) {
return null;
}

Expand Down Expand Up @@ -254,7 +254,7 @@ private Object processFixed(Schema schemaNode) {
private Object processField(Field fieldDefinition) throws ConfigurationGenerationException {
// if this a "uuid" type then generate it
if (UUID_FIELD.equals(fieldDefinition.name())) {
return CommonUtils.generateUuidObject();
return AvroUtils.generateUuidObject();
}

return processType(fieldDefinition.schema(), fieldDefinition.getJsonProp(BY_DEFAULT_FIELD));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type;
import org.kaaproject.kaa.server.common.core.algorithms.CommonUtils;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;


/**
Expand Down Expand Up @@ -88,7 +88,7 @@ private String findMergeStrategy(Schema root, String arrayFieldName) {
List<Field> fields = root.getFields();
for (Field field : fields) {
if (arrayFieldName.equals(field.name())) {
Schema arraySchema = CommonUtils.getSchemaByType(field.schema(), Type.ARRAY);
Schema arraySchema = AvroUtils.getSchemaByType(field.schema(), Type.ARRAY);
if (arraySchema != null) {
return arraySchema.getProp(FIELD_OVERRIDE_STRATEGY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.apache.avro.Schema;
import org.apache.avro.Schema.Field;
import org.apache.avro.Schema.Type;
import org.kaaproject.kaa.server.common.core.algorithms.CommonUtils;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;
import org.kaaproject.kaa.server.common.core.schema.DataSchema;
import org.kaaproject.kaa.server.common.core.schema.KaaSchema;
import org.slf4j.Logger;
Expand Down Expand Up @@ -140,7 +140,7 @@ private Schema processArray(Schema root) throws SchemaCreationException {
newItems = new ArrayList<Schema>(items.size() + 1);
for (Schema itemIter : items) {
Schema updatedItem = itemIter;
if (CommonUtils.isComplexSchema(itemIter)) {
if (AvroUtils.isComplexSchema(itemIter)) {
updatedItem = convert(itemIter);
}
newItems.add(updatedItem);
Expand All @@ -163,7 +163,7 @@ private Schema processArray(Schema root) throws SchemaCreationException {
} else {
copySchema = Schema.createArray(convert(root.getElementType()));
}
CommonUtils.copyJsonProperties(root, copySchema);
AvroUtils.copyJsonProperties(root, copySchema);
return copySchema;
}

Expand All @@ -183,14 +183,14 @@ private Schema processRecord(Schema root) throws SchemaCreationException {

List<Schema> newUnion = new ArrayList<Schema>();

if (CommonUtils.isComplexSchema(fieldIter.schema())) {
if (AvroUtils.isComplexSchema(fieldIter.schema())) {
addResetTypeIfArray(fieldIter.schema(), newUnion);
newUnion.add(convert(fieldIter.schema()));
} else if (fieldIter.schema().getType().equals(Type.UNION)) {
List<Schema> oldUnion = fieldIter.schema().getTypes();
for (Schema unionIter : oldUnion) {
Schema newItem = unionIter;
if (CommonUtils.isComplexSchema(unionIter)) {
if (AvroUtils.isComplexSchema(unionIter)) {
addResetTypeIfArray(unionIter, newUnion);
newItem = convert(unionIter);
}
Expand All @@ -216,15 +216,15 @@ private Schema processRecord(Schema root) throws SchemaCreationException {
} else {
newField = new Field(fieldIter.name(), newUnion.get(0), fieldIter.doc(), fieldIter.defaultValue());
}
CommonUtils.copyJsonProperties(fieldIter, newField);
AvroUtils.copyJsonProperties(fieldIter, newField);
newFields.add(newField);
}
if (addressable) {
// This record supports partial updates, adding "uuid" field
newFields.add(getUuidField());
}
Schema copySchema = Schema.createRecord(root.getName(), root.getDoc(), root.getNamespace(), root.isError());
CommonUtils.copyJsonProperties(root, copySchema);
AvroUtils.copyJsonProperties(root, copySchema);
copySchema.setFields(newFields);
if (addressable) {
// Adding addressable record's name to the storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.apache.avro.generic.GenericFixed;
import org.apache.avro.generic.GenericRecord;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
import org.kaaproject.kaa.server.common.core.algorithms.CommonUtils;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;
import org.kaaproject.kaa.server.common.core.configuration.KaaData;
import org.kaaproject.kaa.server.common.core.configuration.KaaDataFactory;
import org.kaaproject.kaa.server.common.core.schema.KaaSchema;
Expand Down Expand Up @@ -97,7 +97,7 @@ private static GenericRecord findRecordByUuid(GenericRecord rootRecord, Object u
}

private void generateUuidForRecord(GenericRecord record) {
GenericData.Fixed newUuid = CommonUtils.generateUuidObject();
GenericData.Fixed newUuid = AvroUtils.generateUuidObject();
LOG.trace("Generated new UUID {}", newUuid);
record.put(UUID_FIELD, newUuid);
processedUuids.add(newUuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import org.junit.Test;
import org.kaaproject.kaa.common.avro.GenericAvroConverter;
import org.kaaproject.kaa.server.common.core.algorithms.CommonConstants;
import org.kaaproject.kaa.server.common.core.algorithms.CommonUtils;
import org.kaaproject.kaa.server.common.core.algorithms.AvroUtils;
import org.kaaproject.kaa.server.common.core.configuration.BaseData;
import org.kaaproject.kaa.server.common.core.configuration.BaseDataFactory;
import org.kaaproject.kaa.server.common.core.configuration.KaaData;
Expand Down Expand Up @@ -68,7 +68,7 @@ public void testValidationWithoutOldConfiguration() throws Exception {
Schema avroSchema = new Schema.Parser().parse(configuraionSchema.getRawSchema());
GenericRecord record = new GenericData.Record(avroSchema);
record.put("intField", 5);
GenericFixed uuid = CommonUtils.generateUuidObject();
GenericFixed uuid = AvroUtils.generateUuidObject();
record.put(CommonConstants.UUID_FIELD, uuid);

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema);
Expand All @@ -89,12 +89,12 @@ public void testValidationWithOldConfiguration() throws Exception {
Schema avroSchema = new Schema.Parser().parse(configuraionSchema.getRawSchema());
GenericRecord recordNew = new GenericData.Record(avroSchema);
recordNew.put("intField", 4);
GenericFixed uuidNew = CommonUtils.generateUuidObject();
GenericFixed uuidNew = AvroUtils.generateUuidObject();
recordNew.put(CommonConstants.UUID_FIELD, uuidNew);

GenericRecord recordOld = new GenericData.Record(avroSchema);
recordOld.put("intField", 5);
GenericFixed uuidOld = CommonUtils.generateUuidObject();
GenericFixed uuidOld = AvroUtils.generateUuidObject();
recordOld.put(CommonConstants.UUID_FIELD, uuidOld);

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema);
Expand All @@ -118,12 +118,12 @@ public void testValidationOfArrayFields() throws Exception {

GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew1.put("intField", 4);
GenericFixed uuidNew1 = CommonUtils.generateUuidObject();
GenericFixed uuidNew1 = AvroUtils.generateUuidObject();
recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1);

GenericRecord recordNew2 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew2.put("intField", 5);
GenericFixed uuidNew2 = CommonUtils.generateUuidObject();
GenericFixed uuidNew2 = AvroUtils.generateUuidObject();
recordNew2.put(CommonConstants.UUID_FIELD, uuidNew2);

GenericRecord rootNew = new GenericData.Record(avroSchema);
Expand All @@ -138,15 +138,15 @@ public void testValidationOfArrayFields() throws Exception {

GenericRecord recordOld2 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordOld2.put("intField", 7);
GenericFixed uuidOld2 = CommonUtils.generateUuidObject();
GenericFixed uuidOld2 = AvroUtils.generateUuidObject();
recordOld2.put(CommonConstants.UUID_FIELD, uuidOld2);

GenericRecord rootOld = new GenericData.Record(avroSchema);
GenericArray arrayOld = new GenericData.Array<>(2, rootOld.getSchema().getField("complexArrayField").schema());
arrayOld.add(recordOld1);
arrayOld.add(recordOld2);
rootOld.put("complexArrayField", arrayOld);
rootOld.put(CommonConstants.UUID_FIELD, CommonUtils.generateUuidObject());
rootOld.put(CommonConstants.UUID_FIELD, AvroUtils.generateUuidObject());

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema);
String configurationBodyNew = converter.endcodeToJson(rootNew);
Expand Down Expand Up @@ -175,12 +175,12 @@ public void testValidationOfDifferentTypes() throws Exception {

GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew1.put("intField", 4);
GenericFixed uuidNew1 = CommonUtils.generateUuidObject();
GenericFixed uuidNew1 = AvroUtils.generateUuidObject();
recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1);

GenericRecord recordNew2 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew2.put("intField", 5);
GenericFixed uuidNew2 = CommonUtils.generateUuidObject();
GenericFixed uuidNew2 = AvroUtils.generateUuidObject();
recordNew2.put(CommonConstants.UUID_FIELD, uuidNew2);

GenericRecord rootNew = new GenericData.Record(avroSchema);
Expand All @@ -196,7 +196,7 @@ public void testValidationOfDifferentTypes() throws Exception {

GenericRecord recordOld2 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordOld2.put("intField", 7);
GenericFixed uuidOld2 = CommonUtils.generateUuidObject();
GenericFixed uuidOld2 = AvroUtils.generateUuidObject();
recordOld2.put(CommonConstants.UUID_FIELD, uuidOld2);

GenericRecord rootOld = new GenericData.Record(avroSchema);
Expand Down Expand Up @@ -232,7 +232,7 @@ public void testValidationOfIdenticalUuids() throws Exception {

GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew1.put("intField", 4);
GenericFixed uuidNew1 = CommonUtils.generateUuidObject();
GenericFixed uuidNew1 = AvroUtils.generateUuidObject();
recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1);

GenericRecord recordNew2 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
Expand All @@ -251,15 +251,15 @@ public void testValidationOfIdenticalUuids() throws Exception {

GenericRecord recordOld2 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordOld2.put("intField", 7);
GenericFixed uuidOld2 = CommonUtils.generateUuidObject();
GenericFixed uuidOld2 = AvroUtils.generateUuidObject();
recordOld2.put(CommonConstants.UUID_FIELD, uuidOld2);

GenericRecord rootOld = new GenericData.Record(avroSchema);
GenericArray arrayOld = new GenericData.Array<>(2, rootOld.getSchema().getField("complexArrayField").schema());
arrayOld.add(recordOld1);
arrayOld.add(recordOld2);
rootOld.put("complexArrayField", arrayOld);
rootOld.put(CommonConstants.UUID_FIELD, CommonUtils.generateUuidObject());
rootOld.put(CommonConstants.UUID_FIELD, AvroUtils.generateUuidObject());

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema);
String configurationBodyNew = converter.endcodeToJson(rootNew);
Expand Down Expand Up @@ -287,7 +287,7 @@ public void testValidationOfComplexTypes() throws Exception {

GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew1.put("intField", 4);
GenericFixed uuidNew1 = CommonUtils.generateUuidObject();
GenericFixed uuidNew1 = AvroUtils.generateUuidObject();
recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1);

GenericRecord rootNew = new GenericData.Record(avroSchema);
Expand All @@ -299,7 +299,7 @@ public void testValidationOfComplexTypes() throws Exception {

GenericRecord rootOld = new GenericData.Record(avroSchema);
rootOld.put("recordField", recordOld1);
rootOld.put(CommonConstants.UUID_FIELD, CommonUtils.generateUuidObject());
rootOld.put(CommonConstants.UUID_FIELD, AvroUtils.generateUuidObject());

GenericAvroConverter<GenericRecord> converter = new GenericAvroConverter<>(avroSchema);
String configurationBodyNew = converter.endcodeToJson(rootNew);
Expand All @@ -324,7 +324,7 @@ public void testValidationOfComplexWithoutOldConfiguration() throws Exception {

GenericRecord recordNew1 = new GenericData.Record(schemaParser.getTypes().get("org.kaaproject.recordT"));
recordNew1.put("intField", 4);
GenericFixed uuidNew1 = CommonUtils.generateUuidObject();
GenericFixed uuidNew1 = AvroUtils.generateUuidObject();
recordNew1.put(CommonConstants.UUID_FIELD, uuidNew1);

GenericRecord rootNew = new GenericData.Record(avroSchema);
Expand Down

0 comments on commit b9fbc33

Please sign in to comment.