kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | d854e0040cd757693dac43e88bc79d6562434057e36a885e1f8e5043c90a92f2 |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2016-01-15 11:31 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-01-15 11:31 +01:00 |
| Parents | 3ba5e964edec |
6 files changed
added driver-core/src/main/java/com/datastax/driver/core/AbstractTableMetadata.java +335 −0
| @@ -0,0 +1,335 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | + | |
| 19 | +import com.google.common.base.Predicate; | |
| 20 | +import com.google.common.collect.Iterables; | |
| 21 | + | |
| 22 | +import java.util.*; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * Base class for Tables and Materialized Views metadata. | |
| 26 | + */ | |
| 27 | +public abstract class AbstractTableMetadata { | |
| 28 | + | |
| 29 | + static final Comparator<ColumnMetadata> columnMetadataComparator = new Comparator<ColumnMetadata>() { | |
| 30 | + @Override | |
| 31 | + public int compare(ColumnMetadata c1, ColumnMetadata c2) { | |
| 32 | + return c1.getName().compareTo(c2.getName()); | |
| 33 | + } | |
| 34 | + }; | |
| 35 | + | |
| 36 | + static final Predicate<ClusteringOrder> isAscending = new Predicate<ClusteringOrder>() { | |
| 37 | + @Override | |
| 38 | + public boolean apply(ClusteringOrder o) { | |
| 39 | + return o == ClusteringOrder.ASC; | |
| 40 | + } | |
| 41 | + }; | |
| 42 | + | |
| 43 | + protected final KeyspaceMetadata keyspace; | |
| 44 | + protected final String name; | |
| 45 | + protected final UUID id; | |
| 46 | + protected final List<ColumnMetadata> partitionKey; | |
| 47 | + protected final List<ColumnMetadata> clusteringColumns; | |
| 48 | + protected final Map<String, ColumnMetadata> columns; | |
| 49 | + protected final TableOptionsMetadata options; | |
| 50 | + protected final List<ClusteringOrder> clusteringOrder; | |
| 51 | + protected final VersionNumber cassandraVersion; | |
| 52 | + | |
| 53 | + protected AbstractTableMetadata(KeyspaceMetadata keyspace, | |
| 54 | + String name, | |
| 55 | + UUID id, | |
| 56 | + List<ColumnMetadata> partitionKey, | |
| 57 | + List<ColumnMetadata> clusteringColumns, | |
| 58 | + Map<String, ColumnMetadata> columns, | |
| 59 | + TableOptionsMetadata options, | |
| 60 | + List<ClusteringOrder> clusteringOrder, | |
| 61 | + VersionNumber cassandraVersion) { | |
| 62 | + this.keyspace = keyspace; | |
| 63 | + this.name = name; | |
| 64 | + this.id = id; | |
| 65 | + this.partitionKey = partitionKey; | |
| 66 | + this.clusteringColumns = clusteringColumns; | |
| 67 | + this.columns = columns; | |
| 68 | + this.options = options; | |
| 69 | + this.clusteringOrder = clusteringOrder; | |
| 70 | + this.cassandraVersion = cassandraVersion; | |
| 71 | + } | |
| 72 | + | |
| 73 | + /** | |
| 74 | + * Returns the name of this table. | |
| 75 | + * | |
| 76 | + * @return the name of this CQL table. | |
| 77 | + */ | |
| 78 | + public String getName() { | |
| 79 | + return name; | |
| 80 | + } | |
| 81 | + | |
| 82 | + /** | |
| 83 | + * Returns the unique id of this table. | |
| 84 | + * <p/> | |
| 85 | + * Note: this id is available in Cassandra 2.1 and above. It will be | |
| 86 | + * {@code null} for earlier versions. | |
| 87 | + * | |
| 88 | + * @return the unique id of the table. | |
| 89 | + */ | |
| 90 | + public UUID getId() { | |
| 91 | + return id; | |
| 92 | + } | |
| 93 | + | |
| 94 | + /** | |
| 95 | + * Returns the keyspace this table belong to. | |
| 96 | + * | |
| 97 | + * @return the keyspace metadata of the keyspace this table belong to. | |
| 98 | + */ | |
| 99 | + public KeyspaceMetadata getKeyspace() { | |
| 100 | + return keyspace; | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Returns metadata on a column of this table. | |
| 105 | + * | |
| 106 | + * @param name the name of the column to retrieve ({@code name} will be | |
| 107 | + * interpreted as a case-insensitive identifier unless enclosed in double-quotes, | |
| 108 | + * see {@link Metadata#quote}). | |
| 109 | + * @return the metadata for the column if it exists, or | |
| 110 | + * {@code null} otherwise. | |
| 111 | + */ | |
| 112 | + public ColumnMetadata getColumn(String name) { | |
| 113 | + return columns.get(Metadata.handleId(name)); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Returns a list containing all the columns of this table. | |
| 118 | + * <p/> | |
| 119 | + * The order of the columns in the list is consistent with | |
| 120 | + * the order of the columns returned by a {@code SELECT * FROM thisTable}: | |
| 121 | + * the first column is the partition key, next are the clustering | |
| 122 | + * columns in their defined order, and then the rest of the | |
| 123 | + * columns follow in alphabetic order. | |
| 124 | + * | |
| 125 | + * @return a list containing the metadata for the columns of this table. | |
| 126 | + */ | |
| 127 | + public List<ColumnMetadata> getColumns() { | |
| 128 | + return new ArrayList<ColumnMetadata>(columns.values()); | |
| 129 | + } | |
| 130 | + | |
| 131 | + /** | |
| 132 | + * Returns the list of columns composing the primary key for this table. | |
| 133 | + * <p/> | |
| 134 | + * A table will always at least have a partition key (that | |
| 135 | + * may itself be one or more columns), so the returned list at least | |
| 136 | + * has one element. | |
| 137 | + * | |
| 138 | + * @return the list of columns composing the primary key for this table. | |
| 139 | + */ | |
| 140 | + public List<ColumnMetadata> getPrimaryKey() { | |
| 141 | + List<ColumnMetadata> pk = new ArrayList<ColumnMetadata>(partitionKey.size() + clusteringColumns.size()); | |
| 142 | + pk.addAll(partitionKey); | |
| 143 | + pk.addAll(clusteringColumns); | |
| 144 | + return pk; | |
| 145 | + } | |
| 146 | + | |
| 147 | + /** | |
| 148 | + * Returns the list of columns composing the partition key for this table. | |
| 149 | + * <p/> | |
| 150 | + * A table always has a partition key so the returned list has | |
| 151 | + * at least one element. | |
| 152 | + * | |
| 153 | + * @return the list of columns composing the partition key for this table. | |
| 154 | + */ | |
| 155 | + public List<ColumnMetadata> getPartitionKey() { | |
| 156 | + return Collections.unmodifiableList(partitionKey); | |
| 157 | + } | |
| 158 | + | |
| 159 | + /** | |
| 160 | + * Returns the list of clustering columns for this table. | |
| 161 | + * | |
| 162 | + * @return the list of clustering columns for this table. | |
| 163 | + * If there is no clustering columns, an empty list is returned. | |
| 164 | + */ | |
| 165 | + public List<ColumnMetadata> getClusteringColumns() { | |
| 166 | + return Collections.unmodifiableList(clusteringColumns); | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * Returns the clustering order for this table. | |
| 171 | + * <p/> | |
| 172 | + * The returned contains the clustering order of each clustering column. The | |
| 173 | + * {@code i}th element of the result correspond to the order (ascending or | |
| 174 | + * descending) of the {@code i}th clustering column (see | |
| 175 | + * {@link #getClusteringColumns}). Note that a table defined without any | |
| 176 | + * particular clustering order is equivalent to one for which all the | |
| 177 | + * clustering keys are in ascending order. | |
| 178 | + * | |
| 179 | + * @return a list with the clustering order for each clustering column. | |
| 180 | + */ | |
| 181 | + public List<ClusteringOrder> getClusteringOrder() { | |
| 182 | + return clusteringOrder; | |
| 183 | + } | |
| 184 | + | |
| 185 | + /** | |
| 186 | + * Returns the options for this table. | |
| 187 | + * | |
| 188 | + * @return the options for this table. | |
| 189 | + */ | |
| 190 | + public TableOptionsMetadata getOptions() { | |
| 191 | + return options; | |
| 192 | + } | |
| 193 | + | |
| 194 | + void add(ColumnMetadata column) { | |
| 195 | + columns.put(column.getName(), column); | |
| 196 | + } | |
| 197 | + | |
| 198 | + /** | |
| 199 | + * Returns a {@code String} containing CQL queries representing this | |
| 200 | + * table and the index on it. | |
| 201 | + * <p/> | |
| 202 | + * In other words, this method returns the queries that would allow you to | |
| 203 | + * recreate the schema of this table, along with the indexes and views defined on | |
| 204 | + * this table, if any. | |
| 205 | + * <p/> | |
| 206 | + * Note that the returned String is formatted to be human readable (for | |
| 207 | + * some definition of human readable at least). | |
| 208 | + * | |
| 209 | + * @return the CQL queries representing this table schema as a {code | |
| 210 | + * String}. | |
| 211 | + */ | |
| 212 | + public String exportAsString() { | |
| 213 | + StringBuilder sb = new StringBuilder(); | |
| 214 | + | |
| 215 | + sb.append(asCQLQuery(true)); | |
| 216 | + | |
| 217 | + return sb.toString(); | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Returns a CQL query representing this table. | |
| 222 | + * <p/> | |
| 223 | + * This method returns a single 'CREATE TABLE' query with the options | |
| 224 | + * corresponding to this table definition. | |
| 225 | + * <p/> | |
| 226 | + * Note that the returned string is a single line; the returned query | |
| 227 | + * is not formatted in any way. | |
| 228 | + * | |
| 229 | + * @return the 'CREATE TABLE' query corresponding to this table. | |
| 230 | + * @see #exportAsString | |
| 231 | + */ | |
| 232 | + public String asCQLQuery() { | |
| 233 | + return asCQLQuery(false); | |
| 234 | + } | |
| 235 | + | |
| 236 | + protected abstract String asCQLQuery(boolean formatted); | |
| 237 | + | |
| 238 | + protected StringBuilder appendOptions(StringBuilder sb, boolean formatted) { | |
| 239 | + // Options | |
| 240 | + sb.append(" WITH "); | |
| 241 | + if (options.isCompactStorage()) | |
| 242 | + and(sb.append("COMPACT STORAGE"), formatted); | |
| 243 | + if (!Iterables.all(clusteringOrder, isAscending)) | |
| 244 | + and(appendClusteringOrder(sb), formatted); | |
| 245 | + sb.append("read_repair_chance = ").append(options.getReadRepairChance()); | |
| 246 | + and(sb, formatted).append("dclocal_read_repair_chance = ").append(options.getLocalReadRepairChance()); | |
| 247 | + if (cassandraVersion.getMajor() < 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() == 0)) | |
| 248 | + and(sb, formatted).append("replicate_on_write = ").append(options.getReplicateOnWrite()); | |
| 249 | + and(sb, formatted).append("gc_grace_seconds = ").append(options.getGcGraceInSeconds()); | |
| 250 | + and(sb, formatted).append("bloom_filter_fp_chance = ").append(options.getBloomFilterFalsePositiveChance()); | |
| 251 | + if (cassandraVersion.getMajor() < 2 || cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() < 1) | |
| 252 | + and(sb, formatted).append("caching = '").append(options.getCaching().get("keys")).append('\''); | |
| 253 | + else | |
| 254 | + and(sb, formatted).append("caching = ").append(formatOptionMap(options.getCaching())); | |
| 255 | + if (options.getComment() != null) | |
| 256 | + and(sb, formatted).append("comment = '").append(options.getComment().replace("'", "''")).append('\''); | |
| 257 | + and(sb, formatted).append("compaction = ").append(formatOptionMap(options.getCompaction())); | |
| 258 | + and(sb, formatted).append("compression = ").append(formatOptionMap(options.getCompression())); | |
| 259 | + if (cassandraVersion.getMajor() >= 2) { | |
| 260 | + and(sb, formatted).append("default_time_to_live = ").append(options.getDefaultTimeToLive()); | |
| 261 | + and(sb, formatted).append("speculative_retry = '").append(options.getSpeculativeRetry()).append('\''); | |
| 262 | + if (options.getIndexInterval() != null) | |
| 263 | + and(sb, formatted).append("index_interval = ").append(options.getIndexInterval()); | |
| 264 | + } | |
| 265 | + if (cassandraVersion.getMajor() > 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1)) { | |
| 266 | + and(sb, formatted).append("min_index_interval = ").append(options.getMinIndexInterval()); | |
| 267 | + and(sb, formatted).append("max_index_interval = ").append(options.getMaxIndexInterval()); | |
| 268 | + } | |
| 269 | + if (cassandraVersion.getMajor() > 2) { | |
| 270 | + and(sb, formatted).append("crc_check_chance = ").append(options.getCrcCheckChance()); | |
| 271 | + } | |
| 272 | + sb.append(';'); | |
| 273 | + return sb; | |
| 274 | + } | |
| 275 | + | |
| 276 | + @Override | |
| 277 | + public String toString() { | |
| 278 | + return asCQLQuery(); | |
| 279 | + } | |
| 280 | + | |
| 281 | + private StringBuilder appendClusteringOrder(StringBuilder sb) { | |
| 282 | + sb.append("CLUSTERING ORDER BY ("); | |
| 283 | + for (int i = 0; i < clusteringColumns.size(); i++) { | |
| 284 | + if (i > 0) sb.append(", "); | |
| 285 | + sb.append(clusteringColumns.get(i).getName()).append(' ').append(clusteringOrder.get(i)); | |
| 286 | + } | |
| 287 | + return sb.append(')'); | |
| 288 | + } | |
| 289 | + | |
| 290 | + private static String formatOptionMap(Map<String, String> m) { | |
| 291 | + StringBuilder sb = new StringBuilder(); | |
| 292 | + sb.append("{ "); | |
| 293 | + boolean first = true; | |
| 294 | + for (Map.Entry<String, String> entry : m.entrySet()) { | |
| 295 | + if (first) first = false; | |
| 296 | + else sb.append(", "); | |
| 297 | + sb.append('\'').append(entry.getKey()).append('\''); | |
| 298 | + sb.append(" : "); | |
| 299 | + try { | |
| 300 | + sb.append(Integer.parseInt(entry.getValue())); | |
| 301 | + } catch (NumberFormatException e) { | |
| 302 | + sb.append('\'').append(entry.getValue()).append('\''); | |
| 303 | + } | |
| 304 | + } | |
| 305 | + sb.append(" }"); | |
| 306 | + return sb.toString(); | |
| 307 | + } | |
| 308 | + | |
| 309 | + private StringBuilder and(StringBuilder sb, boolean formatted) { | |
| 310 | + return newLine(sb, formatted).append(spaces(2, formatted)).append(" AND "); | |
| 311 | + } | |
| 312 | + | |
| 313 | + static String spaces(int n, boolean formatted) { | |
| 314 | + if (!formatted) | |
| 315 | + return ""; | |
| 316 | + | |
| 317 | + StringBuilder sb = new StringBuilder(); | |
| 318 | + for (int i = 0; i < n; i++) | |
| 319 | + sb.append(' '); | |
| 320 | + | |
| 321 | + return sb.toString(); | |
| 322 | + } | |
| 323 | + | |
| 324 | + static StringBuilder newLine(StringBuilder sb, boolean formatted) { | |
| 325 | + if (formatted) | |
| 326 | + sb.append('\n'); | |
| 327 | + return sb; | |
| 328 | + } | |
| 329 | + | |
| 330 | + static StringBuilder spaceOrNewLine(StringBuilder sb, boolean formatted) { | |
| 331 | + sb.append(formatted ? '\n' : ' '); | |
| 332 | + return sb; | |
| 333 | + } | |
| 334 | + | |
| 335 | +} |
modified driver-core/src/main/java/com/datastax/driver/core/ColumnMetadata.java +4 −4
| @@ -44,19 +44,19 @@ | ||
| 44 | 44 | static final String INDEX_OPTIONS = "index_options"; |
| 45 | 45 | static final String INDEX_NAME = "index_name"; |
| 46 | 46 | |
| 47 | - private final TableOrView parent; | |
| 47 | + private final AbstractTableMetadata parent; | |
| 48 | 48 | private final String name; |
| 49 | 49 | private final DataType type; |
| 50 | 50 | private final boolean isStatic; |
| 51 | 51 | |
| 52 | - private ColumnMetadata(TableOrView parent, String name, DataType type, boolean isStatic) { | |
| 52 | + private ColumnMetadata(AbstractTableMetadata parent, String name, DataType type, boolean isStatic) { | |
| 53 | 53 | this.parent = parent; |
| 54 | 54 | this.name = name; |
| 55 | 55 | this.type = type; |
| 56 | 56 | this.isStatic = isStatic; |
| 57 | 57 | } |
| 58 | 58 | |
| 59 | - static ColumnMetadata fromRaw(TableOrView tm, Raw raw, DataType dataType) { | |
| 59 | + static ColumnMetadata fromRaw(AbstractTableMetadata tm, Raw raw, DataType dataType) { | |
| 60 | 60 | return new ColumnMetadata(tm, raw.name, dataType, raw.kind == Raw.Kind.STATIC); |
| 61 | 61 | } |
| 62 | 62 | |
| @@ -79,7 +79,7 @@ | ||
| 79 | 79 | * |
| 80 | 80 | * @return the parent object of this column. |
| 81 | 81 | */ |
| 82 | - public TableOrView getParent() { | |
| 82 | + public AbstractTableMetadata getParent() { | |
| 83 | 83 | return parent; |
| 84 | 84 | } |
| 85 | 85 |
modified driver-core/src/main/java/com/datastax/driver/core/MaterializedViewMetadata.java +1 −1
| @@ -26,7 +26,7 @@ | ||
| 26 | 26 | * An immutable representation of a materialized view. |
| 27 | 27 | * Materialized views are available starting from Cassandra 3.0. |
| 28 | 28 | */ |
| 29 | -public class MaterializedViewMetadata extends TableOrView { | |
| 29 | +public class MaterializedViewMetadata extends AbstractTableMetadata { | |
| 30 | 30 | |
| 31 | 31 | private static final Logger logger = LoggerFactory.getLogger(MaterializedViewMetadata.class); |
| 32 | 32 |
modified driver-core/src/main/java/com/datastax/driver/core/TableMetadata.java +1 −1
| @@ -24,7 +24,7 @@ | ||
| 24 | 24 | /** |
| 25 | 25 | * Describes a Table. |
| 26 | 26 | */ |
| 27 | -public class TableMetadata extends TableOrView { | |
| 27 | +public class TableMetadata extends AbstractTableMetadata { | |
| 28 | 28 | |
| 29 | 29 | private static final Logger logger = LoggerFactory.getLogger(TableMetadata.class); |
| 30 | 30 |
deleted driver-core/src/main/java/com/datastax/driver/core/TableOrView.java +0 −335
| @@ -1,335 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | - * | |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | - * you may not use this file except in compliance with the License. | |
| 6 | - * You may obtain a copy of the License at | |
| 7 | - * | |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | - * | |
| 10 | - * Unless required by applicable law or agreed to in writing, software | |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | - * See the License for the specific language governing permissions and | |
| 14 | - * limitations under the License. | |
| 15 | - */ | |
| 16 | -package com.datastax.driver.core; | |
| 17 | - | |
| 18 | - | |
| 19 | -import com.google.common.base.Predicate; | |
| 20 | -import com.google.common.collect.Iterables; | |
| 21 | - | |
| 22 | -import java.util.*; | |
| 23 | - | |
| 24 | -/** | |
| 25 | - * Base class for Tables and Materialized Views. | |
| 26 | - */ | |
| 27 | -abstract class TableOrView { | |
| 28 | - | |
| 29 | - static final Comparator<ColumnMetadata> columnMetadataComparator = new Comparator<ColumnMetadata>() { | |
| 30 | - @Override | |
| 31 | - public int compare(ColumnMetadata c1, ColumnMetadata c2) { | |
| 32 | - return c1.getName().compareTo(c2.getName()); | |
| 33 | - } | |
| 34 | - }; | |
| 35 | - | |
| 36 | - static final Predicate<ClusteringOrder> isAscending = new Predicate<ClusteringOrder>() { | |
| 37 | - @Override | |
| 38 | - public boolean apply(ClusteringOrder o) { | |
| 39 | - return o == ClusteringOrder.ASC; | |
| 40 | - } | |
| 41 | - }; | |
| 42 | - | |
| 43 | - protected final KeyspaceMetadata keyspace; | |
| 44 | - protected final String name; | |
| 45 | - protected final UUID id; | |
| 46 | - protected final List<ColumnMetadata> partitionKey; | |
| 47 | - protected final List<ColumnMetadata> clusteringColumns; | |
| 48 | - protected final Map<String, ColumnMetadata> columns; | |
| 49 | - protected final TableOptionsMetadata options; | |
| 50 | - protected final List<ClusteringOrder> clusteringOrder; | |
| 51 | - protected final VersionNumber cassandraVersion; | |
| 52 | - | |
| 53 | - TableOrView(KeyspaceMetadata keyspace, | |
| 54 | - String name, | |
| 55 | - UUID id, | |
| 56 | - List<ColumnMetadata> partitionKey, | |
| 57 | - List<ColumnMetadata> clusteringColumns, | |
| 58 | - Map<String, ColumnMetadata> columns, | |
| 59 | - TableOptionsMetadata options, | |
| 60 | - List<ClusteringOrder> clusteringOrder, | |
| 61 | - VersionNumber cassandraVersion) { | |
| 62 | - this.keyspace = keyspace; | |
| 63 | - this.name = name; | |
| 64 | - this.id = id; | |
| 65 | - this.partitionKey = partitionKey; | |
| 66 | - this.clusteringColumns = clusteringColumns; | |
| 67 | - this.columns = columns; | |
| 68 | - this.options = options; | |
| 69 | - this.clusteringOrder = clusteringOrder; | |
| 70 | - this.cassandraVersion = cassandraVersion; | |
| 71 | - } | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * Returns the name of this table. | |
| 75 | - * | |
| 76 | - * @return the name of this CQL table. | |
| 77 | - */ | |
| 78 | - public String getName() { | |
| 79 | - return name; | |
| 80 | - } | |
| 81 | - | |
| 82 | - /** | |
| 83 | - * Returns the unique id of this table. | |
| 84 | - * <p/> | |
| 85 | - * Note: this id is available in Cassandra 2.1 and above. It will be | |
| 86 | - * {@code null} for earlier versions. | |
| 87 | - * | |
| 88 | - * @return the unique id of the table. | |
| 89 | - */ | |
| 90 | - public UUID getId() { | |
| 91 | - return id; | |
| 92 | - } | |
| 93 | - | |
| 94 | - /** | |
| 95 | - * Returns the keyspace this table belong to. | |
| 96 | - * | |
| 97 | - * @return the keyspace metadata of the keyspace this table belong to. | |
| 98 | - */ | |
| 99 | - public KeyspaceMetadata getKeyspace() { | |
| 100 | - return keyspace; | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | - * Returns metadata on a column of this table. | |
| 105 | - * | |
| 106 | - * @param name the name of the column to retrieve ({@code name} will be | |
| 107 | - * interpreted as a case-insensitive identifier unless enclosed in double-quotes, | |
| 108 | - * see {@link Metadata#quote}). | |
| 109 | - * @return the metadata for the column if it exists, or | |
| 110 | - * {@code null} otherwise. | |
| 111 | - */ | |
| 112 | - public ColumnMetadata getColumn(String name) { | |
| 113 | - return columns.get(Metadata.handleId(name)); | |
| 114 | - } | |
| 115 | - | |
| 116 | - /** | |
| 117 | - * Returns a list containing all the columns of this table. | |
| 118 | - * <p/> | |
| 119 | - * The order of the columns in the list is consistent with | |
| 120 | - * the order of the columns returned by a {@code SELECT * FROM thisTable}: | |
| 121 | - * the first column is the partition key, next are the clustering | |
| 122 | - * columns in their defined order, and then the rest of the | |
| 123 | - * columns follow in alphabetic order. | |
| 124 | - * | |
| 125 | - * @return a list containing the metadata for the columns of this table. | |
| 126 | - */ | |
| 127 | - public List<ColumnMetadata> getColumns() { | |
| 128 | - return new ArrayList<ColumnMetadata>(columns.values()); | |
| 129 | - } | |
| 130 | - | |
| 131 | - /** | |
| 132 | - * Returns the list of columns composing the primary key for this table. | |
| 133 | - * <p/> | |
| 134 | - * A table will always at least have a partition key (that | |
| 135 | - * may itself be one or more columns), so the returned list at least | |
| 136 | - * has one element. | |
| 137 | - * | |
| 138 | - * @return the list of columns composing the primary key for this table. | |
| 139 | - */ | |
| 140 | - public List<ColumnMetadata> getPrimaryKey() { | |
| 141 | - List<ColumnMetadata> pk = new ArrayList<ColumnMetadata>(partitionKey.size() + clusteringColumns.size()); | |
| 142 | - pk.addAll(partitionKey); | |
| 143 | - pk.addAll(clusteringColumns); | |
| 144 | - return pk; | |
| 145 | - } | |
| 146 | - | |
| 147 | - /** | |
| 148 | - * Returns the list of columns composing the partition key for this table. | |
| 149 | - * <p/> | |
| 150 | - * A table always has a partition key so the returned list has | |
| 151 | - * at least one element. | |
| 152 | - * | |
| 153 | - * @return the list of columns composing the partition key for this table. | |
| 154 | - */ | |
| 155 | - public List<ColumnMetadata> getPartitionKey() { | |
| 156 | - return Collections.unmodifiableList(partitionKey); | |
| 157 | - } | |
| 158 | - | |
| 159 | - /** | |
| 160 | - * Returns the list of clustering columns for this table. | |
| 161 | - * | |
| 162 | - * @return the list of clustering columns for this table. | |
| 163 | - * If there is no clustering columns, an empty list is returned. | |
| 164 | - */ | |
| 165 | - public List<ColumnMetadata> getClusteringColumns() { | |
| 166 | - return Collections.unmodifiableList(clusteringColumns); | |
| 167 | - } | |
| 168 | - | |
| 169 | - /** | |
| 170 | - * Returns the clustering order for this table. | |
| 171 | - * <p/> | |
| 172 | - * The returned contains the clustering order of each clustering column. The | |
| 173 | - * {@code i}th element of the result correspond to the order (ascending or | |
| 174 | - * descending) of the {@code i}th clustering column (see | |
| 175 | - * {@link #getClusteringColumns}). Note that a table defined without any | |
| 176 | - * particular clustering order is equivalent to one for which all the | |
| 177 | - * clustering keys are in ascending order. | |
| 178 | - * | |
| 179 | - * @return a list with the clustering order for each clustering column. | |
| 180 | - */ | |
| 181 | - public List<ClusteringOrder> getClusteringOrder() { | |
| 182 | - return clusteringOrder; | |
| 183 | - } | |
| 184 | - | |
| 185 | - /** | |
| 186 | - * Returns the options for this table. | |
| 187 | - * | |
| 188 | - * @return the options for this table. | |
| 189 | - */ | |
| 190 | - public TableOptionsMetadata getOptions() { | |
| 191 | - return options; | |
| 192 | - } | |
| 193 | - | |
| 194 | - void add(ColumnMetadata column) { | |
| 195 | - columns.put(column.getName(), column); | |
| 196 | - } | |
| 197 | - | |
| 198 | - /** | |
| 199 | - * Returns a {@code String} containing CQL queries representing this | |
| 200 | - * table and the index on it. | |
| 201 | - * <p/> | |
| 202 | - * In other words, this method returns the queries that would allow you to | |
| 203 | - * recreate the schema of this table, along with the indexes and views defined on | |
| 204 | - * this table, if any. | |
| 205 | - * <p/> | |
| 206 | - * Note that the returned String is formatted to be human readable (for | |
| 207 | - * some definition of human readable at least). | |
| 208 | - * | |
| 209 | - * @return the CQL queries representing this table schema as a {code | |
| 210 | - * String}. | |
| 211 | - */ | |
| 212 | - public String exportAsString() { | |
| 213 | - StringBuilder sb = new StringBuilder(); | |
| 214 | - | |
| 215 | - sb.append(asCQLQuery(true)); | |
| 216 | - | |
| 217 | - return sb.toString(); | |
| 218 | - } | |
| 219 | - | |
| 220 | - /** | |
| 221 | - * Returns a CQL query representing this table. | |
| 222 | - * <p/> | |
| 223 | - * This method returns a single 'CREATE TABLE' query with the options | |
| 224 | - * corresponding to this table definition. | |
| 225 | - * <p/> | |
| 226 | - * Note that the returned string is a single line; the returned query | |
| 227 | - * is not formatted in any way. | |
| 228 | - * | |
| 229 | - * @return the 'CREATE TABLE' query corresponding to this table. | |
| 230 | - * @see #exportAsString | |
| 231 | - */ | |
| 232 | - public String asCQLQuery() { | |
| 233 | - return asCQLQuery(false); | |
| 234 | - } | |
| 235 | - | |
| 236 | - protected abstract String asCQLQuery(boolean formatted); | |
| 237 | - | |
| 238 | - protected StringBuilder appendOptions(StringBuilder sb, boolean formatted) { | |
| 239 | - // Options | |
| 240 | - sb.append(" WITH "); | |
| 241 | - if (options.isCompactStorage()) | |
| 242 | - and(sb.append("COMPACT STORAGE"), formatted); | |
| 243 | - if (!Iterables.all(clusteringOrder, isAscending)) | |
| 244 | - and(appendClusteringOrder(sb), formatted); | |
| 245 | - sb.append("read_repair_chance = ").append(options.getReadRepairChance()); | |
| 246 | - and(sb, formatted).append("dclocal_read_repair_chance = ").append(options.getLocalReadRepairChance()); | |
| 247 | - if (cassandraVersion.getMajor() < 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() == 0)) | |
| 248 | - and(sb, formatted).append("replicate_on_write = ").append(options.getReplicateOnWrite()); | |
| 249 | - and(sb, formatted).append("gc_grace_seconds = ").append(options.getGcGraceInSeconds()); | |
| 250 | - and(sb, formatted).append("bloom_filter_fp_chance = ").append(options.getBloomFilterFalsePositiveChance()); | |
| 251 | - if (cassandraVersion.getMajor() < 2 || cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() < 1) | |
| 252 | - and(sb, formatted).append("caching = '").append(options.getCaching().get("keys")).append('\''); | |
| 253 | - else | |
| 254 | - and(sb, formatted).append("caching = ").append(formatOptionMap(options.getCaching())); | |
| 255 | - if (options.getComment() != null) | |
| 256 | - and(sb, formatted).append("comment = '").append(options.getComment().replace("'", "''")).append('\''); | |
| 257 | - and(sb, formatted).append("compaction = ").append(formatOptionMap(options.getCompaction())); | |
| 258 | - and(sb, formatted).append("compression = ").append(formatOptionMap(options.getCompression())); | |
| 259 | - if (cassandraVersion.getMajor() >= 2) { | |
| 260 | - and(sb, formatted).append("default_time_to_live = ").append(options.getDefaultTimeToLive()); | |
| 261 | - and(sb, formatted).append("speculative_retry = '").append(options.getSpeculativeRetry()).append('\''); | |
| 262 | - if (options.getIndexInterval() != null) | |
| 263 | - and(sb, formatted).append("index_interval = ").append(options.getIndexInterval()); | |
| 264 | - } | |
| 265 | - if (cassandraVersion.getMajor() > 2 || (cassandraVersion.getMajor() == 2 && cassandraVersion.getMinor() >= 1)) { | |
| 266 | - and(sb, formatted).append("min_index_interval = ").append(options.getMinIndexInterval()); | |
| 267 | - and(sb, formatted).append("max_index_interval = ").append(options.getMaxIndexInterval()); | |
| 268 | - } | |
| 269 | - if (cassandraVersion.getMajor() > 2) { | |
| 270 | - and(sb, formatted).append("crc_check_chance = ").append(options.getCrcCheckChance()); | |
| 271 | - } | |
| 272 | - sb.append(';'); | |
| 273 | - return sb; | |
| 274 | - } | |
| 275 | - | |
| 276 | - @Override | |
| 277 | - public String toString() { | |
| 278 | - return asCQLQuery(); | |
| 279 | - } | |
| 280 | - | |
| 281 | - private StringBuilder appendClusteringOrder(StringBuilder sb) { | |
| 282 | - sb.append("CLUSTERING ORDER BY ("); | |
| 283 | - for (int i = 0; i < clusteringColumns.size(); i++) { | |
| 284 | - if (i > 0) sb.append(", "); | |
| 285 | - sb.append(clusteringColumns.get(i).getName()).append(' ').append(clusteringOrder.get(i)); | |
| 286 | - } | |
| 287 | - return sb.append(')'); | |
| 288 | - } | |
| 289 | - | |
| 290 | - private static String formatOptionMap(Map<String, String> m) { | |
| 291 | - StringBuilder sb = new StringBuilder(); | |
| 292 | - sb.append("{ "); | |
| 293 | - boolean first = true; | |
| 294 | - for (Map.Entry<String, String> entry : m.entrySet()) { | |
| 295 | - if (first) first = false; | |
| 296 | - else sb.append(", "); | |
| 297 | - sb.append('\'').append(entry.getKey()).append('\''); | |
| 298 | - sb.append(" : "); | |
| 299 | - try { | |
| 300 | - sb.append(Integer.parseInt(entry.getValue())); | |
| 301 | - } catch (NumberFormatException e) { | |
| 302 | - sb.append('\'').append(entry.getValue()).append('\''); | |
| 303 | - } | |
| 304 | - } | |
| 305 | - sb.append(" }"); | |
| 306 | - return sb.toString(); | |
| 307 | - } | |
| 308 | - | |
| 309 | - private StringBuilder and(StringBuilder sb, boolean formatted) { | |
| 310 | - return newLine(sb, formatted).append(spaces(2, formatted)).append(" AND "); | |
| 311 | - } | |
| 312 | - | |
| 313 | - static String spaces(int n, boolean formatted) { | |
| 314 | - if (!formatted) | |
| 315 | - return ""; | |
| 316 | - | |
| 317 | - StringBuilder sb = new StringBuilder(); | |
| 318 | - for (int i = 0; i < n; i++) | |
| 319 | - sb.append(' '); | |
| 320 | - | |
| 321 | - return sb.toString(); | |
| 322 | - } | |
| 323 | - | |
| 324 | - static StringBuilder newLine(StringBuilder sb, boolean formatted) { | |
| 325 | - if (formatted) | |
| 326 | - sb.append('\n'); | |
| 327 | - return sb; | |
| 328 | - } | |
| 329 | - | |
| 330 | - static StringBuilder spaceOrNewLine(StringBuilder sb, boolean formatted) { | |
| 331 | - sb.append(formatted ? '\n' : ' '); | |
| 332 | - return sb; | |
| 333 | - } | |
| 334 | - | |
| 335 | -} |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +1 −1
| @@ -366,7 +366,7 @@ | ||
| 366 | 366 | } |
| 367 | 367 | |
| 368 | 368 | private ColumnMetadata getColumn(String name, boolean fromTable) { |
| 369 | - TableOrView target = fromTable ? getTable("indexing") : getMaterializedView("mv1"); | |
| 369 | + AbstractTableMetadata target = fromTable ? getTable("indexing") : getMaterializedView("mv1"); | |
| 370 | 370 | return target.getColumn(name); |
| 371 | 371 | } |
| 372 | 372 |