jpountz commented on a change in pull request #1440: URL: https://github.com/apache/lucene-solr/pull/1440#discussion_r426378746
########## File path: lucene/core/src/java/org/apache/lucene/search/SortedNumericSortField.java ########## @@ -83,6 +89,86 @@ public SortedNumericSortField(String field, SortField.Type type, boolean reverse this.type = type; } + /** A SortFieldProvider for this sort field */ + public static final class Provider extends SortFieldProvider { + + /** The name this provider is registered under */ + public static final String NAME = "SortedNumericSortField"; + + /** Creates a new Provider */ + public Provider() { + super(NAME); + } + + @Override + public SortField readSortField(DataInput in) throws IOException { + SortedNumericSortField sf = new SortedNumericSortField(in.readString(), readType(in), in.readInt() == 1, readSelectorType(in)); + if (in.readInt() == 1) { + switch (sf.type) { + case INT: + sf.setMissingValue(in.readInt()); + break; + case LONG: + sf.setMissingValue(in.readLong()); + break; + case FLOAT: + sf.setMissingValue(NumericUtils.sortableIntToFloat(in.readInt())); + break; + case DOUBLE: + sf.setMissingValue(NumericUtils.sortableLongToDouble(in.readLong())); + break; + default: + throw new AssertionError(); + } + } + return sf; + } + + @Override + public void writeSortField(SortField sf, DataOutput out) throws IOException { + assert sf instanceof SortedNumericSortField; + ((SortedNumericSortField)sf).serialize(out); + } + } + + private static SortedNumericSelector.Type readSelectorType(DataInput in) throws IOException { + int selectorType = in.readInt(); + if (selectorType >= SortedNumericSelector.Type.values().length) { + throw new IllegalArgumentException("Can't deserialize SortedNumericSortField - unknown selector type " + selectorType); + } + return SortedNumericSelector.Type.values()[selectorType]; Review comment: same here? ########## File path: lucene/core/src/java/org/apache/lucene/search/SortField.java ########## @@ -120,6 +126,104 @@ public SortField(String field, Type type, boolean reverse) { this.reverse = reverse; } + /** A SortFieldProvider for field sorts */ + public static final class Provider extends SortFieldProvider { + + /** The name this Provider is registered under */ + public static final String NAME = "SortField"; + + /** Creates a new Provider */ + public Provider() { + super(NAME); + } + + @Override + public SortField readSortField(DataInput in) throws IOException { + SortField sf = new SortField(in.readString(), readType(in), in.readInt() == 1); + if (in.readInt() == 1) { + // missing object + switch (sf.type) { + case STRING: + int missingString = in.readInt(); + if (missingString == 1) { + sf.setMissingValue(STRING_FIRST); + } + else { + sf.setMissingValue(STRING_LAST); + } + break; + case INT: + sf.setMissingValue(in.readInt()); + break; + case LONG: + sf.setMissingValue(in.readLong()); + break; + case FLOAT: + sf.setMissingValue(NumericUtils.sortableIntToFloat(in.readInt())); + break; + case DOUBLE: + sf.setMissingValue(NumericUtils.sortableLongToDouble(in.readLong())); + break; + default: + throw new IllegalArgumentException("Cannot deserialize sort of type " + sf.type); + } + } + return sf; + } + + @Override + public void writeSortField(SortField sf, DataOutput out) throws IOException { + sf.serialize(out); + } + } + + protected static Type readType(DataInput in) throws IOException { + int type = in.readInt(); + if (type >= Type.values().length) { + throw new IllegalArgumentException("Can't deserialize SortField - unknown type " + type); + } + return Type.values()[type]; + } + + private void serialize(DataOutput out) throws IOException { + out.writeString(field); + out.writeInt(type.ordinal()); Review comment: Can we look into not using enum ordinals for serialization? It makes something that looks as harmless as reordering an enum cause backward-compatibility breaks. E.g. maybe we could have a mapping between Type constants and int IDs as a constant on this class? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org