ManagedSchema managedSchema = getManagedSchema(tableName, entityName);
KeyEntitySchemaParser<?, ?> schemaParser = getSchemaParser(managedSchema
.getSchemaType());
// validate it's a valid avro schema by parsing it
EntitySchema newEntitySchema = schemaParser.parseEntitySchema(newSchemaStr);
KeySchema newKeySchema = schemaParser.parseKeySchema(newSchemaStr);
// verify that the newSchema isn't a duplicate of a previous schema version.
int existingVersion = getEntityVersion(tableName, entityName,
newEntitySchema);
if (existingVersion != -1) {
throw new IncompatibleSchemaException(
"Schema already exists as version: "
+ Integer.toString(existingVersion));
}
// validate that, for each version of the schema, this schema is
// compatible with those schema version. That means the field mapping
// hasn't changed, and we can read old schemas, and processes that
// are configured with old schemas can read new schemas.
int greatestSchemaVersion = 0;
for (Entry<String, String> entry : managedSchema.getEntitySchemas()
.entrySet()) {
int version = Integer.parseInt(entry.getKey());
if (version > greatestSchemaVersion) {
greatestSchemaVersion = version;
}
String schemaString = entry.getValue();
KeySchema keySchema = schemaParser.parseKeySchema(schemaString);
EntitySchema entitySchema = schemaParser.parseEntitySchema(schemaString);
if (!newKeySchema.compatible(keySchema)) {
String msg = "StorageKey fields of entity schema not compatible with version "
+ Integer.toString(version) + ": Old schema: " + schemaString
+ " New schema: " + newEntitySchema.getRawSchema();
throw new IncompatibleSchemaException(msg);