improved close logic on index query parser

This commit is contained in:
kimchy 2011-04-24 23:42:59 +03:00
parent 013c4e9fa4
commit 23c17d7101
4 changed files with 23 additions and 10 deletions

View File

@ -30,6 +30,8 @@ public interface IndexQueryParser extends IndexComponent {
String name();
void close();
ParsedQuery parse(byte[] source) throws ElasticSearchException;
ParsedQuery parse(byte[] source, int offset, int length) throws ElasticSearchException;

View File

@ -71,6 +71,12 @@ public class IndexQueryParserService extends AbstractIndexComponent {
defaultIndexQueryParser = indexQueryParser(Defaults.DEFAULT);
}
public void close() {
for (IndexQueryParser indexQueryParser : indexQueryParsers.values()) {
indexQueryParser.close();
}
}
public IndexQueryParser indexQueryParser(String name) {
return indexQueryParsers.get(name);
}

View File

@ -28,7 +28,6 @@ import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.io.FastByteArrayOutputStream;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.thread.ThreadLocals;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.AbstractIndexComponent;
@ -62,9 +61,9 @@ public class XContentIndexQueryParser extends AbstractIndexComponent implements
public static final String FILTER_PREFIX = "index.queryparser.filter";
}
private ThreadLocal<ThreadLocals.CleanableValue<QueryParseContext>> cache = new ThreadLocal<ThreadLocals.CleanableValue<QueryParseContext>>() {
@Override protected ThreadLocals.CleanableValue<QueryParseContext> initialValue() {
return new ThreadLocals.CleanableValue<QueryParseContext>(new QueryParseContext(index, XContentIndexQueryParser.this));
private ThreadLocal<QueryParseContext> cache = new ThreadLocal<QueryParseContext>() {
@Override protected QueryParseContext initialValue() {
return new QueryParseContext(index, XContentIndexQueryParser.this);
}
};
@ -144,6 +143,10 @@ public class XContentIndexQueryParser extends AbstractIndexComponent implements
this.filterParsers = ImmutableMap.copyOf(filterParsersMap);
}
@Override public void close() {
cache.remove();
}
@Override public String name() {
return this.name;
}
@ -161,7 +164,7 @@ public class XContentIndexQueryParser extends AbstractIndexComponent implements
try {
FastByteArrayOutputStream unsafeBytes = queryBuilder.buildAsUnsafeBytes();
parser = XContentFactory.xContent(unsafeBytes.unsafeByteArray(), 0, unsafeBytes.size()).createParser(unsafeBytes.unsafeByteArray(), 0, unsafeBytes.size());
return parse(cache.get().get(), parser);
return parse(cache.get(), parser);
} catch (QueryParsingException e) {
throw e;
} catch (Exception e) {
@ -181,7 +184,7 @@ public class XContentIndexQueryParser extends AbstractIndexComponent implements
XContentParser parser = null;
try {
parser = XContentFactory.xContent(source, offset, length).createParser(source, offset, length);
return parse(cache.get().get(), parser);
return parse(cache.get(), parser);
} catch (QueryParsingException e) {
throw e;
} catch (Exception e) {
@ -197,7 +200,7 @@ public class XContentIndexQueryParser extends AbstractIndexComponent implements
XContentParser parser = null;
try {
parser = XContentFactory.xContent(source).createParser(source);
return parse(cache.get().get(), parser);
return parse(cache.get(), parser);
} catch (QueryParsingException e) {
throw e;
} catch (Exception e) {
@ -211,20 +214,20 @@ public class XContentIndexQueryParser extends AbstractIndexComponent implements
public ParsedQuery parse(XContentParser parser) {
try {
return parse(cache.get().get(), parser);
return parse(cache.get(), parser);
} catch (IOException e) {
throw new QueryParsingException(index, "Failed to parse", e);
}
}
public Filter parseInnerFilter(XContentParser parser) throws IOException {
QueryParseContext context = cache.get().get();
QueryParseContext context = cache.get();
context.reset(parser);
return context.parseInnerFilter();
}
public Query parseInnerQuery(XContentParser parser) throws IOException {
QueryParseContext context = cache.get().get();
QueryParseContext context = cache.get();
context.reset(parser);
return context.parseInnerQuery();
}

View File

@ -52,6 +52,7 @@ import org.elasticsearch.index.merge.MergeStats;
import org.elasticsearch.index.percolator.PercolatorModule;
import org.elasticsearch.index.percolator.PercolatorService;
import org.elasticsearch.index.query.IndexQueryParserModule;
import org.elasticsearch.index.query.IndexQueryParserService;
import org.elasticsearch.index.service.IndexService;
import org.elasticsearch.index.service.InternalIndexService;
import org.elasticsearch.index.settings.IndexSettingsModule;
@ -302,6 +303,7 @@ public class InternalIndicesService extends AbstractLifecycleComponent<IndicesSe
indexInjector.getInstance(IndexGateway.class).close(delete);
indexInjector.getInstance(MapperService.class).close();
indexInjector.getInstance(IndexQueryParserService.class).close();
Injectors.close(injector);