Allow to specify a specific field in the clear cache API, closes #1374.

This commit is contained in:
Shay Banon 2011-10-03 12:13:02 +02:00
parent d7f7f77d81
commit 56a4c98e8b
8 changed files with 86 additions and 3 deletions

View File

@ -35,6 +35,7 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest {
private boolean fieldDataCache = false;
private boolean idCache = false;
private boolean bloomCache = false;
private String[] fields = null;
ClearIndicesCacheRequest() {
}
@ -79,6 +80,15 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest {
return this;
}
public ClearIndicesCacheRequest fields(String... fields) {
this.fields = fields;
return this;
}
public String[] fields() {
return this.fields;
}
public boolean idCache() {
return this.idCache;
}
@ -103,6 +113,13 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest {
fieldDataCache = in.readBoolean();
idCache = in.readBoolean();
bloomCache = in.readBoolean();
int size = in.readVInt();
if (size > 0) {
fields = new String[size];
for (int i = 0; i < size; i++) {
fields[i] = in.readUTF();
}
}
}
public void writeTo(StreamOutput out) throws IOException {
@ -111,5 +128,13 @@ public class ClearIndicesCacheRequest extends BroadcastOperationRequest {
out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache);
out.writeBoolean(bloomCache);
if (fields == null) {
out.writeVInt(0);
} else {
out.writeVInt(fields.length);
for (String field : fields) {
out.writeUTF(field);
}
}
}
}

View File

@ -34,6 +34,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
private boolean fieldDataCache = false;
private boolean idCache = false;
private boolean bloomCache = false;
private String[] fields = null;
ShardClearIndicesCacheRequest() {
}
@ -44,6 +45,7 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
fieldDataCache = request.fieldDataCache();
idCache = request.idCache();
bloomCache = request.bloomCache();
fields = request.fields();
}
public boolean filterCache() {
@ -62,6 +64,10 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
return this.bloomCache;
}
public String[] fields() {
return this.fields;
}
public ShardClearIndicesCacheRequest waitForOperations(boolean waitForOperations) {
this.filterCache = waitForOperations;
return this;
@ -73,6 +79,13 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
fieldDataCache = in.readBoolean();
idCache = in.readBoolean();
bloomCache = in.readBoolean();
int size = in.readVInt();
if (size > 0) {
fields = new String[size];
for (int i = 0; i < size; i++) {
fields[i] = in.readUTF();
}
}
}
@Override public void writeTo(StreamOutput out) throws IOException {
@ -81,5 +94,13 @@ class ShardClearIndicesCacheRequest extends BroadcastShardOperationRequest {
out.writeBoolean(fieldDataCache);
out.writeBoolean(idCache);
out.writeBoolean(bloomCache);
if (fields == null) {
out.writeVInt(0);
} else {
out.writeVInt(fields.length);
for (String field : fields) {
out.writeUTF(field);
}
}
}
}

View File

@ -122,7 +122,13 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
}
if (request.fieldDataCache()) {
clearedAtLeastOne = true;
service.cache().fieldData().clear();
if (request.fields() == null || request.fields().length == 0) {
service.cache().fieldData().clear();
} else {
for (String field : request.fields()) {
service.cache().fieldData().clear(field);
}
}
}
if (request.idCache()) {
clearedAtLeastOne = true;
@ -133,7 +139,14 @@ public class TransportClearIndicesCacheAction extends TransportBroadcastOperatio
service.cache().bloomCache().clear();
}
if (!clearedAtLeastOne) {
service.cache().clear();
if (request.fields() != null && request.fields().length > 0) {
// only clear caches relating to the specified fields
for (String field : request.fields()) {
service.cache().fieldData().clear(field);
}
} else {
service.cache().clear();
}
}
service.cache().invalidateCache();
}

View File

@ -50,6 +50,11 @@ public class ClearIndicesCacheRequestBuilder extends BaseIndicesRequestBuilder<C
return this;
}
public ClearIndicesCacheRequestBuilder setFields(String... fields) {
request.fields(fields);
return this;
}
public ClearIndicesCacheRequestBuilder setIdCache(boolean idCache) {
request.idCache(idCache);
return this;

View File

@ -36,6 +36,8 @@ public interface FieldDataCache extends IndexComponent, CloseableComponent {
String type();
void clear(String fieldName);
void clear();
void clear(IndexReader reader);

View File

@ -50,6 +50,10 @@ public class NoneFieldDataCache extends AbstractIndexComponent implements FieldD
return "none";
}
@Override public void clear(String fieldName) {
}
@Override public void clear() {
}

View File

@ -31,6 +31,7 @@ import org.elasticsearch.index.field.data.FieldDataType;
import org.elasticsearch.index.settings.IndexSettings;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
@ -54,6 +55,12 @@ public abstract class AbstractConcurrentMapFieldDataCache extends AbstractIndexC
clear();
}
@Override public void clear(String fieldName) {
for (Map.Entry<Object, ConcurrentMap<String, FieldData>> entry : cache.entrySet()) {
entry.getValue().remove(fieldName);
}
}
@Override public void clear() {
cache.clear();
}

View File

@ -27,7 +27,12 @@ import org.elasticsearch.client.Client;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.XContentRestResponse;
import org.elasticsearch.rest.XContentThrowableRestResponse;
import org.elasticsearch.rest.action.support.RestActions;
import org.elasticsearch.rest.action.support.RestXContentBuilder;
@ -58,6 +63,7 @@ public class RestClearIndicesCacheAction extends BaseRestHandler {
clearIndicesCacheRequest.fieldDataCache(request.paramAsBoolean("field_data", clearIndicesCacheRequest.fieldDataCache()));
clearIndicesCacheRequest.idCache(request.paramAsBoolean("id", clearIndicesCacheRequest.idCache()));
clearIndicesCacheRequest.bloomCache(request.paramAsBoolean("bloom", clearIndicesCacheRequest.bloomCache()));
clearIndicesCacheRequest.fields(request.paramAsStringArray("fields", clearIndicesCacheRequest.fields()));
// we just send back a response, no need to fork a listener
clearIndicesCacheRequest.listenerThreaded(false);