nicer code, internalize Facets and create a clean external interface

This commit is contained in:
kimchy 2010-06-06 03:14:34 +03:00
parent 81deb833de
commit f0c56ce18e
17 changed files with 230 additions and 142 deletions

View File

@ -32,7 +32,7 @@
<option name="LOCAL" value="true" />
</RunnerSettings>
<RunnerSettings RunnerId="Profile ">
<option name="myExternalizedOptions" value="&#10;snapshots-dir=&#10;additional-options2=onexit\=snapshot&#10;" />
<option name="myExternalizedOptions" value="&#10;additional-options2=onexit\=snapshot&#10;" />
</RunnerSettings>
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Debug" />

View File

@ -23,11 +23,10 @@ import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ListenableActionFuture;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.util.collect.Lists;
import java.util.List;
import static org.elasticsearch.util.collect.Lists.*;
/**
* @author kimchy (shay.banon)
*/
@ -67,7 +66,7 @@ public abstract class AbstractListenableActionFuture<T, L> extends AdapterAction
((List) this.listeners).add(listener);
} else {
Object orig = listeners;
listeners = newArrayListWithExpectedSize(2);
listeners = Lists.newArrayListWithCapacity(2);
((List) listeners).add(orig);
((List) listeners).add(listener);
}

View File

