|
| 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 | +package org.elasticsearch.common.bytes; |
| 20 | + |
| 21 | +import org.apache.lucene.util.BytesRef; |
| 22 | +import org.apache.lucene.util.BytesRefIterator; |
| 23 | +import org.elasticsearch.common.io.stream.StreamInput; |
| 24 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 25 | + |
| 26 | +import java.io.EOFException; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.io.OutputStream; |
| 30 | +import java.util.function.ToIntBiFunction; |
| 31 | + |
| 32 | +public abstract class AbstractBytesReference implements BytesReference { |
| 33 | + |
| 34 | + private Integer hash = null; // we cache the hash of this reference since it can be quite costly to re-calculated it |
| 35 | + |
| 36 | + @Override |
| 37 | + public int getInt(int index) { |
| 38 | + return (get(index) & 0xFF) << 24 | (get(index + 1) & 0xFF) << 16 | (get(index + 2) & 0xFF) << 8 | get(index + 3) & 0xFF; |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + public int indexOf(byte marker, int from) { |
| 43 | + final int to = length(); |
| 44 | + for (int i = from; i < to; i++) { |
| 45 | + if (get(i) == marker) { |
| 46 | + return i; |
| 47 | + } |
| 48 | + } |
| 49 | + return -1; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public StreamInput streamInput() throws IOException { |
| 54 | + return new MarkSupportingStreamInputWrapper(this); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void writeTo(OutputStream os) throws IOException { |
| 59 | + final BytesRefIterator iterator = iterator(); |
| 60 | + BytesRef ref; |
| 61 | + while ((ref = iterator.next()) != null) { |
| 62 | + os.write(ref.bytes, ref.offset, ref.length); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String utf8ToString() { |
| 68 | + return toBytesRef().utf8ToString(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public BytesRefIterator iterator() { |
| 73 | + return new BytesRefIterator() { |
| 74 | + BytesRef ref = length() == 0 ? null : toBytesRef(); |
| 75 | + @Override |
| 76 | + public BytesRef next() throws IOException { |
| 77 | + BytesRef r = ref; |
| 78 | + ref = null; // only return it once... |
| 79 | + return r; |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public boolean equals(Object other) { |
| 86 | + if (this == other) { |
| 87 | + return true; |
| 88 | + } |
| 89 | + if (other instanceof BytesReference) { |
| 90 | + final BytesReference otherRef = (BytesReference) other; |
| 91 | + if (length() != otherRef.length()) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + return compareIterators(this, otherRef, (a, b) -> |
| 95 | + a.bytesEquals(b) ? 0 : 1 // this is a call to BytesRef#bytesEquals - this method is the hot one in the comparison |
| 96 | + ) == 0; |
| 97 | + } |
| 98 | + return false; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public int hashCode() { |
| 103 | + if (hash == null) { |
| 104 | + final BytesRefIterator iterator = iterator(); |
| 105 | + BytesRef ref; |
| 106 | + int result = 1; |
| 107 | + try { |
| 108 | + while ((ref = iterator.next()) != null) { |
| 109 | + for (int i = 0; i < ref.length; i++) { |
| 110 | + result = 31 * result + ref.bytes[ref.offset + i]; |
| 111 | + } |
| 112 | + } |
| 113 | + } catch (IOException ex) { |
| 114 | + throw new AssertionError("wont happen", ex); |
| 115 | + } |
| 116 | + return hash = result; |
| 117 | + } else { |
| 118 | + return hash.intValue(); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public int compareTo(final BytesReference other) { |
| 124 | + return compareIterators(this, other, BytesRef::compareTo); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Compares the two references using the given int function. |
| 129 | + */ |
| 130 | + private static int compareIterators(final BytesReference a, final BytesReference b, final ToIntBiFunction<BytesRef, BytesRef> f) { |
| 131 | + try { |
| 132 | + // we use the iterators since it's a 0-copy comparison where possible! |
| 133 | + final long lengthToCompare = Math.min(a.length(), b.length()); |
| 134 | + final BytesRefIterator aIter = a.iterator(); |
| 135 | + final BytesRefIterator bIter = b.iterator(); |
| 136 | + BytesRef aRef = aIter.next(); |
| 137 | + BytesRef bRef = bIter.next(); |
| 138 | + if (aRef != null && bRef != null) { // do we have any data? |
| 139 | + aRef = aRef.clone(); // we clone since we modify the offsets and length in the iteration below |
| 140 | + bRef = bRef.clone(); |
| 141 | + if (aRef.length == a.length() && bRef.length == b.length()) { // is it only one array slice we are comparing? |
| 142 | + return f.applyAsInt(aRef, bRef); |
| 143 | + } else { |
| 144 | + for (int i = 0; i < lengthToCompare;) { |
| 145 | + if (aRef.length == 0) { |
| 146 | + aRef = aIter.next().clone(); // must be non null otherwise we have a bug |
| 147 | + } |
| 148 | + if (bRef.length == 0) { |
| 149 | + bRef = bIter.next().clone(); // must be non null otherwise we have a bug |
| 150 | + } |
| 151 | + final int aLength = aRef.length; |
| 152 | + final int bLength = bRef.length; |
| 153 | + final int length = Math.min(aLength, bLength); // shrink to the same length and use the fast compare in lucene |
| 154 | + aRef.length = bRef.length = length; |
| 155 | + // now we move to the fast comparison - this is the hot part of the loop |
| 156 | + int diff = f.applyAsInt(aRef, bRef); |
| 157 | + aRef.length = aLength; |
| 158 | + bRef.length = bLength; |
| 159 | + |
| 160 | + if (diff != 0) { |
| 161 | + return diff; |
| 162 | + } |
| 163 | + advance(aRef, length); |
| 164 | + advance(bRef, length); |
| 165 | + i += length; |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + // One is a prefix of the other, or, they are equal: |
| 170 | + return a.length() - b.length(); |
| 171 | + } catch (IOException ex) { |
| 172 | + throw new AssertionError("can not happen", ex); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static void advance(final BytesRef ref, final int length) { |
| 177 | + assert ref.length >= length : " ref.length: " + ref.length + " length: " + length; |
| 178 | + assert ref.offset+length < ref.bytes.length || (ref.offset+length == ref.bytes.length && ref.length-length == 0) |
| 179 | + : "offset: " + ref.offset + " ref.bytes.length: " + ref.bytes.length + " length: " + length + " ref.length: " + ref.length; |
| 180 | + ref.length -= length; |
| 181 | + ref.offset += length; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Instead of adding the complexity of {@link InputStream#reset()} etc to the actual impl |
| 186 | + * this wrapper builds it on top of the BytesReferenceStreamInput which is much simpler |
| 187 | + * that way. |
| 188 | + */ |
| 189 | + private static final class MarkSupportingStreamInputWrapper extends StreamInput { |
| 190 | + // can't use FilterStreamInput it needs to reset the delegate |
| 191 | + private final BytesReference reference; |
| 192 | + private BytesReferenceStreamInput input; |
| 193 | + private int mark = 0; |
| 194 | + |
| 195 | + private MarkSupportingStreamInputWrapper(BytesReference reference) throws IOException { |
| 196 | + this.reference = reference; |
| 197 | + this.input = new BytesReferenceStreamInput(reference.iterator(), reference.length()); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public byte readByte() throws IOException { |
| 202 | + return input.readByte(); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public void readBytes(byte[] b, int offset, int len) throws IOException { |
| 207 | + input.readBytes(b, offset, len); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public int read(byte[] b, int off, int len) throws IOException { |
| 212 | + return input.read(b, off, len); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public void close() throws IOException { |
| 217 | + input.close(); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public int read() throws IOException { |
| 222 | + return input.read(); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public int available() throws IOException { |
| 227 | + return input.available(); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + protected void ensureCanReadBytes(int length) throws EOFException { |
| 232 | + input.ensureCanReadBytes(length); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public void reset() throws IOException { |
| 237 | + input = new BytesReferenceStreamInput(reference.iterator(), reference.length()); |
| 238 | + input.skip(mark); |
| 239 | + } |
| 240 | + |
| 241 | + @Override |
| 242 | + public boolean markSupported() { |
| 243 | + return true; |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + public void mark(int readLimit) { |
| 248 | + // readLimit is optional it only guarantees that the stream remembers data upto this limit but it can remember more |
| 249 | + // which we do in our case |
| 250 | + this.mark = input.getOffset(); |
| 251 | + } |
| 252 | + |
| 253 | + @Override |
| 254 | + public long skip(long n) throws IOException { |
| 255 | + return input.skip(n); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + @Override |
| 260 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 261 | + BytesRef bytes = toBytesRef(); |
| 262 | + return builder.value(bytes.bytes, bytes.offset, bytes.length); |
| 263 | + } |
| 264 | +} |
0 commit comments