diff options
author | Robert Newson <rnewson@apache.org> | 2023-04-13 17:27:39 +0100 |
---|---|---|
committer | Robert Newson <rnewson@apache.org> | 2023-04-22 11:20:02 +0100 |
commit | 13e87b8e55b88a3e7e3c996525f305c5cfd29817 (patch) | |
tree | 9fcece426757cf7e982cf1fc873913b626f9a238 | |
parent | fd8a1e1d584c59a39ebade5e4e491b1036ca0b28 (diff) | |
download | couchdb-13e87b8e55b88a3e7e3c996525f305c5cfd29817.tar.gz |
introduce PrimitiveWrapper so openapi.yaml is better than 'object'
13 files changed, 245 insertions, 228 deletions
diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/api/After.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/api/After.java deleted file mode 100644 index 22681ab2b..000000000 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/api/After.java +++ /dev/null @@ -1,53 +0,0 @@ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.apache.couchdb.nouveau.api; - -import java.util.Arrays; - -import org.apache.couchdb.nouveau.core.ser.AfterDeserializer; -import org.apache.couchdb.nouveau.core.ser.AfterSerializer; - -import com.fasterxml.jackson.databind.PropertyNamingStrategies; -import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import com.fasterxml.jackson.databind.annotation.JsonNaming; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; - -@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class) -@JsonSerialize(using = AfterSerializer.class) -@JsonDeserialize(using = AfterDeserializer.class) -public class After { - - private Object[] fields; - - public After() { - } - - public After(final Object... fields) { - this.fields = fields; - } - - public Object[] getFields() { - return fields; - } - - public void setFields(Object[] fields) { - this.fields = fields; - } - - @Override - public String toString() { - return "After [fields=" + Arrays.toString(fields) + "]"; - } - -} diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchHit.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchHit.java index 62346197c..678970e04 100644 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchHit.java +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchHit.java @@ -14,11 +14,13 @@ package org.apache.couchdb.nouveau.api; import java.util.Collection; +import java.util.Objects; + +import org.apache.couchdb.nouveau.core.ser.PrimitiveWrapper; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; - import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; @@ -29,7 +31,7 @@ public class SearchHit { private String id; @NotNull - private After order; + private PrimitiveWrapper<?>[] order; @NotNull private Collection<@NotNull StoredField> fields; @@ -37,17 +39,17 @@ public class SearchHit { public SearchHit() { } - public SearchHit(final String id, final After order, final Collection<StoredField> fields) { + public SearchHit(final String id, final PrimitiveWrapper<?>[] order, final Collection<StoredField> fields) { this.id = id; - this.order = order; - this.fields = fields; + this.order = Objects.requireNonNull(order); + this.fields = Objects.requireNonNull(fields); } public String getId() { return id; } - public After getOrder() { + public PrimitiveWrapper<?>[] getOrder() { return order; } diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchRequest.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchRequest.java index 6a86c4e35..c39213e2d 100644 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchRequest.java +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/api/SearchRequest.java @@ -16,11 +16,12 @@ package org.apache.couchdb.nouveau.api; import java.util.List; import java.util.Map; +import org.apache.couchdb.nouveau.core.ser.PrimitiveWrapper; + import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategies; import com.fasterxml.jackson.databind.annotation.JsonNaming; - import jakarta.validation.constraints.Max; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotEmpty; @@ -44,7 +45,7 @@ public class SearchRequest { private Map<@NotEmpty String, List<@NotNull DoubleRange>> ranges; - private After after; + private PrimitiveWrapper<?>[] after; @Min(1) @Max(100) @@ -129,12 +130,12 @@ public class SearchRequest { return topN; } - public void setAfter(final After after) { + public void setAfter(final PrimitiveWrapper<?>[] after) { this.after = after; } @JsonProperty - public After getAfter() { + public PrimitiveWrapper<?>[] getAfter() { return after; } diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/AfterDeserializer.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/AfterDeserializer.java deleted file mode 100644 index 82fadfe86..000000000 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/AfterDeserializer.java +++ /dev/null @@ -1,71 +0,0 @@ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.apache.couchdb.nouveau.core.ser; - -import java.io.IOException; -import java.nio.charset.Charset; - -import org.apache.couchdb.nouveau.api.After; - -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.DeserializationContext; -import com.fasterxml.jackson.databind.JsonNode; -import com.fasterxml.jackson.databind.deser.std.StdDeserializer; -import com.fasterxml.jackson.databind.node.ArrayNode; - -public class AfterDeserializer extends StdDeserializer<After> { - - public AfterDeserializer() { - this(null); - } - - public AfterDeserializer(Class<?> vc) { - super(vc); - } - - @Override - public After deserialize(final JsonParser parser, final DeserializationContext context) - throws IOException, JsonProcessingException { - ArrayNode fieldNode = (ArrayNode) parser.getCodec().readTree(parser); - final Object[] fields = new Object[fieldNode.size()]; - for (int i = 0; i < fields.length; i++) { - final JsonNode field = fieldNode.get(i); - switch (field.get("@type").asText()) { - case "string": - fields[i] = field.get("value").asText().getBytes(Charset.forName("UTF-8")); - break; - case "bytes": - fields[i] = field.get("value").binaryValue(); - break; - case "float": - fields[i] = field.get("value").floatValue(); - break; - case "double": - fields[i] = field.get("value").doubleValue(); - break; - case "int": - fields[i] = field.get("value").intValue(); - break; - case "long": - fields[i] = field.get("value").longValue(); - break; - default: - throw new IOException("Unsupported field value: " + field); - } - } - return new After(fields); - } - -} diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/AfterSerializer.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/AfterSerializer.java deleted file mode 100644 index 3746f4cc3..000000000 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/AfterSerializer.java +++ /dev/null @@ -1,83 +0,0 @@ -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package org.apache.couchdb.nouveau.core.ser; - -import java.io.IOException; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; -import java.nio.charset.CharsetDecoder; - -import org.apache.couchdb.nouveau.api.After; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.SerializerProvider; -import com.fasterxml.jackson.databind.ser.std.StdSerializer; - -public class AfterSerializer extends StdSerializer<After> { - - public AfterSerializer() { - this(null); - } - - public AfterSerializer(Class<After> vc) { - super(vc); - } - - @Override - public void serialize(final After after, final JsonGenerator gen, final SerializerProvider provider) - throws IOException { - final CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); - // We ignore fieldDoc.score as it will be in the fields array if we're sorting for relevance. - // We ignore fieldDoc.doc as _id is always the last field and is unique. - gen.writeStartArray(); - // Preserve type information for correct deserialization of cursor. - for (final Object o : after.getFields()) { - gen.writeStartObject(); - if (o instanceof String) { - gen.writeStringField("@type", "string"); - gen.writeStringField("value", (String) o); - } else if (o instanceof byte[]) { - final byte[] bytes = (byte[]) o; - try { - final CharBuffer buf = utf8Decoder.decode(ByteBuffer.wrap(bytes)); - gen.writeStringField("@type", "string"); - gen.writeStringField("value", buf.toString()); - } catch (final CharacterCodingException e) { - gen.writeStringField("@type", "bytes"); - gen.writeFieldName("value"); - gen.writeBinary(bytes); - } - } else if (o instanceof Float) { - gen.writeStringField("@type", "float"); - gen.writeNumberField("value", (Float) o); - } else if (o instanceof Double) { - gen.writeStringField("@type", "double"); - gen.writeNumberField("value", (Double) o); - } else if (o instanceof Integer) { - gen.writeStringField("@type", "int"); - gen.writeNumberField("value", (Integer) o); - } else if (o instanceof Long) { - gen.writeStringField("@type", "long"); - gen.writeNumberField("value", (Long) o); - } else { - throw new IOException(o.getClass() + " not supported"); - } - gen.writeEndObject(); - } - gen.writeEndArray(); - } - -} diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/ByteArrayWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/ByteArrayWrapper.java new file mode 100644 index 000000000..875d0d8bb --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/ByteArrayWrapper.java @@ -0,0 +1,24 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class ByteArrayWrapper extends PrimitiveWrapper<byte[]> { + + public ByteArrayWrapper(@JsonProperty("value") byte[] value) { + super(value); + } + +} diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/DoubleWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/DoubleWrapper.java new file mode 100644 index 000000000..c9ae3b4cd --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/DoubleWrapper.java @@ -0,0 +1,24 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class DoubleWrapper extends PrimitiveWrapper<Double> { + + public DoubleWrapper(@JsonProperty("value") Double value) { + super(value); + } + +}
\ No newline at end of file diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/FloatWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/FloatWrapper.java new file mode 100644 index 000000000..490afa6d5 --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/FloatWrapper.java @@ -0,0 +1,24 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class FloatWrapper extends PrimitiveWrapper<Float> { + + public FloatWrapper(@JsonProperty("value") float value) { + super(value); + } + +}
\ No newline at end of file diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/IntWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/IntWrapper.java new file mode 100644 index 000000000..c179d0705 --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/IntWrapper.java @@ -0,0 +1,24 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class IntWrapper extends PrimitiveWrapper<Integer> { + + public IntWrapper(@JsonProperty("value") Integer value) { + super(value); + } + +}
\ No newline at end of file diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/LongWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/LongWrapper.java new file mode 100644 index 000000000..0eda4e786 --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/LongWrapper.java @@ -0,0 +1,24 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class LongWrapper extends PrimitiveWrapper<Long> { + + public LongWrapper(@JsonProperty("value") Long value) { + super(value); + } + +}
\ No newline at end of file diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/PrimitiveWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/PrimitiveWrapper.java new file mode 100644 index 000000000..89877da60 --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/PrimitiveWrapper.java @@ -0,0 +1,46 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.annotation.JsonTypeInfo.As; +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id; + +@JsonTypeInfo(use = Id.NAME, include = As.PROPERTY, property = "@type") +@JsonSubTypes({ + @JsonSubTypes.Type(value = ByteArrayWrapper.class, name = "bytes"), + @JsonSubTypes.Type(value = DoubleWrapper.class, name = "double"), + @JsonSubTypes.Type(value = FloatWrapper.class, name = "float"), + @JsonSubTypes.Type(value = IntWrapper.class, name = "int"), + @JsonSubTypes.Type(value = LongWrapper.class, name = "long"), + @JsonSubTypes.Type(value = StringWrapper.class, name = "string"), +}) +public class PrimitiveWrapper<T> { + + private T value; + + public PrimitiveWrapper(T value) { + this.value = value; + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + +} diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/StringWrapper.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/StringWrapper.java new file mode 100644 index 000000000..e53f22ca0 --- /dev/null +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/core/ser/StringWrapper.java @@ -0,0 +1,24 @@ +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package org.apache.couchdb.nouveau.core.ser; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class StringWrapper extends PrimitiveWrapper<String> { + + public StringWrapper(@JsonProperty("value") String value) { + super(value); + } + +} diff --git a/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java b/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java index eed4b0cfa..2da5f9912 100644 --- a/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java +++ b/nouveau/src/main/java/org/apache/couchdb/nouveau/lucene9/Lucene9Index.java @@ -32,7 +32,6 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; -import org.apache.couchdb.nouveau.api.After; import org.apache.couchdb.nouveau.api.DocumentDeleteRequest; import org.apache.couchdb.nouveau.api.DocumentUpdateRequest; import org.apache.couchdb.nouveau.api.DoubleField; @@ -46,6 +45,13 @@ import org.apache.couchdb.nouveau.api.StringField; import org.apache.couchdb.nouveau.api.TextField; import org.apache.couchdb.nouveau.core.IOUtils; import org.apache.couchdb.nouveau.core.Index; +import org.apache.couchdb.nouveau.core.ser.ByteArrayWrapper; +import org.apache.couchdb.nouveau.core.ser.DoubleWrapper; +import org.apache.couchdb.nouveau.core.ser.FloatWrapper; +import org.apache.couchdb.nouveau.core.ser.IntWrapper; +import org.apache.couchdb.nouveau.core.ser.LongWrapper; +import org.apache.couchdb.nouveau.core.ser.PrimitiveWrapper; +import org.apache.couchdb.nouveau.core.ser.StringWrapper; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field.Store; @@ -193,7 +199,7 @@ public class Lucene9Index extends Index { private CollectorManager<?, ? extends TopDocs> hitCollector(final SearchRequest searchRequest) { final Sort sort = toSort(searchRequest); - final After after = searchRequest.getAfter(); + final PrimitiveWrapper<?>[] after = searchRequest.getAfter(); final FieldDoc fieldDoc; if (after != null) { fieldDoc = toFieldDoc(after); @@ -250,7 +256,7 @@ public class Lucene9Index extends Index { } } - final After after = toAfter(((FieldDoc) scoreDoc)); + final PrimitiveWrapper<?>[] after = toAfter(((FieldDoc) scoreDoc)); hits.add(new SearchHit(doc.get("_id"), after, fields)); } @@ -420,24 +426,49 @@ public class Lucene9Index extends Index { return result; } - private FieldDoc toFieldDoc(final After after) { - final Object[] fields = Arrays.copyOf(after.getFields(), after.getFields().length); - for (int i = 0; i < fields.length; i++) { + private FieldDoc toFieldDoc(final Object[] after) { + final Object[] fields = new Object[after.length]; + for (int i = 0; i < after.length; i++) { + if (after[i] instanceof PrimitiveWrapper<?>) { + fields[i] = ((PrimitiveWrapper<?>)after[i]).getValue(); + } if (fields[i] instanceof byte[]) { fields[i] = new BytesRef((byte[]) fields[i]); } + if (fields[i] instanceof String) { + fields[i] = new BytesRef((String) fields[i]); + } } return new FieldDoc(0, Float.NaN, fields); } - private After toAfter(final FieldDoc fieldDoc) { - final Object[] fields = Arrays.copyOf(fieldDoc.fields, fieldDoc.fields.length); + private PrimitiveWrapper<?>[] toAfter(final FieldDoc fieldDoc) { + final CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); + final PrimitiveWrapper<?>[] fields = new PrimitiveWrapper<?>[fieldDoc.fields.length]; for (int i = 0; i < fields.length; i++) { - if (fields[i] instanceof BytesRef) { - fields[i] = toBytes((BytesRef) fields[i]); + if (fieldDoc.fields[i] instanceof String) { + fields[i] = new StringWrapper((String)fieldDoc.fields[i]); + } else if (fieldDoc.fields[i] instanceof BytesRef) { + var bytes = toBytes((BytesRef) fieldDoc.fields[i]); + try { + final CharBuffer buf = utf8Decoder.decode(ByteBuffer.wrap(bytes)); + fields[i] = new StringWrapper(buf.toString()); + } catch (final CharacterCodingException e) { + fields[i] = new ByteArrayWrapper(bytes); + } + } else if (fieldDoc.fields[i] instanceof Double) { + fields[i] = new DoubleWrapper((double) fieldDoc.fields[i]); + } else if (fieldDoc.fields[i] instanceof Integer) { + fields[i] = new IntWrapper((int) fieldDoc.fields[i]); + } else if (fieldDoc.fields[i] instanceof Long) { + fields[i] = new LongWrapper((long) fieldDoc.fields[i]); + } else if (fieldDoc.fields[i] instanceof Float) { + fields[i] = new FloatWrapper((float) fieldDoc.fields[i]); + } else { + throw new WebApplicationException(fieldDoc.fields[i].getClass() + " is not valid", Status.BAD_REQUEST); } } - return new After(fields); + return fields; } private static byte[] toBytes(final BytesRef bytesRef) { |