Example #1
0
 def _make_mutations_insert(self, column_family, columns, timestamp, ttl):
     _pack_name = column_family._pack_name
     _pack_value = column_family._pack_value
     _get_type = column_family._get_data_type_for_col
     if column_family.super:
         for c, v in columns.iteritems():
             cos = ColumnOrSuperColumn()
             dtype = _get_type(c)
             if dtype == 'CounterColumnType':
                 subc = [CounterColumn(_pack_name(subname), subvalue)
                         for subname, subvalue in v.iteritems()]
                 cos.counter_super_column = CounterSuperColumn(name=_pack_name(c, True),
                                                               columns=subc)
             else:
                 subc = [Column(name=_pack_name(subname),
                                value=_pack_value(subvalue, subname),
                                timestamp=timestamp, ttl=ttl)
                             for subname, subvalue in v.iteritems()]
                 cos.super_column = SuperColumn(name=_pack_name(c, True),
                                                columns=subc)
             yield Mutation(column_or_supercolumn=cos)
     else:
         for c, v in columns.iteritems():
             cos = ColumnOrSuperColumn()
             dtype = _get_type(c)
             if dtype == 'CounterColumnType':
                 cos.counter_column = CounterColumn(_pack_name(c), v)
             else:
                 cos.column = Column(name=_pack_name(c), value=_pack_value(v, c),
                                     timestamp=timestamp, ttl=ttl)
             yield Mutation(column_or_supercolumn=cos)
Example #2
0
 def _make_mutations_insert(self, column_family, columns, timestamp, ttl):
     _pack_name = column_family._pack_name
     _pack_value = column_family._pack_value
     for c, v in columns.iteritems():
         cos = ColumnOrSuperColumn()
         if column_family.super:
             subc = [Column(name=_pack_name(subname),
                            value=_pack_value(subvalue, subname),
                            timestamp=timestamp, ttl=ttl)
                         for subname, subvalue in v.iteritems()]
             cos.super_column = SuperColumn(name=_pack_name(c, True),
                                            columns=subc)
         else:
             cos.column = Column(name=_pack_name(c), value=_pack_value(v, c),
                                 timestamp=timestamp, ttl=ttl)
         yield Mutation(column_or_supercolumn=cos)
Example #3
0
    def insert(self,
               key,
               columns,
               timestamp=None,
               ttl=None,
               write_consistency_level=None):
        """
        Insert or update columns in the row with key `key`.

        `columns` should be a dictionary of columns or super columns to insert
        or update.  If this is a standard column family, `columns` should
        look like ``{column_name: column_value}``.  If this is a super
        column family, `columns` should look like
        ``{super_column_name: {sub_column_name: value}}``

        A timestamp may be supplied for all inserted columns with `timestamp`.

        `ttl` sets the "time to live" in number of seconds for the inserted
        columns. After this many seconds, Cassandra will mark the columns as
        deleted.

        The timestamp Cassandra reports as being used for insert is returned.

        """
        if timestamp is None:
            timestamp = self.timestamp()

        packed_key = self._pack_key(key)

        if ((not self.super) and len(columns) == 1) or \
           (self.super and len(columns) == 1 and len(columns.values()[0]) == 1):

            if self.super:
                super_col = columns.keys()[0]
                cp = self._column_path(super_col)
                columns = columns.values()[0]
            else:
                cp = self._column_path()

            colname = columns.keys()[0]
            colval = self._pack_value(columns.values()[0], colname)
            colname = self._pack_name(colname, False)
            column = Column(colname, colval, timestamp, ttl)

            self.pool.execute(
                'insert', packed_key, cp, column, write_consistency_level
                or self.write_consistency_level)
        else:
            mut_list = self._make_mutation_list(columns, timestamp, ttl)
            mutations = {packed_key: {self.column_family: mut_list}}
            self.pool.execute(
                'batch_mutate', mutations, write_consistency_level
                or self.write_consistency_level)

        return timestamp
    def insert(self,
               key,
               columns,
               timestamp=None,
               ttl=None,
               write_consistency_level=None):
        """
        Insert or update columns in the row with key `key`.

        `columns` should be a dictionary of columns or super columns to insert
        or update.  If this is a standard column family, `columns` should
        look like ``{column_name: column_value}``.  If this is a super
        column family, `columns` should look like
        ``{super_column_name: {sub_column_name: value}}``

        A timestamp may be supplied for all inserted columns with `timestamp`.

        `ttl` sets the "time to live" in number of seconds for the inserted
        columns. After this many seconds, Cassandra will mark the columns as
        deleted.

        The timestamp Cassandra reports as being used for insert is returned.

        """
        packed_key = self._pack_key(key)
        if ((not self.super) and len(columns) == 1) or \
           (self.super and len(columns) == 1 and len(columns.values()[0]) == 1):

            if timestamp is None:
                timestamp = self.timestamp()

            if self.super:
                super_col = columns.keys()[0]
                cp = self._column_path(super_col)
                columns = columns.values()[0]
            else:
                cp = self._column_path()

            colname = self._pack_name(columns.keys()[0], False)
            colval = self._pack_value(columns.values()[0], colname)
            column = Column(colname, colval, timestamp, ttl)
            try:
                self._obtain_connection()
                self._tlocal.client.insert(
                    packed_key, cp, column, write_consistency_level
                    or self.write_consistency_level)
            finally:
                self._release_connection()
            return timestamp
        else:
            return self.batch_insert(
                {key: columns},
                timestamp=timestamp,
                ttl=ttl,
                write_consistency_level=write_consistency_level)
Example #5
0
 def _make_normal_cosc(name, value, timestamp, ttl):
     return ColumnOrSuperColumn(Column(name, value, timestamp, ttl))