Java Code Examples for org.apache.cassandra.cql3.statements.AlterTableStatementColumn

Following code examples demonstrate how to use org.apache.cassandra.cql3.statements.AlterTableStatementColumn from cassandra. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.apache.cassandra.cql3.statements.AlterTableStatementColumn and various code implementation of this class.
Example 1
Project : cassandra Source File : SchemaManager.java View Source Code on GitHub

    }

    public KeyspaceMetadata updateTable(final KeyspaceMetadata ksm, String cfName, String indexName,
            List<ColumnDescriptor> columns,
            TableAttributes tableAttrs,
            final IndexMetaData indexMetaData,
            final Collection<Mutation> mutations,
            final Collection<Event.SchemaChange> events) {
        KeyspaceMetadata ksm2 = ksm;
        CFMetaData cfm = ThriftValidation.validateColumnFamily(ksm, cfName);
        CFName cfn = new CFName();
        cfn.setColumnFamily(cfName, true);
        cfn.setKeyspace(ksm.name, true);

        List<AlterTableStatementColumn> colDataList = columns.stream()
                .filter(cd -> !cd.exists())
                .map(cd ->  new AlterTableStatementColumn(ColumnDefinition.Raw.forColumn(cd.createColumnDefinition(ksm, cfName)), cd.type, cd.kind == Kind.STATIC))
                .collect(Collectors.toList());

        boolean changed = false;
        Pair<CFMetaData, List<ViewDefinition>> x = Pair.create(cfm, StreamSupport.stream(ksm.views(cfName).spliterator(), false).collect(Collectors.toList()));
        if (colDataList.size() > 0) {
            logger.debug("table {}.{} add columns={}", ksm.name, cfName, columns);
            AlterTableStatement ats = new AlterTableStatement(cfn, AlterTableStatement.Type.ADD, colDataList, tableAttrs, null, null);
            x = ats.updateTable(ksm, cfm, FBUtilities.timestampMicros());
            mutations.add(SchemaKeyspace.makeUpdateTableMutation(ksm, cfm, x.left, FBUtilities.timestampMicros()).build());
            changed = true;
        }

        logger.debug("table {}.{} set extensions for index {}", ksm.name, cfName, indexName);
        Mutation.SimpleBuilder builder = SchemaKeyspace.makeCreateKeyspaceMutation(ksm.name, FBUtilities.timestampMicros());
        x = Pair.create(addTableExtensionsToMutationBuilder(x.left, indexName, indexMetaData, builder), x.right);
        mutations.add(builder.build());

        ksm2 = ksm.withSwapped(ksm.tables.without(cfm.cfName).with(x.left));
        if (x.right != null && x.right.size() > 0) {
            ksm2 = ksm2.withSwapped(Views.builder().add(ksm2.views).add(x.right).build());
            for(ViewDefinition view : x.right) {
                builder = SchemaKeyspace.makeCreateKeyspaceMutation(ksm.name, FBUtilities.timestampMicros());
                mutations.add(SchemaKeyspace.makeUpdateViewMutation(builder, ksm2.views.getNullable(view.viewName), view).build());
            }
        }
        events.add(new Event.SchemaChange(Event.SchemaChange.Change.UPDATED, Event.SchemaChange.Target.TABLE, ksm.name, cfName));
        return ksm2;
    }