Skip to content

Commit 8c52244

Browse files
committed
Add the ability to store objects with a ScrollContext (#24777)
This commit adds the ability to store and retrieve data that should be associated with a ScrollContext. Additionally the ScrollContext was made final as we should only have a single implementation of this concept.
1 parent b6acb5c commit 8c52244

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

core/src/main/java/org/elasticsearch/search/internal/ScrollContext.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,35 @@
2222
import org.apache.lucene.search.ScoreDoc;
2323
import org.elasticsearch.search.Scroll;
2424

25+
import java.util.HashMap;
26+
import java.util.Map;
27+
2528
/** Wrapper around information that needs to stay around when scrolling. */
26-
public class ScrollContext {
29+
public final class ScrollContext {
30+
31+
private Map<String, Object> context = null;
2732

2833
public int totalHits = -1;
2934
public float maxScore;
3035
public ScoreDoc lastEmittedDoc;
3136
public Scroll scroll;
37+
38+
/**
39+
* Returns the object or <code>null</code> if the given key does not have a
40+
* value in the context
41+
*/
42+
@SuppressWarnings("unchecked") // (T)object
43+
public <T> T getFromContext(String key) {
44+
return context != null ? (T) context.get(key) : null;
45+
}
46+
47+
/**
48+
* Puts the object into the context
49+
*/
50+
public void putInContext(String key, Object value) {
51+
if (context == null) {
52+
context = new HashMap<>();
53+
}
54+
context.put(key, value);
55+
}
3256
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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.elasticsearch.test.ESTestCase;
23+
24+
public class ScrollContextTests extends ESTestCase {
25+
26+
public void testStoringObjectsInScrollContext() {
27+
final ScrollContext scrollContext = new ScrollContext();
28+
final String key = randomAlphaOfLengthBetween(1, 16);
29+
assertNull(scrollContext.getFromContext(key));
30+
31+
final String value = randomAlphaOfLength(6);
32+
scrollContext.putInContext(key, value);
33+
34+
assertEquals(value, scrollContext.getFromContext(key));
35+
}
36+
}

0 commit comments

Comments
 (0)