Skip to content

Commit

Permalink
Implement #522: add AvroReadFeature, AvroWriteFeature (refactorin…
Browse files Browse the repository at this point in the history
…g) (#523)
  • Loading branch information
cowtowncoder authored Nov 30, 2024
1 parent 9d75e3d commit ab14f2b
Show file tree
Hide file tree
Showing 15 changed files with 194 additions and 181 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ public class AvroFactory
* Bitfield (set of flags) of all parser features that are enabled
* by default.
*/
final static int DEFAULT_AVRO_PARSER_FEATURE_FLAGS = AvroParser.Feature.collectDefaults();
final static int DEFAULT_AVRO_PARSER_FEATURE_FLAGS = AvroReadFeature.collectDefaults();

/**
* Bitfield (set of flags) of all generator features that are enabled
* by default.
*/
final static int DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS = AvroGenerator.Feature.collectDefaults();
final static int DEFAULT_AVRO_GENERATOR_FEATURE_FLAGS = AvroWriteFeature.collectDefaults();

/*
/**********************************************************
Expand Down Expand Up @@ -188,14 +188,14 @@ public boolean canParseAsync() {
/**
* Checked whether specified parser feature is enabled.
*/
public final boolean isEnabled(AvroParser.Feature f) {
public final boolean isEnabled(AvroReadFeature f) {
return (_formatReadFeatures & f.getMask()) != 0;
}

/**
* Check whether specified generator feature is enabled.
*/
public final boolean isEnabled(AvroGenerator.Feature f) {
public final boolean isEnabled(AvroWriteFeature f) {
return (_formatWriteFeatures & f.getMask()) != 0;
}

Expand All @@ -216,13 +216,13 @@ public boolean canUseSchema(FormatSchema schema) {
}

@Override
public Class<AvroParser.Feature> getFormatReadFeatureType() {
return AvroParser.Feature.class;
public Class<AvroReadFeature> getFormatReadFeatureType() {
return AvroReadFeature.class;
}

@Override
public Class<AvroGenerator.Feature> getFormatWriteFeatureType() {
return AvroGenerator.Feature.class;
public Class<AvroWriteFeature> getFormatWriteFeatureType() {
return AvroWriteFeature.class;
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,65 +84,65 @@ public AvroFactory build() {

// // // Parser features

public AvroFactoryBuilder enable(AvroParser.Feature f) {
public AvroFactoryBuilder enable(AvroReadFeature f) {
_formatReadFeatures |= f.getMask();
return _this();
}

public AvroFactoryBuilder enable(AvroParser.Feature first, AvroParser.Feature... other) {
public AvroFactoryBuilder enable(AvroReadFeature first, AvroReadFeature... other) {
_formatReadFeatures |= first.getMask();
for (AvroParser.Feature f : other) {
for (AvroReadFeature f : other) {
_formatReadFeatures |= f.getMask();
}
return _this();
}

public AvroFactoryBuilder disable(AvroParser.Feature f) {
public AvroFactoryBuilder disable(AvroReadFeature f) {
_formatReadFeatures &= ~f.getMask();
return _this();
}

public AvroFactoryBuilder disable(AvroParser.Feature first, AvroParser.Feature... other) {
public AvroFactoryBuilder disable(AvroReadFeature first, AvroReadFeature... other) {
_formatReadFeatures &= ~first.getMask();
for (AvroParser.Feature f : other) {
for (AvroReadFeature f : other) {
_formatReadFeatures &= ~f.getMask();
}
return _this();
}

public AvroFactoryBuilder configure(AvroParser.Feature f, boolean state) {
public AvroFactoryBuilder configure(AvroReadFeature f, boolean state) {
return state ? enable(f) : disable(f);
}

// // // Generator features

public AvroFactoryBuilder enable(AvroGenerator.Feature f) {
public AvroFactoryBuilder enable(AvroWriteFeature f) {
_formatWriteFeatures |= f.getMask();
return _this();
}

public AvroFactoryBuilder enable(AvroGenerator.Feature first, AvroGenerator.Feature... other) {
public AvroFactoryBuilder enable(AvroWriteFeature first, AvroWriteFeature... other) {
_formatWriteFeatures |= first.getMask();
for (AvroGenerator.Feature f : other) {
for (AvroWriteFeature f : other) {
_formatWriteFeatures |= f.getMask();
}
return _this();
}

public AvroFactoryBuilder disable(AvroGenerator.Feature f) {
public AvroFactoryBuilder disable(AvroWriteFeature f) {
_formatWriteFeatures &= ~f.getMask();
return _this();
}

public AvroFactoryBuilder disable(AvroGenerator.Feature first, AvroGenerator.Feature... other) {
public AvroFactoryBuilder disable(AvroWriteFeature first, AvroWriteFeature... other) {
_formatWriteFeatures &= ~first.getMask();
for (AvroGenerator.Feature f : other) {
for (AvroWriteFeature f : other) {
_formatWriteFeatures &= ~f.getMask();
}
return _this();
}

public AvroFactoryBuilder configure(AvroGenerator.Feature f, boolean state) {
public AvroFactoryBuilder configure(AvroWriteFeature f, boolean state) {
return state ? enable(f) : disable(f);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,79 +20,6 @@

public class AvroGenerator extends GeneratorBase
{
/**
* Enumeration that defines all togglable features for Avro generators
*/
public enum Feature
implements FormatFeature
{
/**
* Feature that can be disabled to prevent Avro from buffering any more
* data then absolutely necessary.
* This affects buffering by underlying codec.
* Note that disabling buffer is likely to reduce performance if the underlying
* input/output is unbuffered.
*<p>
* Enabled by default to preserve the existing behavior.
*/
AVRO_BUFFERING(true),

/**
* Feature that tells Avro to write data in file format (i.e. including the schema with the data)
* rather than the RPC format which is otherwise default
*<p>
* NOTE: reader-side will have to be aware of distinction as well, since possible inclusion
* of this header is not 100% reliably auto-detectable (while header has distinct marker,
* "raw" Avro content has no limitations and could theoretically have same pre-amble from data).
*/
AVRO_FILE_OUTPUT(false),

/**
* Feature that enables addition of {@code null} as default value in generated schema
* when no real default value is defined and {@code null} is legal value for type
* (union type with {@code null} included).
*<p>
* Disabled by default.
*
* @since 3.0
*
*/
ADD_NULL_AS_DEFAULT_VALUE_IN_SCHEMA(false)
;

protected final boolean _defaultState;
protected final int _mask;

/**
* Method that calculates bit set (flags) of all features that
* are enabled by default.
*/
public static int collectDefaults()
{
int flags = 0;
for (Feature f : values()) {
if (f.enabledByDefault()) {
flags |= f.getMask();
}
}
return flags;
}

private Feature(boolean defaultState) {
_defaultState = defaultState;
_mask = (1 << ordinal());
}

@Override
public boolean enabledByDefault() { return _defaultState; }

@Override
public int getMask() { return _mask; }

@Override
public boolean enabledIn(int flags) { return (flags & _mask) != 0; }
}

/*
/**********************************************************************
/* Configuration
Expand All @@ -111,7 +38,7 @@ private Feature(boolean defaultState) {

/**
* Bit flag composed of bits that indicate which
* {@link AvroGenerator.Feature}s
* {@link AvroWriteFeature}s
* are enabled.
*/
protected int _formatWriteFeatures;
Expand Down Expand Up @@ -165,7 +92,7 @@ public AvroGenerator(ObjectWriteContext writeCtxt, IOContext ioCtxt,
_output = output;
_streamWriteContext = AvroWriteContext.nullContext();
_apacheCodecRecycler = apacheCodecRecycler;
final boolean buffering = isEnabled(Feature.AVRO_BUFFERING);
final boolean buffering = isEnabled(AvroWriteFeature.AVRO_BUFFERING);
BinaryEncoder encoderToReuse = apacheCodecRecycler.acquireEncoder();
_encoder = buffering
? ENCODER_FACTORY.binaryEncoder(output, encoderToReuse)
Expand Down Expand Up @@ -247,21 +174,21 @@ public JacksonFeatureSet<StreamWriteCapability> streamWriteCapabilities() {
/**********************************************************************
*/

public AvroGenerator enable(Feature f) {
public AvroGenerator enable(AvroWriteFeature f) {
_formatWriteFeatures |= f.getMask();
return this;
}

public AvroGenerator disable(Feature f) {
public AvroGenerator disable(AvroWriteFeature f) {
_formatWriteFeatures &= ~f.getMask();
return this;
}

public final boolean isEnabled(Feature f) {
public final boolean isEnabled(AvroWriteFeature f) {
return (_formatWriteFeatures & f.getMask()) != 0;
}

public AvroGenerator configure(Feature f, boolean state) {
public AvroGenerator configure(AvroWriteFeature f, boolean state) {
if (state) {
enable(f);
} else {
Expand Down
20 changes: 10 additions & 10 deletions avro/src/main/java/tools/jackson/dataformat/avro/AvroMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ protected MapperBuilderState _saveState() {
/******************************************************************
*/

public Builder enable(AvroParser.Feature... features) {
for (AvroParser.Feature f : features) {
public Builder enable(AvroReadFeature... features) {
for (AvroReadFeature f : features) {
_formatReadFeatures |= f.getMask();
}
return this;
}

public Builder disable(AvroParser.Feature... features) {
for (AvroParser.Feature f : features) {
public Builder disable(AvroReadFeature... features) {
for (AvroReadFeature f : features) {
_formatReadFeatures &= ~f.getMask();
}
return this;
}

public Builder configure(AvroParser.Feature feature, boolean state)
public Builder configure(AvroReadFeature feature, boolean state)
{
if (state) {
_formatReadFeatures |= feature.getMask();
Expand All @@ -91,21 +91,21 @@ public Builder configure(AvroParser.Feature feature, boolean state)
return this;
}

public Builder enable(AvroGenerator.Feature... features) {
for (AvroGenerator.Feature f : features) {
public Builder enable(AvroWriteFeature... features) {
for (AvroWriteFeature f : features) {
_formatWriteFeatures |= f.getMask();
}
return this;
}

public Builder disable(AvroGenerator.Feature... features) {
for (AvroGenerator.Feature f : features) {
public Builder disable(AvroWriteFeature... features) {
for (AvroWriteFeature f : features) {
_formatWriteFeatures &= ~f.getMask();
}
return this;
}

public Builder configure(AvroGenerator.Feature feature, boolean state)
public Builder configure(AvroWriteFeature feature, boolean state)
{
if (state) {
_formatWriteFeatures |= feature.getMask();
Expand Down
Loading

0 comments on commit ab14f2b

Please sign in to comment.