|
20 | 20 | * #L%
|
21 | 21 | */
|
22 | 22 |
|
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 | + |
24 | 236 | }
|
0 commit comments