Skip to content

Commit bea7d92

Browse files
committed
Fixing Meta
1 parent c1a1241 commit bea7d92

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

astra-db-client/src/main/java/com/dtsx/astra/sdk/cassio/AbstractCassandraTable.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public AbstractCassandraTable(CqlSession session, String keyspaceName, String ta
8686
/**
8787
* Create table if not exist.
8888
*/
89-
public abstract void createSchema();
89+
public abstract void create();
9090

9191
/**
9292
* Upsert a row of the table.
@@ -132,4 +132,12 @@ public void clear() {
132132
cqlSession.execute("TRUNCATE " + keyspaceName + "." + tableName);
133133
}
134134

135+
/**
136+
* Gets cqlSession
137+
*
138+
* @return value of cqlSession
139+
*/
140+
public CqlSession getCqlSession() {
141+
return cqlSession;
142+
}
135143
}

astra-db-client/src/main/java/com/dtsx/astra/sdk/cassio/ClusteredCassandraTable.java

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.datastax.oss.driver.api.core.cql.Row;
99
import lombok.AllArgsConstructor;
1010
import lombok.Data;
11-
import lombok.NoArgsConstructor;
1211
import lombok.NonNull;
1312
import lombok.extern.slf4j.Slf4j;
1413

@@ -26,11 +25,11 @@ public class ClusteredCassandraTable extends AbstractCassandraTable<ClusteredCas
2625
/**
2726
* Prepared statements
2827
*/
29-
private final PreparedStatement findPartitionStatement;
30-
private final PreparedStatement deletePartitionStatement;
31-
private final PreparedStatement deleteRowStatement;
32-
private final PreparedStatement insertRowStatement;
33-
private final PreparedStatement findRowStatement;
28+
private PreparedStatement findPartitionStatement;
29+
private PreparedStatement deletePartitionStatement;
30+
private PreparedStatement deleteRowStatement;
31+
private PreparedStatement insertRowStatement;
32+
private PreparedStatement findRowStatement;
3433

3534
/**
3635
* Constructor with the mandatory parameters.
@@ -44,41 +43,50 @@ public class ClusteredCassandraTable extends AbstractCassandraTable<ClusteredCas
4443
*/
4544
public ClusteredCassandraTable(@NonNull CqlSession session, @NonNull String keyspaceName, @NonNull String tableName) {
4645
super(session, keyspaceName, tableName);
47-
createSchema();
48-
findPartitionStatement = session.prepare(
49-
"select * from " + keyspaceName + "." + tableName
50-
+ " where " + PARTITION_ID + " = ? ");
51-
deletePartitionStatement = session.prepare(
52-
"delete from " + keyspaceName + "." + tableName
53-
+ " where " + PARTITION_ID + " = ? ");
54-
findRowStatement = session.prepare(
55-
"select * from " + keyspaceName + "." + tableName
56-
+ " where " + PARTITION_ID + " = ? "
57-
+ " and " + ROW_ID + " = ? ");
58-
deleteRowStatement = session.prepare(
59-
"delete from " + keyspaceName + "." + tableName
60-
+ " where " + PARTITION_ID + " = ? "
61-
+ " and " + ROW_ID + " = ? ");
62-
insertRowStatement = session.prepare(
63-
"insert into " + keyspaceName + "." + tableName
64-
+ " (" + PARTITION_ID + ", " + ROW_ID + ", " + BODY_BLOB + ") "
65-
+ " values (?, ?, ?)");
6646
}
6747

48+
/**
49+
* Prepare statements on first request.
50+
*/
51+
private synchronized void prepareStatements() {
52+
if (findPartitionStatement == null) {
53+
findPartitionStatement = cqlSession.prepare(
54+
"select * from " + keyspaceName + "." + tableName
55+
+ " where " + PARTITION_ID + " = ? ");
56+
deletePartitionStatement = cqlSession.prepare(
57+
"delete from " + keyspaceName + "." + tableName
58+
+ " where " + PARTITION_ID + " = ? ");
59+
findRowStatement = cqlSession.prepare(
60+
"select * from " + keyspaceName + "." + tableName
61+
+ " where " + PARTITION_ID + " = ? "
62+
+ " and " + ROW_ID + " = ? ");
63+
deleteRowStatement = cqlSession.prepare(
64+
"delete from " + keyspaceName + "." + tableName
65+
+ " where " + PARTITION_ID + " = ? "
66+
+ " and " + ROW_ID + " = ? ");
67+
insertRowStatement = cqlSession.prepare(
68+
"insert into " + keyspaceName + "." + tableName
69+
+ " (" + PARTITION_ID + ", " + ROW_ID + ", " + BODY_BLOB + ") "
70+
+ " values (?, ?, ?)");
71+
}
72+
}
73+
74+
/** {@inheritDoc} */
6875
@Override
69-
public void createSchema() {
76+
public void create() {
7077
cqlSession.execute("CREATE TABLE IF NOT EXISTS " + keyspaceName + "." + tableName + " ("
7178
+ PARTITION_ID + " text, "
7279
+ ROW_ID + " timeuuid, "
7380
+ BODY_BLOB + " text, "
7481
+ "PRIMARY KEY ((" + PARTITION_ID + "), " + ROW_ID + ")) "
75-
+ "WITH CLUSTERING ORDER BY (" + ROW_ID + " DESC");
82+
+ "WITH CLUSTERING ORDER BY (" + ROW_ID + " DESC)");
7683
log.info("+ Table '{}' has been created (if needed).", tableName);
7784
}
7885

7986
/** {@inheritDoc} */
8087
@Override
8188
public void put(@NonNull ClusteredCassandraTable.Record row) {
89+
prepareStatements();
8290
cqlSession.execute(insertRowStatement.bind(row.getPartitionId(), row.getRowId(), row.getBody()));
8391
}
8492

