Skip to content

Commit

Permalink
Fixes #643, needs verification
Browse files Browse the repository at this point in the history
  • Loading branch information
stalep authored and jesperpedersen committed Oct 3, 2023
1 parent 0e37b15 commit ed231d2
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,117 @@
package io.hyperfoil.tools.horreum.hibernate;


import org.hibernate.type.BasicTypeReference;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.CustomType;
import org.hibernate.type.SqlTypes;
import org.hibernate.type.spi.TypeConfiguration;
import org.hibernate.usertype.UserType;

public class IntArrayType {
import java.io.Serializable;
import java.sql.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.IntStream;

public static final BasicTypeReference<int[]> INT_ARRAY = new BasicTypeReference("int-array", int[].class, 666);
import static java.lang.String.format;

public class IntArrayType implements UserType<int[]> {

public static final CustomType INSTANCE = new CustomType<>(new IntArrayType(), new TypeConfiguration());
@Override
public int getSqlType() {
return SqlTypes.ARRAY;
}

@Override
public Class<int[]> returnedClass() {
return int[].class;
}

@Override
public boolean equals(int[] x, int[] y) {
return Arrays.equals(x, y);
}

@Override
public int hashCode(int[] x) {
return Objects.hashCode(x);
}

@Override
public int[] nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner)
throws SQLException {
if(rs.wasNull())
return null;
Array array = rs.getArray(position);
if (array == null) {
return null;
}
try {
return (int[]) array.getArray();
} catch (final Exception ex) {
throw new RuntimeException("Failed to convert ResultSet to int[]: " + ex.getMessage(), ex);
}
}

@Override
public void nullSafeSet(PreparedStatement ps, int[] value, int index, SharedSessionContractImplementor session)
throws SQLException {
if (value == null) {
ps.setNull(index, Types.ARRAY);
return;
}
try {
Integer[] castArray = IntStream.of(value).boxed().toArray( Integer[]::new );
Array array = ps.getConnection().createArrayOf("integer", castArray);
ps.setArray(index, array);
} catch (final Exception ex) {
throw new RuntimeException(format("Failed to convert JSON to String: %s", ex.getMessage()), ex);
}
}

@Override
public int[] deepCopy(int[] value) throws HibernateException {
if (value == null) {
return null;
}
return Arrays.copyOf(value, value.length);
}

@Override
public boolean isMutable() {
return true;
}

@Override
public Serializable disassemble(int[] value) throws HibernateException {
return (Serializable) this.deepCopy(value);
}

@Override
public int[] assemble(Serializable cached, Object owner) throws HibernateException {
String stringArray = cached.toString().replaceAll("[\\[\\]]", "");
String[] tokens = stringArray.split(",");

int length = tokens.length;
int[] array = new int[length];
for (int i = 0; i < tokens.length; i++) {
array[i] = Integer.valueOf(tokens[i]);
}
return array;
}

@Override
public int[] replace(int[] original, int[] target, Object owner) throws HibernateException {
return original;
}

public String getName() {
return "int-array";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ public List<DatapointLastTimestamp> findLastDatapoints(LastDatapointsParams para
Query query = em.createNativeQuery(FIND_LAST_DATAPOINTS)
.unwrap(NativeQuery.class)
.setParameter(1, Util.parseFingerprint(params.fingerprint), JsonBinaryType.INSTANCE)
.setParameter(2, params.variables, IntArrayType.INT_ARRAY);
.setParameter(2, params.variables, IntArrayType.INSTANCE);
SqlServiceImpl.setResultTransformer(query, Transformers.aliasToBean(DatapointLastTimestamp.class));
//noinspection unchecked
return query.getResultList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -23,6 +24,7 @@
import io.hyperfoil.tools.horreum.api.alerting.DataPoint;
import io.hyperfoil.tools.horreum.api.alerting.RunExpectation;
import io.hyperfoil.tools.horreum.api.data.DataSet;
import io.hyperfoil.tools.horreum.api.services.ExperimentService;
import io.hyperfoil.tools.horreum.bus.MessageBusChannels;
import jakarta.inject.Inject;

Expand Down Expand Up @@ -739,6 +741,18 @@ public void testRandomOrder(TestInfo info) throws InterruptedException {
checkChanges(test);
}

@org.junit.jupiter.api.Test
public void testFindLastDatapoints(TestInfo info) throws IOException {
populateDataFromFiles();
AlertingService.LastDatapointsParams params = new AlertingService.LastDatapointsParams();
params.fingerprint = mapper.valueToTree("{ \"fingerprint\":\"benchmark_test\"}").asText();
params.variables = new int[] {1,2,3};

List<AlertingService.DatapointLastTimestamp> timestamps =
jsonRequest().body(params).post("/api/alerting/datapoint/last")
.then().statusCode(200).extract().body().as(new ParameterizedTypeImpl(List.class, AlertingService.DatapointLastTimestamp.class));
}

private void checkChanges(Test test) {
List<ChangeDAO> list = ChangeDAO.list("variable.testId", test.id);
assertEquals(Arrays.asList(1L, 4L, 6L, 7L, 9L),
Expand All @@ -761,4 +775,4 @@ private void drainQueue(BlockingQueue<Change.Event> changeQueue) throws Interrup
}
}
}
}
}

0 comments on commit ed231d2

Please sign in to comment.