Пример #1
0
 def _load_comparator_classes(self):
     if not self.super:
         self.column_name_class = util.extract_type_name(self._cfdef.comparator_type)
         self.super_column_name_class = None
     else:
         self.column_name_class = util.extract_type_name(self._cfdef.subcomparator_type)
         self.super_column_name_class = util.extract_type_name(self._cfdef.comparator_type)
Пример #2
0
 def _load_comparator_classes(self):
     if not self.super:
         self.column_name_class = util.extract_type_name(self._cfdef.comparator_type)
         self.super_column_name_class = None
     else:
         self.column_name_class = util.extract_type_name(self._cfdef.subcomparator_type)
         self.super_column_name_class = util.extract_type_name(self._cfdef.comparator_type)
Пример #3
0
 def _load_validation_classes(self):
     self.default_validation_class = util.extract_type_name(
         self._cfdef.default_validation_class)
     self.column_validators = {}
     for name, coldef in self._cfdef.column_metadata.items():
         self.column_validators[name] = util.extract_type_name(
             coldef.validation_class)
Пример #4
0
    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)
Пример #6
0
    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 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)
Пример #8
0
    def __init__(self, pool, column_family, buffer_size=1024,
                 read_consistency_level=ConsistencyLevel.ONE,
                 write_consistency_level=ConsistencyLevel.ONE,
                 timestamp=gm_timestamp, super=False,
                 dict_class=util.OrderedDict, autopack_names=True,
                 autopack_values=True):
        """
        An abstraction of a Cassandra column family or super column family.
        Operations on this, such as :meth:`get` or :meth:`insert` will get data from or
        insert data into the corresponding Cassandra column family with
        name `column_family`.

        `pool` is a :class:`~pycassa.pool.ConnectionPool` that the column
        family will use for all operations.  A connection is drawn from the
        pool before each operations and is returned afterwards.  Note that
        the keyspace to be used is determined by the pool.

        When calling :meth:`get_range()` or :meth:`get_indexed_slices()`,
        the intermediate results need to be buffered if we are fetching many
        rows, otherwise the Cassandra server will overallocate memory and fail.
        `buffer_size` is the size of that buffer in number of rows.  The default
        is 1024.

        `read_consistency_level` and `write_consistency_level` set the default
        consistency levels for every operation; these may be overridden
        per-operation. These should be instances of
        :class:`~pycassa.cassandra.ttypes.ConsistencyLevel`.  These default
        to level ``ONE``.

        Each :meth:`insert()` or :meth:`remove` sends a timestamp with every
        column. The `timestamp` parameter is a function that is used to get
        this timestamp when needed.  The default function is :meth:`gm_timestamp()`.

        Results are returned as dictionaries. :class:`~pycassa.util.OrderedDict` is
        used by default so that order is maintained. A different class, such as
        :class:`dict` may be used instead by passing `dict_class`.

        By default, column family definitions will be examined to determine
        what data type Cassandra expects for column names and values. When
        columns are retrieved or inserted, their names and values will be
        packed or unpacked if necessary to convert them to or from their
        binary representation. Automatic packing of names and values can
        be individually enabled or disabled with `autopack_names` and
        `autopack_values`.  When using :class:`~pycassa.columnfamilymap.ColumnFamilyMap`,
        these should both be set to ``False``.

        """

        self.pool = pool
        self._tlocal = threading.local()
        self._tlocal.client = None
        self.column_family = column_family
        self.buffer_size = buffer_size
        self.read_consistency_level = read_consistency_level
        self.write_consistency_level = write_consistency_level
        self.timestamp = timestamp
        self.dict_class = dict_class
        self.autopack_names = autopack_names
        self.autopack_values = autopack_values

        # Determine the ColumnFamily type to allow for auto conversion
        # so that packing/unpacking doesn't need to be done manually
        self.cf_data_type = None
        self.col_name_data_type = None
        self.supercol_name_data_type = None
        self.col_type_dict = dict()

        col_fam = None
        try:
            try:
                self._obtain_connection()
                col_fam = self._tlocal.client.get_keyspace_description(use_dict_for_col_metadata=True)[self.column_family]
            except KeyError:
                nfe = NotFoundException()
                nfe.why = 'Column family %s not found.' % self.column_family
                raise nfe
        finally:
            self._release_connection()

        if col_fam is not None:
            self.super = col_fam.column_type == 'Super'
            if self.autopack_names:
                if not self.super:
                    self.col_name_data_type = col_fam.comparator_type
                else:
                    self.col_name_data_type = col_fam.subcomparator_type
                    self.supercol_name_data_type = util.extract_type_name(col_fam.comparator_type)

                index = self.col_name_data_type = util.extract_type_name(self.col_name_data_type)
            if self.autopack_values:
                self.cf_data_type = util.extract_type_name(col_fam.default_validation_class)
                for name, cdef in col_fam.column_metadata.items():
                    self.col_type_dict[name] = util.extract_type_name(cdef.validation_class)
