/** * @dataProvider getAddItemData */ public function testAddItem($item, $isEmpty) { $batch = WriteRequestBatch::factory($this->getMockedClient()); try { $batch->add($item); } catch (\Aws\Common\Exception\InvalidArgumentException $e) { // Silently fail } $this->assertSame($isEmpty, $batch->isEmpty()); }
/** * @depends testWriteRequestBatchProcessWorksAsExpected */ public function testWriteRequestBatchCanHandleLargeBatches() { // Set up /** @var $client DynamoDbClient */ $client = self::getServiceBuilder()->get('dynamodb'); $table = self::getResourcePrefix() . '-php-test-batch-write'; // Test $numItems = 30; self::log("Testing the WriteRequestBatch with {$numItems} HUGE items."); self::log("Put {$numItems} items into the table."); $writeBatch = WriteRequestBatch::factory($client); for ($i = 1; $i <= $numItems; $i++) { $writeBatch->add(new PutRequest(Item::fromArray(array('foo' => (string) $i, 'data' => str_repeat('X', 50000))), $table)); } $writeBatch->flush(); self::log("Assert that all {$numItems} items made it into the table."); $scanner = $client->getIterator('Scan', array('TableName' => $table)); $this->assertEquals($numItems, iterator_count($scanner), 'Not all of the items were inserted.'); // Tear down self::log("Deleting table {$table}..."); $client->deleteTable(array('TableName' => $table)); }
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(-1); date_default_timezone_set("UTC"); ini_set('max_execution_time', 300); require "src/aws/aws-autoloader.php"; use Aws\DynamoDb\DynamoDbClient; use Aws\DynamoDb\Model\BatchRequest\WriteRequestBatch; use Aws\DynamoDb\Model\BatchRequest\PutRequest; use Aws\DynamoDb\Model\Item; include 'src/GetZooplaData.php'; include '../cred.php'; $zoopla = new GetZooplaData(); //header('Content-type: application/xml'); $client = DynamoDbClient::factory(array('key' => $aws_key, 'secret' => $aws_secret, 'region' => 'eu-west-1')); $putBatch = WriteRequestBatch::factory($client); $updateTime = time() - 7 * 24 * 3600; $dbStationInfo = $client->scan(array('TableName' => 'station_info')); foreach ($dbStationInfo['Items'] as $item) { if (isset($item['last_updated']['S'])) { if (intval($item['last_updated']['S']) < $updateTime) { // die($item['last_updated']['S'] . " : " . $updateTime); $stations_stationName[] = $item['station_name']['S']; $stations_postCode[] = $item['post_code']['S']; $stations_londonZone[] = $item['london_zone']['S']; $stations_stationId[] = $item['station_id']['S']; if (isset($item['last_updated'])) { $stations_lastUpdate[] = $item['last_updated']['S']; } else { $stations_lastUpdate[] = 0; }
/** * Performs garbage collection on the sessions stored in the DynamoDB table * * If triggering garbage collection manually, use this method. If your garbage collection is triggered automatically * by php (not recommended), then use the `gc` method. */ public function garbageCollect() { // Get relevant configuration data $delay = (int) $this->config->get('gc_operation_delay'); $batchSize = (int) $this->config->get('gc_batch_size'); $tableName = $this->config->get('table_name'); $hashKey = $this->config->get('hash_key'); $expires = (string) time(); $isOldApi = $this->client->getApiVersion() < '2012-08-10'; // Instantiate and configure the WriteRequestBatch object that will be deleting the expired sessions if ($delay) { $delayFunction = function () use($delay) { sleep($delay); }; $deleteBatch = WriteRequestBatch::factory($this->client, $batchSize, $delayFunction); } else { $deleteBatch = WriteRequestBatch::factory($this->client, $batchSize); } // Setup a scan table iterator for finding expired session items $scanParams = array('TableName' => $tableName, 'AttributesToGet' => array($this->config->get('hash_key')), 'ScanFilter' => array('expires' => array('ComparisonOperator' => 'LT', 'AttributeValueList' => array(array('N' => $expires))), 'lock' => array('ComparisonOperator' => 'NULL')), Ua::OPTION => Ua::SESSION); if (!$isOldApi) { $scanParams['Select'] = 'SPECIFIC_ATTRIBUTES'; } // Create a scan table iterator for finding expired session items $tableScanner = $this->client->getIterator('Scan', $scanParams); // If a delay has been set, then attach the delay function to execute after each scan operation if (isset($delayFunction)) { $tableScanner->getEventDispatcher()->addListener('resource_iterator.after_send', $delayFunction); } // Perform scan and batch delete operations as needed $keyName = $isOldApi ? 'HashKeyElement' : $hashKey; foreach ($tableScanner as $item) { // @codeCoverageIgnoreStart $deleteBatch->add(new DeleteRequest(array($keyName => $item[$hashKey]), $tableName)); // @codeCoverageIgnoreEnd } // Delete any remaining items $deleteBatch->flush(); }
/** * Ensures that garbage collection functionality is working correctly */ public function testGarbageCollection() { $currentCount = iterator_count($this->client->getIterator('Scan', array('TableName' => $this->table))); self::log('Put 10 expired items into the sessions table'); $writeBatch = WriteRequestBatch::factory($this->client); for ($i = 1; $i <= 10; $i++) { $writeBatch->add(new PutRequest(Item::fromArray(array('id' => "example_{$i}", 'expires' => time() - 5 * Time::SECONDS)), $this->table)); } $writeBatch->flush(); self::log('Assert that all 10 items made it into the sessions table'); $result = $this->client->getCommand('Scan', array('TableName' => $this->table))->execute(); $this->assertEquals(10 + $currentCount, $result['Count'], 'Not all of the items were inserted.'); self::log('Create a session handler to use with a lower batch size'); $sh = SessionHandler::factory(array('dynamodb_client' => $this->client, 'table_name' => $this->table, 'gc_batch_size' => 3)); self::log('Run the garbage collection'); $sh->garbageCollect(); self::log('Assert that all 10 items were deleted from the sessions table'); $result = $this->client->getCommand('Scan', array('TableName' => $this->table))->execute(); $this->assertEquals(0, $result['Count'], 'Not all of the items were removed.'); }
/** * Use WriteRequestBatch to batch several DeleteItem requests * * @depends testWriteRequestBatchForPuts * @example Aws\DynamoDb\Model\BatchRequest\DeleteRequest::__construct 2011-12-05 */ public function testWriteRequestBatchForDeletes(array $state) { /** @var $client DynamoDbClient */ /** @var $tableName string */ /** @var $itemIds array */ list($client, $tableName, $itemIds) = $state; self::log("Test deleting items in batches using WriteRequestBatch."); // @begin // Remove items from the table $deleteBatch = WriteRequestBatch::factory($client); foreach ($itemIds as $itemId) { $key = array('HashKeyElement' => array('S' => $itemId)); $deleteBatch->add(new DeleteRequest($key, $tableName)); } $deleteBatch->flush(); // @end self::log("Assert that all the items have been deleted from the table"); $scanner = $client->getIterator('Scan', array('TableName' => $tableName)); $this->assertEquals(0, iterator_count($scanner), 'Not all of the items were deleted.'); }
/** * Add entity to the batch deletion * @param EntityInterface $entity * @throws \Aws\DynamoDb\Exception\DynamoDBException */ public function deleteBatch(EntityInterface $entity) { if (is_null($this->deleteBatch)) { $this->deleteBatch = WriteRequestBatch::factory($this->dynamoDb); } $this->deleteBatch->add(new DeleteRequest($this->formatKeyCondition($entity), $this->getEntityTable($entity))); }
/** * Performs garbage collection on the sessions stored in the DynamoDB table * * If triggering garbage collection manually, use this method. If your * garbage collection is triggered automatically by php (not recommended), * then use the `gc` method. */ public function garbageCollect() { $deleteBatch = WriteRequestBatch::factory($this->client, $this->config->get('gc_batch_size')); $tableName = $this->config->get('table_name'); $expires = (string) time(); // Setup a scan table command for finding expired session items $tableScan = $this->client->getCommand('Scan', array('TableName' => $tableName, 'AttributesToGet' => array($this->config->get('hash_key')), 'ScanFilter' => array('expires' => array('ComparisonOperator' => 'LT', 'AttributeValueList' => array(array('N' => $expires))), 'lock' => array('ComparisonOperator' => 'NULL')), Ua::OPTION => Ua::SESSION)); // Perform scan and batch delete operations as needed foreach ($this->client->getIterator($tableScan) as $item) { // @codeCoverageIgnoreStart $key = array('HashKeyElement' => $item[$this->config->get('hash_key')]); $deleteBatch->add(new DeleteRequest($key, $tableName)); // @codeCoverageIgnoreEnd } // Delete any remaining items $deleteBatch->flush(); }