def _pack_name(self, value, is_supercol_name=False, slice_end=_NON_SLICE): if not self.autopack_names: if value is not None and not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError( "A str or unicode column name was expected, but %s was received instead (%s)" % (value.__class__.__name__, str(value))) return value if value is None: return if is_supercol_name: d_type = self.supercol_name_data_type else: d_type = self.col_name_data_type if d_type == 'TimeUUIDType': if slice_end: value = util.convert_time_to_uuid( value, lowest_val=(slice_end == _SLICE_START), randomize=False) else: value = util.convert_time_to_uuid(value, randomize=True) elif d_type == 'BytesType' and not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError( "A str or unicode column name was expected, but %s was received instead (%s)" % (value.__class__.__name__, str(value))) return util.pack(value, d_type)
def _pack_name(self, value, is_supercol_name=False, slice_end=_NON_SLICE): if not self.autopack_names: if value is not None and not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("A str or unicode column name was expected, but %s was received instead (%s)" % (value.__class__.__name__, str(value))) return value if value is None: return if is_supercol_name: d_type = self.supercol_name_data_type else: d_type = self.col_name_data_type if d_type == 'TimeUUIDType': if slice_end: value = util.convert_time_to_uuid(value, lowest_val=(slice_end == _SLICE_START), randomize=False) else: value = util.convert_time_to_uuid(value, randomize=True) elif d_type == 'BytesType' and not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("A str or unicode column name was expected, but %s was received instead (%s)" % (value.__class__.__name__, str(value))) return util.pack(value, d_type)
def _pack_value(self, value, col_name): if not self.autopack_values: if value is not None and not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("A str or unicode column value was expected for column '%s', but %s was received instead (%s)" % (str(col_name), value.__class__.__name__, str(value))) return value d_type = self._get_data_type_for_col(col_name) if d_type == 'BytesType' and not (isinstance(value, str) or isinstance(value, unicode)): raise TypeError("A str or unicode column value was expected for column '%s', but %s was received instead (%s)" % (str(col_name), value.__class__.__name__, str(value))) return util.pack(value, d_type)
def create_index(self, keyspace, column_family, column, value_type, index_type=KEYS_INDEX, index_name=None): """ Creates an index on a column. This allows efficient for index usage via :meth:`~pycassa.columnfamily.ColumnFamily.get_indexed_slices()` `column` specifies what column to index, and `value_type` is a string that describes that column's value's data type; see :meth:`alter_column()` for a full description of `value_type`. `index_type` determines how the index will be stored internally. Currently, :const:`KEYS_INDEX` is the only option. `index_name` is an optional name for the index. Example Usage: .. code-block:: python >>> from pycassa.system_manager import * >>> sys = SystemManager('192.168.2.10:9160') >>> sys.create_index('Keyspace1', 'Standard1', 'birthdate', LONG_TYPE, index_name='bday_index') >>> sys.close """ self._conn.set_keyspace(keyspace) cfdef = self.get_keyspace_column_families(keyspace)[column_family] col_name_data_type = util.extract_type_name(cfdef.comparator_type) packed_column = util.pack(column, col_name_data_type) if value_type.find('.') == -1: value_type = 'org.apache.cassandra.db.marshal.%s' % value_type coldef = ColumnDef(packed_column, value_type, index_type, index_name) for c in cfdef.column_metadata: if c.name == packed_column: cfdef.column_metadata.remove(c) break cfdef.column_metadata.append(coldef) self._system_update_column_family(cfdef)
def alter_column(self, keyspace, column_family, column, value_type): """ Sets a data type for the value of a specific column. `value_type` is a string that determines what type the column value will be. By default, :const:`LONG_TYPE`, :const:`INT_TYPE`, :const:`ASCII_TYPE`, :const:`UTF8_TYPE`, :const:`TIME_UUID_TYPE`, :const:`LEXICAL_UUID_TYPE` and :const:`BYTES_TYPE` are provided. Custom types may be used as well by providing the class name; if the custom comparator class is not in ``org.apache.cassandra.db.marshal``, the fully qualified class name must be given. For super column families, this sets the subcolumn value type for any subcolumn named `column`, regardless of the super column name. """ self._conn.set_keyspace(keyspace) cfdef = self.get_keyspace_column_families(keyspace)[column_family] if cfdef.column_type == 'Super': col_name_data_type = util.extract_type_name( cfdef.subcomparator_type) else: col_name_data_type = util.extract_type_name(cfdef.comparator_type) packed_column = util.pack(column, col_name_data_type) if value_type.find('.') == -1: value_type = 'org.apache.cassandra.db.marshal.%s' % value_type matched = False for c in cfdef.column_metadata: if c.name == packed_column: c.validation_class = value_type matched = True break if not matched: cfdef.column_metadata.append( ColumnDef(packed_column, value_type, None, None)) self._system_update_column_family(cfdef)
def alter_column(self, keyspace, column_family, column, value_type): """ Sets a data type for the value of a specific column. `value_type` is a string that determines what type the column value will be. By default, :const:`LONG_TYPE`, :const:`INT_TYPE`, :const:`ASCII_TYPE`, :const:`UTF8_TYPE`, :const:`TIME_UUID_TYPE`, :const:`LEXICAL_UUID_TYPE` and :const:`BYTES_TYPE` are provided. Custom types may be used as well by providing the class name; if the custom comparator class is not in ``org.apache.cassandra.db.marshal``, the fully qualified class name must be given. For super column families, this sets the subcolumn value type for any subcolumn named `column`, regardless of the super column name. """ self._conn.set_keyspace(keyspace) cfdef = self.get_keyspace_column_families(keyspace)[column_family] if cfdef.column_type == 'Super': col_name_data_type = util.extract_type_name(cfdef.subcomparator_type) else: col_name_data_type = util.extract_type_name(cfdef.comparator_type) packed_column = util.pack(column, col_name_data_type) if value_type.find('.') == -1: value_type = 'org.apache.cassandra.db.marshal.%s' % value_type matched = False for c in cfdef.column_metadata: if c.name == packed_column: c.validation_class = value_type matched = True break if not matched: cfdef.column_metadata.append(ColumnDef(packed_column, value_type, None, None)) self._system_update_column_family(cfdef)
def _pack_key(self, key): if not self.autopack_keys or not key: return key return util.pack(key, self.key_validation_class)