mirror of
https://github.com/nosqlbench/nosqlbench.git
synced 2025-02-20 11:38:28 -06:00
sketch of predicate ast
This commit is contained in:
parent
2cf277fe8a
commit
135c1893cb
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.predicates;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateExpr;
|
||||
import io.nosqlbench.virtdata.predicates.types.PredicateSerDes;
|
||||
|
||||
public class JsonPredicateSerDes implements PredicateSerDes {
|
||||
private final static Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||
private final static TypeToken<PredicateExpr> pexprType = TypeToken.get(PredicateExpr.class);
|
||||
|
||||
@Override
|
||||
public PredicateExpr unserialize(String predicateData) {
|
||||
PredicateExpr expr = gson.fromJson(predicateData, pexprType);
|
||||
return expr;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String serialize(PredicateExpr predicateExpr) {
|
||||
return gson.toJson(predicateExpr);
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
/*
|
||||
* 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.predicates.adapter;
|
||||
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateAndExpr;
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateExpr;
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateOrExpr;
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateTerm;
|
||||
import io.nosqlbench.virtdata.predicates.types.PredicateAdapter;
|
||||
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* <P>This predicate adapter is not based on any real type of system.
|
||||
* It provides the ability to render a string-form predicate clause
|
||||
* for an imaginary system. This can be used for integrated testing
|
||||
* and validation of high level configurations in the
|
||||
* hands of users, like the diag adapter.</P>
|
||||
*
|
||||
* <P>The vernacular for this is simply JSON in the most obvious format,
|
||||
* using verbs and nouns which are familiar from classic SQL systems.</P>
|
||||
*/
|
||||
public class ExamplePredicateAdapter implements PredicateAdapter {
|
||||
|
||||
@Override
|
||||
public String getPredicate(PredicateExpr model) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String fragment = switch (model) {
|
||||
case PredicateTerm pt -> renderTerm(pt);
|
||||
case PredicateAndExpr pae -> renderTerm(pae);
|
||||
case PredicateOrExpr po -> renderTerm(po);
|
||||
default -> throw new IllegalStateException("Unexpected value: " + model);
|
||||
};
|
||||
sb.append(fragment);
|
||||
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String renderTerm(PredicateTerm pt) {
|
||||
String value = pt.field.name + " " + pt.operator.name() + " " + pt.comparator.value;
|
||||
return value;
|
||||
}
|
||||
|
||||
private String renderTerm(PredicateAndExpr pae) {
|
||||
String value = pae.terms.stream().map(this::renderTerm).collect(Collectors.joining(" and "));
|
||||
return value;
|
||||
}
|
||||
|
||||
private String renderTerm(PredicateOrExpr poe) {
|
||||
String value = poe.terms.stream().map(this::renderTerm).collect(Collectors.joining(" or "));
|
||||
return value;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
public class PComparator {
|
||||
public Object value;
|
||||
|
||||
public PComparator(Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
public class PField {
|
||||
public String name;
|
||||
|
||||
public PField(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
public enum POperator {
|
||||
eq,
|
||||
gt,
|
||||
lt
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PredicateAndExpr implements PredicateExpr {
|
||||
public final List<PredicateTerm> terms = new ArrayList<>();
|
||||
|
||||
public PredicateAndExpr() {
|
||||
|
||||
}
|
||||
public PredicateAndExpr(List<PredicateTerm> terms) {
|
||||
this.terms.addAll(terms);
|
||||
}
|
||||
|
||||
public PredicateAndExpr term(PredicateTerm term) {
|
||||
this.terms.add(term);
|
||||
return this;
|
||||
}
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
public interface PredicateExpr {
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PredicateOrExpr implements PredicateExpr {
|
||||
public List<PredicateTerm> terms;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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.predicates.ast;
|
||||
|
||||
public class PredicateTerm implements PredicateExpr {
|
||||
public PredicateTerm(PField field, POperator operator, PComparator value) {
|
||||
this.field = field;
|
||||
this.operator = operator;
|
||||
this.comparator = value;
|
||||
}
|
||||
|
||||
public PField field;
|
||||
public POperator operator;
|
||||
public PComparator comparator;
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* <P>The predicates module defines a few key elements which work together to
|
||||
* allow driver adapters to share a common structure for predicate forms.
|
||||
* These include:
|
||||
* <UL>
|
||||
* <LI>{@link io.nosqlbench.virtdata.predicates.ast.PredicateExpr} - an
|
||||
* abstract syntax which captures the range of predicate forms which are supported.</LI>
|
||||
* <LI>{@link io.nosqlbench.virtdata.predicates.types.PredicateAdapter} - an
|
||||
* adapter type which can allow individual driver adapters to create a compatible
|
||||
* predicate clause or expression object from a
|
||||
* {@link io.nosqlbench.virtdata.predicates.ast.PredicateExpr}</LI>
|
||||
* <LI>{@link io.nosqlbench.virtdata.predicates.types.PredicateSerDes}
|
||||
* - serialization and deserialization functionality which allows
|
||||
* the core machinery of NB to read the common format to and from the
|
||||
* {@link io.nosqlbench.virtdata.predicates.ast.PredicateExpr} form.</LI>
|
||||
* </UL>
|
||||
* </P>
|
||||
*/
|
||||
package io.nosqlbench.virtdata.predicates;
|
@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.predicates.types;
|
||||
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateExpr;
|
||||
|
||||
/**
|
||||
* PredicateAdapters know how to read the predicate abstract syntax
|
||||
* representation (in the form of a {@link PredicateExpr} and all it entails)
|
||||
* and render a protocol-specific form. (adapter or system vernacular).
|
||||
* The initial version presumed that a string form suffice for most cases,
|
||||
* although this interface should be generified or specialized when needed to
|
||||
* support other native in-memory representations, such as those which are
|
||||
* based on native driver APIs and their direct types.
|
||||
*/
|
||||
public interface PredicateAdapter {
|
||||
String getPredicate(PredicateExpr model);
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
/*
|
||||
* 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.predicates.types;
|
||||
|
||||
import io.nosqlbench.virtdata.predicates.ast.PredicateExpr;
|
||||
|
||||
public interface PredicateSerDes {
|
||||
public PredicateExpr unserialize(String predicateData);
|
||||
public String serialize(PredicateExpr predicateExpr);
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* 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.predicates.adapter;
|
||||
|
||||
import io.nosqlbench.virtdata.predicates.ast.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class ExamplePredicateAdapterTest {
|
||||
|
||||
public static PredicateExpr exampleExpr =
|
||||
new PredicateAndExpr()
|
||||
.term(
|
||||
new PredicateTerm(new PField("username"), POperator.eq,new PComparator("joe"))
|
||||
);
|
||||
@Test
|
||||
public void testBasicAdapterExample() {
|
||||
ExamplePredicateAdapter epa = new ExamplePredicateAdapter();
|
||||
String nativeForm = epa.getPredicate(exampleExpr);
|
||||
assertThat(nativeForm)
|
||||
.isEqualTo("username eq joe");
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user