// Perform avro schema validation (if necessary).
switch (mValidationPolicy) {
case STRICT: {
if (!mRegisteredWriters.contains(writerSchema)) {
throw new KijiEncodingException(
String.format("Error trying to use unregistered writer schema: %s",
writerSchema.toString(true)));
}
break;
}
case DEVELOPER: {
if (!mRegisteredWriters.contains(writerSchema)) {
LOG.info("Writer schema {} is currently not registered for column {}, registering now.",
writerSchema, mCellSpec.getColumnURI());
if (mCellSpec.getColumnURI() == null) {
throw new InternalKijiError("CellSpec has no column URI: " + mCellSpec);
}
registerWriterSchema(mCellSpec.getColumnURI(), writerSchema);
}
break;
}
case NONE:
// No-op. No validation required.
break;
case SCHEMA_1_0:
// No-op. Validation happens for primitive types only during Avro serialization by setting
// the writer schema (see getWriterSchema()).
break;
default: {
throw new KijiEncodingException(
String.format("Unrecognized schema validation policy: %s",
mValidationPolicy.toString()));
}
}
// Perform final column schema validation (if necessary).
if (mCellSpec.getCellSchema().getStorage() == SchemaStorage.FINAL
&& !writerSchema.equals(mReaderSchema)) {
throw new KijiEncodingException(
String.format("Writer schema: %s does not match final column schema: %s",
writerSchema.toString(true),
mReaderSchema.toString(true)));
}
// Encode the Avro schema (if necessary):
mSchemaEncoder.encode(writerSchema);
// Encode the cell value:
try {
getDatumWriter(writerSchema).write(cellValue, mByteArrayEncoder);
} catch (ClassCastException cce) {
throw new KijiEncodingException(cce);
} catch (AvroRuntimeException ure) {
throw new KijiEncodingException(ure);
}
return mByteArrayOutputStream.toByteArray();
}