def from_layout(cls, layout, coldefs): """ This constructor accepts a dictionary of column-value pairs from a row of system.schema_columnfamilies, and a sequence of similar dictionaries from corresponding rows in system.schema_columns. """ try: cfname = layout[u'columnfamily_name'] except KeyError: cfname = layout[u'columnfamily'] cf = cls(name=cfname) for attr, val in layout.items(): setattr(cf, attr.encode('ascii'), val) for attr in cls.json_attrs: try: setattr(cf, attr, json.loads(getattr(cf, attr))) except AttributeError: pass if not cf.key_aliases: if cf.key_alias: cf.key_aliases = [cf.key_alias.decode('ascii')] else: cf.key_aliases = [u'KEY'] cf.partition_key_components = cf.key_aliases cf.primary_key_components = cf.key_aliases + list(cf.column_aliases) cf.partition_key_validator = lookup_casstype(cf.key_validator) cf.default_validator = lookup_casstype(cf.default_validator) cf.comparator = lookup_casstype(cf.comparator) cf.coldefs = coldefs cf.parse_types() return cf
def decode_metadata_and_type(self, namebytes): schema = self.schema comparator = schema.name_types.get(namebytes, schema.default_name_type) comptype = cqltypes.lookup_casstype(comparator) validator = schema.value_types.get(namebytes, schema.default_value_type) valdtype = cqltypes.lookup_casstype(validator) try: name = comptype.from_binary(namebytes) except Exception, e: name = self.name_decode_error(e, namebytes, comptype.cql_parameterized_type())
def from_layout(cls, layout, coldefs): """ This constructor accepts a dictionary of column-value pairs from a row of system.schema_columnfamilies, and a sequence of similar dictionaries from corresponding rows in system.schema_columns. """ cf = cls(name=layout[u'columnfamily_name']) cf.keyspace = layout[u'keyspace_name'] for attr, val in layout.items(): setattr(cf, attr.encode('ascii'), val) cf.comparator = lookup_casstype(cf.comparator) for attr in ('compaction_strategy_options', 'compression_parameters'): setattr(cf, attr, json.loads(getattr(cf, attr))) # deal with columns, filter out empty column names (see CASSANDRA-6139) columns = filter(lambda c: c.name, map(CqlColumnDef.from_layout, coldefs)) partition_key_cols = filter(lambda c: c.component_type == u'partition_key', columns) partition_key_cols.sort(key=lambda c: c.component_index) cf.partition_key_columns = map(lambda c: c.name, partition_key_cols) clustering_key_cols = filter(lambda c: c.component_type == u'clustering_key', columns) clustering_key_cols.sort(key=lambda c: c.component_index) cf.clustering_key_columns = map(lambda c: c.name, clustering_key_cols) cf.primary_key_columns = cf.partition_key_columns + cf.clustering_key_columns regular_cols = list(set(columns) - set(partition_key_cols) - set(clustering_key_cols)) regular_cols.sort(key=lambda c: c.name) cf.regular_columns = map(lambda c: c.name, regular_cols) cf.columns = partition_key_cols + clustering_key_cols + regular_cols return cf
def from_layout(cls, layout, coldefs): """ This constructor accepts a dictionary of column-value pairs from a row of system.schema_columnfamilies, and a sequence of similar dictionaries from corresponding rows in system.schema_columns. """ cf = cls(name=layout[u'columnfamily_name']) cf.keyspace = layout[u'keyspace_name'] for attr, val in layout.items(): setattr(cf, attr.encode('ascii'), val) cf.comparator = lookup_casstype(cf.comparator) for attr in ('compaction_strategy_options', 'compression_parameters'): setattr(cf, attr, json.loads(getattr(cf, attr))) # deal with columns columns = map(CqlColumnDef.from_layout, coldefs) partition_key_cols = filter(lambda c: c.component_type == u'partition_key', columns) partition_key_cols.sort(key=lambda c: c.component_index) cf.partition_key_columns = map(lambda c: c.name, partition_key_cols) clustering_key_cols = filter(lambda c: c.component_type == u'clustering_key', columns) clustering_key_cols.sort(key=lambda c: c.component_index) cf.clustering_key_columns = map(lambda c: c.name, clustering_key_cols) cf.primary_key_columns = cf.partition_key_columns + cf.clustering_key_columns regular_cols = list(set(columns) - set(partition_key_cols) - set(clustering_key_cols)) regular_cols.sort(key=lambda c: c.name) cf.regular_columns = map(lambda c: c.name, regular_cols) cf.columns = partition_key_cols + clustering_key_cols + regular_cols return cf
def from_layout(cls, layout): try: colname = layout[u'column_name'] except KeyError: colname = layout[u'column'] c = cls(colname, lookup_casstype(layout[u'validator'])) c.index_name = layout[u'index_name'] return c
def test_marshalling(self): for serializedval, valtype, nativeval in marshalled_value_pairs: marshaller = lookup_casstype(valtype) whatwegot = marshaller.to_binary(nativeval) self.assertEqual(whatwegot, serializedval, msg='Marshaller for %s (%s) failed: marshal(%r) got %r instead of %r' % (valtype, marshaller, nativeval, whatwegot, serializedval)) self.assertEqual(type(whatwegot), type(serializedval), msg='Marshaller for %s (%s) gave wrong type (%s instead of %s)' % (valtype, marshaller, type(whatwegot), type(serializedval)))
def from_layout(cls, layout): c = cls(layout[u'column_name'], lookup_casstype(layout[u'validator'])) c.component_type = layout[u'type'] idx = layout[u'component_index'] # can be None if idx: c.component_index = int(idx) c.index_name = layout[u'index_name'] c.index_type = layout[u'index_type'] if c.index_type == 'CUSTOM': c.index_options = json.loads(layout[u'index_options']) return c
def from_layout(cls, layout): c = cls(layout[u"column_name"], lookup_casstype(layout[u"validator"])) c.component_type = layout[u"type"] idx = layout[u"component_index"] # can be None if idx: c.component_index = int(idx) c.index_name = layout[u"index_name"] c.index_type = layout[u"index_type"] if c.index_type == "CUSTOM": c.index_options = json.loads(layout[u"index_options"]) return c
def test_marshalling(self): for serializedval, valtype, nativeval in marshalled_value_pairs: marshaller = lookup_casstype(valtype) whatwegot = marshaller.to_binary(nativeval) self.assertEqual( whatwegot, serializedval, msg= 'Marshaller for %s (%s) failed: marshal(%r) got %r instead of %r' % (valtype, marshaller, nativeval, whatwegot, serializedval)) self.assertEqual( type(whatwegot), type(serializedval), msg='Marshaller for %s (%s) gave wrong type (%s instead of %s)' % (valtype, marshaller, type(whatwegot), type(serializedval)))
def test_unmarshalling(self): for serializedval, valtype, nativeval in marshalled_value_pairs: unmarshaller = lookup_casstype(valtype) whatwegot = unmarshaller.from_binary(serializedval) self.assertEqual( whatwegot, nativeval, msg="Unmarshaller for %s (%s) failed: unmarshal(%r) got %r instead of %r" % (valtype, unmarshaller, serializedval, whatwegot, nativeval), ) self.assertEqual( type(whatwegot), type(nativeval), msg="Unmarshaller for %s (%s) gave wrong type (%s instead of %s)" % (valtype, unmarshaller, type(whatwegot), type(nativeval)), )
def get_fields_with_types(self, ksname, typename): return [(field[0], lookup_casstype(field[1]).cql_parameterized_type()) for field in self._meta.get(ksname, {}).get(typename, [])]