Skip to content

Commit

Permalink
Merge pull request #43 from psilberk/codacy-errors
Browse files Browse the repository at this point in the history
Fixed codacy errors
  • Loading branch information
psilberk authored Jan 10, 2025
2 parents 98ffb7d + e7fa417 commit 2987a03
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ String buildIndexName(String tableName, String suffix) {
* @return The index name truncated to the max length allowed by the database.
*/
private String truncateIndexName(String indexName, boolean isQuoted) {
String truncatedIndexName = indexName;
int maxLength = isQuoted ? INDEX_NAME_MAX_LENGTH - 2 : INDEX_NAME_MAX_LENGTH;
if (indexName.length() > maxLength) {
indexName = indexName.substring(0, maxLength);
if (truncatedIndexName.length() > maxLength) {
truncatedIndexName = truncatedIndexName.substring(0, maxLength);
}
return indexName;
return truncatedIndexName;
}

/**
Expand All @@ -93,7 +94,8 @@ private String truncateIndexName(String indexName, boolean isQuoted) {
* @return The unquoted table name.
*/
private String unquoteTableName(String tableName) {
return tableName.substring(1, tableName.length() - 1);
String unquotedTableName = tableName;
return unquotedTableName.substring(1, unquotedTableName.length() - 1);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,8 +468,9 @@ public String toSQL() {

@Override
public int setParameters(PreparedStatement preparedStatement, int parameterIndex) throws SQLException {
int currentParameterIndex = parameterIndex;
for (Object object : comparisonValues) {
setObject(preparedStatement, parameterIndex++, object, sqlType);
setObject(preparedStatement, currentParameterIndex++, object, sqlType);
}

return comparisonValues.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,27 @@ final class CommonTestOperations {
/** Name of a database table used by tests */
public static final String TABLE_NAME = "LANGCHAIN4J_EMBEDDING_STORE";

private static final PoolDataSource DATA_SOURCE = PoolDataSourceFactory.getPoolDataSource();
private static final PoolDataSource SYSDBA_DATA_SOURCE = PoolDataSourceFactory.getPoolDataSource();

private static final String ORACLE_IMAGE_NAME = "gvenzl/oracle-free:23.6-slim-faststart";

/**
* Seed for random numbers. When a test fails, "-Ddev.langchain4j.store.embedding.oracle.SEED=..." can be used to
* re-execute it with the same random numbers.
*/
private static final long SEED =
Long.getLong("dev.langchain4j.store.embedding.oracle.SEED", System.currentTimeMillis());

static {
Logger.getLogger(CommonTestOperations.class.getName())
.info("dev.langchain4j.store.embedding.oracle.SEED=" + SEED);
}

/**
* Used to generate random numbers, such as those for an embedding vector.
*/
private static final Random RANDOM = new Random(SEED);

private CommonTestOperations() {}

private static final PoolDataSource DATA_SOURCE = PoolDataSourceFactory.getPoolDataSource();
private static final PoolDataSource SYSDBA_DATA_SOURCE = PoolDataSourceFactory.getPoolDataSource();

public static final String ORACLE_IMAGE_NAME = "gvenzl/oracle-free:23.6-slim-faststart";

static {
Logger.getLogger(CommonTestOperations.class.getName())
.info("dev.langchain4j.store.embedding.oracle.SEED=" + SEED);

try {
DATA_SOURCE.setConnectionFactoryClassName("oracle.jdbc.datasource.impl.OracleDataSource");
String urlFromEnv = System.getenv("ORACLE_JDBC_URL");
Expand Down Expand Up @@ -104,6 +100,8 @@ private CommonTestOperations() {}
}
}

private CommonTestOperations() {}

static void initDataSource(PoolDataSource dataSource, String url, String username, String password) {
try {
dataSource.setConnectionFactoryClassName("oracle.jdbc.datasource.impl.OracleDataSource");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@

public class OracleEmbeddingStoreWithFilteringIT extends EmbeddingStoreWithFilteringIT {

/**
* A String of more than 32767 characters. Appending this to any other String will require a conversion to CLOB.
* The value of 32767 is the maximum size of a VARCHAR for Oracle Database if the "MAX_STRING_SIZE" initialization
* parameter is set to "EXTENDED". Otherwise, the maximum size is 4000. Either way, this length will force a CLOB
* conversion.
*/
private static final String STRING_32K =
Stream.generate(() -> "A").limit(32768).collect(Collectors.joining());

private final OracleEmbeddingStore embeddingStore = CommonTestOperations.newEmbeddingStore();

@Override
Expand Down Expand Up @@ -160,15 +169,6 @@ protected static Stream<Arguments> should_filter_by_metadata() {
toClobMetadata((List<Metadata>) argumentsArray[2]))));
}

/**
* A String of more than 32767 characters. Appending this to any other String will require a conversion to CLOB.
* The value of 32767 is the maximum size of a VARCHAR for Oracle Database if the "MAX_STRING_SIZE" initialization
* parameter is set to "EXTENDED". Otherwise, the maximum size is 4000. Either way, this length will force a CLOB
* conversion.
*/
private static final String STRING_32K =
Stream.generate(() -> "A").limit(32768).collect(Collectors.joining());

/**
* Converts a Filter into one which uses a long String value.
*
Expand Down Expand Up @@ -212,7 +212,7 @@ private static Filter toClobFilter(Filter filter) {
Not not = (Not) filter;
return new Not(toClobFilter(not.expression()));
} else {
throw new RuntimeException("Need to add a case for: " + filter.getClass());
throw new UnsupportedOperationException("Need to add a case for: " + filter.getClass());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,4 @@ public void testSQLType() throws SQLException {
// CLOB is lossless for all Java Strings (assuming the database character set is UTF-8)

}

/**
* Verifies that a Java to SQL conversion is lossless. A PreparedStatement converts Java objects into SQL data
* types, and a ResultSet converts SQL data types into Java objects. A conversion is lossless if, after converting a
* Java object into SQL data, that SQL data can be converted back into a Java object which is equal to the original,
* according to {@link Object#equals(Object)}.
*
* @param sqlType A SQL data type. Not null.
* @param javaObject Java object to convert into the SQL data type. Not null.
*/
private void verifyLosslessConversion(SQLType sqlType, Object javaObject) throws SQLException {
try (Connection connection = CommonTestOperations.getDataSource().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT ? FROM sys.dual")) {

preparedStatement.setObject(1, javaObject, sqlType);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
assertThat(resultSet.next()).isTrue();
assertThat(resultSet.getObject(1, javaObject.getClass())).isEqualTo(javaObject);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static dev.langchain4j.store.embedding.oracle.CommonTestOperations.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

import java.sql.*;
Expand Down Expand Up @@ -79,29 +78,33 @@ public void testCreateIndexOnStoreCreation(
}

OracleEmbeddingStore oracleEmbeddingStore = OracleEmbeddingStore.builder()
.dataSource(CommonTestOperations.getDataSource())
.dataSource(getDataSource())
.embeddingTable(EmbeddingTable.builder()
.createOption(CreateOption.CREATE_IF_NOT_EXISTS)
.name(TABLE_NAME)
.build())
.index(ivfIndexBuilder.build())
.build();

try (Connection connection =
CommonTestOperations.getSysDBADataSource().getConnection();
try (Connection connection = getSysDBADataSource().getConnection();
PreparedStatement stmt = connection.prepareStatement(
"select IDX_PARAMS from vecsys.vector$index where IDX_NAME = ?")) {
stmt.setString(1, TABLE_NAME + "_VECTOR_INDEX");
ResultSet rs = stmt.executeQuery();
assertThat(rs.next()).as("A index should be returned").isTrue();
OracleJsonObject params = rs.getObject("IDX_PARAMS", OracleJsonObject.class);
assertIndexType("IVF_FLAT", params);
assertTargetAccuracy(targetAccuracy, params);
assertDegreeOfParallelism(degreeOfParallelism, params);
assertNeighborPartitions(neighborPartitions, params);
assertSamplePerPartition(samplePerPartition, params);
assertMinVectorsPerPartition(minVectorsPerPartition, params);
assertThat(rs.next()).as("Only one index should be returned").isFalse();
if (rs.next()) {
OracleJsonObject params = rs.getObject("IDX_PARAMS", OracleJsonObject.class);
assertIndexType("IVF_FLAT", params);
assertTargetAccuracy(targetAccuracy, params);
assertDegreeOfParallelism(degreeOfParallelism, params);
assertNeighborPartitions(neighborPartitions, params);
assertSamplePerPartition(samplePerPartition, params);
assertMinVectorsPerPartition(minVectorsPerPartition, params);
assertThat(rs.next())
.as("Only one index should be returned")
.isFalse();
} else {
fail("The result set should have returned a line");
}
}
verifySearch(oracleEmbeddingStore);
} finally {
Expand Down Expand Up @@ -132,7 +135,7 @@ public void testMetadataKeyAndVectorIndex() throws SQLException {
.build();

OracleEmbeddingStore oracleEmbeddingStore = OracleEmbeddingStore.builder()
.dataSource(CommonTestOperations.getDataSource())
.dataSource(getDataSource())
.embeddingTable(EmbeddingTable.builder()
.createOption(CreateOption.CREATE_IF_NOT_EXISTS)
.name(TABLE_NAME)
Expand Down Expand Up @@ -166,7 +169,7 @@ public void testMetadataKeysIndex() throws SQLException {
.build();

OracleEmbeddingStore oracleEmbeddingStore = OracleEmbeddingStore.builder()
.dataSource(CommonTestOperations.getDataSource())
.dataSource(getDataSource())
.embeddingTable(EmbeddingTable.builder()
.createOption(CreateOption.CREATE_IF_NOT_EXISTS)
.name(TABLE_NAME)
Expand Down Expand Up @@ -199,7 +202,7 @@ public void testMetadataKeysIndex() throws SQLException {
public void InvalidIndexNameTest(String indexName) throws Exception {
try {
OracleEmbeddingStore.builder()
.dataSource(CommonTestOperations.getDataSource())
.dataSource(getDataSource())
.embeddingTable(EmbeddingTable.builder()
.createOption(CreateOption.CREATE_OR_REPLACE)
.name(TABLE_NAME)
Expand All @@ -217,7 +220,7 @@ public void InvalidIndexNameTest(String indexName) throws Exception {
}
try {
OracleEmbeddingStore.builder()
.dataSource(CommonTestOperations.getDataSource())
.dataSource(getDataSource())
.embeddingTable(EmbeddingTable.builder()
.createOption(CreateOption.CREATE_OR_REPLACE)
.name(TABLE_NAME)
Expand Down Expand Up @@ -257,17 +260,17 @@ private void verifyIndexExists(CreateOption createOption) throws SQLException {
*/
private void verifyIndexExists(CreateOption createOption, String tableName, String indexName, String indexType)
throws SQLException {
try (Connection connection = CommonTestOperations.getDataSource().getConnection();
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT 'OK'" + " FROM user_indexes"
+ " WHERE table_name='"
+ tableName + "'" + " AND index_name='"
+ indexName + "'" + " AND index_type='"
+ indexType + "'")) {

if (createOption == CreateOption.CREATE_NONE)
assertThat(resultSet.next()).isFalse();
else assertThat(resultSet.next()).isTrue();
try (Connection connection = getDataSource().getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(
"SELECT 'OK' FROM user_indexes" + "WHERE table_name=? AND index_name=? AND index_type?")) {
preparedStatement.setString(1, tableName);
preparedStatement.setString(2, indexName);
preparedStatement.setString(3, indexType);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
if (createOption == CreateOption.CREATE_NONE)
assertThat(resultSet.next()).isFalse();
else assertThat(resultSet.next()).isTrue();
}
}
}

Expand Down

0 comments on commit 2987a03

Please sign in to comment.