Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UDT Type 조회 추가 #42

Merged
merged 5 commits into from
Jul 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
timeout-minutes: 3

- name: Test
run: ./gradlew test
run: ./gradlew unitTest

integration-test:
name: Build on Cassandra
Expand Down
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ subprojects {
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
}

tasks.register('unitTest', Test) {
group = "verification"
useJUnitPlatform {
excludeTags 'integration-test'
}
}

tasks.register('integrationTest', Test) {
group = "verification"
useJUnitPlatform {
Expand All @@ -60,9 +67,7 @@ subprojects {
}

tasks.named('test') {
useJUnitPlatform {
excludeTags 'integration-test'
}
useJUnitPlatform()
testLogging {
showExceptions = true
exceptionFormat = 'FULL'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public enum ErrorCode {
E404_NOT_FOUND_CLUSTER_NODE(404, "not_exists_node"),
E404_NOT_FOUND_KEYSPACE(404, "not_exists_keyspace"),
E404_NOT_FOUND_TABLE(404, "not_exists_table"),
E404_NOT_FOUND_UDT_TYPE(404, "not_exists_udt_type"),
E500_INTERNAL_SERVER_ERROR(500, "internal_server_error"),
;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum CassandraSystemTable {

SYSTEM_SCHEMA_TABLES(SYSTEM_SCHEMA, "tables"),
SYSTEM_SCHEMA_COLUMNS(SYSTEM_SCHEMA, "columns"),
SYSTEM_SCHEMA_TYPES(SYSTEM_SCHEMA, "types"),
;

private final CassandraSystemKeyspace keyspace;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public enum CassandraSystemTablesColumn {

TABLES_KEYSPACE_NAME(CassandraSystemTable.SYSTEM_SCHEMA_TABLES, "keyspace_name"),
TABLES_TABLE_NAME(CassandraSystemTable.SYSTEM_SCHEMA_TABLES, "table_name"),
TYPES_TYPE_NAME(CassandraSystemTable.SYSTEM_SCHEMA_TYPES, "type_name"),
TYPES_FIELD_NAMES(CassandraSystemTable.SYSTEM_SCHEMA_TYPES, "field_names"),
TYPES_FIELD_TYPES(CassandraSystemTable.SYSTEM_SCHEMA_TYPES, "field_types"),
;

private final CassandraSystemTable table;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.udt;

import com.datastax.oss.driver.api.core.cql.Row;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;
import org.apache.commons.lang3.tuple.Pair;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn.TYPES_FIELD_NAMES;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn.TYPES_FIELD_TYPES;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn.TYPES_TYPE_NAME;

/**
* ClusterUDTType
*
* @author Seungho Kang ([email protected])
* @version 1.0.0
* @since 2024. 07. 06.
*/
@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterUDTType {

private String typeName;
private Map<String, String> columns;

private ClusterUDTType(String typeName, Map<String, String> columns) {
this.typeName = typeName;
this.columns = columns;
}

public static ClusterUDTType from(Row row) {
List<String> fieldNames = row.getList(TYPES_FIELD_NAMES.getColumnName(), String.class);
List<String> fieldDataTypes = row.getList(TYPES_FIELD_TYPES.getColumnName(), String.class);

if (fieldNames == null || fieldDataTypes == null) {
throw new RuntimeException("Unexpected Exception. fieldNames or fieldDataTypes is null");
}

if (fieldNames.size() != fieldDataTypes.size()) {
throw new RuntimeException("Unexpected Exception. fieldNames and fieldDataTypes is not matched");
}

Map<String, String> columns = IntStream.range(0, fieldNames.size())
.mapToObj(i -> Pair.of(fieldNames.get(i), fieldDataTypes.get(i)))
.collect(Collectors.toMap(Pair::getLeft, Pair::getRight));

return new ClusterUDTType(row.getString(TYPES_TYPE_NAME.getColumnName()), columns);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.udt;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

/**
* ClusterUDTTypeArgs
*
* @author seungh0
* @since 2024-07-06
*/
public class ClusterUDTTypeArgs {

@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class ClusterUDTTypeListArgs {

private String keyspace;
private int pageSize = 50;
private String nextPageState;

@Builder
private ClusterUDTTypeListArgs(String keyspace, int pageSize, String nextPageState) {
this.keyspace = keyspace;
this.pageSize = pageSize;
this.nextPageState = nextPageState;
}

}

@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public static class ClusterUDTTypeGetArgs {

private String keyspace;
private String typeName;

@Builder
public ClusterUDTTypeGetArgs(String keyspace, String type) {
this.keyspace = keyspace;
this.typeName = type;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.udt;

import kr.hakdang.cassdio.common.error.BaseException;
import kr.hakdang.cassdio.common.error.ErrorCode;

/**
* ClusterUDTTypeException
*
* @author Seungho Kang ([email protected])
* @version 1.0.0
* @since 2024. 07. 07.
*/
public class ClusterUDTTypeException {

public static class ClusterUDTTypeNotFoundException extends BaseException {

protected ClusterUDTTypeNotFoundException(String message) {
super(message, ErrorCode.E404_NOT_FOUND_UDT_TYPE);
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.udt;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import kr.hakdang.cassdio.core.domain.cluster.BaseClusterCommander;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.udt.ClusterUDTTypeArgs.ClusterUDTTypeGetArgs;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.udt.ClusterUDTTypeException.ClusterUDTTypeNotFoundException;
import org.springframework.stereotype.Service;

import java.util.stream.StreamSupport;

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.CassandraSystemKeyspace.SYSTEM_SCHEMA;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.CassandraSystemTable.SYSTEM_SCHEMA_TYPES;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn.TABLES_KEYSPACE_NAME;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn.TYPES_TYPE_NAME;

/**
* ClusterUDTTypeListCommander
*
* @author seungh0
* @since 2024-07-06
*/
@Service
public class ClusterUDTTypeGetCommander extends BaseClusterCommander {

public ClusterUDTType getType(CqlSession session, ClusterUDTTypeGetArgs args) {
SimpleStatement statement = QueryBuilder
.selectFrom(SYSTEM_SCHEMA.getKeyspaceName(), SYSTEM_SCHEMA_TYPES.getTableName())
.all()
.whereColumn(TABLES_KEYSPACE_NAME.getColumnName()).isEqualTo(bindMarker())
.whereColumn(TYPES_TYPE_NAME.getColumnName()).isEqualTo(bindMarker())
.build(args.getKeyspace(), args.getTypeName())
.setPageSize(1);

ResultSet rs = session.execute(statement);

return StreamSupport.stream(rs.spliterator(), false)
.limit(1)
.map(ClusterUDTType::from)
.findFirst()
.orElseThrow(() -> new ClusterUDTTypeNotFoundException(String.format("not exists udt type(%s) in keyspace(%s)", args.getTypeName(), args.getKeyspace())));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.udt;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.protocol.internal.util.Bytes;
import io.micrometer.common.util.StringUtils;
import kr.hakdang.cassdio.core.domain.cluster.BaseClusterCommander;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.udt.ClusterUDTTypeArgs.ClusterUDTTypeListArgs;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static com.datastax.oss.driver.api.querybuilder.QueryBuilder.bindMarker;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.CassandraSystemKeyspace.SYSTEM_SCHEMA;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.CassandraSystemTable.SYSTEM_SCHEMA_TYPES;
import static kr.hakdang.cassdio.core.domain.cluster.keyspace.table.column.CassandraSystemTablesColumn.TABLES_KEYSPACE_NAME;

/**
* ClusterUDTTypeListCommander
*
* @author seungh0
* @since 2024-07-06
*/
@Service
public class ClusterUDTTypeListCommander extends BaseClusterCommander {

public ClusterUDTTypeListResult listTypes(CqlSession session, ClusterUDTTypeListArgs args) {
SimpleStatement statement = QueryBuilder
.selectFrom(SYSTEM_SCHEMA.getKeyspaceName(), SYSTEM_SCHEMA_TYPES.getTableName())
.all()
.whereColumn(TABLES_KEYSPACE_NAME.getColumnName()).isEqualTo(bindMarker())
.build(args.getKeyspace())
.setPageSize(args.getPageSize())
.setPagingState(StringUtils.isBlank(args.getNextPageState()) ? null : Bytes.fromHexString(args.getNextPageState()));

ResultSet rs = session.execute(statement);

List<ClusterUDTType> types = StreamSupport.stream(rs.spliterator(), false)
.limit(rs.getAvailableWithoutFetching())
.map(ClusterUDTType::from)
.collect(Collectors.toList());

return ClusterUDTTypeListResult.of(types, rs.getExecutionInfo().getPagingState());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package kr.hakdang.cassdio.core.domain.cluster.keyspace.udt;

import com.datastax.oss.protocol.internal.util.Bytes;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.nio.ByteBuffer;
import java.util.List;

/**
* ClusterTableListResult
*
* @author seungh0
* @since 2024-07-01
*/
@ToString
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterUDTTypeListResult {

private List<ClusterUDTType> types;
private String nextPageState;

public ClusterUDTTypeListResult(List<ClusterUDTType> types, String nextPageState) {
this.types = types;
this.nextPageState = nextPageState;
}

public static ClusterUDTTypeListResult of(List<ClusterUDTType> tables, ByteBuffer pagingState) {
return new ClusterUDTTypeListResult(tables, pagingState == null ? null : Bytes.toHexString(pagingState));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void initialize() {
session.execute(createKeyspace.build());

SimpleStatement createTable1 = SchemaBuilder.createTable(keyspaceName, "test_table_1")
.ifNotExists()
.withPartitionKey("partition_key_1", DataTypes.TEXT)
.withPartitionKey("partition_key_2", DataTypes.BIGINT)
.withClusteringColumn("clustering_key_1", DataTypes.BIGINT)
Expand All @@ -53,6 +54,7 @@ public void initialize() {
session.execute(createTable1);

SimpleStatement createTable2 = SchemaBuilder.createTable(keyspaceName, "test_table_2")
.ifNotExists()
.withPartitionKey("partition_key_11", DataTypes.TEXT)
.withPartitionKey("partition_key_12", DataTypes.BIGINT)
.withClusteringColumn("clustering_key_11", DataTypes.BIGINT)
Expand All @@ -62,6 +64,14 @@ public void initialize() {
.withBloomFilterFpChance(0.001)
.build();
session.execute(createTable2);

SimpleStatement createType = SchemaBuilder.createType(keyspaceName, "test_type_1")
.ifNotExists()
.withField("field_1", DataTypes.TEXT)
.withField("field_2", DataTypes.BIGINT)
.withField("field_3", DataTypes.TIME)
.build();
session.execute(createType);
}

log.info("Database initialization complete.....");
Expand Down
Loading
Loading