This commit is contained in:
Shay Banon 2012-06-27 21:50:36 +02:00
parent d6bc17fee5
commit 0aa0b9ef22
3 changed files with 40 additions and 66 deletions

View File

@ -239,7 +239,7 @@ public class TransportUpdateAction extends TransportInstanceSingleOperationActio
if (indexRequest.parent() != null) {
parent = indexRequest.parent();
}
updateSource(updatedSourceAsMap, indexRequest.underlyingSourceAsMap());
XContentHelper.update(updatedSourceAsMap, indexRequest.underlyingSourceAsMap());
} else {
Map<String, Object> ctx = new HashMap<String, Object>(2);
ctx.put("_source", sourceAndContent.v2());
@ -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<String, Object> source, Map<String, Object> changes) {
for (Map.Entry<String, Object> 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<String, Object>) source.get(changesEntry.getKey()), (Map<String, Object>) changesEntry.getValue());
} else {
// update the field
source.put(changesEntry.getKey(), changesEntry.getValue());
}
}
}
}
}

View File

@ -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);

View File

@ -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<String, Object> source, Map<String, Object> changes) {
for (Map.Entry<String, Object> 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<String, Object>) source.get(changesEntry.getKey()), (Map<String, Object>) 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.