-
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.
Eagerly initialize dates when we move to the next document
- Loading branch information
Showing
2 changed files
with
159 additions
and
45 deletions.
There are no files selected for viewing
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
76 changes: 76 additions & 0 deletions
76
core/src/test/java/org/elasticsearch/index/fielddata/ScriptDocValuesDatesTests.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,76 @@ | ||
/* | ||
* 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.index.fielddata; | ||
|
||
import org.apache.lucene.index.SortedNumericDocValues; | ||
import org.elasticsearch.index.fielddata.ScriptDocValues.Dates; | ||
import org.elasticsearch.test.ESTestCase; | ||
import org.joda.time.DateTime; | ||
import org.joda.time.DateTimeZone; | ||
import org.joda.time.ReadableDateTime; | ||
|
||
public class ScriptDocValuesDatesTests extends ESTestCase { | ||
public void test() { | ||
long[][] values = new long[between(3, 10)][]; | ||
ReadableDateTime[][] expectedDates = new ReadableDateTime[values.length][]; | ||
for (int d = 0; d < values.length; d++) { | ||
values[d] = new long[randomBoolean() ? randomBoolean() ? 0 : 1 : between(2, 100)]; | ||
expectedDates[d] = new ReadableDateTime[values[d].length]; | ||
for (int i = 0; i < values[d].length; i++) { | ||
expectedDates[d][i] = new DateTime(randomNonNegativeLong(), DateTimeZone.UTC); | ||
values[d][i] = expectedDates[d][i].getMillis(); | ||
} | ||
} | ||
Dates dates = wrap(values); | ||
|
||
for (int round = 0; round < 10; round++) { | ||
int d = between(0, values.length - 1); | ||
dates.setNextDocId(d); | ||
assertEquals(expectedDates[d].length > 0 ? expectedDates[d][0] : new DateTime(0, DateTimeZone.UTC), dates.getValue()); | ||
|
||
assertEquals(values[d].length, dates.size()); | ||
for (int i = 0; i < values[d].length; i++) { | ||
assertEquals(expectedDates[d][i], dates.get(i)); | ||
} | ||
|
||
Exception e = expectThrows(UnsupportedOperationException.class, () -> dates.add(new DateTime())); | ||
assertEquals("doc values are unmodifiable", e.getMessage()); | ||
} | ||
} | ||
|
||
private Dates wrap(long[][] values) { | ||
return new Dates(new SortedNumericDocValues() { | ||
long[] current; | ||
|
||
@Override | ||
public void setDocument(int doc) { | ||
current = values[doc]; | ||
} | ||
@Override | ||
public int count() { | ||
return current.length; | ||
} | ||
@Override | ||
public long valueAt(int index) { | ||
return current[index]; | ||
} | ||
}); | ||
} | ||
} |