8000 JAVA-825: Parse materialized views metadata. by adutra · Pull Request #453 · apache/cassandra-java-driver · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,28 @@
package com.datastax.driver.core;

import java.util.*;
import java.util.Map.Entry;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

/**
* Describes a Column.
*/
public class ColumnMetadata {

static final String COLUMN_NAME = "column_name";
static final String VALIDATOR = "validator";
static final String COMPONENT_INDEX = "component_index";
static final String KIND = "type";

static final String VALIDATOR = "validator"; // v2 only
static final String TYPE = "type"; // replaces validator, v3 onwards

static final String COMPONENT_INDEX = "component_index"; // v2 only
static final String POSITION = "position"; // replaces component_index, v3 onwards

static final String KIND_V2 = "type"; // v2 only
static final String KIND_V3 = "kind"; // replaces type, v3 onwards

static final String INDEX_TYPE = "index_type";
static final String INDEX_OPTIONS = "index_options";
static final String INDEX_NAME = "index_name";

private final TableMetadata table;
private final TableOrView parent;
private final String name;
private final DataType type;
private final boolean isStatic;
Expand All @@ -45,14 +46,14 @@ public class ColumnMetadata {
// between columns and indexes, and is updated only after the column is created
final Map<String, IndexMetadata> indexes = new LinkedHashMap<String, IndexMetadata>();

private ColumnMetadata(TableMetadata table, String name, DataType type, boolean isStatic) {
this.table = table;
private ColumnMetadata(TableOrView parent, String name, DataType type, boolean isStatic) {
this.parent = parent;
this.name = name;
this.type = type;
this.isStatic = isStatic;
}

static ColumnMetadata fromRaw(TableMetadata tm, Raw raw) {
static ColumnMetadata fromRaw(TableOrView tm, Raw raw) {
return new ColumnMetadata(tm, raw.name, raw.dataType, raw.kind == Raw.Kind.STATIC);
}

Expand All @@ -70,12 +71,13 @@ public String getName() {
}

/**
* Returns the metadata of the table this column is part of.
* Returns the parent object of this column. This can be a {@link TableMetadata}
* or a {@link MaterializedViewMetadata} object.
*
* @return the {@code TableMetadata} for the table this column is part of.
* @return the parent object of this column.
*/
public TableMetadata getTable() {
return table;
public TableOrView getParent() {
return parent;
}

/**
Expand Down Expand Up @@ -163,35 +165,50 @@ static Kind fromStringV3(String s) {

public final String name;
public Kind kind;
public final int componentIndex;
public final int position;
public final DataType dataType;
public final boolean isReversed;

public final Map<String, String> indexColumns = new HashMap<String, String>();

Raw(String name, Kind kind, int componentIndex, DataType dataType, boolean isReversed) {
Raw(String name, Kind kind, int position, DataType dataType, boolean isReversed) {
this.name = name;
this.kind = kind;
this.componentIndex = componentIndex;
this.position = position;
this.dataType = dataType;
this.isReversed = isReversed;
}

static Raw fromRow(Row row, VersionNumber version, ProtocolVersion protocolVersion, CodecRegistry codecRegistry) {
String name = row.getString(COLUMN_NAME);

Kind kind;
if(version.getMajor() < 2 || row.isNull(KIND)) {
if(version.getMajor() < 2) {
kind = Kind.REGULAR;
} else if (version.getMajor() < 3) {
kind = Kind.fromStringV2(row.getString(KIND));
kind = row.isNull(KIND_V2) ? Kind.REGULAR : Kind.fromStringV2(row.getString(KIND_V2));
} else {
kind = row.isNull(KIND_V3) ? Kind.REGULAR : Kind.fromStringV3(row.getString(KIND_V3));
}

int position;
if(version.getMajor() >= 3) {
position = row.getInt(POSITION); // cannot be null, -1 is used as a special value instead of null to avoid tombstones
if(position == -1) position = 0;
} else {
position = row.isNull(COMPONENT_INDEX) ? 0 : row.getInt(COMPONENT_INDEX);
}

String dataTypeStr;
if(version.getMajor() >= 3) {
dataTypeStr = row.getString(TYPE);
} else {
kind = Kind.fromStringV3(row.getString(KIND));
dataTypeStr = row.getString(VALIDATOR);
}
int componentIndex = row.isNull(COMPONENT_INDEX) ? 0 : row.getInt(COMPONENT_INDEX);
String validatorStr = row.getString(VALIDATOR);
boolean reversed = CassandraTypeParser.isReversed(validatorStr);
DataType dataType = CassandraTypeParser.parseOne(validatorStr, protocolVersion, codecRegistry);
Raw c = new Raw(name, kind, componentIndex, dataType, reversed);
DataType dataType = CassandraTypeParser.parseOne(dataTypeStr, protocolVersion, codecRegistry);
boolean reversed = CassandraTypeParser.isReversed(dataTypeStr);

Raw c = new Raw(name, kind, position, dataType, reversed);

// secondary indexes (C* < 3.0.0)
// from C* 3.0 onwards 2i are defined in a separate table
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ static IndexMetadata fromLegacy(ColumnMetadata column, ColumnMetadata.Raw raw) {
} else {
options = SimpleJSONParser.parseStringMap(indexOptionsCol);
}
return new IndexMetadata(column.getTable(), indexName, columns, indexType, TargetType.COLUMN, options);
return new IndexMetadata((TableMetadata)column.getParent(), indexName, columns, indexType, TargetType.COLUMN, options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class KeyspaceMetadata {
private final Map<String, String> replication;

private final Map<String, TableMetadata> tables = new ConcurrentHashMap<String, TableMetadata>();
private final Map<String, MaterializedViewMetadata> views = new ConcurrentHashMap<String, MaterializedViewMetadata>();
private final Map<String, UserType> userTypes = new ConcurrentHashMap<String, UserType>();
final Map<String, FunctionMetadata> functions = new ConcurrentHashMap<String, FunctionMetadata>();
private final Map<String, AggregateMetadata> aggregates = new ConcurrentHashMap<String, AggregateMetadata>();
Expand Down Expand Up @@ -123,6 +124,31 @@ public Collection<TableMetadata> getTables() {
return Collections.<TableMetadata>unmodifiableCollection(tables.values());
}

/**
* Returns the metadata for a materialized view contained in this keyspace.
*
* @param name the name of materialized view to retrieve
* @return the metadata for materialized view {@code name} if it exists in this keyspace,
* {@code null} otherwise.
*/
public MaterializedViewMetadata getMaterializedView(String name) {
return views.get(Metadata.handleId(name));
}

void removeMaterializedView(String materializedView) {
views.remove(materializedView);
}

/**
* Returns the materialized views defined in this keyspace.
*
* @return a collection of the metadata for the materialized views defined in this
* keyspace.
*/
public Collection<MaterializedViewMetadata> getMaterializedViews() {
return Collections.unmodifiableCollection(views.values());
}

/**
* Returns the definition for a user defined type (UDT) in this keyspace.
*
Expand Down Expand Up @@ -291,6 +317,10 @@ void add(TableMetadata tm) {
tables.put(tm.getName(), tm);
}

void add(MaterializedViewMetadata view) {
views.put(view.getName(), view);
}

void add(FunctionMetadata function) {
functions.put(function.getFullName(), function);
}
Expand Down
Loading
0