From 55bec2d65fe5c5a8dccb5f8a0b2d99340e7ef8e4 Mon Sep 17 00:00:00 2001 From: Mark Wolters Date: Tue, 11 Mar 2025 09:34:36 -0400 Subject: [PATCH] Mwolters/metadata (#2166) * add processing of data type metadata * switching internal classes to nbvector lib * cleanup and adding comments --- nb-virtdata/virtdata-lib-hdf5/pom.xml | 6 + .../buildhdf5/predicates/types/BBWriter.java | 23 --- .../predicates/types/ConjugateNode.java | 71 ---------- .../predicates/types/ConjugateType.java | 23 --- .../predicates/types/NodeRepresenter.java | 22 --- .../buildhdf5/predicates/types/OpType.java | 37 ----- .../buildhdf5/predicates/types/PNode.java | 30 ---- .../predicates/types/PredicateNode.java | 96 ------------- .../AbstractHdfFileToVectorType.java | 56 ++++++-- .../hdf5/helpers/HdfAttributesProcessor.java | 23 ++- .../nodewalk/repr/CqlNodeRenderer.java | 132 +++++++++++------- .../nodewalk/repr/H5JsonNodeRenderer.java | 73 ++++++---- .../predicates/nodewalk/PredicateTest.java | 18 +-- .../nodewalk/repr/H5JsonNodeRendererTest.java | 32 ----- 14 files changed, 212 insertions(+), 430 deletions(-) delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/BBWriter.java delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateNode.java delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateType.java delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/NodeRepresenter.java delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/OpType.java delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PNode.java delete mode 100644 nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PredicateNode.java diff --git a/nb-virtdata/virtdata-lib-hdf5/pom.xml b/nb-virtdata/virtdata-lib-hdf5/pom.xml index c1dd263e6..34eec6149 100644 --- a/nb-virtdata/virtdata-lib-hdf5/pom.xml +++ b/nb-virtdata/virtdata-lib-hdf5/pom.xml @@ -45,6 +45,12 @@ 0.9.0 + + io.nosqlbench + nbvectors + 0.1.1 + + diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/BBWriter.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/BBWriter.java deleted file mode 100644 index 9da78b0e4..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/BBWriter.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -import java.nio.ByteBuffer; - -public interface BBWriter { - ByteBuffer encode(ByteBuffer out); -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateNode.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateNode.java deleted file mode 100644 index c776cc476..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateNode.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -import java.nio.ByteBuffer; -import java.util.Arrays; - -public record ConjugateNode(ConjugateType type, PNode... values) - implements BBWriter, PNode -{ - - public ConjugateNode(ByteBuffer b) { - this(ConjugateType.values()[b.get()], readValues(b)); - } - - private static PNode[] readValues(ByteBuffer b) { - byte count = b.get(); - PNode[] elements = new PNode[count]; - for (int i = 0; i < elements.length; i++) { - elements[i] = PNode.fromBuffer(b); - } - return elements; - } - - @Override - public ByteBuffer encode(ByteBuffer out) { - out.put((byte) this.type.ordinal()).put((byte) this.values.length); - for (PNode element : values) { - element.encode(out); - } - return out; - } - - @Override - public String toString() { - final StringBuffer sb = new StringBuffer("ConjugateNode{"); - sb.append("type=").append(type); - sb.append(", v=").append(values == null ? "null" : Arrays.asList(values).toString()); - sb.append('}'); - return sb.toString(); - } - - @Override - public boolean equals(Object o) { - if (!(o instanceof ConjugateNode(ConjugateType type1, PNode[] values1))) - return false; - - return Arrays.equals(values, values1) && type == type1; - } - - @Override - public int hashCode() { - int result = type.hashCode(); - result = 31 * result + Arrays.hashCode(values); - return result; - } -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateType.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateType.java deleted file mode 100644 index 75bff88fd..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/ConjugateType.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -public enum ConjugateType { - PRED, - AND, - OR -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/NodeRepresenter.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/NodeRepresenter.java deleted file mode 100644 index da60775cc..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/NodeRepresenter.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -import java.util.function.Function; - -public interface NodeRepresenter extends Function, String> { -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/OpType.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/OpType.java deleted file mode 100644 index a1fbf857d..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/OpType.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -public enum OpType { - GT(">"), - LT("<"), - EQ("="), - NE("!="), - GE(">="), - LE("<="), - IN("IN"); - - private final String symbol; - - OpType(String symbol) { - this.symbol=symbol; - } - - public String symbol() { - return this.symbol; - } -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PNode.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PNode.java deleted file mode 100644 index d47c500bf..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PNode.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -import java.nio.ByteBuffer; - -public sealed interface PNode extends BBWriter permits ConjugateNode, PredicateNode { - public static PNode fromBuffer(ByteBuffer b) { - byte typeOrdinal = b.get(b.position()); - return switch(ConjugateType.values()[typeOrdinal]) { - case AND, OR -> new ConjugateNode(b); - case PRED -> new PredicateNode(b); - }; - } -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PredicateNode.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PredicateNode.java deleted file mode 100644 index cb39d3fa6..000000000 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/nbvectors/buildhdf5/predicates/types/PredicateNode.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright (c) 2025 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.nbvectors.buildhdf5.predicates.types; - -import java.nio.ByteBuffer; -import java.util.Arrays; -import java.util.Objects; - -public record PredicateNode( - /* encoded as byte */ - int field, - /* encoded as byte */ - OpType op, - /* encoded as short len, and then longs */ - long... v -) implements BBWriter, PNode -{ - public PredicateNode(byte type, int field, OpType op, long... v) { - this(field, op, v); - } - public PredicateNode(ByteBuffer b) { - this( - b.get(), - b.get(), - OpType.values()[b.get()], - readValues(b) - ); - } - - private static long[] readValues(ByteBuffer b) { - int len = b.getShort(); - long[] values = new long[len]; - for (int i = 0; i < values.length; i++) { - values[i] = b.getLong(); - } - return values; - } - - @Override - public ByteBuffer encode(ByteBuffer out) { - out.put((byte) ConjugateType.PRED.ordinal()); - out.put((byte) field).put((byte) op.ordinal()); - out.putShort((short) v.length); - for (long l : v) { - out.putLong(l); - } - return out; - } - - @Override - public boolean equals(Object o) { - if (!(o instanceof PredicateNode(int fieldOffset, OpType op1, long[] v1))) - return false; - return field == fieldOffset && Arrays.equals(v, v1) && op == op1; - } - - @Override - public int hashCode() { - int result = field; - result = 31 * result + Objects.hashCode(op); - result = 31 * result + Arrays.hashCode(v); - return result; - } - - @Override - public String toString() { - final StringBuffer sb = new StringBuffer("PredicateNode{"); - sb.append("field=").append(field); - sb.append(", op=").append(op); - sb.append(", v="); - if (v == null) - sb.append("null"); - else { - sb.append('['); - for (int i = 0; i < v.length; ++i) - sb.append(i == 0 ? "" : ", ").append(v[i]); - sb.append(']'); - } - sb.append('}'); - return sb.toString(); - } -} diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/from_long/AbstractHdfFileToVectorType.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/from_long/AbstractHdfFileToVectorType.java index 4dce74db3..dea0ad13e 100644 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/from_long/AbstractHdfFileToVectorType.java +++ b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/from_long/AbstractHdfFileToVectorType.java @@ -28,19 +28,40 @@ import org.apache.logging.log4j.Logger; import java.util.Map; +/** + * Abstract base class for reading data from HDF5 files. + * Provides common functionality for accessing and processing HDF5 datasets and their attributes. + */ public abstract class AbstractHdfFileToVectorType implements HdfAttributesProcessor { - protected final HdfFile hdfFile; - protected final Dataset dataset; - protected final int[] dims; - private final static Logger logger = LogManager.getLogger(AbstractHdfFileToVectorType.class); + protected final HdfFile hdfFile; // The HDF5 file handle + protected final Dataset dataset; // The specific dataset within the HDF5 file + protected final String datasetName; // Name/path of the dataset + protected final int[] dims; // Dimensions of the dataset + protected String dataType; // Data type of the dataset + protected final static Logger logger = LogManager.getLogger(AbstractHdfFileToVectorType.class); + /** + * Constructs a new instance for reading data from an HDF5 file. + * + * @param filename The name of the HDF5 file to read + * @param datasetName The path to the dataset within the HDF5 file + */ public AbstractHdfFileToVectorType(String filename, String datasetName) { hdfFile = new HdfFile(NBIO.all().search(filename).one().asPath()); - processAttributes(hdfFile); + this.datasetName = datasetName; dataset = hdfFile.getDatasetByPath(datasetName); dims = dataset.getDimensions(); + processAttributes(hdfFile); + this.dataType = (dataType == null) ? dataset.getJavaType().getSimpleName().toLowerCase() : dataType; } + /** + * Retrieves data from the dataset at a specific position. + * For multi-dimensional datasets, returns a slice of data. + * + * @param l The position to read from + * @return The data at the specified position + */ protected Object getDataFrom(long l) { long[] sliceOffset = new long[dims.length]; sliceOffset[0] = (l % dims[0]); @@ -55,14 +76,24 @@ public abstract class AbstractHdfFileToVectorType implements HdfAttributesProces } } + /** + * Processes attributes of the HDF5 file by traversing its node hierarchy. + * + * @param hdfFile The HDF5 file to process + */ @Override public void processAttributes(HdfFile hdfFile) { processNode(hdfFile); } - private static void processNode(Node node) { + /** + * Recursively processes a node and its children in the HDF5 file hierarchy. + * + * @param node The node to process + */ + private void processNode(Node node) { logger.info("Node: {}", node.getPath()); - printAttributes(node); + readAttributes(node); // If the node is a group, iterate through its children if (node instanceof Group group) { @@ -72,7 +103,13 @@ public abstract class AbstractHdfFileToVectorType implements HdfAttributesProces } } - private static void printAttributes(Node node) { + /** + * Reads and processes attributes of a node, updating the dataType if a + * COMPONENT_ENCODING attribute is found for the target dataset. + * + * @param node The node whose attributes should be read + */ + private void readAttributes(Node node) { Map attributes = node.getAttributes(); if (attributes.isEmpty()) { logger.info(() -> "No attributes"); @@ -80,6 +117,9 @@ public abstract class AbstractHdfFileToVectorType implements HdfAttributesProces logger.info(() -> "Attributes:"); for (Map.Entry entry : attributes.entrySet()) { logger.info("{} = {}", entry.getKey(), entry.getValue().getData()); + if (entry.getKey().equals(COMPONENT_ENCODING) && (node.getPath().equalsIgnoreCase(datasetName))) { + dataType = entry.getValue().getData().toString(); + } } } } diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/helpers/HdfAttributesProcessor.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/helpers/HdfAttributesProcessor.java index 5ad10636f..3259b6a31 100644 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/helpers/HdfAttributesProcessor.java +++ b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/library/hdf5/helpers/HdfAttributesProcessor.java @@ -19,8 +19,29 @@ package io.nosqlbench.virtdata.library.hdf5.helpers; import io.jhdf.HdfFile; +/** + * Interface for processing attributes from HDF5 files. + * Defines constants for common HDF5 dataset and attribute names used in nosqlbench contexts. + */ public interface HdfAttributesProcessor { + /** Number of nearest neighbors */ + public static final String NEIGHBORS = "neighbors"; + /** Dimensionality of the vector space */ + public static final String DIMENSIONS = "dimensions"; + /** Training dataset vectors */ + public static final String TRAIN_VECTORS = "train_vectors"; + /** Test dataset vectors */ + public static final String TEST_VECTORS = "test_vectors"; + /** Machine learning model information */ + public static final String MODEL = "model"; + /** Distance metric used for similarity calculations */ + public static final String DISTANCE_FUNCTION = "distance_function"; + /** Encoding format for vector components */ + public static final String COMPONENT_ENCODING = "component_encoding"; + /** + * Process attributes from an HDF5 file. + * @param hdfFile The HDF5 file to process attributes from + */ public void processAttributes(HdfFile hdfFile); - } diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/CqlNodeRenderer.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/CqlNodeRenderer.java index e947c9945..2d2aff011 100644 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/CqlNodeRenderer.java +++ b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/CqlNodeRenderer.java @@ -21,66 +21,104 @@ import io.nosqlbench.nbvectors.buildhdf5.predicates.types.NodeRepresenter; import io.nosqlbench.nbvectors.buildhdf5.predicates.types.PNode; import io.nosqlbench.nbvectors.buildhdf5.predicates.types.PredicateNode; +/** + * Renders predicate nodes into CQL-compatible string representations. + * This class implements NodeRepresenter to convert different types of nodes + * into their corresponding CQL syntax. + */ public class CqlNodeRenderer implements NodeRepresenter { + /** Schema field names used for rendering */ private final String[] schema; + /** Constant for space character used in string building */ + public static final String SPACE = " "; + /** + * Creates a new CQL node renderer with the specified schema. + * @param schema Array of field names used for rendering predicates + */ public CqlNodeRenderer(String[] schema) { this.schema = schema; } + /** + * Converts a predicate node into its CQL string representation. + * @param node The node to convert + * @return CQL string representation of the node + */ @Override - public String apply(PNode node) { - return switch (node) { - case ConjugateNode n -> reprConjugate(n); - case PredicateNode p -> reprPredicate(p); - }; - } - - private String reprPredicate(PredicateNode p) { - StringBuilder sb = new StringBuilder(); - sb.append(schema[p.field()]); - if (isChar(p.op().symbol())) { - sb.append(" "); + public String apply(PNode node) { + return switch (node) { + case ConjugateNode n -> reprConjugate(n); + case PredicateNode p -> reprPredicate(p); + }; } - sb.append(p.op().symbol()); - if (p.v().length>1) { - sb.append("("); - String delim=""; - for (long v : p.v()) { - sb.append(delim); - sb.append(v); - delim=","; - } - sb.append(")"); - } else { - sb.append(p.v()[0]); + + /** + * Renders a predicate node into CQL syntax. + * @param p The predicate node to render + * @return CQL representation like "field_name operator value" + */ + private String reprPredicate(PredicateNode p) { + StringBuilder sb = new StringBuilder(); + sb.append(schema[p.field()]); + if (isChar(p.op().symbol())) { + sb.append(SPACE); + } + sb.append(p.op().symbol()); + if (p.v().length>1) { + sb.append("("); + String delim=""; + for (long v : p.v()) { + sb.append(delim); + sb.append(v); + delim=","; + } + sb.append(")"); + } else { + sb.append(p.v()[0]); + } + return sb.toString(); } - return sb.toString(); - } - private boolean isChar(String symbol) { - char c = symbol.charAt(0); - return (c >= 'A' && c <= 'Z') || (c>='a' && c<='z'); - } + /** + * Checks if the first character of a symbol is alphabetic. + * @param symbol The symbol to check + * @return true if first character is a letter, false otherwise + */ + private boolean isChar(String symbol) { + char c = symbol.charAt(0); + return (c >= 'A' && c <= 'Z') || (c>='a' && c<='z'); + } - private String reprConjugate(ConjugateNode n) { - return switch (n.type()) { - case AND,OR -> concatenate(n.type().name(),n.values()); - case PRED -> throw new RuntimeException("impossible unless broken code"); - }; - } + /** + * Renders a conjugate node (AND/OR combinations) into CQL syntax. + * @param n The conjugate node to render + * @return CQL representation of the conjugated predicates + */ + private String reprConjugate(ConjugateNode n) { + return switch (n.type()) { + case AND,OR -> concatenate(n.type().name(),n.values()); + case PRED -> throw new RuntimeException("impossible unless broken code"); + }; + } - private String concatenate(String name, PNode[] values) { - StringBuilder sb = new StringBuilder(); + /** + * Concatenates multiple nodes with the specified operator. + * @param name The operator name (AND/OR) + * @param values The nodes to concatenate + * @return Space-separated string of nodes joined by the operator + */ + private String concatenate(String name, PNode[] values) { + StringBuilder sb = new StringBuilder(); - for (PNode value : values) { - String nodeRep = apply(value); - if (!sb.isEmpty()) { - sb.append(" ").append(name).append(" "); - } - sb.append(nodeRep); - } - return sb.toString(); - } + for (PNode value : values) { + String nodeRep = apply(value); + if (!sb.isEmpty()) { + sb.append(SPACE).append(name).append(SPACE); + } + sb.append(nodeRep); + } + return sb.toString(); + } } diff --git a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRenderer.java b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRenderer.java index 5fbc1e317..318298107 100644 --- a/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRenderer.java +++ b/nb-virtdata/virtdata-lib-hdf5/src/main/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRenderer.java @@ -23,46 +23,57 @@ import io.nosqlbench.nbvectors.buildhdf5.predicates.types.NodeRepresenter; import io.nosqlbench.nbvectors.buildhdf5.predicates.types.PNode; import io.nosqlbench.nbvectors.buildhdf5.predicates.types.PredicateNode; +/** + * Renders HDF5 predicate nodes as JSON strings. + * This class implements NodeRepresenter to provide JSON string representations + * of predicate nodes used in HDF5 operations. + */ public class H5JsonNodeRenderer implements NodeRepresenter { + /** Schema information used for rendering */ private final String[] schema; + /** Gson instance configured for pretty printing */ private final static Gson gson = new GsonBuilder().setPrettyPrinting().create(); + /** + * Creates a new renderer with the specified schema. + * + * @param schema Array of strings representing the schema structure + */ public H5JsonNodeRenderer(String[] schema) { this.schema = schema; } + /** + * Converts a predicate node to its JSON string representation. + * + * @param node The predicate node to render + * @return JSON string representation of the node + */ @Override - public String apply(PNode node) { - return switch (node) { - case ConjugateNode n -> renderConjugate(n); - case PredicateNode p -> renderPredicate(p); - }; - } + public String apply(PNode node) { + return switch (node) { + case ConjugateNode n -> renderConjugate(n); + case PredicateNode p -> renderPredicate(p); + }; + } - private String renderPredicate(PredicateNode p) { - return gson.toJson(p); - } - - private boolean isChar(String symbol) { - char c = symbol.charAt(0); - return (c >= 'A' && c <= 'Z') || (c>='a' && c<='z'); - } - - private String renderConjugate(ConjugateNode n) { - return gson.toJson(n); - } - - private String concatenate(String name, PNode[] values) { - StringBuilder sb = new StringBuilder(); - - for (PNode value : values) { - String nodeRep = apply(value); - if (!sb.isEmpty()) { - sb.append(" ").append(name).append(" "); - } - sb.append(nodeRep); - } - return sb.toString(); - } + /** + * Renders a predicate node as JSON. + * + * @param p The predicate node to render + * @return JSON string representation of the predicate + */ + private String renderPredicate(PredicateNode p) { + return gson.toJson(p); + } + /** + * Renders a conjugate node as JSON. + * + * @param n The conjugate node to render + * @return JSON string representation of the conjugate + */ + private String renderConjugate(ConjugateNode n) { + return gson.toJson(n); + } } diff --git a/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/PredicateTest.java b/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/PredicateTest.java index 127d30ba8..76eef71f5 100644 --- a/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/PredicateTest.java +++ b/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/PredicateTest.java @@ -36,15 +36,15 @@ public class PredicateTest { System.out.println(p1); } -// @Test -// public void testNesting() { -// Node tn = getTestNode1(); -// ByteBuffer b = ByteBuffer.allocate(100); -// tn.encode(b); -// b.flip(); -// Node node = new ConjugateNode(b); -// System.out.println(node); -// } + @Test + public void testNesting() { + PNode tn = getTestNode1(); + ByteBuffer b = ByteBuffer.allocate(100); + tn.encode(b); + b.flip(); + PNode node = new ConjugateNode(b); + System.out.println(node); + } private PNode getTestNode1() { PredicateNode inp = new PredicateNode(1, OpType.IN, new long[]{3L, 4L}); diff --git a/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRendererTest.java b/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRendererTest.java index a78ccee86..18e4c04b5 100644 --- a/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRendererTest.java +++ b/nb-virtdata/virtdata-lib-hdf5/src/test/java/io/nosqlbench/virtdata/predicates/nodewalk/repr/H5JsonNodeRendererTest.java @@ -109,36 +109,4 @@ public class H5JsonNodeRendererTest { ] }"""; - public static final String testExample = """ - { - "conjunction": "none", - "terms": [ - { - "field": {"name": "firstname"}, - "operator": "in", - "comparator": {"value": - ["Mark","Mark's friend Joe"] - } - } - ] - } - """; - public static final String test3 = """ - { - "conjunction": "or", - "terms": [ - { - "field": {"name": "highprice"}, - "operator": "gt", - "comparator": {"value": 1000} - }, - { - "field": {"name": "lowprice"}, - "operator": "lt", - "comparator": {"value": 1} - } - ] - } - """; - }