/**
  * Constructs a new put request
  *
  * @param Item   $item      The item to put into DynamoDB
  * @param string $tableName The name of the table which has the item
  *
  * @throw InvalidArgumentException if the table name is not provided
  */
 public function __construct(Item $item, $tableName = null)
 {
     $this->item = $item;
     $this->tableName = $tableName ?: $item->getTableName();
     if (!$this->tableName) {
         throw new InvalidArgumentException('A table name is required to create a PutRequest.');
     }
 }
Beispiel #2
0
 /**
  * Constructs a new put request
  *
  * @param array|Item $item      The item to put into DynamoDB
  * @param string     $tableName The name of the table which has the item
  *
  * @throw InvalidArgumentException if the table name is not provided
  */
 public function __construct($item, $tableName = null)
 {
     if ($item instanceof Item) {
         $this->item = $item->toArray();
         $this->tableName = $tableName ?: $item->getTableName();
     } elseif (is_array($item)) {
         $this->item = $item;
         $this->tableName = $tableName;
     } else {
         throw new InvalidArgumentException('The item must be an array or an Item object.');
     }
     if (!$this->tableName) {
         throw new InvalidArgumentException('A table name is required to create a PutRequest.');
     }
 }
 /**
  * Use WriteRequestBatch to batch several PutItem requests
  */
 public function testWriteRequestBatchForPuts()
 {
     /** @var $client DynamoDbClient */
     $client = self::getServiceBuilder()->get('dynamodb', array('version' => '2011-12-05'));
     $tableName = 'batch-write-test';
     try {
         $client->describeTable(array('TableName' => $tableName));
         self::log("The {$tableName} table exists.");
     } catch (ResourceNotFoundException $e) {
         self::log("Create the {$tableName} table.");
         $client->createTable(array('TableName' => $tableName, 'KeySchema' => array('HashKeyElement' => array('AttributeName' => 'id', 'AttributeType' => 'S')), 'ProvisionedThroughput' => array('ReadCapacityUnits' => 20, 'WriteCapacityUnits' => 20)));
     }
     self::log("Wait until the {$tableName} table's status is ACTIVE...");
     $client->waitUntilTableExists(array('TableName' => $tableName));
     self::log("Test writing items in batches using WriteRequestBatch.");
     // @begin
     $tableName = 'batch-write-test';
     // This table has a HashKey named "id"
     $itemIds = array();
     // Put 55 items into the table
     $putBatch = WriteRequestBatch::factory($client);
     for ($i = 0; $i < 55; $i++) {
         $itemIds[] = $itemId = uniqid();
         $item = Item::fromArray(array('id' => $itemId, 'timestamp' => time()));
         $putBatch->add(new PutRequest($item, $tableName));
     }
     $putBatch->flush();
     // @end
     self::log("Assert that all the items made it into the table.");
     $scan = $client->getIterator('Scan', array('TableName' => $tableName));
     $this->assertGreaterThanOrEqual(55, iterator_count($scan), 'Not all of the items were inserted.');
     return array($client, $tableName, $itemIds);
 }
Beispiel #4
0
 /**
  * @covers Aws\DynamoDb\Model\Item::fromArray
  */
 public function testCanCreateFromArray()
 {
     $item = Item::fromArray(array('foo' => 'bar', 'baz' => array(1, 2, 3), 'list' => array('foo', 'bar'), 'test' => 2));
     $this->assertEquals('S', $item->get('foo')->getType());
     $this->assertEquals('bar', $item['foo']);
     $this->assertEquals('NS', $item->get('baz')->getType());
     $this->assertEquals(array(1, 2, 3), $item['baz']->getValue());
     $this->assertEquals('SS', $item->get('list')->getType());
     $this->assertEquals(array('foo', 'bar'), $item['list']->getValue());
     $this->assertEquals('N', $item->get('test')->getType());
     $this->assertEquals(2, $item['test']->getValue());
 }
 /**
  * @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));
 }
 /**
  * @depends testCreatesTable
  */
 public function testIteratesOverScan()
 {
     self::log('Adding 3 items to the table');
     $this->client->putItem(array('TableName' => $this->table, 'Item' => Item::fromArray(array('foo' => 'Bar', 'bar' => 10))));
     self::log('Added 1 item');
     $this->client->putItem(array('TableName' => $this->table, 'Item' => Item::fromArray(array('foo' => 'Bar', 'bar' => 20))));
     self::log('Added 2 items');
     $this->client->putItem(array('TableName' => $this->table, 'Item' => Item::fromArray(array('foo' => 'Bar', 'bar' => 30))));
     self::log('Added 3 items');
     self::log('Waiting until at least 3 items are in the table');
     $client = $this->client;
     $table = $this->table;
     $waiter = new CallableWaiter();
     $waiter->setCallable(function () use($client, $table) {
         $result = $client->scan(array('TableName' => $table));
         return count($result['Items']) >= 3;
     })->setMaxAttempts(10)->setInterval(1);
     $iterator = $client->getIterator('Scan', array('TableName' => $this->table, 'Limit' => 2, 'ScanFilter' => array('bar' => array('AttributeValueList' => array(array('N' => '5')), 'ComparisonOperator' => 'GT'))));
     $items = $iterator->toArray();
     $this->assertTrue(count($items) >= 3, 'Expected 3 items, got ' . count($items));
     $this->assertTrue($iterator->getRequestCount() >= 2);
     $mustMatch = $this->rangeKeyValues;
     foreach ($items as $item) {
         if (false !== ($pos = array_search($item['bar']['N'], $mustMatch))) {
             unset($mustMatch[$pos]);
             if (empty($mustMatch)) {
                 break;
             }
         }
     }
     if (!empty($mustMatch)) {
         $this->fail('All known items were not found in scan: ' . var_export($mustMatch, true) . ' - found: ' . var_export($items, true));
     }
     return $mustMatch;
 }
Beispiel #7
0
 public function testCanConvertToArray()
 {
     $putRequest = new PutRequest(Item::fromArray(array('foo' => 'bar', 'baz' => 123)), 'table');
     $this->assertEquals(array('PutRequest' => array('Item' => array('foo' => array('S' => 'bar'), 'baz' => array('N' => '123')))), $putRequest->toArray());
 }
 /**
  * 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.');
 }
 /**
  * Add entity to the batch creation
  * @param  EntityInterface $entity
  * @throws \Aws\DynamoDb\Exception\DynamoDBException
  */
 public function createBatch(EntityInterface $entity)
 {
     if (is_null($this->putBatch)) {
         $this->putBatch = WriteRequestBatch::factory($this->dynamoDb);
     }
     $this->putBatch->add(new PutRequest(Item::fromArray($entity->getArrayCopy()), $this->getEntityTable($entity)));
 }