static FileDescriptorProto validateAndGenerate(HasStorage object,
ProtobufRowFormat.Type formatType,
FileDescriptorProto fileProto,
AISValidationOutput output) {
if (!(object instanceof Group)) {
output.reportFailure(new AISValidationFailure(new StorageDescriptionInvalidException(object, "is not a Group and cannot use Protocol Buffers")));
return null;
}
Group group = (Group)object;
if (formatType == ProtobufRowFormat.Type.SINGLE_TABLE) {
if (!group.getRoot().getChildJoins().isEmpty()) {
output.reportFailure(new AISValidationFailure(new StorageDescriptionInvalidException(object, "has more than one table")));
return null;
}
}
int currentVersion = sumTableVersions(group.getRoot());
if (fileProto != null) {
int storedVersion = fileProto.getOptions()
.getExtension(CustomOptions.GroupOptions.fdbsql).getVersion();
if (storedVersion == currentVersion) {
return fileProto;
}
}
FileDescriptorSet set = null;
if (fileProto != null) {
FileDescriptorSet.Builder builder = FileDescriptorSet.newBuilder();
builder.addFile(fileProto);
set = builder.build();
}
AISToProtobuf ais2p = new AISToProtobuf(formatType, set);
ais2p.addGroup(group);
set = ais2p.build();
fileProto = set.getFile(0); // Only added one group.
// Make sure it will build before committing to this format.
try {
FileDescriptor.buildFrom(fileProto, DEPENDENCIES);
}
catch (DescriptorValidationException ex) {
output.reportFailure(new AISValidationFailure(new ProtobufBuildException(ex)));
}
return fileProto;
}