Skip to content

Commit

Permalink
Clear Cache API: Add specific cache clear for id, filter, `field_…
Browse files Browse the repository at this point in the history
…data`, `bloom`, closes elastic#716.
  • Loading branch information
kimchy committed Feb 23, 2011
1 parent bc011bd commit d8b278b
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 11 deletions.
Expand Up @@ -31,7 +31,10 @@
*/
public class ClearIndicesCacheRequest extends BroadcastOperationRequest {

private boolean filterCache = true;
private boolean filterCache = false;
private boolean fieldDataCache = false;
private boolean idCache = false;
private boolean bloomCache = false;

ClearIndicesCacheRequest() {
}
Expand Down Expand Up @@ -62,21 +65,51 @@ public boolean filterCache() {
return filterCache;
}

/**
* Should the filter cache be cleared or not. Defaults to <tt>true</tt>.
*/
public ClearIndicesCacheRequest filterCache(boolean filterCache) {
this.filterCache = filterCache;
return this;
}

public boolean fieldDataCache() {
return this.fieldDataCache;
}

public ClearIndicesCacheRequest fieldDataCache(boolean fieldDataCache) {
this.fieldDataCache = fieldDataCache;
return this;
}

public boolean idCache() {
return this.idCache;
}

public ClearIndicesCacheRequest idCache(boolean idCache) {
this.idCache = idCache;
return this;
}

public boolean bloomCache() {
return this.bloomCache;
}

public ClearIndicesCacheRequest bloomCache(boolean bloomCache) {
this.bloomCache = bloomCache;
return this;
}

public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
filterCache = in.readBoolean();
fieldDataCache = in.readBoolean();
idCache = in.readBoolean();
bloomCache = in.readBoolean();
}

public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(filterCache);
out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache);
out.writeBoolean(bloomCache);
}
}
Expand Up @@ -30,20 +30,38 @@
*/
class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {

private boolean filterCache = true;
private boolean filterCache = false;
private boolean fieldDataCache = false;
private boolean idCache = false;
private boolean bloomCache = false;

ShardClearIndicesCacheRequest() {
}

public ShardClearIndicesCacheRequest(String index, int shardId, ClearIndicesCacheRequest request) {
super(index, shardId);
filterCache = request.filterCache();
fieldDataCache = request.fieldDataCache();
idCache = request.idCache();
bloomCache = request.bloomCache();
}

public boolean filterCache() {
return filterCache;
}

public boolean fieldDataCache() {
return this.fieldDataCache;
}

public boolean idCache() {
return this.idCache;
}

public boolean bloomCache() {
return this.bloomCache;
}

public ShardClearIndicesCacheRequest waitForOperations(boolean waitForOperations) {
this.filterCache = waitForOperations;
return this;
Expand All @@ -52,10 +70,16 @@ public ShardClearIndicesCacheRequest waitForOperations(boolean waitForOperations
@Override public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
filterCache = in.readBoolean();
fieldDataCache = in.readBoolean();
idCache = in.readBoolean();
bloomCache = in.readBoolean();
}

@Override public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeBoolean(filterCache);
out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache);
out.writeBoolean(bloomCache);
}
}
Expand Up @@ -111,10 +111,30 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
}

@Override protected ShardClearIndicesCacheResponse shardOperation(ShardClearIndicesCacheRequest request) throws ElasticSearchException {
// TODO we can optimize to go to a single node where the index exists
IndexService service = indicesService.indexService(request.index());
if (service != null) {
service.cache().clear();
// we always clear the query cache
service.cache().queryParserCache().clear();
boolean clearedAtLeastOne = false;
if (request.filterCache()) {
clearedAtLeastOne = true;
service.cache().filter().clear();
}
if (request.fieldDataCache()) {
clearedAtLeastOne = true;
service.cache().fieldData().clear();
}
if (request.idCache()) {
clearedAtLeastOne = true;
service.cache().idCache().clear();
}
if (request.bloomCache()) {
clearedAtLeastOne = true;
service.cache().bloomCache().clear();
}
if (!clearedAtLeastOne) {
service.cache().clear();
}
}
return new ShardClearIndicesCacheResponse(request.index(), request.shardId());
}
Expand Down
Expand Up @@ -40,14 +40,26 @@ public ClearIndicesCacheRequestBuilder setIndices(String... indices) {
return this;
}

/**
* Should the filter cache be cleared or not. Defaults to <tt>true</tt>.
*/
public ClearIndicesCacheRequestBuilder setFilterCache(boolean filterCache) {
request.filterCache(filterCache);
return this;
}

public ClearIndicesCacheRequestBuilder setFieldDataCache(boolean fieldDataCache) {
request.fieldDataCache(fieldDataCache);
return this;
}

public ClearIndicesCacheRequestBuilder setIdCache(boolean idCache) {
request.idCache(idCache);
return this;
}

public ClearIndicesCacheRequestBuilder setBloomCache(boolean bloomCache) {
request.bloomCache(bloomCache);
return this;
}

/**
* Should the listener be called on a separate thread if needed.
*/
Expand Down
Expand Up @@ -54,7 +54,10 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
@Override public void handleRequest(final RestRequest request, final RestChannel channel) {
ClearIndicesCacheRequest clearIndicesCacheRequest = new ClearIndicesCacheRequest(RestActions.splitIndices(request.param("index")));
try {
clearIndicesCacheRequest.filterCache(request.paramAsBoolean("filter_cache", clearIndicesCacheRequest.filterCache()));
clearIndicesCacheRequest.filterCache(request.paramAsBoolean("filter", clearIndicesCacheRequest.filterCache()));
clearIndicesCacheRequest.fieldDataCache(request.paramAsBoolean("field_data", clearIndicesCacheRequest.fieldDataCache()));
clearIndicesCacheRequest.idCache(request.paramAsBoolean("id", clearIndicesCacheRequest.idCache()));
clearIndicesCacheRequest.bloomCache(request.paramAsBoolean("bloom", clearIndicesCacheRequest.bloomCache()));

// we just send back a response, no need to fork a listener
clearIndicesCacheRequest.listenerThreaded(false);
Expand Down

0 comments on commit d8b278b

Please sign in to comment.