mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-20 11:38:28 -06:00
adding single condition filter
This commit is contained in:
parent
cc5978c98c
commit
31459a9ab1
@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Copyright (c) 2024 nosqlbench
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package io.nosqlbench.virtdata.library.hdf5.from_long.to_string.predicate_parser.from_json;
|
||||
|
||||
import com.google.gson.JsonObject;
|
||||
import io.nosqlbench.virtdata.library.hdf5.from_long.to_string.predicate_parser.DatasetFilter;
|
||||
|
||||
public class SingleConditionFilterByKeyword implements DatasetFilter {
|
||||
private final String filterString;
|
||||
private static final String AND = "and";
|
||||
private static final String OR = "or";
|
||||
private static final String FIELD = "field";
|
||||
private static final String OPERATOR = "operator";
|
||||
private static final String COMPARATOR = "comparator";
|
||||
private static final String COMMA = ",";
|
||||
private static final String VALUE = "value";
|
||||
|
||||
public SingleConditionFilterByKeyword(String filterString) {
|
||||
this.filterString = filterString;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String applyFilter(JsonObject json) {
|
||||
if (json.has(AND)) {
|
||||
return parseConditions(json, AND);
|
||||
} else if (json.has(OR)) {
|
||||
return parseConditions(json, OR);
|
||||
} else {
|
||||
throw new RuntimeException("Unknown predicate type: " + json.keySet());
|
||||
}
|
||||
}
|
||||
|
||||
private String parseConditions(JsonObject json, String conditionType) {
|
||||
return switch (filterString) {
|
||||
case FIELD ->
|
||||
json.get(conditionType).getAsJsonArray().get(0).getAsJsonObject().keySet().stream().findFirst()
|
||||
.get().replaceAll("\"", "");
|
||||
case OPERATOR ->
|
||||
json.get(conditionType).getAsJsonArray().get(0).getAsJsonObject().entrySet().stream().findFirst()
|
||||
.get().getValue().getAsJsonObject().keySet().stream().findFirst().get().replaceAll("\"", "");
|
||||
case COMPARATOR ->
|
||||
json.get(conditionType).getAsJsonArray().get(0).getAsJsonObject().entrySet().stream().findFirst()
|
||||
.get().getValue().getAsJsonObject().entrySet().stream().findFirst().get().getValue().getAsJsonObject()
|
||||
.get(VALUE).toString().replaceAll("\"", "");
|
||||
default -> throw new RuntimeException("Unknown filter string: " + filterString);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -17,6 +17,7 @@
|
||||
|
||||
package io.nosqlbench.virtdata.library.hdf5.from_long.to_string.predicate_parser.from_json.to_pineconefilter;
|
||||
|
||||
import io.nosqlbench.virtdata.library.hdf5.from_long.to_string.predicate_parser.from_json.MultiConditionFilterByKeyword;
|
||||
import io.nosqlbench.virtdata.library.hdf5.from_long.to_string.predicate_parser.from_json.MultiConditionFilterByLevel;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@ -24,28 +25,29 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class PineconeFilterParserTest {
|
||||
|
||||
private static String test1 = "{\n" +
|
||||
" \"conditions\": {\n" +
|
||||
" \"and\": [\n" +
|
||||
" {\n" +
|
||||
" \"department_name\": {\n" +
|
||||
" \"EQ\": {\n" +
|
||||
" \"value\": \"Divided Shoes\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" },\n" +
|
||||
" {\n" +
|
||||
" \"department_type\": {\n" +
|
||||
" \"EQ\": {\n" +
|
||||
" \"value\": \"Footwear\"\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" }\n" +
|
||||
" ]\n" +
|
||||
" }\n" +
|
||||
"}";
|
||||
private static final String test1 = """
|
||||
{
|
||||
"conditions": {
|
||||
"and": [
|
||||
{
|
||||
"department_name": {
|
||||
"EQ": {
|
||||
"value": "Divided Shoes"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"department_type": {
|
||||
"EQ": {
|
||||
"value": "Footwear"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
}""";
|
||||
@Test
|
||||
public void testComparatorParse() {
|
||||
public void testComparatorParseByLevel() {
|
||||
PineconeFilterParser parser = new PineconeFilterParser();
|
||||
MultiConditionFilterByLevel mcf = new MultiConditionFilterByLevel(3, true);
|
||||
parser.setFilter(mcf);
|
||||
@ -54,7 +56,16 @@ public class PineconeFilterParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldParse() {
|
||||
public void testComparatorParseByKeyword() {
|
||||
PineconeFilterParser parser = new PineconeFilterParser();
|
||||
MultiConditionFilterByKeyword mcf = new MultiConditionFilterByKeyword("comparator");
|
||||
parser.setFilter(mcf);
|
||||
String parsed = parser.parse(test1);
|
||||
assertEquals("Divided Shoes,Footwear", parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFieldParseByLevel() {
|
||||
PineconeFilterParser parser = new PineconeFilterParser();
|
||||
MultiConditionFilterByLevel mcf = new MultiConditionFilterByLevel(1, false);
|
||||
parser.setFilter(mcf);
|
||||
@ -63,7 +74,16 @@ public class PineconeFilterParserTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOperatorParse() {
|
||||
public void testFieldParseByKeyword() {
|
||||
PineconeFilterParser parser = new PineconeFilterParser();
|
||||
MultiConditionFilterByKeyword mcf = new MultiConditionFilterByKeyword("field");
|
||||
parser.setFilter(mcf);
|
||||
String parsed = parser.parse(test1);
|
||||
assertEquals("department_name,department_type", parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOperatorParseByLevel() {
|
||||
PineconeFilterParser parser = new PineconeFilterParser();
|
||||
MultiConditionFilterByLevel mcf = new MultiConditionFilterByLevel(2, false);
|
||||
parser.setFilter(mcf);
|
||||
@ -71,4 +91,13 @@ public class PineconeFilterParserTest {
|
||||
assertEquals("EQ,EQ", parsed);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOperatorParseByKeyword() {
|
||||
PineconeFilterParser parser = new PineconeFilterParser();
|
||||
MultiConditionFilterByKeyword mcf = new MultiConditionFilterByKeyword("operator");
|
||||
parser.setFilter(mcf);
|
||||
String parsed = parser.parse(test1);
|
||||
assertEquals("EQ,EQ", parsed);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user