Skip to content

Commit

Permalink
Cassandra Type Converter (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
tgianos authored and ajoymajumdar committed May 11, 2017
1 parent 98783fe commit 28e457e
Show file tree
Hide file tree
Showing 20 changed files with 650 additions and 109 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ metacat-main/data/

# Functional tests
metacat-functional-tests/metacat-test-cluster/build/
/metacat-functional-tests/metastore_db/
9 changes: 5 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ release.scope=major

amazon_sns_version=1.11.60
airlift_version=0.116
cassandra_driver_version=3.1.4
commons_dbcp_version=2.1
guava_version=19.0
guice_version=4.0
hadoopcore_version=1.2.1
hive_version=1.2.1
jackson_version=2.5.5
jersey_version=2.19
slf4j_version=1.7.12
swagger_version=1.3.12
hive_version=1.2.1
hadoopcore_version=1.2.1
mysql_connector_version=5.1.35
postgresql_driver_version=42.0.0
redshift_driver_version=1.2.1.1001
slf4j_version=1.7.12
swagger_version=1.3.12
Original file line number Diff line number Diff line change
@@ -1,37 +1,49 @@
/*
* Copyright 2016 Netflix, Inc.
*
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package com.netflix.metacat.common.server.connectors;

import com.netflix.metacat.common.type.Type;
import lombok.NonNull;

import javax.annotation.Nonnull;

/**
* Canonical type converter class.
*
* @author tgianos
* @author zhenl
* @since 1.0.0
*/
public interface ConnectorTypeConverter {

/**
* Converts to metacat type.
*
* @param type type
* @return metacat type
*/
Type toMetacatType(String type);
Type toMetacatType(@Nonnull @NonNull final String type);

/**
* Converts from metacat type.
*
* @param type type
* @return connector type
*/
String fromMetacatType(Type type);
String fromMetacatType(@Nonnull @NonNull final Type type);
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,51 @@
/*
*
* Copyright 2017 Netflix, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.netflix.metacat.common.server.converter;

import com.netflix.metacat.common.server.connectors.ConnectorTypeConverter;
import com.netflix.metacat.common.type.Type;
import com.netflix.metacat.common.type.TypeRegistry;
import com.netflix.metacat.common.type.TypeSignature;
import lombok.NonNull;

import javax.annotation.Nonnull;

/**
* Default type converter. Converter for metacat type representations.
*
* @author amajumdar
* @since 1.0.0
*/
public class DefaultTypeConverter implements ConnectorTypeConverter {
/**
* {@inheritDoc}
*/
@Override
public Type toMetacatType(final String type) {
public Type toMetacatType(@Nonnull @NonNull final String type) {
final TypeSignature signature = TypeSignature.parseTypeSignature(type);
return TypeRegistry.getTypeRegistry().getType(signature);
}

/**
* {@inheritDoc}
*/
@Override
public String fromMetacatType(final Type type) {
public String fromMetacatType(@Nonnull @NonNull final Type type) {
return type.getDisplayName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,43 +18,96 @@
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import lombok.Getter;
import lombok.NonNull;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

/**
* Row type.
*
* @author tgianos
* @author zhenl
* @since 1.0.0
*/
public class RowType extends AbstractType implements ParametricType {
/** default type. */
public static final RowType ROW = new RowType(Collections.<Type>emptyList(), Collections.<String>emptyList());
/**
* default type.
*/
static final RowType ROW = new RowType(Collections.<RowField>emptyList());

@Getter
private final List<RowField> fields;

/**
* Constructor.
* @param fieldTypes fieldTypes
* @param fieldNames fieldNames
*
* @param fields The fields of this row
*/
public RowType(final List<Type> fieldTypes, final List<String> fieldNames) {
super(new TypeSignature(
public RowType(@Nonnull @NonNull final List<RowField> fields) {
super(
new TypeSignature(
TypeEnum.ROW,
Lists.transform(fieldTypes, new Function<Type, TypeSignature>() {
public TypeSignature apply(@Nullable final Type input) {
return input == null ? null : input.getTypeSignature();
Lists.transform(
Lists.transform(
fields,
new Function<RowField, Type>() {
public Type apply(@Nullable final RowField input) {
return input == null ? null : input.getType();
}
}
),
new Function<Type, TypeSignature>() {
public TypeSignature apply(@Nullable final Type input) {
return input == null ? null : input.getTypeSignature();
}
}),
!fields.isEmpty() && fields.get(0).getName() != null
? Lists.transform(fields, new Function<RowField, Object>() {
public Object apply(@Nullable final RowField input) {
return input == null ? null : input.getName();
}
}
}),
fieldNames == null ? Lists.newArrayList() : Lists.<Object>newArrayList(fieldNames)
)
: null
)
);

this.fields = ImmutableList.copyOf(fields);
}

/**
* Create a new Row Type.
*
* @param types The types to create can not be empty
* @param names The literals to use. Can be null but if not must be the same length as types.
* @return a new RowType
*/
public static RowType createRowType(
@Nonnull @NonNull final List<Type> types,
@Nullable final List<String> names
) {
Preconditions.checkArgument(!types.isEmpty(), "types is empty");

final ImmutableList.Builder<RowField> builder = ImmutableList.builder();
for (int i = 0; i < fieldTypes.size(); i++) {
builder.add(new RowField(fieldTypes.get(i), fieldNames.get(i)));
if (names == null) {
for (final Type type : types) {
builder.add(new RowField(type, null));
}
} else {
Preconditions.checkArgument(
types.size() == names.size(),
"types and names must be matched in size"
);
for (int i = 0; i < types.size(); i++) {
builder.add(
new RowField(types.get(i), names.get(i))
);
}
}
fields = builder.build();
return new RowType(builder.build());
}

@Override
Expand All @@ -63,47 +116,45 @@ public TypeEnum getBaseType() {
}

@Override
public RowType createType(final List<Type> types, final List<Object> literals) {
Preconditions.checkArgument(!types.isEmpty(), "types is empty");

if (literals.isEmpty()) {
return new RowType(types, Lists.<String>newArrayList());
public RowType createType(@Nonnull @NonNull final List<Type> types, @Nullable final List<Object> literals) {
if (literals != null) {
final ImmutableList.Builder<String> builder = ImmutableList.builder();
for (final Object literal : literals) {
builder.add(TypeUtils.checkType(literal, String.class, "literal"));
}
return RowType.createRowType(types, builder.build());
} else {
return RowType.createRowType(types, null);
}
}

Preconditions.checkArgument(types.size() == literals.size(), "types and literals must be matched in size");

final ImmutableList.Builder<String> builder = ImmutableList.builder();
for (Object literal : literals) {
builder.add(TypeUtils.checkType(literal, String.class, "literal"));
@Override
public List<Type> getParameters() {
final ImmutableList.Builder<Type> result = ImmutableList.builder();
for (final RowField field : this.fields) {
result.add(field.getType());
}
return new RowType(types, builder.build());
return result.build();
}

/**
* Row field.
*/
public static class RowField {
@Getter private final Type type;
@Getter private final String name;

/** constructor.
@Getter
private final Type type;
@Getter
private final String name;

/**
* constructor.
*
* @param type type
* @param name name
*/
public RowField(final Type type, final String name) {
this.type = Preconditions.checkNotNull(type, "type is null");
this.name = Preconditions.checkNotNull(name, "name is null");
}

}

@Override
public List<Type> getParameters() {
final ImmutableList.Builder<Type> result = ImmutableList.builder();
for (RowField field: fields) {
result.add(field.getType());
public RowField(@Nonnull @NonNull final Type type, @Nullable final String name) {
this.type = type;
this.name = name;
}
return result.build();
}

}
Loading

0 comments on commit 28e457e

Please sign in to comment.