Пример #9
0
    def __init__(self,
                 pool,
                 column_family,
                 buffer_size=1024,
                 read_consistency_level=ConsistencyLevel.ONE,
                 write_consistency_level=ConsistencyLevel.ONE,
                 timestamp=gm_timestamp,
                 super=False,
                 dict_class=util.OrderedDict,
                 autopack_names=True,
                 autopack_values=True):
        """
        An abstraction of a Cassandra column family or super column family.
        Operations on this, such as :meth:`get` or :meth:`insert` will get data from or
        insert data into the corresponding Cassandra column family with
        name `column_family`.

        `pool` is a :class:`~pycassa.pool.ConnectionPool` that the column
        family will use for all operations.  A connection is drawn from the
        pool before each operations and is returned afterwards.  Note that
        the keyspace to be used is determined by the pool.

        When calling :meth:`get_range()` or :meth:`get_indexed_slices()`,
        the intermediate results need to be buffered if we are fetching many
        rows, otherwise the Cassandra server will overallocate memory and fail.
        `buffer_size` is the size of that buffer in number of rows.  The default
        is 1024.

        `read_consistency_level` and `write_consistency_level` set the default
        consistency levels for every operation; these may be overridden
        per-operation. These should be instances of
        :class:`~pycassa.cassandra.ttypes.ConsistencyLevel`.  These default
        to level ``ONE``.

        Each :meth:`insert()` or :meth:`remove` sends a timestamp with every
        column. The `timestamp` parameter is a function that is used to get
        this timestamp when needed.  The default function is :meth:`gm_timestamp()`.

        Results are returned as dictionaries. :class:`~pycassa.util.OrderedDict` is
        used by default so that order is maintained. A different class, such as
        :class:`dict` may be used instead by passing `dict_class`.

        By default, column family definitions will be examined to determine
        what data type Cassandra expects for column names and values. When
        columns are retrieved or inserted, their names and values will be
        packed or unpacked if necessary to convert them to or from their
        binary representation. Automatic packing of names and values can
        be individually enabled or disabled with `autopack_names` and
        `autopack_values`.  When using :class:`~pycassa.columnfamilymap.ColumnFamilyMap`,
        these should both be set to ``False``.

        """

        self.pool = pool
        self._tlocal = threading.local()
        self._tlocal.client = None
        self.column_family = column_family
        self.buffer_size = buffer_size
        self.read_consistency_level = read_consistency_level
        self.write_consistency_level = write_consistency_level
        self.timestamp = timestamp
        self.dict_class = dict_class
        self.autopack_names = autopack_names
        self.autopack_values = autopack_values

        # Determine the ColumnFamily type to allow for auto conversion
        # so that packing/unpacking doesn't need to be done manually
        self.cf_data_type = None
        self.col_name_data_type = None
        self.supercol_name_data_type = None
        self.col_type_dict = dict()

        col_fam = None
        try:
            try:
                self._obtain_connection()
                col_fam = self._tlocal.client.get_keyspace_description(
                    use_dict_for_col_metadata=True)[self.column_family]
            except KeyError:
                nfe = NotFoundException()
                nfe.why = 'Column family %s not found.' % self.column_family
                raise nfe
        finally:
            self._release_connection()

        if col_fam is not None:
            self.super = col_fam.column_type == 'Super'
            if self.autopack_names:
                if not self.super:
                    self.col_name_data_type = col_fam.comparator_type
                else:
                    self.col_name_data_type = col_fam.subcomparator_type
                    self.supercol_name_data_type = util.extract_type_name(
                        col_fam.comparator_type)

                index = self.col_name_data_type = util.extract_type_name(
                    self.col_name_data_type)
            if self.autopack_values:
                self.cf_data_type = util.extract_type_name(
                    col_fam.default_validation_class)
                for name, cdef in col_fam.column_metadata.items():
                    self.col_type_dict[name] = util.extract_type_name(
                        cdef.validation_class)
Пример #10
0
 def _load_key_class(self):
     if hasattr(self._cfdef, "key_validation_class"):
         self.key_validation_class = util.extract_type_name(self._cfdef.key_validation_class)
     else:
         self.key_validation_class = "BytesType"
Пример #11
0
 def _load_validation_classes(self):
     self.default_validation_class = util.extract_type_name(self._cfdef.default_validation_class)
     self.column_validators = {}
     for name, coldef in self._cfdef.column_metadata.items():
         self.column_validators[name] = util.extract_type_name(coldef.validation_class)
Пример #12
0
 def _load_key_class(self):
     if hasattr(self._cfdef, "key_validation_class"):
         self.key_validation_class = util.extract_type_name(self._cfdef.key_validation_class)
     else:
         self.key_validation_class = 'BytesType'