From 0aa0b9ef2229f74095e5e116bd92accd63bae5fd Mon Sep 17 00:00:00 2001 From: Shay Banon Date: Wed, 27 Jun 2012 21:50:36 +0200 Subject: [PATCH] cleanups --- .../action/update/TransportUpdateAction.java | 32 +++--------- .../action/update/UpdateRequest.java | 52 +++++-------------- .../common/xcontent/XContentHelper.java | 22 ++++++++ 3 files changed, 40 insertions(+), 66 deletions(-) diff --git a/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java b/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java index 26fef66c26c..22770916ebf 100644 --- a/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java +++ b/src/main/java/org/elasticsearch/action/update/TransportUpdateAction.java @@ -216,7 +216,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio return; } - Tuple> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef().bytes(), getResult.internalSourceRef().offset(), getResult.internalSourceRef().length(), true); + Tuple> sourceAndContent = XContentHelper.convertToMap(getResult.internalSourceRef().bytes(), getResult.internalSourceRef().offset(), getResult.internalSourceRef().length(), true); String operation = null; String timestamp = null; Long ttl = null; @@ -225,7 +225,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio final XContentType updateSourceContentType = sourceAndContent.v1(); String routing = getResult.fields().containsKey(RoutingFieldMapper.NAME) ? getResult.field(RoutingFieldMapper.NAME).value().toString() : null; String parent = getResult.fields().containsKey(ParentFieldMapper.NAME) ? getResult.field(ParentFieldMapper.NAME).value().toString() : null; - + if (request.script() == null && request.doc() != null) { IndexRequest indexRequest = request.doc(); updatedSourceAsMap = sourceAndContent.v2(); @@ -239,11 +239,11 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio if (indexRequest.parent() != null) { parent = indexRequest.parent(); } - updateSource(updatedSourceAsMap, indexRequest.underlyingSourceAsMap()); + XContentHelper.update(updatedSourceAsMap, indexRequest.underlyingSourceAsMap()); } else { Map ctx = new HashMap(2); ctx.put("_source", sourceAndContent.v2()); - + try { ExecutableScript script = scriptService.executable(request.scriptLang, request.script, request.scriptParams); script.setNextVar("ctx", ctx); @@ -264,10 +264,10 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio ttl = TimeValue.parseTimeValue((String) fetchedTTL, null).millis(); } } - + updatedSourceAsMap = (Map) ctx.get("_source"); } - + // apply script to update the source // No TTL has been given in the update script so we keep previous TTL value if there is one if (ttl == null) { @@ -388,24 +388,4 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio return new GetResult(request.index(), request.type(), request.id(), version, true, sourceRequested ? sourceAsBytes : null, fields); } - - /** - * Updates the source with the specified changes. Maps are updated recursively. - */ - private void updateSource(Map source, Map changes) { - for (Map.Entry changesEntry : changes.entrySet()) { - if (!source.containsKey(changesEntry.getKey())) { - // safe to copy, change does not exist in source - source.put(changesEntry.getKey(), changesEntry.getValue()); - } else { - if (source.get(changesEntry.getKey()) instanceof Map && changesEntry.getValue() instanceof Map) { - // recursive merge maps - updateSource((Map) source.get(changesEntry.getKey()), (Map) changesEntry.getValue()); - } else { - // update the field - source.put(changesEntry.getKey(), changesEntry.getValue()); - } - } - } - } } diff --git a/src/main/java/org/elasticsearch/action/update/UpdateRequest.java b/src/main/java/org/elasticsearch/action/update/UpdateRequest.java index 97769606b24..483aba84729 100644 --- a/src/main/java/org/elasticsearch/action/update/UpdateRequest.java +++ b/src/main/java/org/elasticsearch/action/update/UpdateRequest.java @@ -70,7 +70,7 @@ public class UpdateRequest extends InstanceShardOperationRequest { @Nullable private IndexRequest doc; - + UpdateRequest() { } @@ -377,8 +377,8 @@ public class UpdateRequest extends InstanceShardOperationRequest { * Sets the doc to use for updates when a script is not specified. */ public UpdateRequest doc(Map source, XContentType contentType) { - safeDoc().source(source, contentType); - return this; + safeDoc().source(source, contentType); + return this; } /** @@ -415,7 +415,7 @@ public class UpdateRequest extends InstanceShardOperationRequest { } return doc; } - + /** * Sets the index request to be used if the document does not exists. Otherwise, a {@link org.elasticsearch.index.engine.DocumentMissingException} * is thrown. @@ -529,20 +529,12 @@ public class UpdateRequest extends InstanceShardOperationRequest { consistencyLevel = WriteConsistencyLevel.fromId(in.readByte()); type = in.readUTF(); id = in.readUTF(); - if (in.readBoolean()) { - routing = in.readUTF(); - } - if (in.readBoolean()) { - script = in.readUTF(); - } - if (in.readBoolean()) { - scriptLang = in.readUTF(); - } + routing = in.readOptionalUTF(); + script = in.readOptionalUTF(); + scriptLang = in.readOptionalUTF(); scriptParams = in.readMap(); retryOnConflict = in.readVInt(); - if (in.readBoolean()) { - percolate = in.readUTF(); - } + percolate = in.readOptionalUTF(); refresh = in.readBoolean(); if (in.readBoolean()) { doc = new IndexRequest(); @@ -568,32 +560,12 @@ public class UpdateRequest extends InstanceShardOperationRequest { out.writeByte(consistencyLevel.id()); out.writeUTF(type); out.writeUTF(id); - if (routing == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeUTF(routing); - } - if (script == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeUTF(script); - } - if (scriptLang == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeUTF(scriptLang); - } + out.writeOptionalUTF(routing); + out.writeOptionalUTF(script); + out.writeOptionalUTF(scriptLang); out.writeMap(scriptParams); out.writeVInt(retryOnConflict); - if (percolate == null) { - out.writeBoolean(false); - } else { - out.writeBoolean(true); - out.writeUTF(percolate); - } + out.writeOptionalUTF(percolate); out.writeBoolean(refresh); if (doc == null) { out.writeBoolean(false); diff --git a/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 12abf3fb5de..137ebd26572 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -37,6 +37,7 @@ import java.util.Map; /** * */ +@SuppressWarnings("unchecked") public class XContentHelper { public static XContentParser createParser(byte[] data, int offset, int length) throws IOException { @@ -105,6 +106,27 @@ public class XContentHelper { } } + /** + * Updates the provided changes into the source. If the key exists in the changes, it overrides the one in source + * unless both are Maps, in which case it recuersively updated it. + */ + public static void update(Map source, Map changes) { + for (Map.Entry changesEntry : changes.entrySet()) { + if (!source.containsKey(changesEntry.getKey())) { + // safe to copy, change does not exist in source + source.put(changesEntry.getKey(), changesEntry.getValue()); + } else { + if (source.get(changesEntry.getKey()) instanceof Map && changesEntry.getValue() instanceof Map) { + // recursive merge maps + update((Map) source.get(changesEntry.getKey()), (Map) changesEntry.getValue()); + } else { + // update the field + source.put(changesEntry.getKey(), changesEntry.getValue()); + } + } + } + } + /** * Merges the defaults provided as the second parameter into the content of the first. Only does recursive merge * for inner maps.