From a87d932820689dd4bf03432f1cbd2486ca419459 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 1 Mar 2022 15:15:22 +0100 Subject: [PATCH 1/2] No functional changes --- .../yahoo/document/BucketDistribution.java | 4 +-- .../java/com/yahoo/document/DocumentId.java | 27 ++++++++----------- .../java/com/yahoo/document/GlobalId.java | 8 +++--- .../resource/DocumentV1ApiHandler.java | 2 +- 4 files changed, 17 insertions(+), 24 deletions(-) diff --git a/document/src/main/java/com/yahoo/document/BucketDistribution.java b/document/src/main/java/com/yahoo/document/BucketDistribution.java index abacd4fdc2f9..e963f57e22b1 100644 --- a/document/src/main/java/com/yahoo/document/BucketDistribution.java +++ b/document/src/main/java/com/yahoo/document/BucketDistribution.java @@ -1,8 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.document; -import com.yahoo.document.BucketId; - import java.util.ArrayList; import java.util.List; import java.util.logging.Level; @@ -14,7 +12,7 @@ public class BucketDistribution { // A logger object to enable proper logging. - private static Logger log = Logger.getLogger(BucketDistribution.class.getName()); + private static final Logger log = Logger.getLogger(BucketDistribution.class.getName()); // A map from bucket id to column index. private int[] bucketToColumn; diff --git a/document/src/main/java/com/yahoo/document/DocumentId.java b/document/src/main/java/com/yahoo/document/DocumentId.java index 8c35eaa03296..3512c5cb7b79 100644 --- a/document/src/main/java/com/yahoo/document/DocumentId.java +++ b/document/src/main/java/com/yahoo/document/DocumentId.java @@ -11,6 +11,7 @@ import com.yahoo.vespa.objects.Serializer; import java.io.Serializable; +import java.util.Objects; /** * The id of a document @@ -18,11 +19,9 @@ public class DocumentId extends Identifiable implements Serializable { private IdString id; - private GlobalId globalId; + private GlobalId globalId = null; - /** - * Constructor used for deserialization. - */ + /** Constructor used for deserialization. */ public DocumentId(Deserializer buf) { deserialize(buf); } @@ -33,21 +32,14 @@ public DocumentId(Deserializer buf) { * The document id string can only contain text characters. */ public DocumentId(String id) { - if (id == null) { - throw new IllegalArgumentException("Cannot create DocumentId from null id."); - } - if (id.length() > IdString.MAX_LENGTH) { - throw new IllegalArgumentException("The document id(" + id.length() + ") is too long(" + IdString.MAX_LENGTH + "). " + - "However if you have already fed a document earlier on and want to remove it, you can do so by " + - "calling new DocumentId(IdString.createIdStringLessStrict()) that will bypass this restriction."); - } - this.id = IdString.createIdString(id); - globalId = null; + this.id = IdString.createIdString(Objects.requireNonNull(id)); + if (id.length() > IdString.MAX_LENGTH) + throw new IllegalArgumentException("Document id of length " + id.length() + + " is longer than the max " + IdString.MAX_LENGTH); } public DocumentId(IdString id) { this.id = id; - globalId = null; } /** @@ -86,14 +78,17 @@ public int compareTo(Object o) { return id.toString().compareTo(cmp.id.toString()); } + @Override public boolean equals(Object o) { return o instanceof DocumentId && id.equals(((DocumentId)o).id); } + @Override public int hashCode() { return id.hashCode(); } + @Override public String toString() { return id.toString(); } @@ -107,7 +102,6 @@ public void onSerialize(Serializer target) throws SerializationException { } } - public void onDeserialize(Deserializer data) throws DeserializationException { if (data instanceof DocumentReader) { id = ((DocumentReader)data).readDocumentId().getScheme(); @@ -123,4 +117,5 @@ public boolean hasDocType() { public String getDocType() { return id.getDocType(); } + } diff --git a/document/src/main/java/com/yahoo/document/GlobalId.java b/document/src/main/java/com/yahoo/document/GlobalId.java index 9e90b59171cd..b9d454dd0077 100644 --- a/document/src/main/java/com/yahoo/document/GlobalId.java +++ b/document/src/main/java/com/yahoo/document/GlobalId.java @@ -40,10 +40,10 @@ public GlobalId(byte[] raw) { /** * Constructs a new global id from a document id string. * - * @param id The document id to derive from. + * @param id the document id to derive from */ public GlobalId(IdString id) { - byte [] raw = MD5.md5.get().digest(id.toUtf8().wrap().array()); + byte[] raw = MD5.md5.get().digest(id.toUtf8().wrap().array()); long location = id.getLocation(); this.raw = new byte [LENGTH]; for (int i = 0; i < 4; ++i) { @@ -57,7 +57,7 @@ public GlobalId(IdString id) { /** * Constructs a global id by deserializing content from the given byte buffer. * - * @param buf The buffer to deserialize from. + * @param buf the buffer to deserialize from */ public GlobalId(Deserializer buf) { raw = buf.getBytes(null, LENGTH); @@ -66,7 +66,7 @@ public GlobalId(Deserializer buf) { /** * Serializes the content of this global id into the given byte buffer. * - * @param buf The buffer to serialize to. + * @param buf the buffer to serialize to */ public void serialize(Serializer buf) { buf.put(null, raw); diff --git a/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/DocumentV1ApiHandler.java b/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/DocumentV1ApiHandler.java index 10e55527a2a9..7b3e488a5a58 100644 --- a/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/DocumentV1ApiHandler.java +++ b/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/DocumentV1ApiHandler.java @@ -214,7 +214,7 @@ public DocumentV1ApiHandler(Metric metric, this.operations = new ConcurrentLinkedDeque<>(); long resendDelayMS = SystemTimer.adjustTimeoutByDetectedHz(Duration.ofMillis(executorConfig.resendDelayMillis())).toMillis(); - //TODO Here it would be better do have dedicated threads with different wait depending on blocked or empty. + // TODO: Here it would be better do have dedicated threads with different wait depending on blocked or empty. this.dispatcher.scheduleWithFixedDelay(this::dispatchEnqueued, resendDelayMS, resendDelayMS, MILLISECONDS); this.visitDispatcher.scheduleWithFixedDelay(this::dispatchVisitEnqueued, resendDelayMS, resendDelayMS, MILLISECONDS); } From 0b59d84c3bdba4a6a4e9062de1afa6a8c0700cde Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Tue, 1 Mar 2022 15:50:43 +0100 Subject: [PATCH 2/2] Expect updated error message --- .../src/test/java/com/yahoo/document/DocumentIdTestCase.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java b/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java index 63a0f8d25ed2..0d3b07fd6eea 100644 --- a/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java +++ b/document/src/test/java/com/yahoo/document/DocumentIdTestCase.java @@ -266,8 +266,7 @@ public void testTooLongDocId() { new DocumentId(sb.toString()); fail("Expected an IllegalArgumentException to be thrown"); } catch (IllegalArgumentException ex) { - assertTrue(ex.getMessage().contains("However if you have already fed a document earlier on and want to remove it, " + - "you can do so by calling new DocumentId(IdString.createIdStringLessStrict()) that will bypass this restriction.")); + assertEquals("Document id length 65548 is longer than max length of 65536", ex.getMessage()); } assertEquals(65548, new DocumentId(IdString.createIdStringLessStrict(sb.toString())).toString().length()); }