Skip to content

Commit

Permalink
Refactor Code Smell (#104)
Browse files Browse the repository at this point in the history
* Refactor Code Smell

* Refactor Code Smell
  • Loading branch information
seungh0 authored Jul 31, 2024
1 parent 0250602 commit f51ff2b
Show file tree
Hide file tree
Showing 20 changed files with 26 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kr.hakdang.cassdio.core.domain.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
import kr.hakdang.cassdio.common.utils.IdGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package kr.hakdang.cassdio.core.domain.cluster;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.Version;
import com.datastax.oss.driver.api.core.config.DefaultDriverOption;
import com.datastax.oss.driver.api.core.context.DriverContext;
import com.datastax.oss.driver.api.core.metadata.Node;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.driver.api.querybuilder.select.SelectFrom;
import com.datastax.oss.driver.internal.core.channel.DriverChannel;
import com.datastax.oss.driver.internal.core.context.InternalDriverContext;
import com.datastax.oss.driver.internal.core.metadata.schema.queries.KeyspaceFilter;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.CassandraSystemKeyspace;
import kr.hakdang.cassdio.core.domain.cluster.keyspace.table.CassandraSystemTable;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Collections;

Expand All @@ -21,7 +19,8 @@
* @author akageun
* @since 2024-07-05
*/
public abstract class ClusterUtils {
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class ClusterUtils {

public static KeyspaceFilter makeKeyspaceFilter(DriverContext context) {
return KeyspaceFilter.newInstance(context.getSessionName(), context.getConfig()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
@Service
public class ClusterVersionGetCommander extends BaseClusterCommander {

private final CqlSessionFactory cqlSessionFactory;

public ClusterVersionGetCommander(
CqlSessionFactory cqlSessionFactory
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import com.datastax.oss.driver.api.core.CqlSession;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* CqlSessionFactory
Expand All @@ -21,7 +21,7 @@ public class CqlSessionFactory {
private final ClusterProvider clusterProvider;
private final ClusterConnector clusterConnector;

public static ConcurrentHashMap<String, CqlSession> SESSION = new ConcurrentHashMap<>();
private static final ConcurrentMap<String, CqlSession> SESSION = new ConcurrentHashMap<>();

public CqlSessionFactory(ClusterProvider clusterProvider, ClusterConnector clusterConnector) {
this.clusterProvider = clusterProvider;
Expand All @@ -30,13 +30,7 @@ public CqlSessionFactory(ClusterProvider clusterProvider, ClusterConnector clust

public CqlSession get(String clusterId) {
String key = String.format("%s", clusterId);
CqlSession session = SESSION.get(key);
if (session == null) {
session = makeSession(clusterId);
SESSION.put(key, session);
}

return session;
return SESSION.computeIfAbsent(key, this::makeSession);
}

public CqlSession makeSession(String clusterId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.springframework.stereotype.Service;

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

/**
Expand All @@ -26,8 +25,6 @@
@Service
public class ClusterClientListCommander extends BaseClusterCommander {

private final CqlSessionFactory cqlSessionFactory;

private final ClusterVersionEvaluator clusterVersionEvaluator;

public ClusterClientListCommander(
Expand All @@ -53,7 +50,7 @@ public ClusterClientListResult getClients(String clusterId) {

List<ClusterClient> clients = StreamSupport.stream(rs.spliterator(), false)
.map(ClusterClient::from)
.collect(Collectors.toList());
.toList();

return ClusterClientListResult.builder()
.clients(clients)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

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

/**
Expand All @@ -39,7 +38,7 @@ public CompactionHistoryListResult getCompactionHistories(String clusterId, Stri
.map(CompactionHistory::from)
.filter(history -> StringUtils.isBlank(keyspace) || history.getKeyspaceName().equals(keyspace))
.sorted(Comparator.comparing(CompactionHistory::getCompactedAt).reversed())
.collect(Collectors.toList());
.toList();

return CompactionHistoryListResult.builder()
.histories(historyList)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
Expand All @@ -18,6 +17,7 @@
* @author akageun
* @since 2024-07-09
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class KeyspaceDTO {

@Getter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
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;
Expand Down Expand Up @@ -44,7 +43,7 @@ public ClusterUDTTypeListResult listTypes(String clusterId, ClusterUDTTypeListAr
List<ClusterUDTType> types = StreamSupport.stream(rs.spliterator(), false)
.limit(rs.getAvailableWithoutFetching())
.map(ClusterUDTType::from)
.collect(Collectors.toList());
.toList();

return ClusterUDTTypeListResult.of(types, rs.getExecutionInfo().getPagingState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
@Service
public class ClusterNodeGetCommander extends BaseClusterCommander {

private final CqlSessionFactory cqlSessionFactory;

public ClusterNodeGetCommander(
CqlSessionFactory cqlSessionFactory
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

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

/**
* ClusterNodeListCommander
Expand All @@ -18,8 +17,6 @@
@Service
public class ClusterNodeListCommander extends BaseClusterCommander {

private final CqlSessionFactory cqlSessionFactory;

public ClusterNodeListCommander(
CqlSessionFactory cqlSessionFactory
) {
Expand All @@ -33,7 +30,7 @@ public List<ClusterNode> listNodes(String clusterId) {
.map(ClusterNode::from)
.sorted(Comparator.comparing(ClusterNode::getDatacenter)
.thenComparing(ClusterNode::getRack))
.collect(Collectors.toList());
.toList();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,8 @@ public QueryDTO.ClusterQueryCommanderResult execute(
QueryDTO.ClusterQueryCommanderArgs args
) {
CqlSession session = cqlSessionFactory.get(clusterId);
if (useKeyspaceQueryCommandNotSupportWithSession(session)) {
if (StringUtils.isNotBlank(args.getKeyspace())) {
throw new NotSupportedCassandraVersionException("It is available in Cassandra version 4.0 and later");
}
if (useKeyspaceQueryCommandNotSupportWithSession(session) && StringUtils.isNotBlank(args.getKeyspace())) {
throw new NotSupportedCassandraVersionException("It is available in Cassandra version 4.0 and later");
}

SimpleStatementBuilder simpleBuilder = SimpleStatement.builder(args.getQuery())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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

/**
* LocalCacheConfig
Expand All @@ -30,7 +29,7 @@ public CacheManager cacheManager() {
.expireAfterWrite(cache.getDuration())
.build()
))
.collect(Collectors.toList());
.toList();
cacheManager.setCaches(caches);
return cacheManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
@ExtendWith(SpringExtension.class)
public abstract class BaseTest {

protected final static String CLUSTER_ID = "12345";
protected static final String CLUSTER_ID = "12345";
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.net.InetSocketAddress;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given;

/**
Expand All @@ -32,7 +31,7 @@ public abstract class IntegrationTest extends BaseTest {

@BeforeEach
void setup() {
given(clusterManager.findById(eq(CLUSTER_ID))).willReturn(ClusterInfo.builder()
given(clusterManager.findById(CLUSTER_ID)).willReturn(ClusterInfo.builder()
.contactPoints("127.0.0.1")
.port(29042)
.localDatacenter("dc1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ void anonymous_cluster_connection() {
assertThat(connection.isAuthCredentials()).isFalse();
assertThat(connection.getContactPoints()).isEqualTo("127.0.0.1");
assertThat(connection.getPort()).isEqualTo(29042);
assertThat(connection.getUsername()).isEqualTo(null);
assertThat(connection.getPassword()).isEqualTo(null);
assertThat(connection.getUsername()).isNull();
assertThat(connection.getPassword()).isNull();
assertThat(connection.getLocalDatacenter()).isEqualTo("dc1");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import kr.hakdang.cassdio.core.domain.cluster.CqlSessionSelectResults;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.test.context.TestConstructor;

import static org.assertj.core.api.Assertions.assertThat;

Expand All @@ -15,8 +14,7 @@
* @since 2024-07-01
*/
@Slf4j
@TestConstructor(autowireMode = TestConstructor.AutowireMode.ALL)
public class ClusterTableListCommanderTest extends IntegrationTest {
class ClusterTableListCommanderTest extends IntegrationTest {

private final ClusterTableListCommander clusterTableListCommander;

Expand Down Expand Up @@ -81,7 +79,7 @@ void when_empty_table_in_keyspace_result_empty() {

// then
assertThat(sut).isNotNull();
assertThat(sut.getRows()).hasSize(0);
assertThat(sut.getRows()).isEmpty();
// assertThat(sut.getNextPageState()).isNull();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class ClusterQueryCommanderTest extends IntegrationTest {
@Value("${cassdio.test-cassandra.keyspace}")
private String keyspaceName;

private final static String TABLE_NAME = "test_table_1";
private static final String TABLE_NAME = "test_table_1";

@Test
void queryTest() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package kr.hakdang.cassdio.web.config.context;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

/**
* CassdioContextHolder
*
* @author akageun
* @since 2024-07-29
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class CassdioContextHolder {

private static final ThreadLocal<CassdioContext> DATA = new ThreadLocal<>();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package kr.hakdang.cassdio.web.route.cluster;

import kr.hakdang.cassdio.core.domain.cluster.ClusterConnection;
import kr.hakdang.cassdio.core.domain.cluster.info.ClusterInfoArgs;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down

0 comments on commit f51ff2b

Please sign in to comment.