From 45450e22eb5dad1d1a7e84b9f19b2612b9bdbdaa Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Thu, 25 Oct 2018 21:04:30 +0200 Subject: Only deprecate the badly scaling methods. Make the replacements and prepare for using a map next time. --- .../java/com/yahoo/document/DocumentUpdate.java | 82 ++++++++++----------- document/src/tests/data/serializeupdatejava.dat | Bin 112 -> 112 bytes 2 files changed, 38 insertions(+), 44 deletions(-) diff --git a/document/src/main/java/com/yahoo/document/DocumentUpdate.java b/document/src/main/java/com/yahoo/document/DocumentUpdate.java index 1a84a14939e..54ced1321a0 100644 --- a/document/src/main/java/com/yahoo/document/DocumentUpdate.java +++ b/document/src/main/java/com/yahoo/document/DocumentUpdate.java @@ -15,10 +15,8 @@ import com.yahoo.io.GrowableByteBuffer; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.List; -import java.util.Map; import java.util.Optional; /** @@ -40,7 +38,7 @@ import java.util.Optional; * @see com.yahoo.document.update.FieldUpdate * @see com.yahoo.document.update.ValueUpdate */ -//TODO Vespa 7 Remove all deprecated methods +//TODO Vespa 7 Remove all deprecated methods and use a map to avoid quadratic scaling on insert/update/remove public class DocumentUpdate extends DocumentOperation implements Iterable { @@ -49,10 +47,9 @@ public class DocumentUpdate extends DocumentOperation implements Iterable fieldUpdates; - private final Map id2FieldUpdateMap; private final List fieldPathUpdates; private DocumentType documentType; - private Optional createIfNonExistent = Optional.empty(); + private Boolean createIfNonExistent; /** * Creates a DocumentUpdate. @@ -61,7 +58,10 @@ public class DocumentUpdate extends DocumentOperation implements Iterable()); + this.docId = docId; + this.documentType = docType; + this.fieldUpdates = new ArrayList<>(); + this.fieldPathUpdates = new ArrayList<>(); } /** @@ -71,7 +71,6 @@ public class DocumentUpdate extends DocumentOperation implements Iterable(); - id2FieldUpdateMap = new HashMap<>(); fieldPathUpdates = new ArrayList<>(); reader.read(this); } @@ -86,14 +85,6 @@ public class DocumentUpdate extends DocumentOperation implements Iterable id2fieldUpdateMap) { - this.docId = docId; - this.documentType = docType; - this.fieldUpdates = new ArrayList<>(id2fieldUpdateMap.values()); - id2FieldUpdateMap = id2fieldUpdateMap; - this.fieldPathUpdates = new ArrayList<>(); - } - public DocumentId getId() { return docId; } @@ -122,7 +113,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable> iter = id2FieldUpdateMap.entrySet().iterator(); iter.hasNext();) { - Map.Entry entry = iter.next(); - FieldUpdate update = entry.getValue(); + for (Iterator iter = fieldUpdates.iterator(); iter.hasNext();) { + FieldUpdate update = iter.next(); if (!update.isEmpty()) { ValueUpdate last = update.getValueUpdate(update.size() - 1); if (last instanceof AssignValueUpdate) { @@ -175,7 +165,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable getFieldUpdates() { - return Collections.unmodifiableList(new ArrayList<>(id2FieldUpdateMap.values())); + return Collections.unmodifiableList(fieldUpdates); } /** @@ -184,7 +174,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable fieldUpdates() { - return Collections.unmodifiableCollection(id2FieldUpdateMap.values()); + return Collections.unmodifiableCollection(fieldUpdates); } /** @@ -249,8 +239,6 @@ public class DocumentUpdate extends DocumentOperation implements Iterable it = fieldUpdates.iterator(); it.hasNext();) { + FieldUpdate fieldUpdate = it.next(); + if (fieldUpdate.getField().equals(field)) { + it.remove(); + return fieldUpdate; + } + } + return null; } public FieldUpdate removeFieldUpdate(String fieldName) { Field field = documentType.getField(fieldName); @@ -429,7 +426,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable i = id2FieldUpdateMap.values().iterator(); i.hasNext();) { - FieldUpdate fieldUpdate = i.next(); - string.append(fieldUpdate); - if (i.hasNext()) { - string.append(", "); - } + for (FieldUpdate fieldUpdate : fieldUpdates) { + string.append(fieldUpdate).append(" "); } string.append("]"); if (fieldPathUpdates.size() > 0) { string.append(" [ "); for (FieldPathUpdate up : fieldPathUpdates) { - string.append(up.toString() + " "); + string.append(up.toString()).append(" "); } string.append(" ]"); } @@ -475,6 +468,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable iterator() { return fieldPathUpdates.iterator(); } @@ -485,7 +479,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable getOptionalCreateIfNonExistent() { - return createIfNonExistent; + return Optional.ofNullable(createIfNonExistent); } } diff --git a/document/src/tests/data/serializeupdatejava.dat b/document/src/tests/data/serializeupdatejava.dat index 20c56228bde..e2a98d42fb1 100644 Binary files a/document/src/tests/data/serializeupdatejava.dat and b/document/src/tests/data/serializeupdatejava.dat differ -- cgit v1.2.3