@Override public void update(Entry entry, Entry existingEntry) { readWriteLock.writeLock().lock(); try { Map<String, AttributeValue> keys = createKey(entry); Map<String, AttributeValueUpdate> attributes = createAttributes(entry); Map<String, ExpectedAttributeValue> expected = expectExists(existingEntry); try { executeUpdate(keys, attributes, expected); } catch (ConditionalCheckFailedException e) { throw new DoesNotExistException("Precondition to update entry in DynamoDB failed:" + keys.toString()); } } finally { readWriteLock.writeLock().unlock(); } }
/** * Queries the DynamoDB GSI for unprocessed {@link Metadata} with a maximum of 96 items. * * Iterates over all of the different partitions, returning a maximum of three items from each. * * @return {@link List} of unprocessed {@link Metadata} */ public List<Metadata> getUnprocessedCpcPlusMetaData() { return IntStream.range(0, Constants.CPC_DYNAMO_PARTITIONS).mapToObj(partition -> { Map<String, AttributeValue> valueMap = new HashMap<>(); valueMap.put(":cpcValue", new AttributeValue().withS(Constants.CPC_DYNAMO_PARTITION_START + partition)); valueMap.put(":cpcProcessedValue", new AttributeValue().withS("false")); DynamoDBQueryExpression<Metadata> metadataQuery = new DynamoDBQueryExpression<Metadata>() .withIndexName("Cpc-CpcProcessed_CreateDate-index") .withKeyConditionExpression(Constants.DYNAMO_CPC_ATTRIBUTE + " = :cpcValue and begins_with(" + Constants.DYNAMO_CPC_PROCESSED_CREATE_DATE_ATTRIBUTE + ", :cpcProcessedValue)") .withExpressionAttributeValues(valueMap) .withConsistentRead(false) .withLimit(LIMIT); return mapper.queryPage(Metadata.class, metadataQuery).getResults().stream(); }).flatMap(Function.identity()).collect(Collectors.toList()); }
/** * Get ticket. * * @param ticketId the ticket id * @return the ticket */ public Ticket get(final String ticketId) { final TicketDefinition metadata = this.ticketCatalog.find(ticketId); if (metadata != null) { final Map<String, AttributeValue> keys = new HashMap<>(); keys.put(ColumnNames.ID.getName(), new AttributeValue(ticketId)); final GetItemRequest request = new GetItemRequest() .withKey(keys) .withTableName(metadata.getProperties().getStorageName()); LOGGER.debug("Submitting request [{}] to get ticket item [{}]", request, ticketId); final Map<String, AttributeValue> returnItem = amazonDynamoDBClient.getItem(request).getItem(); if (returnItem != null) { final Ticket ticket = deserializeTicket(returnItem); LOGGER.debug("Located ticket [{}]", ticket); return ticket; } } else { LOGGER.warn("No ticket definition could be found in the catalog to match [{}]", ticketId); } return null; }
private Mono<Map<String, AttributeValue>> putItemResultMono(String url) { PutItemRequest putItemRequest = new PutItemRequest(); putItemRequest.setTableName(Utils.table.websites); HashMap<String, AttributeValue> newWebsite = new HashMap<>(); newWebsite.put(Utils.params.url, new AttributeValue(url)); newWebsite.put(Utils.params.status, new AttributeValue(EStatus.NEW.name())); putItemRequest.setItem(newWebsite); return Mono.fromFuture( Utils.makeCompletableFuture( dynamoDBAsync.putItemAsync(putItemRequest))) .doOnError((throwable -> LOG.error(Utils.error.failed_dynamo_put, url))) .flatMap((created) -> this.sendMessage(url)) .map(((result) -> putItemRequest.getItem())); }
@Override public Map<String, AttributeValue> getNodes(WebsiteModel websiteModel) { try { ObjectMapper mapper = new ObjectMapper(); String string = mapper.writeValueAsString(websiteModel); Item item = new Item().withJSON(Utils.params.nodes, string); return InternalUtils.toAttributeValues(item); } catch (JsonProcessingException e) { LOG.error(e.getMessage()); } return new HashMap<>(); }
private Mono<Map<String, AttributeValue>> putItemResultMono( String seedUrl, EStatus status, String title, WebsiteModel websiteModel ) { PutItemRequest putItemRequest = new PutItemRequest(); putItemRequest.setTableName(Utils.table.websites); Map<String, AttributeValue> newWebsite = new HashMap<>(); if (Objects.nonNull(websiteModel)) newWebsite = crawlerBatchService.getNodes(websiteModel); newWebsite.put(Utils.params.url, new AttributeValue(seedUrl)); newWebsite.put(Utils.params.status, new AttributeValue(status.name())); if (StringUtils.isNotEmpty(title)) newWebsite.put(Utils.params.title, new AttributeValue(title)); putItemRequest.setItem(newWebsite); return Mono.fromFuture( Utils.makeCompletableFuture( dynamoDBAsync.putItemAsync(putItemRequest))) .doOnError((throwable -> LOG.error(Utils.error.failed_dynamo_put, seedUrl))) .doOnSuccess((a) -> LOG.info(Utils.success.saved_dynamo, String.format("%s [%s]", seedUrl, status))) .map(((result) -> putItemRequest.getItem())); }
@Override public Iterable<Take> expired() { return new Mapped<>( this.table() .frame() .through( new QueryValve() .withIndexName("expired") .withConsistentRead(false) .withSelect(Select.ALL_ATTRIBUTES) ) .where("success", Conditions.equalTo(Boolean.toString(false))) .where( "when", new Condition() .withComparisonOperator(ComparisonOperator.LT) .withAttributeValueList( new AttributeValue().withN( Long.toString(System.currentTimeMillis()) ) ) ), item -> new DyTake(item, this.delay) ); }
@Override public void create(Entry entry) { readWriteLock.writeLock().lock(); try { Map<String, AttributeValue> keys = createKey(entry); Map<String, AttributeValueUpdate> attributes = createAttributes(entry); Map<String, ExpectedAttributeValue> expected = expectNotExists(); try { executeUpdate(keys, attributes, expected); } catch (ConditionalCheckFailedException e) { throw new AlreadyExistsException("DynamoDB store entry already exists:" + keys.toString()); } } finally { readWriteLock.writeLock().unlock(); } }
private Map<String, AttributeValueUpdate> createAttributes(Entry entry) { Map<String, AttributeValueUpdate> attributes = new HashMap<>(); attributes.put(SCHEMA_VERSION_FIELD_NAME, new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withN(SCHEMA_VERSION))); attributes.put(OPTIMISTIC_LOCK_FIELD_NAME, new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(new AttributeValue().withS(sha(entry)))); for (Map.Entry<Integer, String> e : attributeMappings.entrySet()) { Object value = getValue(entry, e.getValue()); if (value != null) { attributes.put(e.getKey().toString(), new AttributeValueUpdate() .withAction(AttributeAction.PUT) .withValue(getAttribute(value))); } } return attributes; }
@Override public String ocket(final long time) throws IOException { final Iterator<Item> items = this.region.table("logs") .frame() .through(new QueryValve().withLimit(1)) .where("group", this.group()) .where( "start", new Condition() .withComparisonOperator(ComparisonOperator.EQ) .withAttributeValueList( new AttributeValue().withN(Long.toString(time)) ) ) .iterator(); if (!items.hasNext()) { throw new RsForward( new RsFlash("Can't find log"), "/scripts" ); } return items.next().get("ocket").getS(); }
@Test public void createsOnlyThreeOpenLogs() throws Exception { final User user = new DyUser(new Dynamo(), "yegor256"); final Script script = user.script("test5"); final AttributeValueUpdate upd = new AttributeValueUpdate().withValue( new AttributeValue().withN( Long.toString(System.currentTimeMillis()) ) ).withAction(AttributeAction.PUT); // @checkstyle MagicNumber (1 line) for (int idx = 0; idx < 3; ++idx) { final Item item = script.open().iterator().next(); item.put("finish", upd); } MatcherAssert.assertThat( script.open(), Matchers.emptyIterable() ); }
/** * The item to work with. * @return Item to work with * @throws Exception If some problem inside */ private static Item item() throws Exception { final Region region = new MkRegion( new H2Data().with( "domains", new String[] {"domain"}, "owner", "usage", "total" ) ); final Table table = region.table("domains"); table.put( new Attributes() .with("domain", "yegor256.com") .with("owner", new AttributeValue("yegor256")) .with("usage", new AttributeValue("<usage/>")) .with("total", new AttributeValue().withN("0")) ); return table.frame() .where("domain", "yegor256.com") .iterator().next(); }
@Test public void execute() { Map<String, AttributeValue> key = new HashMap<String, AttributeValue>(); key.put("1", new AttributeValue("Key_1")); Map<String, AttributeValue> unprocessedKey = new HashMap<String, AttributeValue>(); unprocessedKey.put("1", new AttributeValue("UNPROCESSED_KEY")); Map<String, KeysAndAttributes> keysAndAttributesMap = new HashMap<String, KeysAndAttributes>(); KeysAndAttributes keysAndAttributes = new KeysAndAttributes().withKeys(key); keysAndAttributesMap.put("DOMAIN1", keysAndAttributes); exchange.getIn().setHeader(DdbConstants.BATCH_ITEMS, keysAndAttributesMap); command.execute(); assertEquals(keysAndAttributesMap, ddbClient.batchGetItemRequest.getRequestItems()); List<Map<String, AttributeValue>> batchResponse = (List<Map<String, AttributeValue>>)exchange.getIn().getHeader(DdbConstants.BATCH_RESPONSE, Map.class).get("DOMAIN1"); AttributeValue value = batchResponse.get(0).get("attrName"); KeysAndAttributes unProcessedAttributes = (KeysAndAttributes)exchange.getIn().getHeader( DdbConstants.UNPROCESSED_KEYS, Map.class).get("DOMAIN1"); Map<String, AttributeValue> next = unProcessedAttributes.getKeys().iterator().next(); assertEquals(new AttributeValue("attrValue"), value); assertEquals(unprocessedKey, next); }
boolean createItemIfNotExists(String key, long currentTimeMillis, Context context) { LambdaLogger logger = context.getLogger(); AmazonDynamoDB client = createDynamoDBClient(cc); String functionName = context.getFunctionName(); try { // Create a record if it does not exist PutItemRequest req = new PutItemRequest().withTableName(TABLE_NAME) .addItemEntry(COL_FUNCTION_NAME, new AttributeValue(functionName)) .addItemEntry(COL_KEY, new AttributeValue(key)) .addItemEntry(COL_CREATED_TIME, new AttributeValue().withN(Long.toString(currentTimeMillis))) .addExpectedEntry(COL_FUNCTION_NAME, new ExpectedAttributeValue().withExists(false)) .addExpectedEntry(COL_KEY, new ExpectedAttributeValue().withExists(false)); client.putItem(req); return true; } catch (ConditionalCheckFailedException e) { logger.log("Record exsited. functionName[" + functionName + "] key[" + key + "]"); return false; } finally { client.shutdown(); } }
private static int getAttributeSizeBytes(AttributeValue att) throws UnsupportedEncodingException { int byteSize = 0; if (att.getN() != null) { byteSize += att.getN().getBytes(CHARACTER_ENCODING).length; } else if (att.getS() != null) { byteSize += att.getS().getBytes(CHARACTER_ENCODING).length; } else if (att.getB() != null) { byteSize += att.getB().array().length; } else if (att.getNS() != null) { for (String number : att.getNS()) { byteSize += number.getBytes(CHARACTER_ENCODING).length; } } else if (att.getSS() != null) { for (String string : att.getSS()) { byteSize += string.getBytes(CHARACTER_ENCODING).length; } } else if (att.getBS() != null) { for (ByteBuffer byteBuffer : att.getBS()) { byteSize += byteBuffer.array().length; } } return byteSize; }
@Override public boolean executeOperation() { final FoodDO itemToFind = new FoodDO(); itemToFind.setFoodId(getDemoPartitionValue()); final Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.LT.toString()) .withAttributeValueList(new AttributeValue().withS(DEMO_SORT_VALUE)); final DynamoDBQueryExpression<FoodDO> queryExpression = new DynamoDBQueryExpression<FoodDO>() .withHashKeyValues(itemToFind) .withRangeKeyCondition(DEMO_SORT_KEY, rangeKeyCondition) .withConsistentRead(false) .withLimit(RESULTS_PER_RESULT_GROUP); results = mapper.query(FoodDO.class, queryExpression); if (results != null) { resultsIterator = results.iterator(); if (resultsIterator.hasNext()) { return true; } } return false; }
@SuppressWarnings("unchecked") @Override public BatchGetItemResult batchGetItem(BatchGetItemRequest batchGetItemRequest) { this.batchGetItemRequest = batchGetItemRequest; Map<String, List<Map<String, AttributeValue>>> responseMap = new HashMap<String, List<Map<String, AttributeValue>>>(); List<Map<String, AttributeValue>> p = new ArrayList<Map<String, AttributeValue>>(); p.add(getAttributes()); responseMap.put("DOMAIN1", p); Map<String, AttributeValue> keysMap = new HashMap<String, AttributeValue>(); keysMap.put("1", new AttributeValue("UNPROCESSED_KEY")); Map<String, KeysAndAttributes> unprocessedKeys = new HashMap<String, KeysAndAttributes>(); unprocessedKeys.put("DOMAIN1", new KeysAndAttributes().withKeys(keysMap)); return new BatchGetItemResult() .withResponses(responseMap) .withUnprocessedKeys(unprocessedKeys); }
public boolean executeOperation() { final ListDO itemToFind = new ListDO(); itemToFind.setUserId(getDemoPartitionValue()); final Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.LT.toString()) .withAttributeValueList(new AttributeValue().withS(DEMO_SORT_VALUE)); // Use an expression names Map to avoid the potential for attribute names // colliding with DynamoDB reserved words. final Map <String, String> filterExpressionAttributeNames = new HashMap<>(); filterExpressionAttributeNames.put("#creationDate", DEMO_PRIMARY_CONDITION_KEY); final Map<String, AttributeValue> filterExpressionAttributeValues = new HashMap<>(); filterExpressionAttributeValues.put(":MincreationDate", new AttributeValue().withN(DEMO_PRIMARY_CONDITION_VALUE)); final DynamoDBQueryExpression<ListDO> queryExpression = new DynamoDBQueryExpression<ListDO>() .withHashKeyValues(itemToFind) .withRangeKeyCondition(DEMO_SORT_KEY, rangeKeyCondition) .withFilterExpression("#creationDate > :MincreationDate") .withExpressionAttributeNames(filterExpressionAttributeNames) .withExpressionAttributeValues(filterExpressionAttributeValues) .withConsistentRead(false) .withLimit(RESULTS_PER_RESULT_GROUP); results = mapper.query(ListDO.class, queryExpression); if (results != null) { resultsIterator = results.iterator(); if (resultsIterator.hasNext()) { return true; } } return false; }
@Override public boolean executeOperation() { // Use an expression names Map to avoid the potential for attribute names // colliding with DynamoDB reserved words. final Map <String, String> filterExpressionAttributeNames = new HashMap<>(); filterExpressionAttributeNames.put("#creationDate", DEMO_PRIMARY_CONDITION_KEY); final Map<String, AttributeValue> filterExpressionAttributeValues = new HashMap<>(); filterExpressionAttributeValues.put(":MincreationDate", new AttributeValue().withN(DEMO_PRIMARY_CONDITION_VALUE)); final DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression("#creationDate > :MincreationDate") .withExpressionAttributeNames(filterExpressionAttributeNames) .withExpressionAttributeValues(filterExpressionAttributeValues); results = mapper.scan(ListDO.class, scanExpression); if (results != null) { resultsIterator = results.iterator(); if (resultsIterator.hasNext()) { return true; } } return false; }
@Override public boolean executeOperation() { final CommentDO itemToFind = new CommentDO(); itemToFind.setUserId(getDemoPartitionValue()); final Condition rangeKeyCondition = new Condition() .withComparisonOperator(ComparisonOperator.LT.toString()) .withAttributeValueList(new AttributeValue().withS(DEMO_SORT_VALUE)); final DynamoDBQueryExpression<CommentDO> queryExpression = new DynamoDBQueryExpression<CommentDO>() .withHashKeyValues(itemToFind) .withRangeKeyCondition(DEMO_SORT_KEY, rangeKeyCondition) .withConsistentRead(false) .withLimit(RESULTS_PER_RESULT_GROUP); results = mapper.query(CommentDO.class, queryExpression); if (results != null) { resultsIterator = results.iterator(); if (resultsIterator.hasNext()) { return true; } } return false; }
private Collection<String> fetchChannelLanguages(final String channel) { final String id = "channel:" + channel + ":languages"; final GetItemRequest getItemRequest = new GetItemRequest() .withAttributesToGet(Collections.singletonList("value")) .withKey(Collections.singletonMap("id", new AttributeValue(id))) .withTableName(TableName); final GetItemResult getItemResult = ddb.getItem(getItemRequest); final Optional<String> maybeValue = Optional.ofNullable(getItemResult.getItem()) .map(i -> i.get("value")) .map(AttributeValue::getS); if (!maybeValue.isPresent()) return Collections.emptyList(); return Arrays.asList(maybeValue.get().trim().split(" +")); }
private void fetchTheValue() { final GetItemRequest req = new GetItemRequest().withAttributesToGet("value") .withTableName(TableName) .withKey(Collections.singletonMap("id", new AttributeValue(this.id))); try { final GetItemResult result = ddb.getItem(req); synchronized (this.monitor) { if (result.getItem() == null) { this.x = new RuntimeException("not found: id=" + this.id); } else { this.v = result.getItem().get("value").getS(); if (this.v == null) { this.x = new RuntimeException("found but no value for: id=" + this.id); } } } } catch (final RuntimeException x) { synchronized (this.monitor) { this.x = x; } } }
/** * Set value to <code>null</code> by <code>withNULL(Boolean.TRUE)</code> if the specified value is null, * or set it to <code>Boolean</code> by <code>setBOOL((Boolean) value)</code> if it's <code>Boolean</code>, * or set it to <code>ByteBuffer</code> by <code>setB((ByteBuffer) value)</code> if it's <code>ByteBuffer</code>, * otherwise, set it to String by <code>setS(N.stringOf(value))</code> for other types. * That's to say all the types except Number/Boolean/ByteBuffer are defined to String. * * @param value * @return */ public static AttributeValue attrValueOf(Object value) { final AttributeValue attrVal = new AttributeValue(); if (value == null) { attrVal.withNULL(Boolean.TRUE); } else { final Type<Object> type = N.typeOf(value.getClass()); if (type.isNumber()) { attrVal.setN(type.stringOf(value)); } else if (type.isBoolean()) { attrVal.setBOOL((Boolean) value); } else if (type.isByteBuffer()) { attrVal.setB((ByteBuffer) value); } else { attrVal.setS(type.stringOf(value)); } } return attrVal; }
public Account accountGetAccountByToken(String token) { Condition hashKeyCondition = new Condition(); hashKeyCondition.withComparisonOperator(ComparisonOperator.EQ).withAttributeValueList(new AttributeValue().withS(token)); Map<String, Condition> keyConditions = new HashMap<String, Condition>(); keyConditions.put("accountToken", hashKeyCondition); QueryRequest queryRequest = new QueryRequest(); queryRequest.withTableName("accountsv2"); queryRequest.withIndexName("accountToken-index"); queryRequest.withKeyConditions(keyConditions); QueryResult result = dynamoDB.query(queryRequest); for(Map<String, AttributeValue> item : result.getItems()) { Account mappedItem = mapper.marshallIntoObject(Account.class, item); // Only want the First one return mappedItem; } return null; }
private void insert(ValueSource valueSource, Schema schema, Object value, PutRequest put) { final AttributeValue attributeValue; try { attributeValue = schema == null ? AttributeValueConverter.toAttributeValueSchemaless(value) : AttributeValueConverter.toAttributeValue(schema, value); } catch (DataException e) { log.error("Failed to convert record with schema={} value={}", schema, value, e); throw e; } final String topAttributeName = valueSource.topAttributeName(config); if (!topAttributeName.isEmpty()) { put.addItemEntry(topAttributeName, attributeValue); } else if (attributeValue.getM() != null) { put.setItem(attributeValue.getM()); } else { throw new ConnectException("No top attribute name configured for " + valueSource + ", and it could not be converted to Map: " + attributeValue); } }
/** * * @param targetClass * @param queryRequest * @return * @see #find(Class, QueryRequest) */ public DataSet query(final Class<?> targetClass, final QueryRequest queryRequest) { if (targetClass == null || Map.class.isAssignableFrom(targetClass)) { final QueryResult queryResult = dynamoDB.query(queryRequest); final List<Map<String, AttributeValue>> items = queryResult.getItems(); if (N.notNullOrEmpty(queryResult.getLastEvaluatedKey()) && N.isNullOrEmpty(queryRequest.getExclusiveStartKey())) { final QueryRequest newQueryRequest = queryRequest.clone(); QueryResult newQueryResult = queryResult; while (N.notNullOrEmpty(newQueryResult.getLastEvaluatedKey())) { newQueryRequest.setExclusiveStartKey(newQueryResult.getLastEvaluatedKey()); newQueryResult = dynamoDB.query(newQueryRequest); items.addAll(newQueryResult.getItems()); } } return extractData(items, 0, items.size()); } else { return N.newDataSet(find(targetClass, queryRequest)); } }
@Override public Object getHiveData(AttributeValue data, String hiveType) { if (data == null) { return null; } List<ByteBuffer> byteBuffers = data.getBS(); if (byteBuffers == null || byteBuffers.isEmpty()) { return null; } List<byte[]> byteArrays = new ArrayList<byte[]>(byteBuffers.size()); for (ByteBuffer byteBuffer : byteBuffers) { byteArrays.add(Arrays.copyOf(byteBuffer.array(), byteBuffer.array().length)); } return byteArrays; }
public RetryResult<QueryResult> queryTable( String tableName, DynamoDBQueryFilter dynamoDBQueryFilter, Map<String, AttributeValue> exclusiveStartKey, long limit, Reporter reporter) { final QueryRequest queryRequest = new QueryRequest() .withTableName(tableName) .withExclusiveStartKey(exclusiveStartKey) .withKeyConditions(dynamoDBQueryFilter.getKeyConditions()) .withLimit(Ints.checkedCast(limit)) .withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL); RetryResult<QueryResult> retryResult = getRetryDriver().runWithRetry( new Callable<QueryResult>() { @Override public QueryResult call() { log.debug("Executing DynamoDB query: " + queryRequest); return dynamoDB.query(queryRequest); } }, reporter, PrintCounter.DynamoDBReadThrottle); return retryResult; }
/** * Perform a query against DynamoDB. The List returned is a * {@code com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList}, which loads List items on demand * (as List is iterated through). Be aware that List methods requiring full iteration (e.g. size()) will * load all list items immediately into memory. * * @param conditionExpression Key condition expression defining the query * @param expressionAV Map of DynamoDB Expression Attribute Values (used to populate conditionExpression) * @param indexName Index to run the query against * @param consistentRead Whether to require consistent read (only available for LocalSecondaryIndexes) * @return PaginatedQueryList of ResourceTriples * @see com.amazonaws.services.dynamodbv2.datamodeling.PaginatedQueryList */ private List<ResourceTriple> executeFindInIndex(final String conditionExpression, final Map<String, AttributeValue> expressionAV, final String indexName, final boolean consistentRead) { final DynamoDBQueryExpression<ResourceTriple> query = new DynamoDBQueryExpression<ResourceTriple>() .withIndexName(indexName) .withConsistentRead(consistentRead) .withKeyConditionExpression(conditionExpression) .withExpressionAttributeValues(expressionAV); return mapper.query(ResourceTriple.class, query); }
/** * Delete. * * @param ticketId the ticket id * @return the boolean */ public boolean delete(final String ticketId) { final TicketDefinition metadata = this.ticketCatalog.find(ticketId); if (metadata != null) { final DeleteItemRequest del = new DeleteItemRequest() .withTableName(metadata.getProperties().getStorageName()) .withKey(Collections.singletonMap(ColumnNames.ID.getName(), new AttributeValue(ticketId))); LOGGER.debug("Submitting delete request [{}] for ticket [{}]", del, ticketId); final DeleteItemResult res = amazonDynamoDBClient.deleteItem(del); LOGGER.debug("Delete request came back with result [{}]", res); return res != null; } return false; }
/** * Put ticket. * * @param ticket the ticket * @param encodedTicket the encoded ticket */ public void put(final Ticket ticket, final Ticket encodedTicket) { final TicketDefinition metadata = this.ticketCatalog.find(ticket); final Map<String, AttributeValue> values = buildTableAttributeValuesMapFromTicket(ticket, encodedTicket); LOGGER.debug("Adding ticket id [{}] with attribute values [{}]", encodedTicket.getId(), values); final PutItemRequest putItemRequest = new PutItemRequest(metadata.getProperties().getStorageName(), values); LOGGER.debug("Submitting put request [{}] for ticket id [{}]", putItemRequest, encodedTicket.getId()); final PutItemResult putItemResult = amazonDynamoDBClient.putItem(putItemRequest); LOGGER.debug("Ticket added with result [{}]", putItemResult); getAll(); }
/** * Build table attribute values from ticket map. * * @param ticket the ticket * @param encTicket the encoded ticket * @return the map */ public Map<String, AttributeValue> buildTableAttributeValuesMapFromTicket(final Ticket ticket, final Ticket encTicket) { final Map<String, AttributeValue> values = new HashMap<>(); values.put(ColumnNames.ID.getName(), new AttributeValue(encTicket.getId())); values.put(ColumnNames.PREFIX.getName(), new AttributeValue(encTicket.getPrefix())); values.put(ColumnNames.CREATION_TIME.getName(), new AttributeValue(ticket.getCreationTime().toString())); values.put(ColumnNames.COUNT_OF_USES.getName(), new AttributeValue().withN(Integer.toString(ticket.getCountOfUses()))); values.put(ColumnNames.TIME_TO_LIVE.getName(), new AttributeValue().withN(Long.toString(ticket.getExpirationPolicy().getTimeToLive()))); values.put(ColumnNames.TIME_TO_IDLE.getName(), new AttributeValue().withN(Long.toString(ticket.getExpirationPolicy().getTimeToIdle()))); values.put(ColumnNames.ENCODED.getName(), new AttributeValue().withB(ByteBuffer.wrap(SerializationUtils.serialize(encTicket)))); LOGGER.debug("Created attribute values [{}] based on provided ticket [{}]", values, encTicket.getId()); return values; }
/** * Delete boolean. * * @param service the service * @return the boolean */ public boolean delete(final RegisteredService service) { final DeleteItemRequest del = new DeleteItemRequest() .withTableName(TABLE_NAME) .withKey(Collections.singletonMap(ColumnNames.ID.getName(), new AttributeValue(String.valueOf(service.getId())))); LOGGER.debug("Submitting delete request [{}] for service [{}]", del, service); final DeleteItemResult res = amazonDynamoDBClient.deleteItem(del); LOGGER.debug("Delete request came back with result [{}]", res); return res != null; }
private RegisteredService getRegisteredServiceByKeys(final Map<String, AttributeValue> keys) { final GetItemRequest request = new GetItemRequest() .withKey(keys) .withTableName(TABLE_NAME); LOGGER.debug("Submitting request [{}] to get service with keys [{}]", request, keys); final Map<String, AttributeValue> returnItem = amazonDynamoDBClient.getItem(request).getItem(); if (returnItem != null) { final RegisteredService service = deserializeServiceFromBinaryBlob(returnItem); LOGGER.debug("Located service [{}]", service); return service; } return null; }
/** * Put. * * @param service the service */ public void put(final RegisteredService service) { final Map<String, AttributeValue> values = buildTableAttributeValuesMapFromService(service); final PutItemRequest putItemRequest = new PutItemRequest(TABLE_NAME, values); LOGGER.debug("Submitting put request [{}] for service id [{}]", putItemRequest, service.getServiceId()); final PutItemResult putItemResult = amazonDynamoDBClient.putItem(putItemRequest); LOGGER.debug("Service added with result [{}]", putItemResult); }
/** * Build table attribute values from map. * * @param service the service * @return the map */ public Map<String, AttributeValue> buildTableAttributeValuesMapFromService(final RegisteredService service) { final Map<String, AttributeValue> values = new HashMap<>(); values.put(ColumnNames.ID.getName(), new AttributeValue(String.valueOf(service.getId()))); values.put(ColumnNames.NAME.getName(), new AttributeValue(service.getName())); values.put(ColumnNames.DESCRIPTION.getName(), new AttributeValue(service.getDescription())); values.put(ColumnNames.SERVICE_ID.getName(), new AttributeValue(service.getServiceId())); final ByteArrayOutputStream out = new ByteArrayOutputStream(); jsonSerializer.to(out, service); values.put(ColumnNames.ENCODED.getName(), new AttributeValue().withB(ByteBuffer.wrap(out.toByteArray()))); LOGGER.debug("Created attribute values [{}] based on provided service [{}]", values, service); return values; }
protected <T> List<T> scan(Class<T> type, String name, String value) { String attrValue = ":v_attr"; String filterExpression = String.format("%s=%s", name, attrValue); Map<String, AttributeValue> expressionValueMap = new HashMap<>(); expressionValueMap.put(attrValue, new AttributeValue().withS(value)); return scan(type, filterExpression, expressionValueMap); }
@Override public List<User> getUserByName(String firstName, String lastName) { String attrValueFirstName = ":v_first_name"; String attrValueLastName = ":v_last_name"; String filterExpression = String.format("%s=%s and %s=%s", Attr.FirstName, attrValueFirstName, Attr.LastName, attrValueLastName); Map<String, AttributeValue> expressionValueMap = new HashMap<>(); expressionValueMap.put(attrValueFirstName, new AttributeValue().withS(firstName)); expressionValueMap.put(attrValueLastName, new AttributeValue().withS(lastName)); DynamoDBScanExpression scanExpression = new DynamoDBScanExpression() .withFilterExpression(filterExpression) .withExpressionAttributeValues(expressionValueMap); return dbMapper.scan(User.class, scanExpression); }
private Mono<GetItemResult> getItemResultMono(String url) { GetItemRequest getItemRequest = new GetItemRequest(); getItemRequest.setTableName(Utils.table.websites); HashMap<String, AttributeValue> key = new HashMap<>(); key.put(Utils.params.url, new AttributeValue(url)); getItemRequest.setKey(key); return Mono.fromFuture( Utils.makeCompletableFuture( dynamoDBAsync.getItemAsync(getItemRequest))) .doOnError((throwable -> LOG.error(Utils.error.failed_dynamo_get, url))); }