Esempio n. 1
0
    def normal_batch(self, mutations, txid):
        """ Use Cassandra's native batch statement to apply mutations atomically.

    Args:
      mutations: A list of dictionaries representing mutations.
      txid: An integer specifying a transaction ID.
    """
        self.logger.debug('Normal batch: {} mutations'.format(len(mutations)))
        batch = BatchStatement(consistency_level=ConsistencyLevel.QUORUM,
                               retry_policy=BASIC_RETRIES)
        prepared_statements = {'insert': {}, 'delete': {}}
        for mutation in mutations:
            table = mutation['table']

            if table == 'group_updates':
                key = mutation['key']
                insert = ('INSERT INTO group_updates (group, last_update) '
                          'VALUES (%(group)s, %(last_update)s) '
                          'USING TIMESTAMP %(timestamp)s')
                parameters = {
                    'group': key,
                    'last_update': mutation['last_update'],
                    'timestamp': get_write_time(txid)
                }
                batch.add(insert, parameters)
                continue

            if mutation['operation'] == Operations.PUT:
                if table not in prepared_statements['insert']:
                    prepared_statements['insert'][table] = self.prepare_insert(
                        table)
                values = mutation['values']
                for column in values:
                    batch.add(
                        prepared_statements['insert'][table],
                        (bytearray(mutation['key']), column,
                         bytearray(values[column]), get_write_time(txid)))
            elif mutation['operation'] == Operations.DELETE:
                if table not in prepared_statements['delete']:
                    prepared_statements['delete'][table] = self.prepare_delete(
                        table)
                batch.add(prepared_statements['delete'][table],
                          (get_write_time(txid), bytearray(mutation['key'])))

        try:
            yield self.tornado_cassandra.execute(batch)
        except dbconstants.TRANSIENT_CASSANDRA_ERRORS:
            message = 'Unable to apply batch'
            logger.exception(message)
            raise AppScaleDBConnectionError(message)
Esempio n. 2
0
  def normal_batch(self, mutations, txid):
    """ Use Cassandra's native batch statement to apply mutations atomically.

    Args:
      mutations: A list of dictionaries representing mutations.
      txid: An integer specifying a transaction ID.
    """
    self.logger.debug('Normal batch: {} mutations'.format(len(mutations)))
    batch = BatchStatement(consistency_level=ConsistencyLevel.QUORUM,
                           retry_policy=BASIC_RETRIES)
    prepared_statements = {'insert': {}, 'delete': {}}
    for mutation in mutations:
      table = mutation['table']

      if table == 'group_updates':
        key = mutation['key']
        insert = (
          'INSERT INTO group_updates (group, last_update) '
          'VALUES (%(group)s, %(last_update)s) '
          'USING TIMESTAMP %(timestamp)s'
        )
        parameters = {'group': key, 'last_update': mutation['last_update'],
                      'timestamp': get_write_time(txid)}
        batch.add(insert, parameters)
        continue

      if mutation['operation'] == Operations.PUT:
        if table not in prepared_statements['insert']:
          prepared_statements['insert'][table] = self.prepare_insert(table)
        values = mutation['values']
        for column in values:
          batch.add(
            prepared_statements['insert'][table],
            (bytearray(mutation['key']), column, bytearray(values[column]),
             get_write_time(txid))
          )
      elif mutation['operation'] == Operations.DELETE:
        if table not in prepared_statements['delete']:
          prepared_statements['delete'][table] = self.prepare_delete(table)
        batch.add(
          prepared_statements['delete'][table],
          (get_write_time(txid), bytearray(mutation['key']))
        )

    try:
      yield self.tornado_cassandra.execute(batch)
    except dbconstants.TRANSIENT_CASSANDRA_ERRORS:
      message = 'Unable to apply batch'
      logger.exception(message)
      raise AppScaleDBConnectionError(message)
Esempio n. 3
0
    def statements_for_mutations(self, mutations, txid):
        """ Generates Cassandra statements for a list of mutations.

    Args:
      mutations: A list of dictionaries representing mutations.
      txid: An integer specifying a transaction ID.
    Returns:
      A list of tuples containing Cassandra statements and parameters.
    """
        prepared_statements = {'insert': {}, 'delete': {}}
        statements_and_params = []
        for mutation in mutations:
            table = mutation['table']

            if table == 'group_updates':
                key = mutation['key']
                insert = ('INSERT INTO group_updates (group, last_update) '
                          'VALUES (%(group)s, %(last_update)s) '
                          'USING TIMESTAMP %(timestamp)s')
                parameters = {
                    'group': key,
                    'last_update': mutation['last_update'],
                    'timestamp': get_write_time(txid)
                }
                statements_and_params.append(
                    (SimpleStatement(insert), parameters))
                continue

            if mutation['operation'] == Operations.PUT:
                if table not in prepared_statements['insert']:
                    prepared_statements['insert'][table] = self.prepare_insert(
                        table)
                values = mutation['values']
                for column in values:
                    params = (bytearray(mutation['key']), column,
                              bytearray(values[column]), get_write_time(txid))
                    statements_and_params.append(
                        (prepared_statements['insert'][table], params))
            elif mutation['operation'] == Operations.DELETE:
                if table not in prepared_statements['delete']:
                    prepared_statements['delete'][table] = self.prepare_delete(
                        table)
                params = (get_write_time(txid), bytearray(mutation['key']))
                statements_and_params.append(
                    (prepared_statements['delete'][table], params))

        return statements_and_params
Esempio n. 4
0
  def statements_for_mutations(self, mutations, txid):
    """ Generates Cassandra statements for a list of mutations.

    Args:
      mutations: A list of dictionaries representing mutations.
      txid: An integer specifying a transaction ID.
    Returns:
      A list of tuples containing Cassandra statements and parameters.
    """
    prepared_statements = {'insert': {}, 'delete': {}}
    statements_and_params = []
    for mutation in mutations:
      table = mutation['table']

      if table == 'group_updates':
        key = mutation['key']
        insert = (
          'INSERT INTO group_updates (group, last_update) '
          'VALUES (%(group)s, %(last_update)s) '
          'USING TIMESTAMP %(timestamp)s'
        )
        parameters = {'group': key, 'last_update': mutation['last_update'],
                      'timestamp': get_write_time(txid)}
        statements_and_params.append((SimpleStatement(insert), parameters))
        continue

      if mutation['operation'] == Operations.PUT:
        if table not in prepared_statements['insert']:
          prepared_statements['insert'][table] = self.prepare_insert(table)
        values = mutation['values']
        for column in values:
          params = (bytearray(mutation['key']), column,
                    bytearray(values[column]), get_write_time(txid))
          statements_and_params.append(
            (prepared_statements['insert'][table], params))
      elif mutation['operation'] == Operations.DELETE:
        if table not in prepared_statements['delete']:
          prepared_statements['delete'][table] = self.prepare_delete(table)
        params = (get_write_time(txid), bytearray(mutation['key']))
        statements_and_params.append(
          (prepared_statements['delete'][table], params))

    return statements_and_params