Skip to content

Commit

Permalink
Pure Select Table 로직 추가 (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
akageun authored Jul 3, 2024
1 parent 8c15224 commit ace69b0
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package kr.hakdang.cadio.core.domain.cluster.keyspace.table;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.ColumnDefinition;
import com.datastax.oss.driver.api.core.cql.ColumnDefinitions;
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.type.codec.TypeCodec;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.querybuilder.QueryBuilder;
import com.datastax.oss.driver.api.querybuilder.select.Select;
Expand All @@ -11,6 +15,14 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.nio.ByteBuffer;
import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* ClusterTablePureSelectCommander
* - 테이블 단순 조회 용
Expand All @@ -23,21 +35,52 @@
public class ClusterTableCommander extends BaseClusterCommander {

public ClusterTablePureSelectResult pureSelect(CqlSession session, ClusterTablePureSelectArgs args) {
SimpleStatement statement = QueryBuilder
.selectFrom(args.getKeyspace(), args.getTable())
.all()
.build()
.setPageSize(args.getLimit());

if (StringUtils.isNotBlank(args.getNextPageState())) {
statement = statement.setPagingState(Bytes.fromHexString(args.getNextPageState()));
SimpleStatement statement = QueryBuilder.selectFrom(args.getKeyspace(), args.getTable()).all().build().setPageSize(args.getLimit()).setTimeout(Duration.ofSeconds(3)) // 3s timeout
.setPagingState(StringUtils.isNotBlank(args.getCursor()) ? Bytes.fromHexString(args.getCursor()) : null);

ResultSet resultSet = session.execute(statement);
Iterator<Row> page1Iter = resultSet.iterator();
ColumnDefinitions definitions = resultSet.getColumnDefinitions();

List<Map<String, Object>> rows = new ArrayList<>();
while (0 < resultSet.getAvailableWithoutFetching()) {
rows.add(convertMap(definitions, page1Iter.next()));
}

String previewCursor = args.getCursor();
String nextCursor = "";
ByteBuffer pagingState = resultSet.getExecutionInfo().getPagingState();
if (pagingState != null) {
nextCursor = Bytes.toHexString(pagingState);
}

ResultSet rs = session.execute(statement);
//rs.wasApplied()
List<String> columnNames = new ArrayList<>();
for (ColumnDefinition definition : definitions) {
columnNames.add(definition.getName().asCql(true));
}

return ClusterTablePureSelectResult.builder()
.wasApplied(resultSet.wasApplied())
.columnNames(columnNames)
.rows(rows)
.nextToken(nextCursor)
.previewToken(previewCursor)
.build();
}

return null;
//TODO : 중복 제거 필요
private Map<String, Object> convertMap(ColumnDefinitions definitions, Row row) {
Map<String, Object> result = new HashMap<>();

for (int i = 0; i < definitions.size(); i++) {
ColumnDefinition definition = definitions.get(i);
String name = definition.getName().asCql(true);
TypeCodec<Object> codec = row.codecRegistry().codecFor(definition.getType());
Object value = codec.decode(row.getBytesUnsafe(i), row.protocolVersion());

result.put(name, value);
}

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@ public class ClusterTablePureSelectArgs {

private int limit; //TODO : max check

private String nextPageState;
private String cursor;

@Builder
public ClusterTablePureSelectArgs(String keyspace, String table, int limit, String nextPageState) {
public ClusterTablePureSelectArgs(String keyspace, String table, int limit, String cursor) {
this.keyspace = keyspace;
this.table = table;
this.limit = limit;
this.nextPageState = nextPageState;
this.cursor = cursor;
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,34 @@
package kr.hakdang.cadio.core.domain.cluster.keyspace.table;

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

import java.util.List;
import java.util.Map;

/**
* ClusterTablePureSelectResult
*
* @author akageun
* @since 2024-06-30
*/
@Getter
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ClusterTablePureSelectResult {
private boolean wasApplied;
private List<String> columnNames;
private List<Map<String, Object>> rows;
private String previewToken;
private String nextToken;

@Builder
public ClusterTablePureSelectResult(boolean wasApplied, List<String> columnNames, List<Map<String, Object>> rows, String previewToken, String nextToken) {
this.wasApplied = wasApplied;
this.columnNames = columnNames;
this.rows = rows;
this.previewToken = previewToken;
this.nextToken = nextToken;
}
}

0 comments on commit ace69b0

Please sign in to comment.