/** * Adds a quote and the userID of the user who favorited it to the Favorites table * * @param postID id for specific quote * @param userId facebook id of the user who favorited the quote */ public static void addToFavoriteTable(String postID, String userId) { ReplaceableAttribute favoritedPostID = new ReplaceableAttribute("postID", postID, Boolean.FALSE); ReplaceableAttribute accName = new ReplaceableAttribute("likedBy", userId, Boolean.FALSE); List<ReplaceableAttribute> attrs = new ArrayList<ReplaceableAttribute>(2); attrs.add(favoritedPostID); attrs.add(accName); PutAttributesRequest par = new PutAttributesRequest("Favorites", postID + "_likedBy_" + userId, attrs); try { getInstance().putAttributes(par); } catch (Exception exception) { System.out.println("EXCEPTION = " + exception); } }
/** * Adds a user and the user who will follow them to the Following table * * @param followedId the id of the person whose posts are going to be followed * @param followerId id of user who pressed the follow icon */ public static void addToFollowingTable(String followedId, String followerId) { ReplaceableAttribute followedIdAttr = new ReplaceableAttribute("followedId", followedId, Boolean.FALSE); ReplaceableAttribute followerIdAttr = new ReplaceableAttribute("followerId", followerId, Boolean.FALSE); List<ReplaceableAttribute> attrs = new ArrayList<ReplaceableAttribute>(2); attrs.add(followedIdAttr); attrs.add(followerIdAttr); PutAttributesRequest par = new PutAttributesRequest("Following", followedId + "_followedBy_" + followerId, attrs); try { getInstance().putAttributes(par); } catch (Exception exception) { System.out.println("EXCEPTION = " + exception); } }
@Test public void save() { CassandraInstance instance = createMock(CassandraInstance.class); Capture<PutAttributesRequest> requestCapture = new Capture<PutAttributesRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(instance.getId()).andReturn(ID).times(2); expect(instance.getDataCenter()).andReturn(DATACENTER); expect(instance.getRack()).andReturn(RACK); expect(instance.getHostName()).andReturn(HOSTNAME); expect(instance.getPublicIpAddress()).andReturn(PUBLIC_IP_ADDRESS); expect(instance.getFullyQualifiedDomainName()).andReturn(FULLY_QUALIFIED_DOMAIN_NAME); simpleDbClient.putAttributes(capture(requestCapture)); replayAll(); dao.save(RING_NAME, instance); PutAttributesRequest request = requestCapture.getValue(); assertEquals(DOMAIN, request.getDomainName()); assertEquals(String.valueOf(ID), request.getItemName()); assertReplaceableAttributes(request); }
private static void assertReplaceableAttributes(PutAttributesRequest request) { assertEquals(6, request.getAttributes().size()); for (ReplaceableAttribute attr : request.getAttributes()) { if (attr.getName().equals(SdbCassandraInstanceDao.ID_KEY)) { assertEquals(String.valueOf(ID), attr.getValue()); assertEquals(false, attr.getReplace()); } else if (attr.getName().equals(SdbCassandraInstanceDao.DATACENTER_KEY)) { assertEquals(DATACENTER, attr.getValue()); assertEquals(true, attr.getReplace()); } else if (attr.getName().equals(SdbCassandraInstanceDao.RACK_KEY)) { assertEquals(RACK, attr.getValue()); assertEquals(true, attr.getReplace()); } else if (attr.getName().equals(SdbCassandraInstanceDao.HOSTNAME_KEY)) { assertEquals(HOSTNAME, attr.getValue()); assertEquals(true, attr.getReplace()); } else if (attr.getName().endsWith(SdbCassandraInstanceDao.PUBLIC_IP_ADDRESS_KEY)) { assertEquals(PUBLIC_IP_ADDRESS, attr.getValue()); assertEquals(true, attr.getReplace()); } else if (attr.getName().endsWith(SdbCassandraInstanceDao.FULLY_QUALIFIED_DOMAIN_NAME_KEY)) { assertEquals(FULLY_QUALIFIED_DOMAIN_NAME, attr.getValue()); assertEquals(true, attr.getReplace()); } else { assertDuplicateAttribute(attr.getName(), attr.getValue()); } } }
/** * {@inheritDoc} */ @Override @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") public void putAll(final Map<? extends String, ? extends String> map) { final Collection<ReplaceableAttribute> attrs = new ArrayList<ReplaceableAttribute>(map.size()); for (final Map.Entry<?, ?> entry : map.entrySet()) { attrs.add( new ReplaceableAttribute() .withName(entry.getKey().toString()) .withValue(entry.getValue().toString()) .withReplace(true) ); } this.credentials.aws().putAttributes( new PutAttributesRequest() .withDomainName(this.table) .withItemName(this.label) .withAttributes(attrs) ); }
public void execute() { PutAttributesRequest request = new PutAttributesRequest() .withDomainName(determineDomainName()) .withItemName(determineItemName()) .withAttributes(determineReplaceableAttributes()) .withExpected(determineUpdateCondition()); log.trace("Sending request [{}] for exchange [{}]...", request, exchange); this.sdbClient.putAttributes(request); log.trace("Request sent"); }
public <T> T add(T record) throws WPBIOException { try { PutAttributesRequest putAttributesRequest = createAttributesRequests(record); sdbClient.putAttributes(putAttributesRequest); } catch (Exception e) { throw new WPBIOException("cannot create record", e); } return record; }
public <T> T addWithKey(T record) throws WPBIOException { try { PutAttributesRequest putAttributesRequest = createAttributesRequests(record); sdbClient.putAttributes(putAttributesRequest); } catch (Exception e) { throw new WPBIOException("cannot create record with key ", e); } return record; }
/** * Update attribute values * * @param domainName table name * @param itemName itemName for the item * @param attributes map of attribute name and value to replace */ public static void updateAttributesForItem(String domainName, String itemName, HashMap<String, String> attributes) { List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>(attributes.size()); for (String attributeName : attributes.keySet()) { replaceableAttributes.add(new ReplaceableAttribute().withName(attributeName).withValue(attributes.get(attributeName)).withReplace(true)); } getInstance().putAttributes(new PutAttributesRequest(domainName, itemName, replaceableAttributes)); }
public static void addPostIDToDomain(String postID, String tableName) { ReplaceableAttribute postIDAttribute = new ReplaceableAttribute("postID", postID, Boolean.FALSE); List<ReplaceableAttribute> attrs = new ArrayList<ReplaceableAttribute>(1); attrs.add(postIDAttribute); PutAttributesRequest par = new PutAttributesRequest(tableName, postID, attrs); try { getInstance().putAttributes(par); } catch (Exception exception) { System.out.println("EXCEPTION = " + exception); } }
@Override public <T> T createOrUpdateImpl(T domainItem, EntityWrapper<T, ?> entity) { Assert.notNull(entity.getDomain(), "Domain name should not be null"); logOperation("Create or update", entity); for (final Field field : ReflectionUtils.getFirstLevelOfReferenceAttributes(domainItem.getClass())) { final Object referenceEntity = ReflectionUtils.callGetter(domainItem, field.getName()); /* recursive call */ if (referenceEntity != null) { createOrUpdate(referenceEntity); } } if (entity.getItemName() != null) { delete(entity.getDomain(), entity.getItemName()); } entity.generateIdIfNotSet(); Map<String, List<String>> rawAttributes = entity.toMultiValueAttributes(); List<PutAttributesRequest> putAttributesRequests = SimpleDbRequestBuilder.createPutAttributesRequests( entity.getDomain(), entity.getItemName(), rawAttributes); for (PutAttributesRequest request : putAttributesRequests) { getDB().putAttributes(request); } return entity.getItem(); }
public static List<PutAttributesRequest> createPutAttributesRequests(String domain, String itemName, Map<String, List<String>> rawAttributes) { List<PutAttributesRequest> putAttributesRequests = new LinkedList<PutAttributesRequest>(); List<Map<String, List<String>>> attributeChunks = MapUtils.splitToChunksOfSize(rawAttributes, MAX_NUMBER_OF_ATTRIBUTES_PER_SIMPLE_DB_REQUEST); for(Map<String, List<String>> chunk : attributeChunks) { PutAttributesRequest request = createPutAttributesRequest(domain, itemName, chunk); putAttributesRequests.add(request); } return putAttributesRequests; }
private static PutAttributesRequest createPutAttributesRequest(String domain, String itemName, Map<String, List<String>> chunk) { final PutAttributesRequest putRequest = new PutAttributesRequest(); putRequest.setDomainName(domain); putRequest.setItemName(itemName); List<ReplaceableAttribute> simpleDbAttributes = toReplaceableAttributeList(chunk); putRequest.setAttributes(simpleDbAttributes); return putRequest; }
private void doTestPutUsesCorrectVersionWhenCallingTheDatabase(Optional<Integer> version) throws Exception { // ARRANGE initialiseOptimisticPersister(); // Configure attributes for database to return - the get is used for logging // only, so does not really matter. GetAttributesRequest simpleDBRequest = new GetAttributesRequest(testSimpleDBDomainName, testItemName); simpleDBRequest.setConsistentRead(true); GetAttributesResult getAttributesResult = new GetAttributesResult(); mockery.checking(new Expectations() { { allowing(mockSimpleDBClient).getAttributes(with(equal(simpleDBRequest))); will(returnValue(getAttributesResult)); } }); // Configure expectations for the put: UpdateCondition updateCondition = new UpdateCondition(); updateCondition.setName(versionAttributeName); ReplaceableAttribute versionAttribute = new ReplaceableAttribute(); versionAttribute.setName(versionAttributeName); versionAttribute.setReplace(true); if (!version.isPresent()) { // A version number attribute did not exist - so it still should not updateCondition.setExists(false); // Set initial value for our version number attribute versionAttribute.setValue("0"); } else { // A version number attribute exists - so it should be unchanged updateCondition.setValue(Integer.toString(version.get())); // Bump up our version number attribute versionAttribute.setValue(Integer.toString(version.get() + 1)); } List<ReplaceableAttribute> replaceableAttributes = new ArrayList<>(); replaceableAttributes.add(versionAttribute); // Add the new attribute ReplaceableAttribute testAttribute = new ReplaceableAttribute(); testAttribute.setName("Name"); testAttribute.setValue("Value"); replaceableAttributes.add(testAttribute); PutAttributesRequest simpleDBPutRequest = new PutAttributesRequest(testSimpleDBDomainName, testItemName, replaceableAttributes, updateCondition); mockery.checking(new Expectations() { { oneOf(mockSimpleDBClient).putAttributes(with(equal(simpleDBPutRequest))); } }); // ACT optimisticPersister.put(testItemName, version, testAttribute); }
@Override public PutAttributesResult putAttributes(PutAttributesRequest putAttributesRequest) throws AmazonServiceException, AmazonClientException { this.putAttributesRequest = putAttributesRequest; return new PutAttributesResult(); }
public static void createItem(String domainName, String itemName) { List<ReplaceableAttribute> attributes = new ArrayList<ReplaceableAttribute>(1); attributes.add(new ReplaceableAttribute().withName("Name").withValue("Value")); getInstance().putAttributes(new PutAttributesRequest(domainName, itemName, attributes)); }
public static void createAttributeForItem(String domainName, String itemName, String attributeName, String attributeValue) { List<ReplaceableAttribute> attributes = new ArrayList<ReplaceableAttribute>(1); attributes.add(new ReplaceableAttribute().withName(attributeName).withValue(attributeValue).withReplace(true)); getInstance().putAttributes(new PutAttributesRequest(domainName, itemName, attributes)); }
/** * Add a quote to the Quotes Table * * @param quote quote to be added to the table */ public static void addQuote(QuotePost quote) { ReplaceableAttribute quoteAttribute = new ReplaceableAttribute("quoteText", quote.getQuoteText(), Boolean.FALSE); ReplaceableAttribute authorAttribute = new ReplaceableAttribute("author", quote.getAuthorName(), Boolean.FALSE); ReplaceableAttribute timeAttribute = new ReplaceableAttribute("timestamp", "" + quote.getTimestamp(), Boolean.FALSE); ReplaceableAttribute fbNameAttribute = new ReplaceableAttribute("fbName", quote.getFbName(), Boolean.FALSE); ReplaceableAttribute fbIdAttribute = new ReplaceableAttribute("userId", quote.getUserId(), Boolean.FALSE); ReplaceableAttribute numFavorites = new ReplaceableAttribute("favorites", "0", Boolean.TRUE); Integer[] categories = quote.getCategories(); List<ReplaceableAttribute> attrs = new ArrayList<ReplaceableAttribute>(6 + categories.length); attrs.add(quoteAttribute); attrs.add(authorAttribute); attrs.add(timeAttribute); attrs.add(fbNameAttribute); attrs.add(fbIdAttribute); attrs.add(numFavorites); //add every category to the attribute - can have multiple values for (int i = 0; i < categories.length; i++) { switch (categories[i]) { case 0: attrs.add(new ReplaceableAttribute("category", "advice", Boolean.FALSE)); break; case 1: attrs.add(new ReplaceableAttribute("category", "funny", Boolean.FALSE)); break; case 2: attrs.add(new ReplaceableAttribute("category", "inspirational", Boolean.FALSE)); break; case 3: attrs.add(new ReplaceableAttribute("category", "love", Boolean.FALSE)); break; case 4: attrs.add(new ReplaceableAttribute("category", "movie", Boolean.FALSE)); break; case 5: attrs.add(new ReplaceableAttribute("category", "song", Boolean.FALSE)); break; default: throw new IllegalArgumentException("Too many categories."); } } PutAttributesRequest par = new PutAttributesRequest(QUOTES, quote.getFbName().replace(" ", "") + quote.getTimestamp(), attrs); try { getInstance().putAttributes(par); } catch (Exception exception) { System.out.println("EXCEPTION = " + exception); } }
@Override public void save(String ring, CassandraInstance instance) { PutAttributesRequest request = new PutAttributesRequest( domain(ring), String.valueOf(instance.getId()), buildSaveAttributes(instance)); client.putAttributes(request); }
@SuppressWarnings("unchecked") @Override protected <T, ID> void updateImpl(ID id, Class<T> entityClass, Map<String, ? extends Object> propertyMap) { // From the propertyMap, retrieve the Field which will be updated, // from the Field, serialize the corresponding Object value as per // FieldWrapper#serialize semantics, plug into the scheme to convert // to item and send a put request. String domainName = getDomainName(entityClass); Map<String, String> serializedValues = new LinkedHashMap<String, String>(); for (Map.Entry<String, ?> entry : propertyMap.entrySet()) { String propertyPath = entry.getKey(); Object propertyValue = entry.getValue(); if (propertyValue == null) { continue; } String serializedPropertyValue = null; Field propertyField = ReflectionUtils.getPropertyField(entityClass, propertyPath); if (FieldTypeIdentifier.isOfType(propertyField, FieldType.PRIMITIVE, FieldType.CORE_TYPE)) { serializedPropertyValue = SimpleDBAttributeConverter.encode(propertyValue); } else if (FieldTypeIdentifier.isOfType(propertyField, FieldType.NESTED_ENTITY)) { SimpleDbEntityInformation<T, Serializable> entityMetadata = (SimpleDbEntityInformation<T, Serializable>) SimpleDbEntityInformationSupport.getMetadata(propertyValue.getClass(), domainName); EntityWrapper<T, Serializable> entity = new EntityWrapper<T, Serializable>(entityMetadata, (T) propertyValue, true); Map<String, String> nestedAttributes = entity.serialize(); // add to serializedValues after prefixing propertyPath for (Map.Entry<String, String> e : nestedAttributes.entrySet()) { String key = String.format("%s.%s", propertyPath, e.getKey()); serializedValues.put(key, e.getValue()); } } else { serializedPropertyValue = JsonMarshaller.getInstance().marshall(propertyValue); } if (serializedPropertyValue != null) { serializedValues.put(propertyPath, serializedPropertyValue); } } Map<String, List<String>> rawAttributes = SimpleDbAttributeValueSplitter.splitAttributeValuesWithExceedingLengths(serializedValues); List<PutAttributesRequest> putAttributesRequests = SimpleDbRequestBuilder.createPutAttributesRequests( domainName, (String) id, rawAttributes); for (PutAttributesRequest request : putAttributesRequests) { getDB().putAttributes(request); } }