@@ -100,6 +108,7 @@ public Record mapRow(@NonNull Row row) {
100108
* list of rows
101109
*/
102110
public List<Record> findPartition(@NonNull String partitionDd) {
111+
prepareStatements();
103112
return cqlSession.execute(findPartitionStatement.bind(partitionDd))
104113
.all().stream()
105114
.map(this::mapRow)
@@ -113,6 +122,7 @@ public List<Record> findPartition(@NonNull String partitionDd) {
113122
* current rows.
114123
*/
115124
public void upsertPartition(List<Record> rows) {
125+
prepareStatements();
116126
if (rows != null && !rows.isEmpty()) {
117127
BatchStatementBuilder batch = BatchStatement.builder(BatchType.LOGGED);
118128
String currentPartitionId = null;
@@ -137,6 +147,7 @@ public void upsertPartition(List<Record> rows) {
137147
* record if exists
138148
*/
139149
public Optional<Record> findById(String partition, UUID rowId) {
150+
prepareStatements();
140151
return Optional.ofNullable(cqlSession
141152
.execute(findRowStatement.bind(partition, rowId))
142153
.one()).map(this::mapRow);
@@ -149,6 +160,7 @@ public Optional<Record> findById(String partition, UUID rowId) {
149160
* delete the whole partition
150161
*/
151162
public void deletePartition(@NonNull String partitionId) {
163+
prepareStatements();
152164
cqlSession.execute(deletePartitionStatement.bind(partitionId));
153165
}
154166

@@ -161,6 +173,7 @@ public void deletePartition(@NonNull String partitionId) {
161173
* message id
162174
*/
163175
public void delete(@NonNull String partitionId, @NonNull UUID rowId) {
176+
prepareStatements();
164177
cqlSession.execute(deleteRowStatement.bind(partitionId, rowId));
165178
}
166179

@@ -175,6 +188,7 @@ public void delete(@NonNull String partitionId, @NonNull UUID rowId) {
175188
* body
176189
*/
177190
public void insert(@NonNull String partitionId, @NonNull UUID rowId, @NonNull String bodyBlob) {
191+
prepareStatements();
178192
cqlSession.execute(insertRowStatement.bind(partitionId,rowId, bodyBlob));
179193
}
180194

astra-db-client/src/main/java/com/dtsx/astra/sdk/cassio/MetadataVectorCassandraTable.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ public MetadataVectorCassandraTable(CqlSession session, String keyspaceName, Str
5858
super(session, keyspaceName, tableName);
5959
this.vectorDimension = vectorDimension;
6060
this.similarityMetric = metric;
61-
createSchema();
6261
}
6362

6463
/**
6564
* Create table and indexes if not exist.
6665
*/
67-
public void createSchema() {
66+
public void create() {
6867
// Create Table
6968
cqlSession.execute("CREATE TABLE IF NOT EXISTS " + tableName + " (" +
7069
ROW_ID + " text, " +
@@ -76,19 +75,16 @@ public void createSchema() {
7675
ROW_ID + ")" +
7776
")");
7877
log.info("+ Table '{}' has been created (if needed).", tableName);
79-
// Create Vector Index
80-
Map<String, Object> optionMap = new HashMap<>();
81-
optionMap.put("similarity_function", similarityMetric.getOption());
8278
cqlSession.execute(
8379
"CREATE CUSTOM INDEX IF NOT EXISTS idx_vector_" + tableName
8480
+ " ON " + tableName + " (" + VECTOR + ") "
8581
+ "USING 'org.apache.cassandra.index.sai.StorageAttachedIndex' "
86-
+ "WITH OPTIONS = " + optionMap.toString());
82+
+ "WITH OPTIONS = { 'similarity_function': '" + similarityMetric.getOption() + "'};");
8783
log.info("+ Index '{}' has been created (if needed).", "idx_vector_" + tableName);
8884
// Create Metadata Index
8985
cqlSession.execute(
9086
"CREATE CUSTOM INDEX IF NOT EXISTS eidx_metadata_s_" + tableName
91-
+ " ON " + tableName + " ENTRIES(" + METADATA_S + ") "
87+
+ " ON " + tableName + " (ENTRIES(" + METADATA_S + ")) "
9288
+ "USING 'org.apache.cassandra.index.sai.StorageAttachedIndex' ");
9389
log.info("+ Index '{}' has been created (if needed).", "eidx_metadata_s_" + tableName);
9490
}

astra-sdk-devops/src/main/java/com/dtsx/astra/sdk/utils/TestUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public static String setupDatabase(String token, AstraEnvironment env, String db
170170
// Db is active, should I add a keyspace ?
171171
if (!dbClient.keyspaces().findAll().contains(keyspace)) {
172172
dbClient.keyspaces().create(keyspace);
173-
waitForDbStatus(dbClient, DatabaseStatusType.ACTIVE, 100);
173+
waitForDbStatus(dbClient, DatabaseStatusType.ACTIVE, 1000);
174174
}
175175
return db.getId();
176176
} else {
@@ -187,7 +187,7 @@ public static String setupDatabase(String token, AstraEnvironment env, String db
187187
}
188188
String serverlessDbId = devopsDbCli.create(builder.build());
189189
DbOpsClient dbc = new DbOpsClient(devopsDbCli.getToken(), serverlessDbId);
190-
waitForDbStatus(dbc, DatabaseStatusType.ACTIVE, 180);
190+
waitForDbStatus(dbc, DatabaseStatusType.ACTIVE, 1800);
191191
return serverlessDbId;
192192
}
193193
}

0 commit comments

Comments
 (0)