-
Notifications
You must be signed in to change notification settings - Fork 24.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix hashCode values of aggregations' BytesValues.
This commit removes FilterBytesValues which is very trappy as the default implementation forwards all method calls to the delegate. So if you do any non-trivial modification to the terms or to the order of the terms, you need to remember to override currentValueHash, copyShared, and this is very error-prone. FieldDataSource.WithScript.BytesValues and ScriptBytesValues now return correct hash codes, future bugs here would be catched by the new assertion in SortedUniqueBytesValues. This bug was causing performance issues with scripts as all terms were assumed to have the same hash code. Close #5004
- Loading branch information
Showing
4 changed files
with
152 additions
and
77 deletions.
There are no files selected for viewing
61 changes: 0 additions & 61 deletions
61
src/main/java/org/elasticsearch/index/fielddata/FilterBytesValues.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
src/test/java/org/elasticsearch/search/aggregations/support/FieldDataSourceTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you 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.elasticsearch.search.aggregations.support; | ||
|
||
import org.apache.lucene.index.AtomicReaderContext; | ||
import org.apache.lucene.search.Scorer; | ||
import org.apache.lucene.util.BytesRef; | ||
import org.elasticsearch.index.fielddata.BytesValues; | ||
import org.elasticsearch.script.SearchScript; | ||
import org.elasticsearch.test.ElasticsearchTestCase; | ||
import org.junit.Test; | ||
|
||
import java.util.Map; | ||
|
||
public class FieldDataSourceTests extends ElasticsearchTestCase { | ||
|
||
private static BytesValues randomBytesValues() { | ||
final boolean multiValued = randomBoolean(); | ||
return new BytesValues(multiValued) { | ||
@Override | ||
public int setDocument(int docId) { | ||
return randomInt(multiValued ? 10 : 1); | ||
} | ||
@Override | ||
public BytesRef nextValue() { | ||
scratch.copyChars(randomAsciiOfLength(10)); | ||
return scratch; | ||
} | ||
|
||
}; | ||
} | ||
|
||
private static SearchScript randomScript() { | ||
return new SearchScript() { | ||
|
||
@Override | ||
public void setNextVar(String name, Object value) { | ||
} | ||
|
||
@Override | ||
public Object run() { | ||
return randomAsciiOfLength(5); | ||
} | ||
|
||
@Override | ||
public Object unwrap(Object value) { | ||
return value; | ||
} | ||
|
||
@Override | ||
public void setNextReader(AtomicReaderContext reader) { | ||
} | ||
|
||
@Override | ||
public void setScorer(Scorer scorer) { | ||
} | ||
|
||
@Override | ||
public void setNextDocId(int doc) { | ||
} | ||
|
||
@Override | ||
public void setNextSource(Map<String, Object> source) { | ||
} | ||
|
||
@Override | ||
public void setNextScore(float score) { | ||
} | ||
|
||
@Override | ||
public float runAsFloat() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public long runAsLong() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public double runAsDouble() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
}; | ||
} | ||
|
||
private static void assertConsistent(BytesValues values) { | ||
for (int i = 0; i < 10; ++i) { | ||
final int valueCount = values.setDocument(i); | ||
for (int j = 0; j < valueCount; ++j) { | ||
final BytesRef term = values.nextValue(); | ||
assertEquals(term.hashCode(), values.currentValueHash()); | ||
assertTrue(term.bytesEquals(values.copyShared())); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void bytesValuesWithScript() { | ||
final BytesValues values = randomBytesValues(); | ||
FieldDataSource source = new FieldDataSource.Bytes() { | ||
|
||
@Override | ||
public BytesValues bytesValues() { | ||
return values; | ||
} | ||
|
||
@Override | ||
public MetaData metaData() { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
}; | ||
SearchScript script = randomScript(); | ||
assertConsistent(new FieldDataSource.WithScript.BytesValues(source, script)); | ||
} | ||
|
||
@Test | ||
public void sortedUniqueBytesValues() { | ||
assertConsistent(new FieldDataSource.Bytes.SortedAndUnique.SortedUniqueBytesValues(randomBytesValues())); | ||
} | ||
|
||
} |