Skip to content

Commit

Permalink
Added SingleOrderedMap.of(...) for immutable empty or single pair (#2932
Browse files Browse the repository at this point in the history
)

SimpleFacets:  use this, avoiding needless new NamedList creation
  • Loading branch information
renatoh authored Jan 11, 2025
1 parent 440e70d commit 6c02da2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
12 changes: 6 additions & 6 deletions solr/core/src/java/org/apache/solr/request/SimpleFacets.java
Original file line number Diff line number Diff line change
Expand Up @@ -871,12 +871,12 @@ public void execute(Runnable r) {
* @see #getFieldMissingCount
* @see #getFacetTermEnumCounts
*/
public NamedList<Object> getFacetFieldCounts() throws IOException, SyntaxError {
public SimpleOrderedMap<Object> getFacetFieldCounts() throws IOException, SyntaxError {

NamedList<Object> res = new SimpleOrderedMap<>();
String[] facetFs = global.getParams(FacetParams.FACET_FIELD);

if (null == facetFs) {
return res;
return SimpleOrderedMap.of();
}

// Passing a negative number for FACET_THREADS implies an unlimited number of threads is
Expand All @@ -891,6 +891,7 @@ public NamedList<Object> getFacetFieldCounts() throws IOException, SyntaxError {
fdebugParent.putInfoItem("maxThreads", maxThreads);
}

SimpleOrderedMap<Object> res;
try {
// Loop over fields; submit to executor, keeping the future
for (String f : facetFs) {
Expand All @@ -914,10 +915,8 @@ public NamedList<Object> getFacetFieldCounts() throws IOException, SyntaxError {
result.add(key, getTermCounts(facetValue, parsed));
}
return result;
} catch (SolrException se) {
} catch (SolrException | ExitableDirectoryReader.ExitingReaderException se) {
throw se;
} catch (ExitableDirectoryReader.ExitingReaderException timeout) {
throw timeout;
} catch (Exception e) {
throw new SolrException(
ErrorCode.SERVER_ERROR, "Exception during facet.field: " + facetValue, e);
Expand All @@ -932,6 +931,7 @@ public NamedList<Object> getFacetFieldCounts() throws IOException, SyntaxError {
futures.add(runnableFuture);
} // facetFs loop

res = new SimpleOrderedMap<>();
// Loop over futures to get the values. The order is the same as facetFs but shouldn't matter.
for (Future<NamedList<?>> future : futures) {
res.addAll(future.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
* serialized. It aims to minimize overhead and to be efficient at adding new elements.
*/
public class SimpleOrderedMap<T> extends NamedList<T> {

private static final SimpleOrderedMap<Object> EMPTY = new SimpleOrderedMap<>(List.of());
/** Creates an empty instance */
public SimpleOrderedMap() {
super();
Expand Down Expand Up @@ -67,4 +69,23 @@ public SimpleOrderedMap<T> clone() {
newList.addAll(nvPairs);
return new SimpleOrderedMap<>(newList);
}

/**
* Returns a shared, empty, and immutable instance of SimpleOrderedMap.
* @return Empty SimpleOrderedMap (immutable)
*/
public static SimpleOrderedMap<Object> of() {
return EMPTY;
}

/**
* Returns an immutable instance of SimpleOrderedMap with a single key-value pair.
* @return SimpleOrderedMap containing one key-value pair
*/

public static <T> SimpleOrderedMap<T> of(String name, T val)
{
return new SimpleOrderedMap<>(List.of(name, val));
}

}

0 comments on commit 6c02da2

Please sign in to comment.