Package org.apache.avro
Class ParseContext
java.lang.Object
org.apache.avro.ParseContext
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 Summary
ConstructorDescriptionCreate aParseContext
for the default/null
namespace, using default name validation for new schemata.ParseContext
(NameValidator nameValidator) Create aParseContext
using the specified name validation for new schemata. -
Method Summary
Modifier and TypeMethodDescriptionvoid
commit()
boolean
Tell whether this context contains a schema with the given name.Find a schema by name and namespace.getNamedSchema
(String fullName) Get a schema by name.boolean
void
Put the schema into this context.Resolve unresolved references in a schema that was parsed for this context using the types known to this context.Resolve all (named) schemas that were parsed.void
rollback()
Return all known types by their fullname.
-
Constructor Details
-
ParseContext
public ParseContext()Create aParseContext
for the default/null
namespace, using default name validation for new schemata. -
ParseContext
Create aParseContext
using the specified name validation for new schemata.
-
-
Method Details
-
contains
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
Find a schema by name and namespace.
That is:
- If
name
is a primitive name, return a (new) schema for it - Otherwise, determine the full schema name (using the given
namespace
if necessary), and find it - If no schema was found and
name
is a simple name, find the schema in the default (null) namespace - 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. TheSchemaParser
and protocol parsers will ensure you'll only get a resolved schema that is usable.- Parameters:
name
- the schema name to findnamespace
- the namespace to find the schema against- Returns:
- the schema, or an unresolved reference
- If
-
getNamedSchema
Get a schema by name. Note that the schema might not (yet) be resolved/usable untilresolveAllSchemas()
has been called.- Parameters:
fullName
- a full schema name- Returns:
- the schema, if known
-
put
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
-
rollback
public void rollback() -
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
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
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
-