diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2018-10-26 11:41:52 +0200 |
---|---|---|
committer | gjoranv <gv@oath.com> | 2019-01-21 15:09:27 +0100 |
commit | ae22dd9db46912547cd2fc8f3b2f50db04107ef4 (patch) | |
tree | c4f1789cc70a902dbf55696cba0bd78db120c5a9 /document | |
parent | 5b0dbd894b9fefcec344b384e105d627055b6993 (diff) |
remove deprecated methods and switch to hashmap instead of list.
Diffstat (limited to 'document')
-rw-r--r-- | document/src/main/java/com/yahoo/document/DocumentUpdate.java | 54 | ||||
-rw-r--r-- | document/src/tests/data/serializeupdatejava.dat | bin | 112 -> 112 bytes |
2 files changed, 22 insertions, 32 deletions
diff --git a/document/src/main/java/com/yahoo/document/DocumentUpdate.java b/document/src/main/java/com/yahoo/document/DocumentUpdate.java index a1408fc4a46..a47f1546a75 100644 --- a/document/src/main/java/com/yahoo/document/DocumentUpdate.java +++ b/document/src/main/java/com/yahoo/document/DocumentUpdate.java @@ -15,8 +15,10 @@ 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; /** @@ -38,14 +40,14 @@ import java.util.Optional; * @see com.yahoo.document.update.FieldUpdate * @see com.yahoo.document.update.ValueUpdate */ -// TODO: Use a map to avoid quadratic scaling on insert/update/remove + public class DocumentUpdate extends DocumentOperation implements Iterable<FieldPathUpdate> { //see src/vespa/document/util/identifiableid.h public static final int CLASSID = 0x1000 + 6; private DocumentId docId; - private final List<FieldUpdate> fieldUpdates; + private final Map<Integer, FieldUpdate> id2FieldUpdates; private final List<FieldPathUpdate> fieldPathUpdates; private DocumentType documentType; private Boolean createIfNonExistent; @@ -59,7 +61,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP public DocumentUpdate(DocumentType docType, DocumentId docId) { this.docId = docId; this.documentType = docType; - this.fieldUpdates = new ArrayList<>(); + this.id2FieldUpdates = new HashMap<>(); this.fieldPathUpdates = new ArrayList<>(); } @@ -69,7 +71,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP public DocumentUpdate(DocumentUpdateReader reader) { docId = null; documentType = null; - fieldUpdates = new ArrayList<>(); + id2FieldUpdates = new HashMap<>(); fieldPathUpdates = new ArrayList<>(); reader.read(this); } @@ -112,7 +114,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP public DocumentUpdate applyTo(Document doc) { verifyType(doc); - for (FieldUpdate fieldUpdate : fieldUpdates) { + for (FieldUpdate fieldUpdate : id2FieldUpdates.values()) { fieldUpdate.applyTo(doc); } for (FieldPathUpdate fieldPathUpdate : fieldPathUpdates) { @@ -130,8 +132,9 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP public DocumentUpdate prune(Document doc) { verifyType(doc); - for (Iterator<FieldUpdate> iter = fieldUpdates.iterator(); iter.hasNext();) { - FieldUpdate update = iter.next(); + for (Iterator<Map.Entry<Integer, FieldUpdate>> iter = id2FieldUpdates.entrySet().iterator(); iter.hasNext();) { + Map.Entry<Integer, FieldUpdate> entry = iter.next(); + FieldUpdate update = entry.getValue(); if (!update.isEmpty()) { ValueUpdate last = update.getValueUpdate(update.size() - 1); if (last instanceof AssignValueUpdate) { @@ -162,7 +165,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP * @return a collection of all FieldUpdates in this DocumentUpdate */ public Collection<FieldUpdate> fieldUpdates() { - return Collections.unmodifiableCollection(fieldUpdates); + return Collections.unmodifiableCollection(id2FieldUpdates.values()); } /** @@ -202,7 +205,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP /** Removes all field updates from the list for field updates. */ public void clearFieldUpdates() { - fieldUpdates.clear(); + id2FieldUpdates.clear(); } /** @@ -216,12 +219,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP return field != null ? getFieldUpdate(field) : null; } private FieldUpdate getFieldUpdateById(Integer fieldId) { - for (FieldUpdate fieldUpdate : fieldUpdates) { - if (fieldUpdate.getField().getId() == fieldId) { - return fieldUpdate; - } - } - return null; + return id2FieldUpdates.get(fieldId); } /** @@ -257,7 +255,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP * @return the size of the List of FieldUpdates */ public int size() { - return fieldUpdates.size(); + return id2FieldUpdates.size(); } /** @@ -279,7 +277,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP if (prevUpdate != null) { prevUpdate.addAll(update); } else { - fieldUpdates.add(update); + id2FieldUpdates.put(fieldId, update); } } return this; @@ -320,14 +318,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP } public FieldUpdate removeFieldUpdate(Field field) { - for (Iterator<FieldUpdate> it = fieldUpdates.iterator(); it.hasNext();) { - FieldUpdate fieldUpdate = it.next(); - if (fieldUpdate.getField().equals(field)) { - it.remove(); - return fieldUpdate; - } - } - return null; + return id2FieldUpdates.remove(field.getId()); } public FieldUpdate removeFieldUpdate(String fieldName) { @@ -361,9 +352,8 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP if (docId != null ? !docId.equals(that.docId) : that.docId != null) return false; if (documentType != null ? !documentType.equals(that.documentType) : that.documentType != null) return false; - if (fieldPathUpdates != null ? !fieldPathUpdates.equals(that.fieldPathUpdates) : that.fieldPathUpdates != null) - return false; - if (fieldUpdates != null ? !fieldUpdates.equals(that.fieldUpdates) : that.fieldUpdates != null) return false; + if (!fieldPathUpdates.equals(that.fieldPathUpdates)) return false; + if (!id2FieldUpdates.equals(that.id2FieldUpdates)) return false; if (this.getCreateIfNonExistent() != ((DocumentUpdate) o).getCreateIfNonExistent()) return false; return true; @@ -372,8 +362,8 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP @Override public int hashCode() { int result = docId != null ? docId.hashCode() : 0; - result = 31 * result + (fieldUpdates != null ? fieldUpdates.hashCode() : 0); - result = 31 * result + (fieldPathUpdates != null ? fieldPathUpdates.hashCode() : 0); + result = 31 * result + id2FieldUpdates.hashCode(); + result = 31 * result + fieldPathUpdates.hashCode(); result = 31 * result + (documentType != null ? documentType.hashCode() : 0); return result; } @@ -389,7 +379,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP string.append(": "); string.append("["); - for (FieldUpdate fieldUpdate : fieldUpdates) { + for (FieldUpdate fieldUpdate : id2FieldUpdates.values()) { string.append(fieldUpdate).append(" "); } string.append("]"); @@ -416,7 +406,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP * @return True if this update is empty. */ public boolean isEmpty() { - return fieldUpdates.isEmpty() && fieldPathUpdates.isEmpty(); + return id2FieldUpdates.isEmpty() && fieldPathUpdates.isEmpty(); } /** diff --git a/document/src/tests/data/serializeupdatejava.dat b/document/src/tests/data/serializeupdatejava.dat Binary files differindex e2a98d42fb1..20c56228bde 100644 --- a/document/src/tests/data/serializeupdatejava.dat +++ b/document/src/tests/data/serializeupdatejava.dat |