@Override public Void create(Group group) { Item item = preparePutItem(group); PutItemSpec putItemSpec = new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#ns_key)") .withNameMap(new NameMap().with("#ns_key", HASH_KEY)); Table table = dynamoDB.getTable(groupTableName); final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> { try { return table.putItem(putItemSpec); } catch (ConditionalCheckFailedException e) { throwConflictAlreadyExists(group); return null; } }; return putItem(group, putItemOutcomeSupplier); }
private Void putItem(Group group, Supplier<PutItemOutcome> putItemOutcomeSupplier) { DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("save", putItemOutcomeSupplier, () -> { throw new RuntimeException("save"); }, dynamodbGroupWriteHystrix, metrics); PutItemOutcome outcome = cmd.execute(); logger.info("{} /dynamodb_put_item_result=[{}]", kvp("op", "save", "group", group.getId(), HASH_KEY, group.getKey(), "result", "ok"), outcome.getPutItemResult().toString()); return null; }
@Override public Void saveRelation(Group group, String relationHashKey, String relationRangeKey) { Item item = new Item() .withString(GroupStorage.SUBJECT_KEY, relationHashKey) .withString(GroupStorage.OBJECT_RELATION_KEY, relationRangeKey); Table table = dynamoDB.getTable(groupGraphTableName); DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("saveRelation", () -> table.putItem(item), () -> { throw new RuntimeException("saveRelation"); }, dynamodbGraphWriteHystrix, metrics); PutItemOutcome outcome = cmd.execute(); logger.info("{} /dynamodb_put_item_result=[{}]", kvp("op", "saveRelation", "appkey", group.getKey(), "hash_key", relationHashKey, "range_key", relationRangeKey, "result", "ok"), outcome.getPutItemResult().toString()); return null; }
@Override public Void createFeature(Feature feature) { final String key = feature.getKey(); final String group = feature.getGroup(); final Item item = preparePutItem(feature); final PutItemSpec putItemSpec = new PutItemSpec() .withItem(item) .withConditionExpression("attribute_not_exists(#featurekey)") .withNameMap(new NameMap().with("#featurekey", RANGE_KEY)); final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> { try { return dynamoDB.getTable(featureTableName).putItem(putItemSpec); } catch (ConditionalCheckFailedException e) { logger.error("err=conflict_feature_already_exists feature_key={} {}", feature.getKey(), e.getMessage()); throwConflictAlreadyExists(feature); return null; } }; final DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("createFeature", putItemOutcomeSupplier, () -> { throw new RuntimeException("createFeature"); }, hystrixWriteConfiguration, metrics); final PutItemOutcome outcome = cmd.execute(); logger.info("{} /dynamodb_put_item_result=[{}]", kvp("op", "createFeature", HASH_KEY, group, RANGE_KEY, key, "result", "ok"), outcome.getPutItemResult().toString()); return null; }
@Override public Void save(Group group) { Item item = preparePutItem(group); Table table = dynamoDB.getTable(groupTableName); final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> table.putItem(item); return putItem(group, putItemOutcomeSupplier); }
@Override public Void updateFeature(Feature feature, FeatureVersion previousVersion) { logger.info("{}", kvp("op", "updateFeature", HASH_KEY, feature.getGroup(), RANGE_KEY, feature.getKey())); final String key = feature.getKey(); final String group = feature.getGroup(); final Item item = preparePutItem(feature); final PutItemSpec putItemSpec = new PutItemSpec() .withItem(item) .withExpected( new Expected("version_timestamp").eq(previousVersion.getTimestamp()), new Expected("version_counter").eq(previousVersion.getCounter()) ); final Supplier<PutItemOutcome> putItemOutcomeSupplier = () -> { try { return dynamoDB.getTable(featureTableName).putItem(putItemSpec); } catch (ConditionalCheckFailedException e) { logger.error("err=conflict_feature_version_mismatch feature_key={} {}", feature.getKey(), e.getMessage()); throwConflictVersionMismatch(feature); return null; } }; final DynamoDbCommand<PutItemOutcome> cmd = new DynamoDbCommand<>("updateFeature", putItemOutcomeSupplier, () -> { throw new RuntimeException("updateFeature"); }, hystrixWriteConfiguration, metrics); final PutItemOutcome outcome = cmd.execute(); logger.info("{} /dynamodb_update_item_result=[{}]", kvp("op", "updateFeature", HASH_KEY, group, RANGE_KEY, key, "result", "ok"), outcome.getPutItemResult().toString()); return null; }