summaryrefslogtreecommitdiffstats
path: root/document
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2018-09-17 10:42:20 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2018-09-17 10:42:20 +0200
commit1ba72a59a3e2e35045d46e28dd43674e239878e1 (patch)
tree11aafa053da563ad25246574f288d5b038d26ccc /document
parent0f4aa6b514bcca1ed48969680c71e740448fce85 (diff)
Less strict on possible missuse of deprecated API.
Make by name operatons fast by looking up the field in the document type.
Diffstat (limited to 'document')
-rw-r--r--document/src/main/java/com/yahoo/document/DocumentUpdate.java41
-rw-r--r--document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java24
2 files changed, 43 insertions, 22 deletions
diff --git a/document/src/main/java/com/yahoo/document/DocumentUpdate.java b/document/src/main/java/com/yahoo/document/DocumentUpdate.java
index 8a743e58490..301d6af0f54 100644
--- a/document/src/main/java/com/yahoo/document/DocumentUpdate.java
+++ b/document/src/main/java/com/yahoo/document/DocumentUpdate.java
@@ -48,9 +48,9 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
public static final int CLASSID = 0x1000 + 6;
private DocumentId docId;
- private List<FieldUpdate> fieldUpdates;
+ private final List<FieldUpdate> fieldUpdates;
private final Map<Integer, FieldUpdate> id2FieldUpdateMap;
- private List<FieldPathUpdate> fieldPathUpdates;
+ private final List<FieldPathUpdate> fieldPathUpdates;
private DocumentType documentType;
private Optional<Boolean> createIfNonExistent = Optional.empty();
@@ -248,13 +248,9 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
@Deprecated
public FieldUpdate setFieldUpdate(int index, FieldUpdate upd) {
FieldUpdate old = fieldUpdates.get(index);
- if (old.getField().equals(upd.getField())) {
- fieldUpdates.set(index, upd);
- id2FieldUpdateMap.put(upd.getField().getId(), upd);
- } else {
- throw new IllegalArgumentException("You can not replace a FieldUpdate for field '" + old.getField() +
- "' with an update for field '" + upd.getField() + "'");
- }
+ fieldUpdates.set(index, upd);
+ id2FieldUpdateMap.remove(old.getField().getId());
+ id2FieldUpdateMap.put(upd.getField().getId(), upd);
return old;
}
@@ -282,11 +278,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
* @return the update for the field, or null if that field has no update in this
*/
public FieldUpdate getFieldUpdate(String fieldName) {
- for (FieldUpdate fieldUpdate : id2FieldUpdateMap.values()) {
- if (fieldUpdate.getField().getName().equals(fieldName))
- return fieldUpdate;
- }
- return null;
+ return getFieldUpdate(documentType.getField(fieldName));
}
private FieldUpdate getFieldUpdateById(Integer fieldId) {
return id2FieldUpdateMap.get(fieldId);
@@ -295,18 +287,21 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
/**
* Assigns the field updates of this document update.
* This document update receives ownership of the list - it can not be subsequently used
- * by the caller.
+ * by the caller. Also note that there no assumptions can be made on the order of items
+ * after this call. They might have been joined if for the same field or reordered.
*
* @param fieldUpdates the new list of updates of this
* @throws NullPointerException if the argument passed is null
- * @deprecated Iterate and use addFieldUpdate instead
*/
- @Deprecated
public void setFieldUpdates(Collection<FieldUpdate> fieldUpdates) {
if (fieldUpdates == null) {
throw new NullPointerException("The field updates of a document update can not be null");
}
- fieldUpdates.clear();
+ clearFieldUpdates();
+ addFieldUpdates(fieldUpdates);
+ }
+
+ public void addFieldUpdates(Collection<FieldUpdate> fieldUpdates) {
for (FieldUpdate fieldUpdate : fieldUpdates) {
addFieldUpdate(fieldUpdate);
}
@@ -375,9 +370,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
if (!documentType.equals(update.documentType)) {
throw new IllegalArgumentException("Expected " + documentType + ", got " + update.documentType + ".");
}
- for (FieldUpdate fieldUpd : update.fieldUpdates()) {
- addFieldUpdate(fieldUpd);
- }
+ addFieldUpdates(update.fieldUpdates());
for (FieldPathUpdate pathUpd : update.fieldPathUpdates) {
addFieldPathUpdate(pathUpd);
}
@@ -402,6 +395,10 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
return id2FieldUpdateMap.remove(field.getId());
}
+ public FieldUpdate removeFieldUpdate(String fieldName) {
+ return removeFieldUpdate(documentType.getField(fieldName));
+ }
+
/**
* Returns the document type of this document update.
*
@@ -430,7 +427,7 @@ public class DocumentUpdate extends DocumentOperation implements Iterable<FieldP
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 (id2FieldUpdateMap != null ? !id2FieldUpdateMap.equals(that.id2FieldUpdateMap) : that.id2FieldUpdateMap != null) return false;
if (this.getCreateIfNonExistent() != ((DocumentUpdate) o).getCreateIfNonExistent()) return false;
return true;
diff --git a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
index 243102c17df..53f68ca182c 100644
--- a/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
+++ b/document/src/test/java/com/yahoo/document/DocumentUpdateTestCase.java
@@ -442,6 +442,30 @@ public class DocumentUpdateTestCase {
}
@Test
+ public void testgetAndRemoveByName() {
+ DocumentType docType = new DocumentType("my_type");
+ Field my_int = new Field("my_int", DataType.INT);
+ Field your_int = new Field("your_int", DataType.INT);
+ docType.addField(my_int);
+ docType.addField(your_int);
+ DocumentUpdate update = new DocumentUpdate(docType, new DocumentId("doc:this:is:a:test"));
+
+ update.addFieldUpdate(FieldUpdate.createAssign(my_int, new IntegerFieldValue(2)));
+ assertNull(update.getFieldUpdate("your_int"));
+ assertEquals(new IntegerFieldValue(2), update.getFieldUpdate("my_int").getValueUpdate(0).getValue());
+ assertNull(update.removeFieldUpdate("your_int"));
+ assertEquals(new IntegerFieldValue(2), update.removeFieldUpdate("my_int").getValueUpdate(0).getValue());
+ assertNull(update.getFieldUpdate("my_int"));
+
+ update.addFieldUpdate(FieldUpdate.createAssign(my_int, new IntegerFieldValue(2)));
+ assertNull(update.getFieldUpdate(your_int));
+ assertEquals(new IntegerFieldValue(2), update.getFieldUpdate(my_int).getValueUpdate(0).getValue());
+ assertNull(update.removeFieldUpdate(your_int));
+ assertEquals(new IntegerFieldValue(2), update.removeFieldUpdate(my_int).getValueUpdate(0).getValue());
+ assertNull(update.getFieldUpdate(my_int));
+ }
+
+ @Test
public void testInstantiationAndEqualsHashCode() {
DocumentType type = new DocumentType("doo");
DocumentUpdate d1 = new DocumentUpdate(type, new DocumentId("doc:this:is:a:test"));