Skip to content

Commit

Permalink
remove last instance of Filter.encode()
Browse files Browse the repository at this point in the history
implement $mod's codec
  • Loading branch information
evanchooly committed Oct 15, 2023
1 parent 168ea9a commit 2dfa71b
Show file tree
Hide file tree
Showing 18 changed files with 143 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import dev.morphia.query.filters.Filter;

import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.EncoderContext;
import org.bson.codecs.configuration.CodecRegistry;

Expand All @@ -22,6 +23,7 @@ public Class<GraphLookup> getEncoderClass() {
return GraphLookup.class;
}

@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
protected void encodeStage(BsonWriter writer, GraphLookup value, EncoderContext encoderContext) {
document(writer, () -> {
Expand All @@ -41,7 +43,8 @@ protected void encodeStage(BsonWriter writer, GraphLookup value, EncoderContext
if (restriction != null) {
document(writer, "restrictSearchWithMatch", () -> {
for (Filter filter : restriction) {
filter.encode(getDatastore(), writer, encoderContext);
Codec codec = getCodecRegistry().get(filter.getClass());
codec.encode(writer, filter, encoderContext);
}
});
}
Expand Down
32 changes: 32 additions & 0 deletions core/src/main/java/dev/morphia/mapping/codec/CodecHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.mongodb.lang.NonNull;
import com.mongodb.lang.Nullable;

import dev.morphia.MorphiaDatastore;
import dev.morphia.aggregation.expressions.impls.Expression;
import dev.morphia.aggregation.expressions.impls.SingleValuedExpression;
import dev.morphia.annotations.internal.MorphiaInternal;
Expand Down Expand Up @@ -138,4 +139,35 @@ public static void value(CodecRegistry codecRegistry, BsonWriter writer, String
encoderContext.encodeWithChildContext(codec, writer, value);
}
}

/**
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public static void namedValue(BsonWriter writer, MorphiaDatastore datastore, @Nullable String name, @Nullable Object value,
EncoderContext encoderContext) {
writer.writeName(name);
if (value != null) {
Codec codec = datastore.getCodecRegistry().get(value.getClass());
encoderContext.encodeWithChildContext(codec, writer, value);
} else {
writer.writeNull();
}
}

/**
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public static void unnamedValue(BsonWriter writer, MorphiaDatastore datastore, @Nullable Object value,
EncoderContext encoderContext) {
if (value != null) {
Codec codec = datastore.getCodecRegistry().get(value.getClass());
encoderContext.encodeWithChildContext(codec, writer, value);
} else {
writer.writeNull();
}
}
}
33 changes: 33 additions & 0 deletions core/src/main/java/dev/morphia/mapping/codec/ModFilterCodec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.morphia.mapping.codec;

import dev.morphia.MorphiaDatastore;
import dev.morphia.mapping.codec.filters.BaseFilterCodec;
import dev.morphia.query.filters.ModFilter;

import org.bson.BsonWriter;
import org.bson.codecs.EncoderContext;

import static dev.morphia.mapping.codec.CodecHelper.array;
import static dev.morphia.mapping.codec.CodecHelper.document;
import static dev.morphia.mapping.codec.CodecHelper.unnamedValue;

public class ModFilterCodec extends BaseFilterCodec<ModFilter> {
public ModFilterCodec(MorphiaDatastore datastore) {
super(datastore);
}

@Override
public void encode(BsonWriter writer, ModFilter filter, EncoderContext context) {
document(writer, filter.path(datastore.getMapper()), () -> {
array(writer, filter.getName(), () -> {
unnamedValue(writer, datastore, filter.divisor(), context);
unnamedValue(writer, datastore, filter.remainder(), context);
});
});
}

@Override
public Class<ModFilter> getEncoderClass() {
return ModFilter.class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public MorphiaFilterCodecProvider(MorphiaDatastore datastore) {
addCodec(new GeoIntersectsFilterCodec(datastore));
addCodec(new JsonSchemaFilterCodec(datastore));
addCodec(new LogicalFilterCodec(datastore));
addCodec(new ModFilterCodec(datastore));
addCodec(new NearFilterCodec(datastore));
addCodec(new PolygonFilterCodec(datastore));
addCodec(new RegexFilterCodec(datastore));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
package dev.morphia.mapping.codec.filters;

import com.mongodb.lang.Nullable;

import dev.morphia.MorphiaDatastore;
import dev.morphia.annotations.internal.MorphiaInternal;
import dev.morphia.query.filters.Filter;
import dev.morphia.sofia.Sofia;

import org.bson.BsonReader;
import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.DecoderContext;
import org.bson.codecs.EncoderContext;

public abstract class BaseFilterCodec<T extends Filter> implements Codec<T> {
protected MorphiaDatastore datastore;
Expand All @@ -25,34 +20,4 @@ public final T decode(BsonReader reader, DecoderContext decoderContext) {
throw new UnsupportedOperationException(Sofia.encodingOnly());
}

/**
* @hidden
* @morphia.internal
*/
@MorphiaInternal
protected void writeNamedValue(@Nullable String name, @Nullable Object value, MorphiaDatastore datastore, BsonWriter writer,
EncoderContext encoderContext) {
writer.writeName(name);
if (value != null) {
Codec codec = datastore.getCodecRegistry().get(value.getClass());
encoderContext.encodeWithChildContext(codec, writer, value);
} else {
writer.writeNull();
}
}

/**
* @hidden
* @morphia.internal
*/
@MorphiaInternal
protected void writeUnnamedValue(@Nullable Object value, MorphiaDatastore datastore, BsonWriter writer, EncoderContext encoderContext) {
if (value != null) {
Codec codec = datastore.getCodecRegistry().get(value.getClass());
encoderContext.encodeWithChildContext(codec, writer, value);
} else {
writer.writeNull();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.mapping.codec.filters;

import dev.morphia.MorphiaDatastore;
import dev.morphia.mapping.codec.CodecHelper;
import dev.morphia.query.filters.EqFilter;

import org.bson.BsonWriter;
Expand All @@ -19,11 +20,11 @@ public void encode(BsonWriter writer, EqFilter filter, EncoderContext encoderCon
document(writer, filter.path(datastore.getMapper()), () -> {
document(writer, "$not", () -> {
writer.writeName(filter.getName());
writeUnnamedValue(filter.getValue(datastore), datastore, writer, encoderContext);
CodecHelper.unnamedValue(writer, datastore, filter.getValue(datastore), encoderContext);
});
});
} else {
writeNamedValue(filter.path(datastore.getMapper()), filter.getValue(datastore), datastore, writer, encoderContext);
CodecHelper.namedValue(writer, datastore, filter.path(datastore.getMapper()), filter.getValue(datastore), encoderContext);
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.mapping.codec.filters;

import dev.morphia.MorphiaDatastore;
import dev.morphia.mapping.codec.CodecHelper;
import dev.morphia.query.filters.Filter;

import org.bson.BsonWriter;
Expand All @@ -18,10 +19,10 @@ public void encode(BsonWriter writer, Filter filter, EncoderContext encoderConte
document(writer, filter.path(datastore.getMapper()), () -> {
if (filter.isNot()) {
document(writer, "$not", () -> {
writeNamedValue(filter.getName(), filter.getValue(datastore), datastore, writer, encoderContext);
CodecHelper.namedValue(writer, datastore, filter.getName(), filter.getValue(datastore), encoderContext);
});
} else {
writeNamedValue(filter.getName(), filter.getValue(datastore), datastore, writer, encoderContext);
CodecHelper.namedValue(writer, datastore, filter.getName(), filter.getValue(datastore), encoderContext);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.morphia.mapping.codec.filters;

import dev.morphia.MorphiaDatastore;
import dev.morphia.mapping.codec.CodecHelper;
import dev.morphia.query.filters.NearFilter;

import org.bson.BsonWriter;
Expand Down Expand Up @@ -31,7 +32,7 @@ public void encode(BsonWriter writer, NearFilter near, EncoderContext encoderCon
private void encodeFilter(BsonWriter writer, NearFilter near, EncoderContext context) {
document(writer, near.getName(), () -> {
writer.writeName("$geometry");
writeUnnamedValue(near.getValue(datastore), datastore, writer, context);
CodecHelper.unnamedValue(writer, datastore, near.getValue(datastore), context);
value(writer, "$maxDistance", near.maxDistance());
value(writer, "$minDistance", near.minDistance());
value(datastore.getCodecRegistry(), writer, "crs", near.crs(), context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.morphia.MorphiaDatastore;
import dev.morphia.aggregation.expressions.SampleRateFilter;
import dev.morphia.mapping.codec.CodecHelper;

import org.bson.BsonWriter;
import org.bson.codecs.EncoderContext;
Expand All @@ -13,7 +14,7 @@ public SampleRateFilterCodec(MorphiaDatastore datastore) {

@Override
public void encode(BsonWriter writer, SampleRateFilter filter, EncoderContext encoderContext) {
writeNamedValue(filter.getName(), filter.getValue(), datastore, writer, encoderContext);
CodecHelper.namedValue(writer, datastore, filter.getName(), filter.getValue(), encoderContext);
}

@Override
Expand Down
12 changes: 0 additions & 12 deletions core/src/main/java/dev/morphia/query/filters/Filter.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,6 @@ public boolean isNot() {
return not;
}

/**
* @param datastore the datastore
* @param writer the writer
* @param context the context
* @hidden
* @morphia.internal
*/
@Deprecated
public void encode(MorphiaDatastore datastore, BsonWriter writer, EncoderContext context) {
throw new UnsupportedOperationException();
}

/**
* Sets the query entity type on the filter
*
Expand Down
20 changes: 0 additions & 20 deletions core/src/main/java/dev/morphia/query/filters/GeoWithinFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,8 @@
import com.mongodb.client.model.geojson.MultiPolygon;
import com.mongodb.client.model.geojson.Polygon;

import dev.morphia.MorphiaDatastore;
import dev.morphia.annotations.internal.MorphiaInternal;

import org.bson.BsonWriter;
import org.bson.codecs.Codec;
import org.bson.codecs.EncoderContext;

/**
* Defines a $geoWithin filter.
*
Expand Down Expand Up @@ -49,19 +44,4 @@ public GeoWithinFilter crs(CoordinateReferenceSystem crs) {
return this;
}

@Override
public final void encode(MorphiaDatastore datastore, BsonWriter writer, EncoderContext context) {
writer.writeStartDocument(path(datastore.getMapper()));
writer.writeStartDocument(getName());
writer.writeName("$geometry");

Object shape = getValue();
if (shape != null) {
Codec codec = datastore.getCodecRegistry().get(shape.getClass());
codec.encode(writer, shape, context);
}

writer.writeEndDocument();
writer.writeEndDocument();
}
}
39 changes: 25 additions & 14 deletions core/src/main/java/dev/morphia/query/filters/ModFilter.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package dev.morphia.query.filters;

import dev.morphia.MorphiaDatastore;
import dev.morphia.annotations.internal.MorphiaInternal;

import org.bson.BsonWriter;
import org.bson.codecs.EncoderContext;

class ModFilter extends Filter {
/**
* @hidden
* @morphia.internal
*/
@MorphiaInternal
public class ModFilter extends Filter {
private final long divisor;

private final long remainder;
Expand All @@ -16,14 +18,23 @@ public ModFilter(String field, long divisor, long remainder) {
this.remainder = remainder;
}

@Override
public void encode(MorphiaDatastore datastore, BsonWriter writer, EncoderContext context) {
writer.writeStartDocument(path(datastore.getMapper()));
writer.writeName(getName());
writer.writeStartArray();
writeValue(divisor, datastore, writer, context);
writeValue(remainder, datastore, writer, context);
writer.writeEndArray();
writer.writeEndDocument();
/**
* @hidden
* @morphia.internal
* @return the divisor
*/
@MorphiaInternal
public long divisor() {
return divisor;
}

/**
* @hidden
* @morphia.internal
* @return the remainder
*/
@MorphiaInternal
public long remainder() {
return remainder;
}
}
2 changes: 1 addition & 1 deletion core/src/test/java/dev/morphia/test/TemplatedTestBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TemplatedTestBase(MorphiaConfig config) {
}

public final String prefix() {
String root = getClass().getSimpleName().substring(4);
String root = getClass().getSimpleName().replace("Test", "");
return toLowerCase(root.charAt(0)) + root.substring(1);
}

Expand Down
Loading

0 comments on commit 2dfa71b

Please sign in to comment.