DO-NOT-MERGE: partial improvements for non-published artifacts against cql vector types

This commit is contained in:
Jonathan Shook 2023-05-30 14:10:22 -05:00 committed by jeffbanks
parent 1442d13e11
commit 1b042fa2d1
6 changed files with 132 additions and 3 deletions

View File

@ -67,7 +67,7 @@
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-query-builder</artifactId>
<version>4.15.0</version>
<version>4.15.1-SNAPSHOT</version>
</dependency>
<dependency>

View File

@ -0,0 +1,42 @@
/*
* Copyright (c) 2023 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.datamappers.functions.to_cqlvector;
import com.datastax.oss.driver.api.core.data.CqlVector;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.function.Function;
import java.util.List;
@ThreadSafeMapper
@Categories(Category.experimental)
public class ToCqlVector implements Function<Object, CqlVector> {
@Override
public CqlVector apply(Object object) {
if (object instanceof List list) {
CqlVector.Builder vbuilder = CqlVector.builder();
vbuilder.add(list.toArray());
return vbuilder.build();
} else {
// handle ary types, etc
throw new RuntimeException("Unsupported input type for CqlVector: " + object.getClass().getCanonicalName());
}
}
}

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2023 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.datamappers.functions.to_cqlvector;
import com.datastax.oss.driver.api.core.data.CqlVector;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class ToCqlVectorTest {
@Test
public void testDoubleListToCqlVector() {
ToCqlVector toCqlVector = new ToCqlVector();
assertThat(toCqlVector.apply(List.of(123.d,456.d))).isInstanceOf(CqlVector.class);
}
@Test
public void testFloatListToCqlVector() {
ToCqlVector toCqlVector = new ToCqlVector();
assertThat(toCqlVector.apply(List.of(123.f,456.f))).isInstanceOf(CqlVector.class);
}
}

View File

@ -29,7 +29,7 @@ import java.util.function.Function;
*/
@ThreadSafeMapper
@Categories(Category.experimental)
public class NormalizeVector implements Function<List<Double>,List<Double>> {
public class NormalizeDoubleVectorList implements Function<List<Double>,List<Double>> {
@Override
public List<Double> apply(List<Double> doubles) {
ArrayList<Double> unit = new ArrayList<>(doubles.size());

View File

@ -0,0 +1,46 @@
/*
* Copyright (c) 2023 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.basics.shared.from_long.to_vector;
import io.nosqlbench.virtdata.api.annotations.Categories;
import io.nosqlbench.virtdata.api.annotations.Category;
import io.nosqlbench.virtdata.api.annotations.ThreadSafeMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
/**
* Normalize a vector.
*/
@ThreadSafeMapper
@Categories(Category.experimental)
public class NormalizeFloatVectorList implements Function<List<Float>,List<Float>> {
@Override
public List<Float> apply(List<Float> floats) {
ArrayList<Float> unit = new ArrayList<>(floats.size());
float accumulator = 0.0f;
for (float scalar : floats) {
accumulator+=scalar*scalar;
}
float scalarLen = (float) Math.sqrt(accumulator);
for (float scalarComponent : floats) {
unit.add(scalarComponent/scalarLen);
}
return unit;
}
}

View File

@ -27,7 +27,7 @@ public class ToNormalizedVectorTest {
@Test
public void testNormalizeBasic() {
NormalizeVector normalize = new NormalizeVector();
NormalizeDoubleVectorList normalize = new NormalizeDoubleVectorList();
List<Double> normalized = normalize.apply(List.of(1.0d));
for (int i = 0; i < normalized.size(); i++) {
assertThat(normalized.get(i)).isCloseTo(1.0d, Offset.offset(0.00001d));