public ClusteredLoader(Mapper<Data> mapper, Class<Data> dataClass, Class<CKey> ckeyClass, String tableName) { MappingManager manager = mapper.getManager(); session = manager.getSession(); this.mapper = manager.mapper(dataClass); String keyspace = mapper.getTableMetadata().getKeyspace().getName(); MaterializedViewMetadata mv = mapper.getTableMetadata().getKeyspace().getMaterializedView(tableName); AbstractTableMetadata tableMetadata = mv == null ? mapper.getTableMetadata().getKeyspace().getTable(tableName) : mv; if (tableMetadata == null) { throw new IllegalArgumentException("No table or materialized view " + keyspace + "." + tableName + "found"); } List<ColumnMetadata> primaryKey = tableMetadata.getPrimaryKey(); String pkEq = exceptLast(primaryKey).stream() .map(c -> c.getName() + "=?") .collect(Collectors.joining(" and ")); List<ColumnMetadata> clusteringColumns = tableMetadata.getClusteringColumns(); String orderByDesc = orderBy(clusteringColumns, "DESC"); String orderByAsc = orderBy(clusteringColumns, "ASC"); String indexColumn = clusteringColumns.get(clusteringColumns.size() - 1).getName(); indexAccessor = CassandraUtil.findProperty(dataClass, ckeyClass, indexColumn); selectUnbounded = prepare(String.format("select * from %s.%s where " + pkEq + " order by %s limit ?", keyspace, tableName, orderByDesc)); selectBefore = prepare(String.format("select * from %s.%s where "+pkEq+" and %s < ? order by %s limit ?", keyspace, tableName, indexColumn, orderByDesc)); selectAfter = prepare(String.format("select * from %s.%s where "+pkEq+" and %s > ? order by %s limit ?", keyspace, tableName, indexColumn, orderByDesc)); selectBeforeAfter = prepare(String.format("select * from %s.%s where "+pkEq+" and %s < ? and %s > ? order by %s limit ?", keyspace, tableName, indexColumn, indexColumn, orderByDesc)); selectUnboundedAsc = prepare(String.format("select * from %s.%s where "+pkEq+" order by %s limit ?", keyspace, tableName, orderByAsc)); selectBeforeAsc = prepare(String.format("select * from %s.%s where "+pkEq+" and %s < ? order by %s limit ?", keyspace, tableName, indexColumn, orderByAsc)); selectAfterAsc = prepare(String.format("select * from %s.%s where "+pkEq+" and %s > ? order by %s limit ?", keyspace, tableName, indexColumn, orderByAsc)); selectBeforeAfterAsc = prepare(String.format("select * from %s.%s where "+pkEq+" and %s < ? and %s > ? order by %s limit ?", keyspace, tableName, indexColumn, indexColumn, orderByAsc)); selectByIdKey = prepare(String.format("select * from %s.%s where "+pkEq+" and %s=?", keyspace, tableName, indexColumn)); deleteByIdKey = prepare(String.format("delete from %s.%s where "+pkEq+" and %s=?", keyspace, tableName, indexColumn)); selectAllById = prepare(String.format("select * from %s.%s where " + pkEq, keyspace, tableName)); deleteAllById = prepare(String.format("delete from %s.%s where "+pkEq, keyspace, tableName)); }
/** * {@inheritDoc} */ @Override public List<QualifiedName> listViewNames( @Nonnull @NonNull final ConnectorRequestContext context, @Nonnull @NonNull final QualifiedName databaseName ) { final String catalogName = databaseName.getCatalogName(); final String keyspace = databaseName.getDatabaseName(); log.debug("Attempting to get materialized view names for keyspace {} due to request {}", keyspace, context); try { final KeyspaceMetadata keyspaceMetadata = this.getCluster().getMetadata().getKeyspace(keyspace); if (keyspaceMetadata == null) { throw new DatabaseNotFoundException(databaseName); } final ImmutableList.Builder<QualifiedName> viewsBuilder = ImmutableList.builder(); for (final MaterializedViewMetadata view : keyspaceMetadata.getMaterializedViews()) { viewsBuilder.add( QualifiedName.ofView(catalogName, keyspace, view.getBaseTable().getName(), view.getName()) ); } final List<QualifiedName> views = viewsBuilder.build(); log.debug("Successfully found {} views for keyspace {} due to request {}", views.size(), keyspace, context); return views; } catch (final DriverException de) { log.error(de.getMessage(), de); throw this.getExceptionMapper().toConnectorException(de, databaseName); } }
@Override protected Map<String, Table> getTableMap() { final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder(); for (TableMetadata table : getKeyspace().getTables()) { String tableName = table.getName(); builder.put(tableName, new CassandraTable(this, tableName)); for (MaterializedViewMetadata view : table.getViews()) { String viewName = view.getName(); builder.put(viewName, new CassandraTable(this, viewName, true)); } } return builder.build(); }
public MaterializedViewMetadata getMaterializedView(String name) throws CEUException;
public Collection<MaterializedViewMetadata> getMaterializedViews() throws CEUException;
public MaterializedViewMetadata getMaterializedView(String name) { return keyspaceMetadata.getMaterializedView(name); }
public Collection<MaterializedViewMetadata> getMaterializedViews() { return keyspaceMetadata.getMaterializedViews(); }
@Override public void onMaterializedViewChanged(MaterializedViewMetadata materializedViewMetadata, MaterializedViewMetadata materializedViewMetadata1) {}
@Override public void onMaterializedViewAdded(MaterializedViewMetadata view) { }
@Override public void onMaterializedViewRemoved(MaterializedViewMetadata view) { }
@Override public void onMaterializedViewChanged(MaterializedViewMetadata current, MaterializedViewMetadata previous) { }
@Override public void onMaterializedViewAdded(MaterializedViewMetadata materializedViewMetadata) {}
@Override public void onMaterializedViewRemoved(MaterializedViewMetadata materializedViewMetadata) {}