Skip to content

Commit 28e457e

Browse files
tgianosajoymajumdar
authored andcommitted
Cassandra Type Converter (Netflix#66)
1 parent 98783fe commit 28e457e

File tree

20 files changed

+650
-109
lines changed

20 files changed

+650
-109
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ metacat-main/data/
7070

7171
# Functional tests
7272
metacat-functional-tests/metacat-test-cluster/build/
73+
/metacat-functional-tests/metastore_db/

gradle.properties

+5-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ release.scope=major
1616

1717
amazon_sns_version=1.11.60
1818
airlift_version=0.116
19+
cassandra_driver_version=3.1.4
1920
commons_dbcp_version=2.1
2021
guava_version=19.0
2122
guice_version=4.0
23+
hadoopcore_version=1.2.1
24+
hive_version=1.2.1
2225
jackson_version=2.5.5
2326
jersey_version=2.19
24-
slf4j_version=1.7.12
25-
swagger_version=1.3.12
26-
hive_version=1.2.1
27-
hadoopcore_version=1.2.1
2827
mysql_connector_version=5.1.35
2928
postgresql_driver_version=42.0.0
3029
redshift_driver_version=1.2.1.1001
30+
slf4j_version=1.7.12
31+
swagger_version=1.3.12
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,49 @@
11
/*
2-
* Copyright 2016 Netflix, Inc.
2+
*
3+
* Copyright 2017 Netflix, Inc.
4+
*
35
* Licensed under the Apache License, Version 2.0 (the "License");
46
* you may not use this file except in compliance with the License.
57
* You may obtain a copy of the License at
8+
*
69
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
711
* Unless required by applicable law or agreed to in writing, software
812
* distributed under the License is distributed on an "AS IS" BASIS,
913
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1014
* See the License for the specific language governing permissions and
1115
* limitations under the License.
16+
*
1217
*/
13-
1418
package com.netflix.metacat.common.server.connectors;
1519

1620
import com.netflix.metacat.common.type.Type;
21+
import lombok.NonNull;
22+
23+
import javax.annotation.Nonnull;
1724

1825
/**
1926
* Canonical type converter class.
27+
*
28+
* @author tgianos
2029
* @author zhenl
30+
* @since 1.0.0
2131
*/
2232
public interface ConnectorTypeConverter {
33+
2334
/**
2435
* Converts to metacat type.
36+
*
2537
* @param type type
2638
* @return metacat type
2739
*/
28-
Type toMetacatType(String type);
40+
Type toMetacatType(@Nonnull @NonNull final String type);
2941

3042
/**
3143
* Converts from metacat type.
3244
*
3345
* @param type type
3446
* @return connector type
3547
*/
36-
String fromMetacatType(Type type);
48+
String fromMetacatType(@Nonnull @NonNull final Type type);
3749
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,51 @@
1+
/*
2+
*
3+
* Copyright 2017 Netflix, Inc.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
118
package com.netflix.metacat.common.server.converter;
219

320
import com.netflix.metacat.common.server.connectors.ConnectorTypeConverter;
421
import com.netflix.metacat.common.type.Type;
522
import com.netflix.metacat.common.type.TypeRegistry;
623
import com.netflix.metacat.common.type.TypeSignature;
24+
import lombok.NonNull;
25+
26+
import javax.annotation.Nonnull;
727

828
/**
929
* Default type converter. Converter for metacat type representations.
1030
*
1131
* @author amajumdar
32+
* @since 1.0.0
1233
*/
1334
public class DefaultTypeConverter implements ConnectorTypeConverter {
35+
/**
36+
* {@inheritDoc}
37+
*/
1438
@Override
15-
public Type toMetacatType(final String type) {
39+
public Type toMetacatType(@Nonnull @NonNull final String type) {
1640
final TypeSignature signature = TypeSignature.parseTypeSignature(type);
1741
return TypeRegistry.getTypeRegistry().getType(signature);
1842
}
1943

44+
/**
45+
* {@inheritDoc}
46+
*/
2047
@Override
21-
public String fromMetacatType(final Type type) {
48+
public String fromMetacatType(@Nonnull @NonNull final Type type) {
2249
return type.getDisplayName();
2350
}
2451
}

metacat-common/src/main/java/com/netflix/metacat/common/type/RowType.java

+94-43
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,96 @@
1818
import com.google.common.collect.ImmutableList;
1919
import com.google.common.collect.Lists;
2020
import lombok.Getter;
21+
import lombok.NonNull;
2122

23+
import javax.annotation.Nonnull;
2224
import javax.annotation.Nullable;
2325
import java.util.Collections;
2426
import java.util.List;
2527

2628
/**
2729
* Row type.
30+
*
31+
* @author tgianos
2832
* @author zhenl
33+
* @since 1.0.0
2934
*/
3035
public class RowType extends AbstractType implements ParametricType {
31-
/** default type. */
32-
public static final RowType ROW = new RowType(Collections.<Type>emptyList(), Collections.<String>emptyList());
36+
/**
37+
* default type.
38+
*/
39+
static final RowType ROW = new RowType(Collections.<RowField>emptyList());
3340

3441
@Getter
3542
private final List<RowField> fields;
43+
3644
/**
3745
* Constructor.
38-
* @param fieldTypes fieldTypes
39-
* @param fieldNames fieldNames
46+
*
47+
* @param fields The fields of this row
4048
*/
41-
public RowType(final List<Type> fieldTypes, final List<String> fieldNames) {
42-
super(new TypeSignature(
49+
public RowType(@Nonnull @NonNull final List<RowField> fields) {
50+
super(
51+
new TypeSignature(
4352
TypeEnum.ROW,
44-
Lists.transform(fieldTypes, new Function<Type, TypeSignature>() {
45-
public TypeSignature apply(@Nullable final Type input) {
46-
return input == null ? null : input.getTypeSignature();
53+
Lists.transform(
54+
Lists.transform(
55+
fields,
56+
new Function<RowField, Type>() {
57+
public Type apply(@Nullable final RowField input) {
58+
return input == null ? null : input.getType();
59+
}
60+
}
61+
),
62+
new Function<Type, TypeSignature>() {
63+
public TypeSignature apply(@Nullable final Type input) {
64+
return input == null ? null : input.getTypeSignature();
65+
}
66+
}),
67+
!fields.isEmpty() && fields.get(0).getName() != null
68+
? Lists.transform(fields, new Function<RowField, Object>() {
69+
public Object apply(@Nullable final RowField input) {
70+
return input == null ? null : input.getName();
71+
}
4772
}
48-
}),
49-
fieldNames == null ? Lists.newArrayList() : Lists.<Object>newArrayList(fieldNames)
73+
)
74+
: null
5075
)
5176
);
5277

78+
this.fields = ImmutableList.copyOf(fields);
79+
}
80+
81+
/**
82+
* Create a new Row Type.
83+
*
84+
* @param types The types to create can not be empty
85+
* @param names The literals to use. Can be null but if not must be the same length as types.
86+
* @return a new RowType
87+
*/
88+
public static RowType createRowType(
89+
@Nonnull @NonNull final List<Type> types,
90+
@Nullable final List<String> names
91+
) {
92+
Preconditions.checkArgument(!types.isEmpty(), "types is empty");
93+
5394
final ImmutableList.Builder<RowField> builder = ImmutableList.builder();
54-
for (int i = 0; i < fieldTypes.size(); i++) {
55-
builder.add(new RowField(fieldTypes.get(i), fieldNames.get(i)));
95+
if (names == null) {
96+
for (final Type type : types) {
97+
builder.add(new RowField(type, null));
98+
}
99+
} else {
100+
Preconditions.checkArgument(
101+
types.size() == names.size(),
102+
"types and names must be matched in size"
103+
);
104+
for (int i = 0; i < types.size(); i++) {
105+
builder.add(
106+
new RowField(types.get(i), names.get(i))
107+
);
108+
}
56109
}
57-
fields = builder.build();
110+
return new RowType(builder.build());
58111
}
59112

60113
@Override
@@ -63,47 +116,45 @@ public TypeEnum getBaseType() {
63116
}
64117

65118
@Override
66-
public RowType createType(final List<Type> types, final List<Object> literals) {
67-
Preconditions.checkArgument(!types.isEmpty(), "types is empty");
68-
69-
if (literals.isEmpty()) {
70-
return new RowType(types, Lists.<String>newArrayList());
119+
public RowType createType(@Nonnull @NonNull final List<Type> types, @Nullable final List<Object> literals) {
120+
if (literals != null) {
121+
final ImmutableList.Builder<String> builder = ImmutableList.builder();
122+
for (final Object literal : literals) {
123+
builder.add(TypeUtils.checkType(literal, String.class, "literal"));
124+
}
125+
return RowType.createRowType(types, builder.build());
126+
} else {
127+
return RowType.createRowType(types, null);
71128
}
129+
}
72130

73-
Preconditions.checkArgument(types.size() == literals.size(), "types and literals must be matched in size");
74-
75-
final ImmutableList.Builder<String> builder = ImmutableList.builder();
76-
for (Object literal : literals) {
77-
builder.add(TypeUtils.checkType(literal, String.class, "literal"));
131+
@Override
132+
public List<Type> getParameters() {
133+
final ImmutableList.Builder<Type> result = ImmutableList.builder();
134+
for (final RowField field : this.fields) {
135+
result.add(field.getType());
78136
}
79-
return new RowType(types, builder.build());
137+
return result.build();
80138
}
81139

82140
/**
83141
* Row field.
84142
*/
85143
public static class RowField {
86-
@Getter private final Type type;
87-
@Getter private final String name;
88-
89-
/** constructor.
144+
@Getter
145+
private final Type type;
146+
@Getter
147+
private final String name;
148+
149+
/**
150+
* constructor.
151+
*
90152
* @param type type
91153
* @param name name
92154
*/
93-
public RowField(final Type type, final String name) {
94-
this.type = Preconditions.checkNotNull(type, "type is null");
95-
this.name = Preconditions.checkNotNull(name, "name is null");
96-
}
97-
98-
}
99-
100-
@Override
101-
public List<Type> getParameters() {
102-
final ImmutableList.Builder<Type> result = ImmutableList.builder();
103-
for (RowField field: fields) {
104-
result.add(field.getType());
155+
public RowField(@Nonnull @NonNull final Type type, @Nullable final String name) {
156+
this.type = type;
157+
this.name = name;
105158
}
106-
return result.build();
107159
}
108-
109160
}

0 commit comments

Comments
 (0)