Search in sources :

Example 1 with ParseException

use of com.linkedin.avroutil1.parser.exceptions.ParseException in project avro-util by linkedin.

the class CodeGenerator method parseSchemas.

private Map<String, SchemaDetails> parseSchemas(Set<File> toParse, Map<String, SchemaDetails> importableSchemas, Map<String, SchemaDetails> externalSchemas) {
    Collection<File> toTry = toParse;
    Map<String, SchemaDetails> successfullyParsed = new HashMap<>();
    int passNumber = 0;
    while (!toTry.isEmpty()) {
        boolean madeProgress = false;
        passNumber++;
        Map<File, FileParseIssue> failedFiles = new HashMap<>();
        for (File schemaFile : toTry) {
            String fileContents = readFile(schemaFile);
            SchemaParseResult result;
            try {
                // build up a collection of all known schemas to hand to avro
                List<Schema> allKnownSchemas = buildUpKnownSchemaLists(successfullyParsed, importableSchemas, externalSchemas);
                // call avro to parse our file given all the known schemas we've built up above
                result = AvroCompatibilityHelper.parse(fileContents, SchemaParseConfiguration.STRICT, allKnownSchemas);
                madeProgress = true;
            } catch (SchemaParseException parseException) {
                failedFiles.put(schemaFile, new FileParseIssue(schemaFile, parseException));
                // to next file
                continue;
            } catch (Exception other) {
                throw new IllegalStateException("while trying to parse file " + schemaFile, other);
            }
            // go over avro results and determine what schemas are new in the file parsed
            Map<String, Schema> actuallyNewSchemas = processParseResults(schemaFile, result, successfullyParsed, importableSchemas, externalSchemas);
            // store new schemas
            actuallyNewSchemas.forEach((fullName, schema) -> {
                if (successfullyParsed.put(fullName, SchemaDetails.fromFile(schema, schemaFile, schema == result.getMainSchema())) != null) {
                    throw new IllegalStateException();
                }
            });
        }
        if (!madeProgress) {
            // classify our issues
            failedFiles.forEach((file, issue) -> issue.setClassification(classifyIssue(issue.getException())));
            // terminate for any issues that we cant handle
            throwForFatalErrors(failedFiles, successfullyParsed, importableSchemas);
            // if we got here none of the issues are fatal
            if (allowClasspathLookup) {
                ClasspathFishingResults fishingResults = goFishingOnTheClasspath(failedFiles);
                Map<String, SchemaDetails> loot = fishingResults.getFqcnsFound();
                if (!loot.isEmpty()) {
                    // yay, we live to iterate another day!
                    externalSchemas.putAll(loot);
                } else {
                    throw new IllegalArgumentException("unable to find records " + fishingResults.getFqcnsNotFound() + " used in " + failedFiles.keySet() + ". not even on the classpath");
                }
            } else {
                // TODO - improve on this error msg?
                throw new IllegalArgumentException("cannot make progress. files left: " + failedFiles + ". did not examine classpath");
            }
        }
        toTry = failedFiles.keySet();
    }
    LOG.info("parsed {} schemas out of {} files in {} passes", successfullyParsed.size(), toParse.size(), passNumber);
    return successfullyParsed;
}
Also used : SchemaParseResult(com.linkedin.avroutil1.compatibility.SchemaParseResult) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) IOException(java.io.IOException) SchemaParseException(org.apache.avro.SchemaParseException) SchemaParseException(org.apache.avro.SchemaParseException) File(java.io.File)

Aggregations

SchemaParseResult (com.linkedin.avroutil1.compatibility.SchemaParseResult)1 File (java.io.File)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Schema (org.apache.avro.Schema)1 SchemaParseException (org.apache.avro.SchemaParseException)1