@ -43,7 +43,6 @@ import java.io.IOException;
import java.util.*;
import static org.elasticsearch.util.MapBuilder.*;
import static org.elasticsearch.util.collect.Lists.*;
import static org.elasticsearch.util.collect.Sets.*;
/**
@ -159,7 +158,7 @@ public class MetaData implements Iterable<IndexMetaData> {
}
}
ArrayList<String> actualIndices = newArrayListWithExpectedSize(indices.length);
ArrayList<String> actualIndices = Lists.newArrayListWithCapacity(indices.length);
for (String index : indices) {
String[] actualLst = aliasAndIndexToIndexMap.get(index);
if (actualLst == null) {

View File

@ -21,6 +21,7 @@ package org.elasticsearch.cluster.routing;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.util.collect.ImmutableList;
import org.elasticsearch.util.collect.Lists;
import org.elasticsearch.util.collect.UnmodifiableIterator;
import org.elasticsearch.util.concurrent.jsr166y.ThreadLocalRandom;
import org.elasticsearch.util.io.stream.StreamInput;
@ -97,7 +98,7 @@ public class IndexShardRoutingTable implements Iterable<ShardRouting> {
}
public List<ShardRouting> backupsShards() {
List<ShardRouting> backupShards = newArrayListWithExpectedSize(2);
List<ShardRouting> backupShards = Lists.newArrayListWithCapacity(2);
for (ShardRouting shardRouting : this) {
if (!shardRouting.primary()) {
backupShards.add(shardRouting);

View File

@ -29,8 +29,6 @@ import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import static org.elasticsearch.util.collect.Lists.*;
/**
* @author kimchy (shay.banon)
*/
@ -50,7 +48,7 @@ public class ElectMasterService extends AbstractComponent {
if (sortedNodes == null) {
return new DiscoveryNode[0];
}
List<DiscoveryNode> nextPossibleMasters = newArrayListWithExpectedSize(numberOfPossibleMasters);
List<DiscoveryNode> nextPossibleMasters = Lists.newArrayListWithCapacity(numberOfPossibleMasters);
int counter = 0;
for (DiscoveryNode nextPossibleMaster : sortedNodes) {
if (++counter >= numberOfPossibleMasters) {

View File

@ -38,6 +38,7 @@ import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.util.SizeUnit;
import org.elasticsearch.util.SizeValue;
import org.elasticsearch.util.TimeValue;
import org.elasticsearch.util.collect.Lists;
import org.elasticsearch.util.inject.Inject;
import org.elasticsearch.util.io.stream.DataInputStreamInput;
import org.elasticsearch.util.io.stream.DataOutputStreamOutput;
@ -54,7 +55,6 @@ import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import static org.elasticsearch.index.translog.TranslogStreams.*;
import static org.elasticsearch.util.collect.Lists.*;
import static org.elasticsearch.util.io.FileSystemUtils.*;
import static org.elasticsearch.util.lucene.Directories.*;
@ -377,7 +377,7 @@ public class FsIndexShardGateway extends AbstractIndexShardComponent implements
File recoveryTranslogFile = new File(locationTranslog, "translog-" + recoveryTranslogId);
raf = new RandomAccessFile(recoveryTranslogFile, "r");
int numberOfOperations = raf.readInt();
ArrayList<Translog.Operation> operations = newArrayListWithExpectedSize(numberOfOperations);
ArrayList<Translog.Operation> operations = Lists.newArrayListWithCapacity(numberOfOperations);
for (int i = 0; i < numberOfOperations; i++) {
operations.add(readTranslogOperation(new DataInputStreamInput(raf)));
}

View File

@ -29,8 +29,8 @@ import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.dfs.AggregatedDfs;
import org.elasticsearch.search.dfs.DfsSearchResult;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.Facets;
import org.elasticsearch.search.facets.InternalFacet;
import org.elasticsearch.search.facets.internal.InternalFacet;
import org.elasticsearch.search.facets.internal.InternalFacets;
import org.elasticsearch.search.fetch.FetchSearchResult;
import org.elasticsearch.search.fetch.FetchSearchResultProvider;
import org.elasticsearch.search.internal.InternalSearchHit;
@ -148,7 +148,7 @@ public class SearchPhaseController {
public InternalSearchResponse merge(ShardDoc[] sortedDocs, Map<SearchShardTarget, ? extends QuerySearchResultProvider> queryResults, Map<SearchShardTarget, ? extends FetchSearchResultProvider> fetchResults) {
// merge facets
Facets facets = null;
InternalFacets facets = null;
if (!queryResults.isEmpty()) {
// we rely on the fact that the order of facets is the same on all query results
QuerySearchResult queryResult = queryResults.values().iterator().next().queryResult();
@ -163,7 +163,7 @@ public class SearchPhaseController {
for (Facet facet : queryResult.facets().facets()) {
mergedFacets.add(((InternalFacet) facet).aggregate(allFacets));
}
facets = new Facets(mergedFacets);
facets = new InternalFacets(mergedFacets);
}
}

View File

@ -20,6 +20,9 @@
package org.elasticsearch.search.facets;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.search.facets.query.QueryFacet;
import org.elasticsearch.search.facets.statistical.StatisticalFacet;
import org.elasticsearch.search.facets.terms.TermsFacet;
/**
* A search facet.
@ -28,21 +31,50 @@ import org.elasticsearch.ElasticSearchIllegalArgumentException;
*/
public interface Facet {
/**
* The type of the facet.
*/
enum Type {
TERMS(0),
QUERY(1),
STATISTICAL(2);
/**
* Terms facet type, matching {@link TermsFacet}.
*/
TERMS(0, TermsFacet.class),
/**
* Query facet type, matching {@link QueryFacet}.
*/
QUERY(1, QueryFacet.class),
/**
* Statistical facet type, matching {@link StatisticalFacet}.
*/
STATISTICAL(2, StatisticalFacet.class);
int id;
private int id;
Type(int id) {
private Class<? extends Facet> type;
Type(int id, Class<? extends Facet> type) {
this.id = id;
this.type = type;
}
public int id() {
return id;
}
/**
* The facet class type.
*/
public Class<? extends Facet> type() {
return this.type;
}
/**
* The facet class type.
*/
public Class<? extends Facet> getType() {
return type();
}
public static Type fromId(int id) {
if (id == 0) {
return TERMS;

View File

@ -19,138 +19,38 @@
package org.elasticsearch.search.facets;
import org.elasticsearch.search.facets.query.InternalQueryFacet;
import org.elasticsearch.search.facets.statistical.InternalStatisticalFacet;
import org.elasticsearch.search.facets.terms.InternalTermsFacet;
import org.elasticsearch.util.collect.ImmutableList;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.util.collect.Lists.*;
import static org.elasticsearch.util.collect.Maps.*;
/**
* Facets of search action.
*
* @author kimchy (shay.banon)
*/
public class Facets implements Streamable, ToXContent, Iterable<Facet> {
private final List<Facet> EMPTY = ImmutableList.of();
private List<Facet> facets = EMPTY;
private Map<String, Facet> facetsAsMap;
private Facets() {
}
/**
* Constructs a new facets.
*/
public Facets(List<Facet> facets) {
this.facets = facets;
}
/**
* Iterates over the {@link Facet}s.
*/
@Override public Iterator<Facet> iterator() {
return facets.iterator();
}
public interface Facets extends Iterable<Facet> {
/**
* The list of {@link Facet}s.
*/
public List<Facet> facets() {
return facets;
}
List<Facet> facets();
/**
* Returns the {@link Facet}s keyed by map.
* Returns the {@link Facet}s keyed by facet name.
*/
public Map<String, Facet> getFacets() {
return facetsAsMap();
}
Map<String, Facet> getFacets();
/**
* Returns the {@link Facet}s keyed by map.
* Returns the {@link Facet}s keyed by facet name.
*/
public Map<String, Facet> facetsAsMap() {
if (facetsAsMap != null) {
return facetsAsMap;
}
Map<String, Facet> facetsAsMap = newHashMap();
for (Facet facet : facets) {
facetsAsMap.put(facet.name(), facet);
}
this.facetsAsMap = facetsAsMap;
return facetsAsMap;
}
Map<String, Facet> facetsAsMap();
/**
* Returns the facet by name already casted to the specified type.
*/
public <T extends Facet> T facet(Class<T> facetType, String name) {
return facetType.cast(facet(name));
}
<T extends Facet> T facet(Class<T> facetType, String name);
/**
* A facet of the specified name.
*/
public Facet facet(String name) {
return facetsAsMap().get(name);
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("facets");
for (Facet facet : facets) {
((InternalFacet) facet).toXContent(builder, params);
}
builder.endObject();
}
public static Facets readFacets(StreamInput in) throws IOException {
Facets result = new Facets();
result.readFrom(in);
return result;
}
@Override public void readFrom(StreamInput in) throws IOException {
int size = in.readVInt();
if (size == 0) {
facets = EMPTY;
} else {
facets = newArrayListWithCapacity(size);
for (int i = 0; i < size; i++) {
int id = in.readVInt();
if (id == Facet.Type.TERMS.id()) {
facets.add(InternalTermsFacet.readTermsFacet(in));
} else if (id == Facet.Type.QUERY.id()) {
facets.add(InternalQueryFacet.readCountFacet(in));
} else if (id == Facet.Type.STATISTICAL.id()) {
facets.add(InternalStatisticalFacet.readStatisticalFacet(in));
} else {
throw new IOException("Can't handle facet type with id [" + id + "]");
}
}
}
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(facets.size());
for (Facet facet : facets) {
out.writeVInt(facet.type().id());
((InternalFacet) facet).writeTo(out);
}
}
Facet facet(String name);
}

View File

@ -25,6 +25,7 @@ import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.SearchPhase;
import org.elasticsearch.search.facets.collector.FacetCollector;
import org.elasticsearch.search.facets.internal.InternalFacets;
import org.elasticsearch.search.internal.SearchContext;
import org.elasticsearch.search.query.QueryPhaseExecutionException;
import org.elasticsearch.util.collect.ImmutableMap;
@ -98,6 +99,6 @@ public class FacetsPhase implements SearchPhase {
facets.add(facetCollector.facet());
}
}
context.queryResult().facets(new Facets(facets));
context.queryResult().facets(new InternalFacets(facets));
}
}

View File

@ -17,8 +17,9 @@
* under the License.
*/
package org.elasticsearch.search.facets;
package org.elasticsearch.search.facets.internal;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;

View File

@ -0,0 +1,157 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search 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.facets.internal;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.Facets;
import org.elasticsearch.search.facets.query.InternalQueryFacet;
import org.elasticsearch.search.facets.statistical.InternalStatisticalFacet;
import org.elasticsearch.search.facets.terms.InternalTermsFacet;
import org.elasticsearch.util.collect.ImmutableList;
import org.elasticsearch.util.collect.ImmutableMap;
import org.elasticsearch.util.collect.Lists;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
import org.elasticsearch.util.xcontent.ToXContent;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import static org.elasticsearch.util.collect.Maps.*;
/**
* @author kimchy (shay.banon)
*/
public class InternalFacets implements Facets, Streamable, ToXContent, Iterable<Facet> {
private List<Facet> facets = ImmutableList.of();
private Map<String, Facet> facetsAsMap;
private InternalFacets() {
}
/**
* Constructs a new facets.
*/
public InternalFacets(List<Facet> facets) {
this.facets = facets;
}
/**
* Iterates over the {@link Facet}s.
*/
@Override public Iterator<Facet> iterator() {
return facets.iterator();
}
/**
* The list of {@link Facet}s.
*/
public List<Facet> facets() {
return facets;
}
/**
* Returns the {@link Facet}s keyed by map.
*/
public Map<String, Facet> getFacets() {
return facetsAsMap();
}
/**
* Returns the {@link Facet}s keyed by map.
*/
public Map<String, Facet> facetsAsMap() {
if (facetsAsMap != null) {
return facetsAsMap;
}
Map<String, Facet> facetsAsMap = newHashMap();
for (Facet facet : facets) {
facetsAsMap.put(facet.name(), facet);
}
this.facetsAsMap = facetsAsMap;
return facetsAsMap;
}
/**
* Returns the facet by name already casted to the specified type.
*/
public <T extends Facet> T facet(Class<T> facetType, String name) {
return facetType.cast(facet(name));
}
/**
* A facet of the specified name.
*/
public Facet facet(String name) {
return facetsAsMap().get(name);
}
@Override public void toXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject("facets");
for (Facet facet : facets) {
((InternalFacet) facet).toXContent(builder, params);
}
builder.endObject();
}
public static InternalFacets readFacets(StreamInput in) throws IOException {
InternalFacets result = new InternalFacets();
result.readFrom(in);
return result;
}
@Override public void readFrom(StreamInput in) throws IOException {
int size = in.readVInt();
if (size == 0) {
facets = ImmutableList.of();
facetsAsMap = ImmutableMap.of();
} else {
facets = Lists.newArrayListWithCapacity(size);
for (int i = 0; i < size; i++) {
int id = in.readVInt();
if (id == Facet.Type.TERMS.id()) {
facets.add(InternalTermsFacet.readTermsFacet(in));
} else if (id == Facet.Type.QUERY.id()) {
facets.add(InternalQueryFacet.readCountFacet(in));
} else if (id == Facet.Type.STATISTICAL.id()) {
facets.add(InternalStatisticalFacet.readStatisticalFacet(in));
} else {
throw new IOException("Can't handle facet type with id [" + id + "]");
}
}
}
}
@Override public void writeTo(StreamOutput out) throws IOException {
out.writeVInt(facets.size());
for (Facet facet : facets) {
out.writeVInt(facet.type().id());
((InternalFacet) facet).writeTo(out);
}
}
}

View File

@ -20,7 +20,7 @@
package org.elasticsearch.search.facets.query;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.InternalFacet;
import org.elasticsearch.search.facets.internal.InternalFacet;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;

View File

@ -20,7 +20,7 @@
package org.elasticsearch.search.facets.statistical;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.InternalFacet;
import org.elasticsearch.search.facets.internal.InternalFacet;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.xcontent.builder.XContentBuilder;

View File

@ -20,7 +20,7 @@
package org.elasticsearch.search.facets.terms;
import org.elasticsearch.search.facets.Facet;
import org.elasticsearch.search.facets.InternalFacet;
import org.elasticsearch.search.facets.internal.InternalFacet;
import org.elasticsearch.util.BoundedTreeSet;
import org.elasticsearch.util.ThreadLocals;
import org.elasticsearch.util.collect.ImmutableList;

View File

@ -21,6 +21,7 @@ package org.elasticsearch.search.internal;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.facets.Facets;
import org.elasticsearch.search.facets.internal.InternalFacets;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
@ -29,7 +30,6 @@ import org.elasticsearch.util.xcontent.builder.XContentBuilder;
import java.io.IOException;
import static org.elasticsearch.search.facets.Facets.*;
import static org.elasticsearch.search.internal.InternalSearchHits.*;
/**
@ -39,12 +39,12 @@ public class InternalSearchResponse implements Streamable, ToXContent {
private InternalSearchHits hits;
private Facets facets;
private InternalFacets facets;
private InternalSearchResponse() {
}
public InternalSearchResponse(InternalSearchHits hits, Facets facets) {
public InternalSearchResponse(InternalSearchHits hits, InternalFacets facets) {
this.hits = hits;
this.facets = facets;
}
@ -73,7 +73,7 @@ public class InternalSearchResponse implements Streamable, ToXContent {
@Override public void readFrom(StreamInput in) throws IOException {
hits = readSearchHits(in);
if (in.readBoolean()) {
facets = readFacets(in);
facets = InternalFacets.readFacets(in);
}
}

View File

@ -22,6 +22,7 @@ package org.elasticsearch.search.query;
import org.apache.lucene.search.TopDocs;
import org.elasticsearch.search.SearchShardTarget;
import org.elasticsearch.search.facets.Facets;
import org.elasticsearch.search.facets.internal.InternalFacets;
import org.elasticsearch.util.io.stream.StreamInput;
import org.elasticsearch.util.io.stream.StreamOutput;
import org.elasticsearch.util.io.stream.Streamable;
@ -29,7 +30,6 @@ import org.elasticsearch.util.io.stream.Streamable;
import java.io.IOException;
import static org.elasticsearch.search.SearchShardTarget.*;
import static org.elasticsearch.search.facets.Facets.*;
import static org.elasticsearch.util.lucene.Lucene.*;
/**
@ -47,7 +47,7 @@ public class QuerySearchResult implements Streamable, QuerySearchResultProvider
private TopDocs topDocs;
private Facets facets;
private InternalFacets facets;
private boolean searchTimedOut;
@ -96,7 +96,7 @@ public class QuerySearchResult implements Streamable, QuerySearchResultProvider
return facets;
}
public void facets(Facets facets) {
public void facets(InternalFacets facets) {
this.facets = facets;
}
@ -131,7 +131,7 @@ public class QuerySearchResult implements Streamable, QuerySearchResultProvider
size = in.readVInt();
topDocs = readTopDocs(in);
if (in.readBoolean()) {
facets = readFacets(in);
facets = InternalFacets.readFacets(in);
}
searchTimedOut = in.readBoolean();
}