Handle latest table options in CREATE statements

For PYTHON-51
This commit is contained in:
Tyler Hobbs
2014-02-25 17:00:07 -06:00
parent c1dd4b65a9
commit e7c8aa4561
3 changed files with 44 additions and 21 deletions

View File

@@ -13,6 +13,8 @@ Bug Fixes
KeyspaceMetadata and TableMetadata.
* Decode TimestampType as DateType. (Cassandra replaced DateType with
TimestampType to fix sorting of pre-unix epoch dates in CASSANDRA-5723.)
* Handle latest table options when parsing the schema and generating
CREATE statements.
Other
-----

View File

@@ -274,13 +274,13 @@ class Metadata(object):
column_meta = self._build_column_metadata(table_meta, col_row)
table_meta.columns[column_meta.name] = column_meta
table_meta.options = self._build_table_options(row, is_compact)
table_meta.options = self._build_table_options(row)
table_meta.is_compact_storage = is_compact
return table_meta
def _build_table_options(self, row, is_compact_storage):
def _build_table_options(self, row):
""" Setup the mostly-non-schema table options, like caching settings """
options = dict((o, row.get(o)) for o in TableMetadata.recognized_options)
options["is_compact_storage"] = is_compact_storage
options = dict((o, row.get(o)) for o in TableMetadata.recognized_options if o in row)
return options
def _build_column_metadata(self, table_metadata, row):
@@ -611,6 +611,8 @@ class TableMetadata(object):
A dict mapping column names to :class:`.ColumnMetadata` instances.
"""
is_compact_storage = False
options = None
"""
A dict mapping table option names to their specific settings for this
@@ -618,11 +620,28 @@ class TableMetadata(object):
"""
recognized_options = (
"comment", "read_repair_chance", # "local_read_repair_chance",
"replicate_on_write", "gc_grace_seconds", "bloom_filter_fp_chance",
"caching", "compaction_strategy_class", "compaction_strategy_options",
"min_compaction_threshold", "max_compression_threshold",
"compression_parameters")
"comment",
"read_repair_chance",
"dclocal_read_repair_chance",
"replicate_on_write",
"gc_grace_seconds",
"bloom_filter_fp_chance",
"caching",
"compaction_strategy_class",
"compaction_strategy_options",
"min_compaction_threshold",
"max_compression_threshold",
"compression_parameters",
"min_index_interval",
"max_index_interval",
"index_interval",
"speculative_retry",
"rows_per_partition_to_cache",
"memtable_flush_period_in_ms",
"populate_io_cache_on_flush",
"compaction",
"compression",
"default_time_to_live")
def __init__(self, keyspace_metadata, name, partition_key=None, clustering_key=None, columns=None, options=None):
self.keyspace = keyspace_metadata
@@ -693,7 +712,7 @@ class TableMetadata(object):
ret += "%s) WITH " % ("\n" if formatted else "")
option_strings = []
if self.options.get("is_compact_storage"):
if self.is_compact_storage:
option_strings.append("COMPACT STORAGE")
if self.clustering_key:
@@ -701,7 +720,7 @@ class TableMetadata(object):
clustering_names = protect_names([c.name for c in self.clustering_key])
if self.options.get("is_compact_storage") and \
if self.is_compact_storage and \
not issubclass(self.comparator, types.CompositeType):
subtypes = [self.comparator]
else:
@@ -715,20 +734,22 @@ class TableMetadata(object):
cluster_str += "(%s)" % ", ".join(inner)
option_strings.append(cluster_str)
option_strings.extend(map(self._make_option_str, self.recognized_options))
option_strings = filter(lambda x: x is not None, option_strings)
option_strings.extend(self._make_option_strings())
join_str = "\n AND " if formatted else " AND "
ret += join_str.join(option_strings)
return ret
def _make_option_str(self, name):
value = self.options.get(name)
if value is not None:
if name == "comment":
value = value or ""
return "%s = %s" % (name, protect_value(value))
def _make_option_strings(self):
ret = []
for name, value in sorted(self.options.items()):
if value is not None:
if name == "comment":
value = value or ""
ret.append("%s = %s" % (name, protect_value(value)))
return ret
def protect_name(name):

View File

@@ -138,8 +138,8 @@ class SchemaMetadataTest(unittest.TestCase):
self.assertEqual([], tablemeta.clustering_key)
self.assertEqual([u'a', u'b', u'c'], sorted(tablemeta.columns.keys()))
for option in TableMetadata.recognized_options:
self.assertTrue(option in tablemeta.options)
for option in tablemeta.options:
self.assertIn(option, TableMetadata.recognized_options)
self.check_create_statement(tablemeta, create_statement)