Skip to content

Commit

Permalink
can take an empty function for default values
Browse files Browse the repository at this point in the history
  • Loading branch information
austin007008 committed Dec 6, 2024
1 parent a6df86f commit 057188b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ public class SummarySize implements Serializable {

private static final long serialVersionUID = 6769159729743311079L;

private static final int DEFAULT_SIZE = 150;
private static final Logger log = LoggerFactory.getLogger(SummarySize.class);

public static final String SIZE_PARAMETER = "SIZE";
public static final String VIEWS_PARAMETER = "VIEWS";

private static final int DEFAULT_SIZE = 150;

private int summarySize;
private ArrayList<String> contentNamesList;
private boolean only;
Expand All @@ -39,8 +43,8 @@ public SummarySize() {
* <ul>
* <li>Given null, null will be returned.</li>
* <li>Given an empty or blank string, an empty {@link SummarySize} will be returned.</li>
* <li>Given {@code SIZE:50/ONLY/NAMES:CONTENT1,CONTENT2}, an {@link SummarySize} will be returned with a size of 50 (size is number of characters), only using the
* specified content names, and list of content names of (CONTENT1, CONTENT2).
* <li>Given {@code SIZE:50/ONLY/NAMES:CONTENT1,CONTENT2}, an {@link SummarySize} will be returned with a size of 50 (size is number of characters), only
* using the specified content names, and list of content names of (CONTENT1, CONTENT2).
* <li>Given malformed input, will return an {@link SummarySize} with a size of 150.</li>
* </ul>
*
Expand All @@ -58,23 +62,21 @@ public static SummarySize from(String string) {

SummarySize summarySize = new SummarySize();

if (string.isEmpty()) {
if (string.isBlank()) {
summarySize.summarySize = DEFAULT_SIZE;
return summarySize;
}

try {
String[] parameterParts = string.split(Constants.FORWARD_SLASH);

for(String parameterPart : parameterParts) {
for (String parameterPart : parameterParts) {
String[] parts = parameterPart.split(Constants.COLON);
if(parts[0].equalsIgnoreCase("SIZE")){
if (parts[0].equalsIgnoreCase(SIZE_PARAMETER)) {
summarySize.summarySize = Integer.parseInt(parts[1]);
}
else if(parts[0].equalsIgnoreCase("ONLY")){
} else if (parts[0].equalsIgnoreCase("ONLY")) {
summarySize.only = true;
}
else if (parts[0].equalsIgnoreCase("NAMES")) {
} else if (parts[0].equalsIgnoreCase(VIEWS_PARAMETER)) {
String[] names = parts[1].split(Constants.COMMA);
for (String name : names) {
summarySize.contentNamesList.add(name.toUpperCase());
Expand Down Expand Up @@ -166,12 +168,12 @@ public static String[] contentNamesListFromString(String string) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("SIZE:").append(summarySize);
if(only) {
sb.append(SIZE_PARAMETER).append(":").append(summarySize);
if (only) {
sb.append("/").append("ONLY");
}
if(!contentNamesList.isEmpty()) {
sb.append("/NAMES:");
if (!contentNamesList.isEmpty()) {
sb.append("/").append(VIEWS_PARAMETER).append(":");
for (String contentName : contentNamesList) {
sb.append(contentName).append(Constants.COMMA);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ private static void verify(String name, int numArgs) {
case QueryOptionsFromQueryVisitor.UniqueFunction.UNIQUE_BY_YEAR_FUNCTION:
case QueryFunctions.GROUPBY_FUNCTION:
case QueryFunctions.EXCERPT_FIELDS_FUNCTION:
case QueryFunctions.SUMMARY_FUNCTION:
case QueryFunctions.MATCH_REGEX:
case QueryFunctions.INCLUDE_TEXT:
case QueryFunctions.NO_EXPANSION:
Expand All @@ -277,6 +276,8 @@ private static void verify(String name, int numArgs) {
throw new IllegalArgumentException("Expected at least one argument to the " + name + " function");
}
break;
case QueryFunctions.SUMMARY_FUNCTION:
break;
default:
throw new IllegalArgumentException("Unknown Query function: " + name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,13 @@ public SummarySize() {

@Override
public void validate() throws IllegalArgumentException {
if (this.parameterList.isEmpty()) {
String parameters = this.parameterList.isEmpty() ? "" : String.join(",", parameterList);
try {
datawave.query.attributes.SummarySize.from(parameters);
} catch (Exception e) {
BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.INVALID_FUNCTION_ARGUMENTS,
MessageFormat.format("{0} requires at least one argument", this.name));
MessageFormat.format("Unable to parse summary options from arguments for function {0}", this.name));
throw new IllegalArgumentException(qe);
} else {
String parameters = String.join(",", parameterList);
try {
datawave.query.attributes.SummarySize.from(parameters);
} catch (Exception e) {
BadRequestQueryException qe = new BadRequestQueryException(DatawaveErrorCode.INVALID_FUNCTION_ARGUMENTS,
MessageFormat.format("Unable to parse summary options from arguments for function {0}", this.name));
throw new IllegalArgumentException(qe);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void testWithOnly() throws Exception {
extraParameters.put("return.fields", "CONTENT_SUMMARY");
extraParameters.put("query.syntax", "LUCENE");

String queryString = "QUOTE:(farther) #SUMMARY_SIZE(NAMES:CONTENT/SIZE:50/ONLY)";
String queryString = "QUOTE:(farther) #SUMMARY_SIZE(VIEWS:CONTENT/SIZE:50/ONLY)";

// not sure why the timestamp and delete flag are present
Set<String> goodResults = new HashSet<>(
Expand All @@ -224,14 +224,30 @@ public void testWithOnly() throws Exception {
runTestQuery(queryString, format.parse("19000101"), format.parse("20240101"), extraParameters, goodResults);
}

@Test
public void testWithNoArg() throws Exception {
Map<String,String> extraParameters = new HashMap<>();
extraParameters.put("include.grouping.context", "true");
extraParameters.put("return.fields", "CONTENT_SUMMARY");
extraParameters.put("query.syntax", "LUCENE");

String queryString = "QUOTE:(farther) #SUMMARY_SIZE()";

// not sure why the timestamp and delete flag are present
Set<String> goodResults = new HashSet<>(Set.of(
"CONTENT_SUMMARY:CONTENT: You can get much farther with a kind word and a gun than you can with a kind word alone: : [] 9223372036854775807 false"));

runTestQuery(queryString, format.parse("19000101"), format.parse("20240101"), extraParameters, goodResults);
}

@Test
public void testWithoutOnly() throws Exception {
Map<String,String> extraParameters = new HashMap<>();
extraParameters.put("include.grouping.context", "true");
extraParameters.put("return.fields", "CONTENT_SUMMARY");
extraParameters.put("query.syntax", "LUCENE");

String queryString = "QUOTE:(farther) #SUMMARY_SIZE(SIZE:50/NAMES:CONTENT)";
String queryString = "QUOTE:(farther) #SUMMARY_SIZE(SIZE:50/VIEWS:CONTENT)";

// not sure why the timestamp and delete flag are present
Set<String> goodResults = new HashSet<>(
Expand Down Expand Up @@ -294,7 +310,7 @@ public void testNoContentFound() throws Exception {
extraParameters.put("return.fields", "CONTENT_SUMMARY");
extraParameters.put("query.syntax", "LUCENE");

String queryString = "QUOTE:(farther) #SUMMARY_SIZE(SIZE:50/ONLY/NAMES:CANTFINDME,ORME)";
String queryString = "QUOTE:(farther) #SUMMARY_SIZE(SIZE:50/ONLY/VIEWS:CANTFINDME,ORME)";

// not sure why the timestamp and delete flag are present
Set<String> goodResults = new HashSet<>(Set.of("CONTENT_SUMMARY:NO CONTENT FOUND TO SUMMARIZE"));
Expand Down

0 comments on commit 057188b

Please sign in to comment.