/** * Insert n values into the table for a given key. * * @param $statementType Type of statement to create for inserts * @param RetryPolicy $policy RetryPolicy to use when executing statements * @param $key Key value * @param $numberOfInserts Number of inserts to perform * @param $consistency Consistency level to execute statement * @param int $retries Number of TimeoutException retries * (DEFAULT: self::NUMBER_OF_TIMEOUT_EXCEPTIONS) */ private function insert($statementType, RetryPolicy $policy, $key, $numberOfInserts, $consistency, $retries = self::NUMBER_OF_TIMEOUT_EXCEPTIONS) { try { // Create all statement types $batch = new BatchStatement(\Cassandra::BATCH_UNLOGGED); $prepare = $this->session->prepare($this->insertQuery); $simple = new SimpleStatement($this->insertQuery); // Create the default execution options $options = array("consistency" => $consistency, "retry_policy" => $policy); // Create the inserts foreach (range(1, $numberOfInserts) as $i) { $values = array($key, $i); if ($statementType == self::BATCH_STATEMENT) { if ($i % 2 == 0) { $batch->add($prepare, $values); } else { $batch->add($simple, $values); } } else { // Execute either the prepare or simple statment $statement = $prepare; if ($statementType == self::SIMPLE_STATEMENT) { $statement = $simple; } $options["arguments"] = $values; $this->session->execute($statement, new ExecutionOptions($options)); } } // Execute the batched insert if ($statementType == self::BATCH_STATEMENT) { $this->session->execute($batch, new ExecutionOptions($options)); } } catch (Exception\TimeoutException $te) { if (Integration::isDebug()) { fprintf(STDOUT, "Insert TimeoutException: %s (%s:%d)" . PHP_EOL, $te->getMessage(), $te->getFile(), $te->getLine()); } if ($retries > 0) { $this->insert($policy, $key, $numberOfInserts, $consistency, $retries - 1); } else { throw $te; } } }
/** * Batch statement timestamps * * This test will ensure that the PHP driver supports timestamps using * batch statements; client, server, and forced (option and using) * timestamps are executed. * * @test * @ticket PHP-59 * * @cassandra-version-2.1 */ public function testBatchStatement() { // Create the batch statement $batch = new BatchStatement(\Cassandra::BATCH_UNLOGGED); $simple = new SimpleStatement($this->insertQuery); $prepare = $this->session->prepare($this->insertQuery); // Simple statement $batch->add($simple, array(0, 1)); // Prepared statement $batch->add($prepare, array("key" => 1, "value_int" => 2)); // Forced timestamp (simple) $query = "{$this->insertQuery} USING TIMESTAMP 90"; $simple = new SimpleStatement($query); $batch->add($simple, array(2, 3)); // Insert the batch and assert the values $this->insert($this->session, $batch, null, null, 11111); $this->assert(0, 11111); $this->assert(1, 11111); $this->assert(2, 90); // Using timestamp generator (client); upsert will occur $now = $this->now(); sleep(1); $this->insert($this->clientSideTimestampSession, $batch); $this->assert(0, $now, false); $this->assert(1, $now, false); $this->assert(2, 90); // Using timestamp generator (server); upsert will occur $serverMow = $this->serverNow(); sleep(1); $this->insert($this->session, $batch); $this->assert(0, $serverMow, false); $this->assert(1, $serverMow, false); $this->assert(2, 90); }