Class ParseContext

java.lang.Object
org.apache.avro.ParseContext

public class ParseContext extends Object
Class to define a name context, useful to reference schemata with. This allows for the following:
  • Collect new named schemata.
  • Find schemata by name, including primitives.
  • Find schemas that do not exist yet.
  • Resolve references to schemas that didn't exist yet when first used.

This class is NOT thread-safe.

Note: this class has no use for most Avro users, but is a key component when implementing a schema parser.

See Also:
  • Constructor Details

    • ParseContext

      public ParseContext()
      Create a ParseContext for the default/null namespace, using default name validation for new schemata.
    • ParseContext

      public ParseContext(NameValidator nameValidator)
      Create a ParseContext using the specified name validation for new schemata.
  • Method Details

    • contains

      public boolean contains(String name)
      Tell whether this context contains a schema with the given name.
      Parameters:
      name - a schema name
      Returns:
      true if the context contains a schema with this name, false otherwise
    • find

      public Schema find(String name, String namespace)

      Find a schema by name and namespace.

      That is:

      1. If name is a primitive name, return a (new) schema for it
      2. Otherwise, determine the full schema name (using the given namespace if necessary), and find it
      3. If no schema was found and name is a simple name, find the schema in the default (null) namespace
      4. If still no schema was found, return an unresolved reference for the full schema name (see step 2)

      Note: as an unresolved reference might be returned, the schema is not directly usable. Please put(Schema) the schema using it in the context. The SchemaParser and protocol parsers will ensure you'll only get a resolved schema that is usable.

      Parameters:
      name - the schema name to find
      namespace - the namespace to find the schema against
      Returns:
      the schema, or an unresolved reference
    • getNamedSchema

      public Schema getNamedSchema(String fullName)
      Get a schema by name. Note that the schema might not (yet) be resolved/usable until resolveAllSchemas() has been called.
      Parameters:
      fullName - a full schema name
      Returns:
      the schema, if known
    • put

      public void put(Schema schema)
      Put the schema into this context. This is an idempotent operation: it only fails if this context already has a different schema with the same name.

      Note that although this method works for all types except for arrays, maps and unions, all primitive types have already been defined upon construction. This means you cannot redefine a 'long' with a logical timestamp type.

      Parameters:
      schema - the schema to put into the context
    • hasNewSchemas

      public boolean hasNewSchemas()
    • commit

      public void commit()
    • commit

      public SchemaParser.ParseResult commit(Schema mainSchema)
    • rollback

      public void rollback()
    • resolveAllSchemas

      public List<Schema> resolveAllSchemas()
      Resolve all (named) schemas that were parsed. This resolves all forward references, even if parsed from different files. Note: the context must be committed for this method to work.
      Returns:
      all parsed schemas, in the order they were parsed
      Throws:
      AvroTypeException - if a schema reference cannot be resolved
    • resolve

      public Schema resolve(Schema schema)
      Resolve unresolved references in a schema that was parsed for this context using the types known to this context. Note: this method will ensure all known schemas are resolved, or throw, and thus requires the context to be committed.
      Parameters:
      schema - the schema resolve
      Returns:
      the fully resolved schema
      Throws:
      AvroTypeException - if a schema reference cannot be resolved
    • typesByName

      public Map<String,Schema> typesByName()
      Return all known types by their fullname. Warning: this returns all types, even uncommitted ones, including unresolved references!
      Returns:
      a map of all types by their name