/** * Save marshaled data * * @param $id * @param $data * @return bool|\Guzzle\Service\Resource\Model */ public function saveData($id, $data) { if (empty($this->table)) { return false; } $client_data = ['TableName' => $this->table, 'Item' => ['id' => ['S' => $id], 'item_status' => ['S' => 'entered'], 'payload' => ['M' => $this->marshallData($data)], 'date' => ['S' => utf8_encode((string) date('Y-m-d H:i:s'))]]]; $result = $this->client->putItem($client_data); return $result; }
/** * {@inheritdoc} */ protected function write(array $record) { $filtered = $this->filterEmptyFields($record['formatted']); if ($this->version === 3) { $formatted = $this->marshaler->marshalItem($filtered); } else { $formatted = $this->client->formatAttributes($filtered); } $this->client->putItem(['TableName' => $this->table, 'Item' => $formatted]); }
public function saved($model) { $attrs = $model->attributesToArray(); // $this->attributeFilter->filter($attrs); try { $this->dynamoDbClient->putItem(['TableName' => $model->getDynamoDbTableName(), 'Item' => $this->marshaler->marshalItem($attrs)]); } catch (Exception $e) { Log::info($e); } }
public function testPutItem() { try { $item = $this->marshaler->marshal($this->message); self::$client->putItem(['TableName' => self::$tableName, 'Item' => $item]); } catch (\Exception $e) { $this->fail($e->getMessage()); return; } }
public function save(array $options = []) { if (!$this->getKey()) { $this->fireModelEvent('creating'); } // $this->attributeFilter->filter($this->attributes); try { $this->client->putItem(['TableName' => $this->getTable(), 'Item' => $this->marshalItem($this->attributes)]); return true; } catch (Exception $e) { Log::info($e); return false; } }
/** * Add an item to DynamoDB via the put_item call * @param Item $item * @param Context\Put|null $context The call context * @return array|null * @throws Exception\AttributesException */ public function put(Item $item, Context\Put $context = null) { $table = $item->getTable(); if (null !== $this->logger) { $this->log('Put on table ' . $table); } if (empty($table)) { throw new \Riverline\DynamoDB\Exception\AttributesException('Item do not have table defined'); } $attributes = array(); foreach ($item as $name => $attribute) { /** @var $attribute \Riverline\DynamoDB\Attribute */ if ("" !== $attribute->getValue()) { // Only not empty string $attributes[$name] = $attribute->getForDynamoDB(); } } $parameters = array('TableName' => $table, 'Item' => $attributes); if (null !== $context) { $parameters += $context->getForDynamoDB(); } if (null !== $this->logger) { $this->log('Put request paramaters : ' . print_r($parameters, true), Logger::DEBUG); } $response = $this->connector->putItem($parameters); if (null !== $this->logger) { $this->log('Put request response : ' . print_r($response, true), Logger::DEBUG); } // Update write counter $this->addConsumedWriteUnits($table, floatval($response['ConsumedCapacityUnits'])); return $this->populateAttributes($response); }
protected function seed($attributes = []) { $item = ['id' => ['S' => str_random(36)], 'name' => ['S' => str_random(36)], 'description' => ['S' => str_random(256)], 'count' => ['N' => rand()]]; $item = array_merge($item, $attributes); $this->dynamoDbClient->putItem(['TableName' => $this->testModel->getTable(), 'Item' => $item]); return $item; }
/** * @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; }
/** * Fill the table with data */ protected static function fillTable() { $data = ['key' => 'dev', 'settings' => self::getTestData()]; $marshaler = new Marshaler(); $item = $marshaler->marshalItem($data); self::$dynamodb->putItem(['TableName' => self::TABLE_NAME, 'Item' => $item]); }
public function set(array $obj, $checkAndSet = false, &$casValue = null) { $requestArgs = ["TableName" => $this->tableName]; if ($this->casField) { $old_cas = isset($obj[$this->casField]) ? $obj[$this->casField] : null; $casValue = time(); $obj[$this->casField] = $casValue; if ($checkAndSet) { if ($old_cas) { $requestArgs['ConditionExpression'] = "#CAS = :cas_val"; $requestArgs['ExpressionAttributeNames'] = ["#CAS" => $this->casField]; $requestArgs['ExpressionAttributeValues'] = [":cas_val" => ["N" => strval(intval($old_cas))]]; } else { $requestArgs['ConditionExpression'] = "attribute_not_exists(#CAS)"; $requestArgs['ExpressionAttributeNames'] = ["#CAS" => $this->casField]; } } } $item = DynamoDbItem::createFromArray($obj, $this->attributeTypes); $requestArgs['Item'] = $item->getData(); try { $this->dbClient->putItem($requestArgs); } catch (DynamoDbException $e) { if ($e->getAwsErrorCode() == "ConditionalCheckFailedException") { return false; } mtrace($e, "Exception while setting dynamo db item, aws code = " . $e->getAwsErrorCode() . ", type = " . $e->getAwsErrorType()); throw $e; } return true; }
/** * {@inheritDoc} * * @throws \Aws\DynamoDb\Exception\DynamoDBException */ public function update(EntityInterface $entity, array $commandOptions = array()) { $this->dispatchEntityRequestEvent(Events::ENTITY_PRE_UPDATE, $entity); $commandOptions += $this->formatPutItemCommandOptions($entity, true); $model = $this->dynamoDb->putItem($commandOptions); $this->dispatchEntityResponseEvent(Events::ENTITY_POST_UPDATE, $entity, $model); return true; }
/** * Creates a new item, or replaces an old item with a new item. * * @param string $tableName The name of the table to contain the item. * @param array $item Associative array of <AttributeName> keys mapping to (associative-array) values. * @param array $expected This is the conditional block for the PutItem operation. All the conditions must be met for the operation to succeed. * @param string $conditionnalOperator Operator between each condition of $expected argument. * @param string $returnValues Use ReturnValues if you want to get the item attributes as they appeared before they were updated. * @param string $returnConsumedCapacity Sets consumed capacity return mode. * @param string $returnItemCollectionMetrics If set to SIZE, statistics about item collections, if any, that were modified during the operation are returned in the response. * * @return Guzzle\Service\Resource\Model * * @see http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_putItem */ public function putItem($tableName, array $item, array $expected = null, $conditionnalOperator = self::COND_AND, $returnValues = self::RETURN_NONE, $returnConsumedCapacity = self::CAPACITY_NONE, $returnItemCollectionMetrics = self::METRICS_NONE) { $args = ['TableName' => $tableName, 'Item' => $item, 'ConditionnalOperator' => $conditionnalOperator, 'ReturnValues' => $returnValues, 'ReturnConsumedCapacity' => $returnConsumedCapacity, 'ReturnItemCollectionMetrics' => $returnItemCollectionMetrics]; if ($expected !== null) { $args['Expected'] = $expected; } return $this->client->putItem($args); }
/** * Updates the current session id with a newly generated one. * Please refer to {@link http://php.net/session_regenerate_id} for more details. * @param boolean $deleteOldSession Whether to delete the old associated session file or not. * @since 1.1.8 */ public function regenerateID($deleteOldSession = false) { $oldId = session_id(); parent::regenerateID(false); $newId = session_id(); $row = $this->getData($oldId); if (!is_null($row)) { if ($deleteOldSession) { // Delete + Put = Update $this->dynamoDb->deleteItem(array('TableName' => $this->tableName, 'Key' => array('id' => array('S' => (string) $oldId)))); $this->dynamoDb->putItem(array('TableName' => $this->tableName, 'Item' => array($this->idColumn => array('S' => (string) $newId), $this->dataColumn => $row[$this->dataColumn], $this->expireColumn => $row[$this->expireColumn]))); } else { $row[$this->idColumn] = array('S' => (string) $newId); $this->dynamoDb->putItem(array('TableName' => $this->tableName, 'Item' => array($row))); } } else { $this->dynamoDb->putItem(array('TableName' => $this->tableName, 'Item' => array($this->idColumn => array('S' => $newId), $this->expireColumn => array('N' => $this->getExpireTime())))); } }
/** * putItem * * @param array $values * @param array $options * @param array $expected * * @return \Guzzle\Service\Resource\Model * * @link http://docs.aws.amazon.com/aws-sdk-php/latest/class-Aws.DynamoDb.DynamoDbClient.html#_putItem */ public function putItem(array $values, array $options = array(), array $expected = array()) { $args = array('TableName' => $this->_table_name, 'Item' => $this->_formatAttributes($values), 'ReturnConsumedCapacity' => 'TOTAL', 'ReturnItemCollectionMetrics' => 'SIZE'); // Set Expected if exists if ($expected || isset($options['Exists'])) { $exists = isset($options['Exists']) ? $options['Exists'] : array(); $args['Expected'] = $this->_formatAttributeExpected($expected, $exists); } // Merge $options to $args $option_names = array('ReturnValues', 'ReturnConsumedCapacity', 'ReturnItemCollectionMetrics'); foreach ($option_names as $option_name) { if (isset($options[$option_name])) { $args[$option_name] = $options[$option_name]; } } $item = self::$_client->putItem($args); self::_logQuery('putItem', $args, $item); return $item; }
/** * @param string $key * @param mixed $value */ public function store($key, $value) { $this->dynamoDbClient->putItem(['TableName' => $this->tableName, 'Item' => [$this->attribute => [$this->getVariableType($key) => (string) $key], 'status' => [$this->getVariableType($value) => (string) $value]]]); }
/** * {@inheritDoc} */ public function insert($storageName, $key, array $data) { $this->createTable($storageName); $this->prepareData($key, $data); $result = $this->client->putItem(['TableName' => $storageName, 'Item' => $this->client->formatAttributes($data), 'ReturnConsumedCapacity' => 'TOTAL']); }
/** * {@inheritdoc} */ protected function write(array $record) { $filtered = $this->filterEmptyFields($record['formatted']); $formatted = $this->client->formatAttributes($filtered); $this->client->putItem(array('TableName' => $this->table, 'Item' => $formatted)); }