From 56a4c98e8b4c26771ce7424d4fc758643293dab0 Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Mon, 3 Oct 2011 12:13:02 +0200 Subject: [PATCH] Allow to specify a specific field in the clear cache API, closes #1374. --- .../cache/clear/ClearIndicesCacheRequest.java | 25 +++++++++++++++++++ .../clear/ShardClearIndicesCacheRequest.java | 21 ++++++++++++++++ .../TransportClearIndicesCacheAction.java | 17 +++++++++++-- .../ClearIndicesCacheRequestBuilder.java | 5 ++++ .../cache/field/data/FieldDataCache.java | 2 ++ .../field/data/none/NoneFieldDataCache.java | 4 +++ .../AbstractConcurrentMapFieldDataCache.java | 7 ++++++ .../clear/RestClearIndicesCacheAction.java | 8 +++++- 8 files changed, 86 insertions(+), 3 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ClearIndicesCacheRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ClearIndicesCacheRequest.java index 51343d9de02..7704af11de9 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ClearIndicesCacheRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ClearIndicesCacheRequest.java @@ -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); + } + } } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ShardClearIndicesCacheRequest.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ShardClearIndicesCacheRequest.java index 78509995efd..3ec0976ac61 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ShardClearIndicesCacheRequest.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/ShardClearIndicesCacheRequest.java @@ -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); + } + } } } \ No newline at end of file diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/TransportClearIndicesCacheAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/TransportClearIndicesCacheAction.java index b06382101e4..9ee04075109 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/TransportClearIndicesCacheAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/cache/clear/TransportClearIndicesCacheAction.java @@ -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(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/cache/clear/ClearIndicesCacheRequestBuilder.java b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/cache/clear/ClearIndicesCacheRequestBuilder.java index 6ba23b53723..c232c4cdaee 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/cache/clear/ClearIndicesCacheRequestBuilder.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/client/action/admin/indices/cache/clear/ClearIndicesCacheRequestBuilder.java @@ -50,6 +50,11 @@ public class ClearIndicesCacheRequestBuilder extends BaseIndicesRequestBuilder> entry : cache.entrySet()) { + entry.getValue().remove(fieldName); + } + } + @Override public void clear() { cache.clear(); } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/cache/clear/RestClearIndicesCacheAction.java b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/cache/clear/RestClearIndicesCacheAction.java index 25843e97e7b..c9836b52e18 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/cache/clear/RestClearIndicesCacheAction.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/rest/action/admin/indices/cache/clear/RestClearIndicesCacheAction.java @@ -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);