forked from apache/cassandra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
TableMetadataTest.java
170 lines (148 loc) · 9.33 KB
/
TableMetadataTest.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://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 org.apache.cassandra.schema;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicReference;
import com.google.common.collect.ImmutableMap;
import org.junit.Test;
import org.apache.cassandra.db.Clustering;
import org.apache.cassandra.db.commitlog.CommitLogPosition;
import org.apache.cassandra.db.marshal.BooleanType;
import org.apache.cassandra.db.marshal.CompositeType;
import org.apache.cassandra.db.marshal.FloatType;
import org.apache.cassandra.db.marshal.Int32Type;
import org.apache.cassandra.db.marshal.IntegerType;
import org.apache.cassandra.db.marshal.TupleType;
import org.apache.cassandra.db.marshal.UTF8Type;
import org.apache.cassandra.db.memtable.Memtable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
public class TableMetadataTest
{
@Test
public void testPartitionKeyAsCQLLiteral()
{
String keyspaceName = "keyspace";
String tableName = "table";
CompositeType compositeType1 = CompositeType.getInstance(UTF8Type.instance, UTF8Type.instance, UTF8Type.instance);
TableMetadata metadata1 = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", compositeType1)
.build();
String keyAsCQLLiteral = metadata1.partitionKeyAsCQLLiteral(compositeType1.decompose("test:", "composite!", "type)"));
assertThat(keyAsCQLLiteral).isEqualTo("('test:', 'composite!', 'type)')");
CompositeType compositeType2 = CompositeType.getInstance(new TupleType(Arrays.asList(FloatType.instance,
UTF8Type.instance)),
IntegerType.instance);
TableMetadata metadata2 = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", compositeType2)
.build();
String keyAsCQLLiteral2 = metadata2.partitionKeyAsCQLLiteral(compositeType2.decompose(TupleType.buildValue(FloatType.instance.decompose(0.33f),
UTF8Type.instance.decompose("tuple test")),
BigInteger.valueOf(10)));
assertThat(keyAsCQLLiteral2).isEqualTo("((0.33, 'tuple test'), 10)");
TableMetadata metadata3 = TableMetadata.builder(keyspaceName, tableName).addPartitionKeyColumn("key", UTF8Type.instance).build();
assertEquals("'non-composite test'", metadata3.partitionKeyAsCQLLiteral(UTF8Type.instance.decompose("non-composite test")));
}
@Test
public void testPrimaryKeyAsCQLLiteral()
{
String keyspaceName = "keyspace";
String tableName = "table";
TableMetadata metadata;
// one partition key column, no clustering key
metadata = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", UTF8Type.instance)
.build();
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("Test"), Clustering.EMPTY)).isEqualTo("'Test'");
// two partition key columns, no clustering key
metadata = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("k1", UTF8Type.instance)
.addPartitionKeyColumn("k2", Int32Type.instance)
.build();
assertThat(metadata.primaryKeyAsCQLLiteral(CompositeType.getInstance(UTF8Type.instance, Int32Type.instance)
.decompose("Test", -12), Clustering.EMPTY)).isEqualTo("('Test', -12)");
// one partition key column, one clustering key column
metadata = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", UTF8Type.instance)
.addClusteringColumn("clustering", UTF8Type.instance)
.build();
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("k"),
Clustering.make(UTF8Type.instance.decompose("Cluster")))).isEqualTo("('k', 'Cluster')");
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("k"), Clustering.EMPTY)).isEqualTo("'k'");
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("k"), Clustering.STATIC_CLUSTERING)).isEqualTo("'k'");
// one partition key column, two clustering key columns
metadata = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", UTF8Type.instance)
.addClusteringColumn("c1", UTF8Type.instance)
.addClusteringColumn("c2", UTF8Type.instance)
.build();
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("k"),
Clustering.make(UTF8Type.instance.decompose("c1"),
UTF8Type.instance.decompose("c2")))).isEqualTo("('k', 'c1', 'c2')");
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("k"), Clustering.EMPTY)).isEqualTo("'k'");
assertThat(metadata.primaryKeyAsCQLLiteral(UTF8Type.instance.decompose("k"), Clustering.STATIC_CLUSTERING)).isEqualTo("'k'");
// two partition key columns, two clustering key columns
CompositeType composite = CompositeType.getInstance(Int32Type.instance, BooleanType.instance);
metadata = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("k1", Int32Type.instance)
.addPartitionKeyColumn("k2", BooleanType.instance)
.addClusteringColumn("c1", UTF8Type.instance)
.addClusteringColumn("c2", UTF8Type.instance)
.build();
assertThat(metadata.primaryKeyAsCQLLiteral(composite.decompose(0, true),
Clustering.make(UTF8Type.instance.decompose("Cluster_1"),
UTF8Type.instance.decompose("Cluster_2"))))
.isEqualTo("(0, true, 'Cluster_1', 'Cluster_2')");
assertThat(metadata.primaryKeyAsCQLLiteral(composite.decompose(1, true), Clustering.EMPTY)).isEqualTo("(1, true)");
assertThat(metadata.primaryKeyAsCQLLiteral(composite.decompose(2, true), Clustering.STATIC_CLUSTERING)).isEqualTo("(2, true)");
}
public static class CustomMemtableFactory implements Memtable.Factory
{
@Override
public Memtable create(AtomicReference<CommitLogPosition> commitLogLowerBound, TableMetadataRef metadaRef, Memtable.Owner owner)
{
return null;
}
}
@Test
public void testMemtableParams()
{
MemtableParams memtableParams = new MemtableParams(new CustomMemtableFactory(),
ImmutableMap.of(MemtableParams.Option.CLASS.toString(), "a.b.c"));
assertThat(memtableParams.factory).isInstanceOf(CustomMemtableFactory.class);
assertThat(memtableParams.options).isEmpty();
}
@Test
public void testCdcParamsChangeAffectsPreparedStatements()
{
String keyspaceName = "ks1";
String tableName = "tbl1";
TableParams noCdcParams = TableParams.builder().cdc(false).build();
TableParams cdcParams = TableParams.builder().cdc(true).build();
TableMetadata metadata = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", UTF8Type.instance)
.params(noCdcParams)
.build();
TableMetadata updated = TableMetadata.builder(keyspaceName, tableName)
.addPartitionKeyColumn("key", UTF8Type.instance)
.params(cdcParams)
.build();
assertThat(metadata.changeAffectsPreparedStatements(updated)).isTrue();
}
}