Skip to content

Commit

Permalink
System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분 (#29)
Browse files Browse the repository at this point in the history
* System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분

* System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분

* System Keyspace 정렬시 앞쪽에 오도록 변경 및 system keyspace 인지 구분
  • Loading branch information
seungh0 authored Jul 4, 2024
1 parent d6a97ec commit 44089fc
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.datastax.oss.driver.api.core.CqlSession;
import org.springframework.stereotype.Service;

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

Expand All @@ -18,14 +19,8 @@ public class ClusterNodeListCommander extends BaseClusterCommander {
public List<ClusterNode> listNodes(CqlSession session) {
return session.getMetadata().getNodes().values().stream()
.map(ClusterNode::from)
.sorted((o1, o2) -> {
if (o1.getDatacenter().compareTo(o2.getDatacenter()) < 0) {
return -1;
} else if (o1.getDatacenter().compareTo(o2.getDatacenter()) > 0) {
return 1;
}
return o1.getRack().compareTo(o2.getRack());
})
.sorted(Comparator.comparing(ClusterNode::getDatacenter)
.thenComparing(ClusterNode::getRack))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import kr.hakdang.cadio.core.domain.cluster.info.ClusterInfoProvider;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.InetSocketAddress;
Expand Down Expand Up @@ -57,6 +56,9 @@ public CqlSession makeSession(ClusterConnection clusterConnection) {

public CqlSession makeSession(String clusterId) {
ClusterInfo info = clusterInfoProvider.findClusterInfo(clusterId);
if (info == null) {
throw new IllegalArgumentException(String.format("failed to load Cluster(%s)", clusterId));
}
return makeSession(info.makeClusterConnector());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import lombok.Getter;

import java.util.Arrays;

/**
* CassandraSystemKeyspace
*
Expand All @@ -26,4 +28,9 @@ public enum CassandraSystemKeyspace {
this.keyspaceName = keyspaceName;
}

public static boolean isSystemKeyspace(String keyspaceName) {
return Arrays.stream(CassandraSystemKeyspace.values())
.anyMatch(keyspace -> keyspace.getKeyspaceName().equals(keyspaceName));
}

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package kr.hakdang.cadio.core.domain.cluster.keyspace;

import com.datastax.oss.driver.api.core.CqlIdentifier;
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.Row;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.driver.shaded.guava.common.collect.Maps;
import kr.hakdang.cadio.core.domain.cluster.BaseClusterCommander;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand Down Expand Up @@ -49,7 +48,10 @@ public ClusterKeyspaceListResult keyspaceList(CqlSession session) {
}

public String describe(CqlSession session, ClusterKeyspaceDescribeArgs args) {
//TODO : system keyspace 는 접근 못함.
if (CassandraSystemKeyspace.isSystemKeyspace(args.getKeyspace())) {
return StringUtils.EMPTY;
}

KeyspaceMetadata keyspaceMetadata = session.getMetadata().getKeyspace(args.getKeyspace())
.orElseThrow(() -> new RuntimeException("not found keyspace"));

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

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

/**
* ClusterKeyspaceListResult
Expand All @@ -23,6 +25,10 @@ public class ClusterKeyspaceListResult {
@Builder
public ClusterKeyspaceListResult(boolean wasApplied, List<KeyspaceResult> keyspaceList) {
this.wasApplied = wasApplied;
this.keyspaceList = keyspaceList;
this.keyspaceList = keyspaceList.stream()
.sorted(Comparator.comparing(KeyspaceResult::isSystemKeyspace).reversed()
.thenComparing(KeyspaceResult::getKeyspaceName))
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ public class KeyspaceResult {
private String keyspaceName;
private boolean durableWrites;
private Map<String, String> replication;
private boolean isSystemKeyspace;

@Builder
public KeyspaceResult(String keyspaceName, boolean durableWrites, Map<String, String> replication) {
this.keyspaceName = keyspaceName;
this.durableWrites = durableWrites;
this.replication = replication;
this.isSystemKeyspace = CassandraSystemKeyspace.isSystemKeyspace(keyspaceName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.core.metadata.schema.KeyspaceMetadata;
import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import kr.hakdang.cadio.core.domain.cluster.BaseClusterCommander;
Expand All @@ -14,6 +13,7 @@
import org.springframework.stereotype.Service;

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

Expand Down Expand Up @@ -46,7 +46,7 @@ public ClusterTableGetResult getTable(CqlSession session, ClusterTableGetArgs ar
}

String tableDescribe = "";
if (args.isWithTableDescribe()) {
if (!CassandraSystemKeyspace.isSystemKeyspace(args.getKeyspace()) && args.isWithTableDescribe()) {
TableMetadata tableMetadata = session.getMetadata().getKeyspace(args.getKeyspace())
.orElseThrow(() -> new RuntimeException("not found keyspace"))
.getTable(args.getTable())
Expand All @@ -72,14 +72,8 @@ private List<Column> getColumnsInTable(CqlSession session, ClusterTableGetArgs a
List<Row> columnRows = session.execute(statement).all();
return columnRows.stream()
.map(Column::from)
.sorted((o1, o2) -> {
if (o1.getKind().getOrder() < o2.getKind().getOrder()) {
return -1;
} else if (o1.getKind().getOrder() > o2.getKind().getOrder()) {
return 1;
}
return o1.getPosition() - o2.getPosition();
})
.sorted(Comparator.comparing(o1 -> ((Column) o1).getKind().getOrder())
.thenComparing(o1 -> ((Column) o1).getPosition()))
.collect(Collectors.toList());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package kr.hakdang.cadio.core.domain.cluster.keyspace;

import org.junit.jupiter.api.Test;

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

/**
* CassandraSystemKeyspaceTest
*
* @author seungh0
* @since 2024-07-05
*/
class CassandraSystemKeyspaceTest {

@Test
void system_keyspace() {
for (CassandraSystemKeyspace keyspace : CassandraSystemKeyspace.values()) {
assertThat(CassandraSystemKeyspace.isSystemKeyspace(keyspace.getKeyspaceName())).isTrue();
}
}

@Test
void not_system_keyspace() {
assertThat(CassandraSystemKeyspace.isSystemKeyspace("demo")).isFalse();
}

}

0 comments on commit 44089fc

Please sign in to comment.