Skip to content

Commit 90d377d

Browse files
committed
Add command listTables and start implementing functions
1 parent 6d04991 commit 90d377d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+1159
-139
lines changed

README.MD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ Here is a sample class that demonstrates how to use the library:
144144
import com.datastax.astra.client.DataAPIClient;
145145
import com.datastax.astra.client.Collection;
146146
import com.datastax.astra.client.Database;
147-
import com.datastax.astra.client.model.Document;
147+
import com.datastax.astra.client.model.collections.Document;
148148
import com.datastax.astra.client.model.FindIterable;
149149
import java.util.List;
150150
import static com.datastax.astra.client.model.query.Filters.eq;

astra-db-java/src/main/java/com/datastax/astra/client/Collection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@
2323
import com.datastax.astra.client.exception.DataAPIFaultyResponseException;
2424
import com.datastax.astra.client.exception.DataApiException;
2525
import com.datastax.astra.client.exception.TooManyDocumentsToCountException;
26-
import com.datastax.astra.client.model.CollectionIdTypes;
27-
import com.datastax.astra.client.model.CollectionInfo;
28-
import com.datastax.astra.client.model.CollectionOptions;
29-
import com.datastax.astra.client.model.CountDocumentsOptions;
26+
import com.datastax.astra.client.model.collections.CollectionIdTypes;
27+
import com.datastax.astra.client.model.collections.CollectionDefinition;
28+
import com.datastax.astra.client.model.collections.CollectionOptions;
29+
import com.datastax.astra.client.model.collections.CountDocumentsOptions;
3030
import com.datastax.astra.client.model.DeleteManyOptions;
3131
import com.datastax.astra.client.model.DeleteOneOptions;
3232
import com.datastax.astra.client.model.DeleteResult;
3333
import com.datastax.astra.client.model.DistinctIterable;
34-
import com.datastax.astra.client.model.Document;
34+
import com.datastax.astra.client.model.collections.Document;
3535
import com.datastax.astra.client.model.EstimatedCountDocumentsOptions;
3636
import com.datastax.astra.client.model.FindIterable;
3737
import com.datastax.astra.client.model.FindOneAndDeleteOptions;
@@ -311,7 +311,7 @@ public String getKeyspaceName() {
311311
* and configuration options. This object provides a comprehensive view of the collection's settings
312312
* and identity within the database.
313313
*/
314-
public CollectionInfo getDefinition() {
314+
public CollectionDefinition getDefinition() {
315315
return database
316316
.listCollections()
317317
.filter(col -> col.getName().equals(collectionName))

astra-db-java/src/main/java/com/datastax/astra/client/Database.java

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
import com.datastax.astra.client.admin.AstraDBDatabaseAdmin;
2525
import com.datastax.astra.client.admin.DataAPIDatabaseAdmin;
2626
import com.datastax.astra.client.admin.DatabaseAdmin;
27-
import com.datastax.astra.client.model.CollectionInfo;
28-
import com.datastax.astra.client.model.CollectionOptions;
29-
import com.datastax.astra.client.model.Document;
27+
import com.datastax.astra.client.model.collections.CollectionDefinition;
28+
import com.datastax.astra.client.model.collections.CollectionOptions;
29+
import com.datastax.astra.client.model.collections.Document;
3030
import com.datastax.astra.client.model.SimilarityMetric;
3131
import com.datastax.astra.client.model.command.Command;
3232
import com.datastax.astra.client.model.command.CommandOptions;
33+
import com.datastax.astra.client.model.tables.TableDefinition;
34+
import com.datastax.astra.client.model.tables.TableInfo;
3335
import com.datastax.astra.internal.api.AstraApiEndpoint;
3436
import com.datastax.astra.internal.command.AbstractCommandRunner;
3537
import com.datastax.astra.internal.command.CommandObserver;
@@ -214,27 +216,45 @@ public DatabaseAdmin getDatabaseAdmin(String superUserToken) {
214216
* a stream containing all the names of all the collections in this database
215217
*/
216218
public Stream<String> listCollectionNames() {
217-
218219
Command findCollections = Command.create("findCollections");
219-
220220
return runCommand(findCollections)
221221
.getStatusKeyAsList("collections", String.class)
222222
.stream();
223223
}
224224

225+
/**
226+
* Finds all the tables in this database.
227+
*
228+
* @return
229+
* list of table definitions
230+
*/
231+
public Stream<TableDefinition> listTables() {
232+
Command findTables = Command
233+
.create("listTables")
234+
.withOptions(new Document().append("explain", true));
235+
return runCommand(findTables)
236+
.getStatusKeyAsList("tables", TableInfo.class)
237+
.stream()
238+
.map(ti -> {
239+
TableDefinition td = ti.getDefinition();
240+
td.setName(ti.getName());
241+
return td;
242+
});
243+
}
244+
225245
/**
226246
* Finds all the collections in this database.
227247
*
228248
* @return
229249
* list of collection definitions
230250
*/
231-
public Stream<CollectionInfo> listCollections() {
251+
public Stream<CollectionDefinition> listCollections() {
232252
Command findCollections = Command
233253
.create("findCollections")
234254
.withOptions(new Document().append("explain", true));
235255

236256
return runCommand(findCollections)
237-
.getStatusKeyAsList("collections", CollectionInfo.class)
257+
.getStatusKeyAsList("collections", CollectionDefinition.class)
238258
.stream();
239259
}
240260

@@ -465,6 +485,15 @@ public void dropCollection(String collectionName) {
465485
log.info("Collection '" + green("{}") + "' has been deleted", collectionName);
466486
}
467487

488+
// ------------------------------------------
489+
// ------- TABLES CRUD -----
490+
// ------------------------------------------
491+
492+
// ------------------------------------------
493+
// ---- Generation Informations ----
494+
// ------------------------------------------
495+
496+
468497
/** {@inheritDoc} */
469498
@Override
470499
protected String getApiEndpoint() {

astra-db-java/src/main/java/com/datastax/astra/client/Table.java

Lines changed: 213 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,217 @@
2020
* #L%
2121
*/
2222

23-
public class Table {
23+
import com.datastax.astra.client.exception.DataApiException;
24+
import com.datastax.astra.client.model.InsertOneOptions;
25+
import com.datastax.astra.client.model.InsertOneResult;
26+
import com.datastax.astra.client.model.collections.Document;
27+
import com.datastax.astra.client.model.command.CommandOptions;
28+
import com.datastax.astra.client.model.tables.TableDefinition;
29+
import com.datastax.astra.internal.command.AbstractCommandRunner;
30+
import com.datastax.astra.internal.command.CommandObserver;
31+
import lombok.Getter;
32+
33+
import static com.datastax.astra.internal.utils.Assert.hasLength;
34+
import static com.datastax.astra.internal.utils.Assert.notNull;
35+
36+
/**
37+
* Execute commands against tables
38+
*/
39+
public class Table<T> extends AbstractCommandRunner {
40+
41+
/** parameters names. */
42+
private static final String ARG_DATABASE = "database";
43+
/** parameters names. */
44+
private static final String ARG_OPTIONS = "options";
45+
/** parameters names. */
46+
private static final String ARG_CLAZZ = "working class 'clazz'";
47+
/** parameters names. */
48+
private static final String ARG_TABLE_NAME = "collectionName";
49+
/** Collection identifier. */
50+
@Getter
51+
private final String tableName;
52+
53+
/** Working class representing documents of the collection. The default value is {@link Document}. */
54+
@Getter
55+
protected final Class<T> rowClass;
56+
57+
/** Parent Database reference. */
58+
@Getter
59+
private final Database database;
60+
61+
/** Get global Settings for the client. */
62+
@Getter
63+
private final DataAPIOptions dataAPIOptions;
64+
65+
/** Api Endpoint for the Database, if using an astra environment it will contain the database id and the database region. */
66+
private final String apiEndpoint;
67+
68+
69+
/**
70+
* Constructs an instance of a table within the specified database. This constructor
71+
* initializes the table with a given name and associates it with a specific class type
72+
* that represents the schema of documents within the table. This setup is designed for
73+
* CRUD (Create, Read, Update, Delete) operations.
74+
*
75+
* @param db The {@code Database} instance representing the client's keyspace for HTTP
76+
* communication with the database. It encapsulates the configuration and management
77+
* of the database connection, ensuring that operations on this collection are
78+
* executed within the context of this database.
79+
* @param tableName A {@code String} that uniquely identifies the table within the
80+
* database. This name is used to route operations to the correct
81+
* table and should adhere to the database's naming conventions.
82+
* @param clazz The {@code Class<DOC>} object that represents the model for rows within
83+
* this table. This class is used for serialization and deserialization of
84+
* rows to and from the database. It ensures type safety and facilitates
85+
* the mapping of database documents to Java objects.
86+
* @param commandOptions the options to apply to the command operation. If left blank the default table
87+
*
88+
* <p>Example usage:</p>
89+
* <pre>
90+
* {@code
91+
* // Given a client
92+
* DataAPIClient client = new DataAPIClient("token");
93+
* // Given a database
94+
* Database myDb = client.getDatabase("myDb");
95+
* // Initialize a collection with a working class
96+
* Table<MyDocumentClass> myTable = new Table<>(myDb, "myTableName", MyDocumentClass.class);
97+
* }
98+
* </pre>
99+
*/
100+
protected Table(Database db, String tableName, CommandOptions<?> commandOptions, Class<T> clazz) {
101+
notNull(db, ARG_DATABASE);
102+
notNull(clazz, ARG_CLAZZ);
103+
hasLength(tableName, ARG_TABLE_NAME);
104+
this.tableName = tableName;
105+
this.database = db;
106+
this.dataAPIOptions = db.getOptions();
107+
this.rowClass = clazz;
108+
this.commandOptions = commandOptions;
109+
this.apiEndpoint = db.getApiEndpoint() + "/" + tableName;
110+
}
111+
112+
// ----------------------------
113+
// --- Global Information ----
114+
// ----------------------------
115+
116+
/**
117+
* Retrieves the name of the parent keyspace associated with this collection. A keyspace in
118+
* this context typically refers to a higher-level categorization or grouping mechanism within
119+
* the database that encompasses one or more collections. This method allows for identifying
120+
* the broader context in which this collection exists, which can be useful for operations
121+
* requiring knowledge of the database structure or for dynamic database interaction patterns.
122+
*
123+
* @return A {@code String} representing the name of the parent keyspace of the current
124+
* collection. This name serves as an identifier for the keyspace and can be used
125+
* to navigate or query the database structure.
126+
*
127+
* <p>Example usage:</p>
128+
* <pre>
129+
* {@code
130+
* Collection myCollection = ... // assume myCollection is already initialized
131+
* String keyspaceName = myCollection.getKeyspaceName();
132+
* System.out.println("The collection belongs to the keyspace: " + namespaceName);
133+
* }
134+
* </pre>
135+
*/
136+
public String getKeyspaceName() {
137+
return getDatabase().getKeyspaceName();
138+
}
139+
140+
/**
141+
* Retrieves the full definition of the collection, encompassing both its name and its configuration options.
142+
* This comprehensive information is encapsulated in a {@code CollectionInfo} object, providing access to the
143+
* collection's metadata and settings.
144+
*
145+
* <p>The returned {@code CollectionInfo} includes details such as the collection's name, which serves as its
146+
* unique identifier within the database, and a set of options that describe its configuration. These options
147+
* may cover aspects like indexing preferences, read/write permissions, and other customizable settings that
148+
* were specified at the time of collection creation.</p>
149+
*
150+
* <p>Example usage:</p>
151+
* <pre>
152+
* {@code
153+
* // Given a collection
154+
* Table<Row> table;
155+
* // Access its Definition
156+
* TableDefinition definition = table.getDefinition();
157+
* System.out.println("Name=" + definition.getName());
158+
* if (options != null) {
159+
* // Operations based on collection options
160+
* }
161+
* }
162+
* </pre>
163+
*
164+
* @return A {@code CollectionInfo} object containing the full definition of the collection, including its name
165+
* and configuration options. This object provides a comprehensive view of the collection's settings
166+
* and identity within the database.
167+
*/
168+
public TableDefinition getDefinition() {
169+
return database
170+
.listTables()
171+
.filter(col -> col.getName().equals(tableName))
172+
.findFirst()
173+
.orElseThrow(() -> new DataApiException("[TABLE_NOT_EXIST] - Table does not exist, " +
174+
"table name: '" + tableName + "'", "TABLE_NOT_EXIST", null));
175+
}
176+
177+
/**
178+
* Retrieves the name of the table. This name serves as a unique identifier within the database and is
179+
* used to reference the collection in database operations such as queries, updates, and deletions. The table
180+
* name is defined at the time of table creation and is immutable.
181+
*
182+
* @return A {@code String} representing the name of the table. This is the same name that was specified
183+
* when the table was created or initialized.
184+
*/
185+
public String getName() {
186+
return tableName;
187+
}
188+
189+
/** {@inheritDoc} */
190+
@Override
191+
protected String getApiEndpoint() {
192+
return apiEndpoint;
193+
}
194+
195+
// --------------------------
196+
// --- Insert* ----
197+
// --------------------------
198+
199+
//FIXME: Add the InsertOneResult
200+
public final InsertOneResult insertOne(T row) {
201+
return null;
202+
}
203+
204+
//FIXME: Add the InsertOneResult
205+
public final InsertOneResult insertOne(T row, InsertOneOptions insertOneOptions) {
206+
return null;
207+
}
208+
209+
// --------------------------
210+
// --- Listeners ----
211+
// --------------------------
212+
213+
/**
214+
* Register a listener to execute commands on the collection. Please now use {@link CommandOptions}.
215+
*
216+
* @param logger
217+
* name for the logger
218+
* @param commandObserver
219+
* class for the logger
220+
*/
221+
public void registerListener(String logger, CommandObserver commandObserver) {
222+
this.commandOptions.registerObserver(logger, commandObserver);
223+
}
224+
225+
/**
226+
* Register a listener to execute commands on the collection. Please now use {@link CommandOptions}.
227+
*
228+
* @param name
229+
* name for the observer
230+
*/
231+
public void deleteListener(String name) {
232+
this.commandOptions.unregisterObserver(name);
233+
}
234+
235+
24236
}

astra-db-java/src/main/java/com/datastax/astra/client/model/DeleteOneOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.collections.Document;
2324
import com.datastax.astra.client.model.command.CommandOptions;
2425
import com.datastax.astra.client.model.query.Sort;
2526
import com.datastax.astra.client.model.query.Sorts;

astra-db-java/src/main/java/com/datastax/astra/client/model/DistinctIterator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.collections.Document;
2324
import com.datastax.astra.internal.utils.JsonUtils;
2425
import lombok.extern.slf4j.Slf4j;
2526

astra-db-java/src/main/java/com/datastax/astra/client/model/FindOneAndDeleteOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.collections.Document;
2324
import com.datastax.astra.client.model.command.CommandOptions;
2425
import com.datastax.astra.client.model.query.Projection;
2526
import com.datastax.astra.client.model.query.Sort;

astra-db-java/src/main/java/com/datastax/astra/client/model/FindOneAndReplaceOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.collections.Document;
2324
import com.datastax.astra.client.model.command.CommandOptions;
2425
import com.datastax.astra.client.model.query.Projection;
2526
import com.datastax.astra.client.model.query.Sort;

astra-db-java/src/main/java/com/datastax/astra/client/model/FindOneAndUpdateOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.collections.Document;
2324
import com.datastax.astra.client.model.command.CommandOptions;
2425
import com.datastax.astra.client.model.query.Projection;
2526
import com.datastax.astra.client.model.query.Sort;

astra-db-java/src/main/java/com/datastax/astra/client/model/FindOneOptions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
* #L%
2121
*/
2222

23+
import com.datastax.astra.client.model.collections.Document;
2324
import com.datastax.astra.client.model.command.CommandOptions;
2425
import com.datastax.astra.client.model.query.Projection;
2526
import com.datastax.astra.client.model.query.Sort;

0 commit comments

Comments
 (0)