forked from hector-client/hector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThriftColumnDef.java
executable file
·119 lines (102 loc) · 3.27 KB
/
ThriftColumnDef.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package me.prettyprint.cassandra.service;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import me.prettyprint.cassandra.utils.Assert;
import me.prettyprint.hector.api.ddl.ColumnDefinition;
import me.prettyprint.hector.api.ddl.ColumnIndexType;
import org.apache.cassandra.thrift.ColumnDef;
import org.apache.cassandra.thrift.IndexType;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
public class ThriftColumnDef implements ColumnDefinition {
private final ByteBuffer name; //TODO(ran): use a serializer and type safety?
private final String validationClass;
private final ColumnIndexType indexType;
private final String indexName;
public ThriftColumnDef(ColumnDef cd) {
Assert.notNull(cd, "ColumnDef is null");
name = cd.name;
validationClass = cd.validation_class;
indexType = indexTypeFromThrift(cd.index_type);
indexName = cd.index_name;
}
public ThriftColumnDef(ColumnDefinition columnDefinition) {
name = columnDefinition.getName();
validationClass = columnDefinition.getValidationClass();
indexType = columnDefinition.getIndexType();
indexName = columnDefinition.getIndexName();
}
private ColumnIndexType indexTypeFromThrift(IndexType tIndexType) {
if ( tIndexType == null ) return null;
switch (tIndexType) {
case KEYS:
return ColumnIndexType.KEYS;
case CUSTOM:
return ColumnIndexType.CUSTOM;
default:
throw new RuntimeException("Unknown thrift IndexType: " + tIndexType);
}
}
public static List<ColumnDefinition> fromThriftList(List<ColumnDef> columnDefs) {
if (columnDefs == null || columnDefs.isEmpty()) {
return Collections.emptyList();
}
List<ColumnDefinition> l = new ArrayList<ColumnDefinition>(columnDefs.size());
for (ColumnDef cd: columnDefs) {
l.add(new ThriftColumnDef(cd));
}
return l;
}
@Override
public ByteBuffer getName() {
return name;
}
@Override
public String getValidationClass() {
return validationClass;
}
@Override
public ColumnIndexType getIndexType() {
return indexType;
}
@Override
public String getIndexName() {
return indexName;
}
public static List<ColumnDef> toThriftList(List<ColumnDefinition> columnDefs) {
if (columnDefs == null || columnDefs.isEmpty()) {
return Collections.emptyList();
}
List<ColumnDef> l = new ArrayList<ColumnDef>(columnDefs.size());
for (ColumnDefinition d: columnDefs) {
l.add((new ThriftColumnDef(d)).toThrift());
}
return l;
}
private ColumnDef toThrift() {
ColumnDef d = new ColumnDef();
if ( indexName != null ) {
d.setIndex_name(indexName);
d.setIndex_type(indexTypeToThrift(indexType));
}
d.setName(name);
d.setValidation_class(validationClass);
return d;
}
private IndexType indexTypeToThrift(ColumnIndexType indexType2) {
switch (indexType2) {
case KEYS:
return IndexType.KEYS;
case CUSTOM:
return IndexType.CUSTOM;
default:
throw new RuntimeException("Unknown ColumnIndexType value: " + indexType2);
}
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
}