|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.search.internal; |
| 21 | + |
| 22 | +import org.apache.lucene.util.BytesRef; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | +import org.elasticsearch.common.io.stream.StreamOutput; |
| 25 | +import org.elasticsearch.common.io.stream.Writeable; |
| 26 | +import org.elasticsearch.common.xcontent.ToXContent; |
| 27 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 28 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 29 | +import org.elasticsearch.common.xcontent.XContentParserUtils; |
| 30 | +import org.elasticsearch.search.DocValueFormat; |
| 31 | +import org.elasticsearch.search.internal.InternalSearchHit.Fields; |
| 32 | + |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Objects; |
| 36 | + |
| 37 | +public class SearchSortValues implements ToXContent, Writeable { |
| 38 | + |
| 39 | + static final SearchSortValues EMPTY = new SearchSortValues(new Object[0]); |
| 40 | + private final Object[] sortValues; |
| 41 | + |
| 42 | + SearchSortValues(Object[] sortValues) { |
| 43 | + this.sortValues = Objects.requireNonNull(sortValues, "sort values must not be empty"); |
| 44 | + } |
| 45 | + |
| 46 | + public SearchSortValues(Object[] sortValues, DocValueFormat[] sortValueFormats) { |
| 47 | + Objects.requireNonNull(sortValues); |
| 48 | + Objects.requireNonNull(sortValueFormats); |
| 49 | + this.sortValues = Arrays.copyOf(sortValues, sortValues.length); |
| 50 | + for (int i = 0; i < sortValues.length; ++i) { |
| 51 | + if (this.sortValues[i] instanceof BytesRef) { |
| 52 | + this.sortValues[i] = sortValueFormats[i].format((BytesRef) sortValues[i]); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public SearchSortValues(StreamInput in) throws IOException { |
| 58 | + int size = in.readVInt(); |
| 59 | + if (size > 0) { |
| 60 | + sortValues = new Object[size]; |
| 61 | + for (int i = 0; i < sortValues.length; i++) { |
| 62 | + byte type = in.readByte(); |
| 63 | + if (type == 0) { |
| 64 | + sortValues[i] = null; |
| 65 | + } else if (type == 1) { |
| 66 | + sortValues[i] = in.readString(); |
| 67 | + } else if (type == 2) { |
| 68 | + sortValues[i] = in.readInt(); |
| 69 | + } else if (type == 3) { |
| 70 | + sortValues[i] = in.readLong(); |
| 71 | + } else if (type == 4) { |
| 72 | + sortValues[i] = in.readFloat(); |
| 73 | + } else if (type == 5) { |
| 74 | + sortValues[i] = in.readDouble(); |
| 75 | + } else if (type == 6) { |
| 76 | + sortValues[i] = in.readByte(); |
| 77 | + } else if (type == 7) { |
| 78 | + sortValues[i] = in.readShort(); |
| 79 | + } else if (type == 8) { |
| 80 | + sortValues[i] = in.readBoolean(); |
| 81 | + } else { |
| 82 | + throw new IOException("Can't match type [" + type + "]"); |
| 83 | + } |
| 84 | + } |
| 85 | + } else { |
| 86 | + sortValues = new Object[0]; |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void writeTo(StreamOutput out) throws IOException { |
| 92 | + out.writeVInt(sortValues.length); |
| 93 | + for (Object sortValue : sortValues) { |
| 94 | + if (sortValue == null) { |
| 95 | + out.writeByte((byte) 0); |
| 96 | + } else { |
| 97 | + Class type = sortValue.getClass(); |
| 98 | + if (type == String.class) { |
| 99 | + out.writeByte((byte) 1); |
| 100 | + out.writeString((String) sortValue); |
| 101 | + } else if (type == Integer.class) { |
| 102 | + out.writeByte((byte) 2); |
| 103 | + out.writeInt((Integer) sortValue); |
| 104 | + } else if (type == Long.class) { |
| 105 | + out.writeByte((byte) 3); |
| 106 | + out.writeLong((Long) sortValue); |
| 107 | + } else if (type == Float.class) { |
| 108 | + out.writeByte((byte) 4); |
| 109 | + out.writeFloat((Float) sortValue); |
| 110 | + } else if (type == Double.class) { |
| 111 | + out.writeByte((byte) 5); |
| 112 | + out.writeDouble((Double) sortValue); |
| 113 | + } else if (type == Byte.class) { |
| 114 | + out.writeByte((byte) 6); |
| 115 | + out.writeByte((Byte) sortValue); |
| 116 | + } else if (type == Short.class) { |
| 117 | + out.writeByte((byte) 7); |
| 118 | + out.writeShort((Short) sortValue); |
| 119 | + } else if (type == Boolean.class) { |
| 120 | + out.writeByte((byte) 8); |
| 121 | + out.writeBoolean((Boolean) sortValue); |
| 122 | + } else { |
| 123 | + throw new IOException("Can't handle sort field value of type [" + type + "]"); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 131 | + if (sortValues.length > 0) { |
| 132 | + builder.startArray(Fields.SORT); |
| 133 | + for (Object sortValue : sortValues) { |
| 134 | + builder.value(sortValue); |
| 135 | + } |
| 136 | + builder.endArray(); |
| 137 | + } |
| 138 | + return builder; |
| 139 | + } |
| 140 | + |
| 141 | + public static SearchSortValues fromXContent(XContentParser parser) throws IOException { |
| 142 | + XContentParserUtils.ensureFieldName(parser, parser.currentToken(), Fields.SORT); |
| 143 | + XContentParser.Token token = parser.nextToken(); |
| 144 | + XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_ARRAY, token, parser::getTokenLocation); |
| 145 | + return new SearchSortValues(parser.list().toArray()); |
| 146 | + } |
| 147 | + |
| 148 | + public Object[] sortValues() { |
| 149 | + return sortValues; |
| 150 | + } |
| 151 | + |
| 152 | + @Override |
| 153 | + public boolean equals(Object obj) { |
| 154 | + if (this == obj) { |
| 155 | + return true; |
| 156 | + } |
| 157 | + if (obj == null || getClass() != obj.getClass()) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + SearchSortValues other = (SearchSortValues) obj; |
| 161 | + return Arrays.equals(sortValues, other.sortValues); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int hashCode() { |
| 166 | + return Arrays.hashCode(sortValues); |
| 167 | + } |
| 168 | +} |
0 commit comments