Skip to content

Commit

Permalink
Merge pull request #19509 from vespa-engine/jonmv/fix-docid-parse-bug
Browse files Browse the repository at this point in the history
Fix document id parse bug in feed client
  • Loading branch information
jonmv authored Oct 11, 2021
2 parents f889f5f + 6587de9 commit f3e402c
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,4 +514,4 @@ private static StorageNode createStorageNode(DeployState deployState, ContentClu
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,34 @@ public static DocumentId of(String namespace, String documentType, String group,
}

public static DocumentId of(String serialized) {
String[] parts = serialized.split(":");
while (parts.length >= 5 && parts[0].equals("id")) {
if (parts[3].startsWith("n="))
return DocumentId.of(parts[1], parts[2], Long.parseLong(parts[3]), parts[4]);
if (parts[3].startsWith("g="))
return DocumentId.of(parts[1], parts[2], parts[3], parts[4]);
else if (parts[3].isEmpty())
return DocumentId.of(parts[1], parts[2], parts[4]);
}
DocumentId parsed = parse(serialized);
if (parsed != null) return parsed;
throw new IllegalArgumentException("Document ID must be on the form " +
"'id:<namespace>:<document-type>:[n=number|g=group]:<user-specific>', " +
"'id:<namespace>:<document-type>:[n=<number>|g=<group>]:<user-specific>', " +
"but was '" + serialized + "'");
}

private static DocumentId parse(String serialized) {
int i, j = -1;
if ((j = serialized.indexOf(':', i = j + 1)) < i) return null;
if ( ! "id".equals(serialized.substring(i, j))) return null;
if ((j = serialized.indexOf(':', i = j + 1)) <= i) return null;
String namespace = serialized.substring(i, j);
if ((j = serialized.indexOf(':', i = j + 1)) <= i) return null;
String documentType = serialized.substring(i, j);
if ((j = serialized.indexOf(':', i = j + 1)) < i) return null;
String group = serialized.substring(i, j);
if (serialized.length() <= (i = j + 1)) return null;
String userSpecific = serialized.substring(i);
if (group.startsWith("n=") && group.length() > 2)
return DocumentId.of(namespace, documentType, Long.parseLong(group.substring(2)), userSpecific);
if (group.startsWith("g=") && group.length() > 2)
return DocumentId.of(namespace, documentType, group.substring(2), userSpecific);
if (group.isEmpty())
return DocumentId.of(namespace, documentType, userSpecific);
return null;
}

public String documentType() {
return documentType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void failure(HttpResponse response) {

@Override
public void failure(Throwable cause) {
failure(cause.getMessage());
failure(cause.toString());
}

private void failure(String detail) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* @author jonmv
*/
class DocumentIdTest {

@Test
void testParsing() {
assertEquals("id:ns:type::user",
DocumentId.of("id:ns:type::user").toString());

assertEquals("id:ns:type:n=123:user",
DocumentId.of("id:ns:type:n=123:user").toString());

assertEquals("id:ns:type:g=foo:user",
DocumentId.of("id:ns:type:g=foo:user").toString());

assertEquals("id:ns:type::user::specific",
DocumentId.of("id:ns:type::user::specific").toString());

assertEquals("id:ns:type:::",
DocumentId.of("id:ns:type:::").toString());

assertThrows(IllegalArgumentException.class,
() -> DocumentId.of("idd:ns:type:user"));

assertThrows(IllegalArgumentException.class,
() -> DocumentId.of("id:ns::user"));

assertThrows(IllegalArgumentException.class,
() -> DocumentId.of("id::type:user"));

assertThrows(IllegalArgumentException.class,
() -> DocumentId.of("id:ns:type:g=:user"));

assertThrows(IllegalArgumentException.class,
() -> DocumentId.of("id:ns:type:n=:user"));

assertThrows(NumberFormatException.class,
() -> DocumentId.of("id:ns:type:n=foo:user"));

assertThrows(IllegalArgumentException.class,
() -> DocumentId.of("id:ns:type::"));
}

}

0 comments on commit f3e402c

Please sign in to comment.