@SuppressWarnings("unchecked") @Test public void execute() { exchange.getIn().setHeader(SdbConstants.NEXT_TOKEN, "TOKEN1"); exchange.getIn().setHeader(SdbConstants.SELECT_EXPRESSION, "SELECT NAME1 FROM DOMAIN1 WHERE NAME1 LIKE 'VALUE1'"); command.execute(); assertEquals(Boolean.TRUE, sdbClient.selectRequest.getConsistentRead()); assertEquals("TOKEN1", sdbClient.selectRequest.getNextToken()); assertEquals("SELECT NAME1 FROM DOMAIN1 WHERE NAME1 LIKE 'VALUE1'", sdbClient.selectRequest.getSelectExpression()); List<Item> items = exchange.getIn().getHeader(SdbConstants.ITEMS, List.class); assertEquals("TOKEN2", exchange.getIn().getHeader(SdbConstants.NEXT_TOKEN)); assertEquals(2, items.size()); assertEquals("ITEM1", items.get(0).getName()); assertEquals("ITEM2", items.get(1).getName()); }
@Override protected List<?> buildRow(Item item) throws TranslatorException { Map<String, List<String>> valueMap = createAttributeMap(item.getAttributes()); List row = new ArrayList(); Object[] results = new Object[valueMap.size()]; int i = 0; for (String attributeName:valueMap.keySet()) { if (SimpleDBMetadataProcessor.isItemName(attributeName)) { results[i++] = item.getName(); continue; } List<String> value = valueMap.get(attributeName); results[i++] = (value.size() == 1)?value.get(0):value.toString(); } row.add(results); return row; }
/** * Retrieve itemNames for all quotes that were not posted by the user * * @param myUserId user ID of the viewer so that their posts do not appear in their main feed */ public static List<String> getFeedItemNames(String myUserId) { SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where userId != '" + myUserId + "' and timestamp is not null order by timestamp desc").withConsistentRead(true); AmazonSimpleDBClient client = getInstance(); if (client != null) { List<Item> items = client.select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add((items.get(i)).getName()); } return itemNames; } else return new ArrayList<String>(); }
/** * Retrieve quotes posted by posters the user is following * * @param myUserId facebook user ID */ public static List<String> getFollowingFeedItemNames(String myUserId) { SelectRequest selectRequestNames = new SelectRequest("select followedId from Following where followerId = '" + myUserId + "'").withConsistentRead(true); List<Item> names = getInstance().select(selectRequestNames).getItems(); // Work-around for no nested queries in SimpleDB String followedSet = "("; for (int j = 0; j < names.size(); j++) { followedSet += "'" + names.get(j).getAttributes().get(0).getValue() + "',"; } followedSet = followedSet.substring(0, followedSet.length() - 1) + ")"; SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where userId in " + followedSet + " and timestamp is not null order by timestamp desc").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add((items.get(i)).getName()); } return itemNames; }
@Override public ImmutableSet<CassandraInstance> findAll(String ring) { List<CassandraInstance> instances = Lists.newArrayList(); String nextToken = null; do { SelectRequest request = new SelectRequest(String.format(ALL_QUERY, domain(ring))) .withNextToken(nextToken); SelectResult result = client.select(request); for (Item item : result.getItems()) { instances.add(transform(item)); } nextToken = result.getNextToken(); } while (nextToken != null); return ImmutableSet.copyOf(instances); }
@VisibleForTesting static CassandraInstance transform(Item item) { CassandraInstance.Builder instanceBuilder = new CassandraInstance.Builder(); for (Attribute attr : item.getAttributes()) { if (attr.getName().equals(ID_KEY)) { instanceBuilder.id(Integer.parseInt(attr.getValue())); } else if (attr.getName().equals(DATACENTER_KEY)) { instanceBuilder.dataCenter(attr.getValue()); } else if (attr.getName().equals(RACK_KEY)) { instanceBuilder.rack(attr.getValue()); } else if (attr.getName().equals(HOSTNAME_KEY)) { instanceBuilder.hostName(attr.getValue()); } else if (attr.getName().equals(PUBLIC_IP_ADDRESS_KEY)) { instanceBuilder.publicIpAddress(attr.getValue()); } else if (attr.getName().equals(FULLY_QUALIFIED_DOMAIN_NAME_KEY)) { instanceBuilder.fullyQualifiedDomainName(attr.getValue()); } } return instanceBuilder.build(); }
@Test public void findAll() { List<Item> items = createItems(); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andReturn(items); expect(result.getNextToken()).andReturn(null); replayAll(); Set<CassandraInstance> expected = transform(items); assertEquals(expected, dao.findAll(RING_NAME)); assertEquals(ALL_QUERY, requestCapture.getValue().getSelectExpression()); assertNull(requestCapture.getValue().getNextToken()); }
@Test public void findById() { List<Item> items = ImmutableList.of(createItem(1)); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andStubReturn(items); replayAll(); Set<CassandraInstance> expected = transform(items); assertEquals(expected.iterator().next(), dao.findById(RING_NAME, ID)); assertEquals(String.format(SdbCassandraInstanceDao.INSTANCE_QUERY, DOMAIN, ID), requestCapture.getValue().getSelectExpression()); assertNull(requestCapture.getValue().getNextToken()); }
private <T> long invokeCountImpl(boolean consistentRead, SimpleDbEntityInformation<T, ?> entityInformation, final String countQuery) { LOGGER.debug("Count items for query " + countQuery); validateSelectQuery(countQuery); final String escapedQuery = getEscapedQuery(countQuery, entityInformation); final SelectResult selectResult = invokeFindImpl(consistentRead, escapedQuery); for (Item item : selectResult.getItems()) { if (item.getName().equals("Domain")) { for (Attribute attribute : item.getAttributes()) { if (attribute.getName().equals("Count")) { return Long.parseLong(attribute.getValue()); } } } } return 0; }
@Test public void testGetAllItemsCorrectlyCallsSimpleDB() throws Exception { // ARRANGE initialiseOptimisticPersister(); SelectRequest selectRequest = new SelectRequest(); selectRequest.setConsistentRead(true); selectRequest.setSelectExpression("select * from `" + testSimpleDBDomainName + "`"); // Configure select result with an item to be returned: SelectResult selectResult = new SelectResult(); Set<Item> items = new HashSet<>(); Item item = new Item(); String itemDate = "2016-07-23"; item.setName(itemDate); item.setAttributes(allAttributes); items.add(item); selectResult.setItems(items); mockery.checking(new Expectations() { { oneOf(mockSimpleDBClient).select(with(equal(selectRequest))); will(returnValue(selectResult)); } }); List<ImmutablePair<String, List<Attribute>>> expectedItems = new ArrayList<>(); ImmutablePair<String, List<Attribute>> pair = new ImmutablePair<>(itemDate, new ArrayList<>( activeNonVersionAttributes)); expectedItems.add(pair); // ACT List<ImmutablePair<String, List<Attribute>>> actualItems = optimisticPersister.getAllItems(); // ASSERT assertTrue("OptimisticPersister should return the correct items. Actual: " + actualItems + ", Expected: " + expectedItems, actualItems.equals(expectedItems)); }
@Override public SelectResult select(SelectRequest selectRequest) throws AmazonServiceException, AmazonClientException { this.selectRequest = selectRequest; SelectResult result = new SelectResult(); result.setNextToken("TOKEN2"); result.getItems().add(new Item("ITEM1", null)); result.getItems().add(new Item("ITEM2", null)); return result; }
@Test public void execute() { List<Item> deletableItems = new ArrayList<Item>(); deletableItems.add(new Item()); exchange.getIn().setHeader(SdbConstants.DELETABLE_ITEMS, deletableItems); command.execute(); assertEquals("DOMAIN1", sdbClient.batchDeleteAttributesRequest.getDomainName()); assertEquals(deletableItems, sdbClient.batchDeleteAttributesRequest.getItems()); }
@Test public void determineDeletableItems() { assertNull(this.command.determineDeletableItems()); List<Item> deletableItems = new ArrayList<Item>(); deletableItems.add(new Item()); exchange.getIn().setHeader(SdbConstants.DELETABLE_ITEMS, deletableItems); assertEquals(deletableItems, this.command.determineDeletableItems()); }
private Iterator<Item> searchAmazonSimpleDB(boolean users,Filter filter,ArrayList<Attribute> attributes) { StringBuffer sqlWhere = new StringBuffer(); ArrayList<Object> vals = new ArrayList<Object>(); this.stringFilter(filter.getRoot(),sqlWhere, vals); StringBuffer SQL = new StringBuffer(); SQL.append("SELECT "); if (attributes.size() == 0) { SQL.append("* "); } else if (attributes.size() == 1 && attributes.get(0).equals("*")) { SQL.append("* "); } else if (attributes.size() == 1 && attributes.get(0).getAttribute().getName().equals("1.1")) { SQL.append("uid "); } else { for(Attribute attr : attributes) { SQL.append(attr.getAttribute().getName()).append(','); } SQL.setLength(SQL.length() - 1); } SQL.append(" FROM ").append('`'); if (users) { SQL.append(this.userDomain); } else { SQL.append(this.groupDomain); } SQL.append("` WHERE ").append(sqlWhere); if (logger.isDebugEnabled()) { logger.debug("SQL : " + SQL.toString()); } SelectResult res = this.sdb.select(new SelectRequest(SQL.toString())); return res.getItems().iterator(); }
public AmazonSimpleDBEntrySet(String dnBase,Iterator<Entry> baseEntries, Iterator<Item> userRes, Iterator<Item> groupRes,Filter filter) { this.done = false; this.isFirst = true; this.dnBase = dnBase; this.baseEntries = baseEntries; this.userRes = userRes; this.groupRes = groupRes; this.filter = filter; }
private Entry createEntry(Item item,boolean user) { StringBuffer dnBuff = new StringBuffer(); LDAPAttribute objClass = null; if (user) { dnBuff.append("uid=").append(item.getName()).append(",ou=users,").append(this.dnBase); objClass = new LDAPAttribute("objectClass",GlobalEntries.getGlobalEntries().getConfigManager().getCfg().getUserObjectClass()); } else { dnBuff.append("cn=").append(item.getName()).append(",ou=groups,").append(this.dnBase); objClass = new LDAPAttribute("objectClass",GlobalEntries.getGlobalEntries().getConfigManager().getCfg().getGroupObjectClass()); } LDAPAttributeSet attrs = new LDAPAttributeSet(); for (Attribute fromAmz : item.getAttributes()) { LDAPAttribute attr = attrs.getAttribute(fromAmz.getName()); if (attr == null) { attr = new LDAPAttribute(fromAmz.getName()); attrs.add(attr); } attr.addValue(fromAmz.getValue()); } attrs.add(objClass); return new Entry(new LDAPEntry(dnBuff.toString(),attrs)); }
private<T> List<T> internalQuery(Class<T> dataClass, SelectRequest selectRequest) throws WPBIOException { List<T> result = new ArrayList<T>(); try { SelectResult selectResult = null; do { if (selectResult != null) { selectRequest.setNextToken(selectResult.getNextToken()); } selectResult = sdbClient.select(selectRequest); List<Item> items = selectResult.getItems(); for(Item item: items) { T t = copyAttributesToInstance(dataClass, item.getAttributes()); result.add(t); } } while (selectResult.getNextToken() != null); } catch (Exception e) { throw new WPBIOException("cannot get all records " + dataClass.getSimpleName(), e); } return result; }
protected List<?> buildRow(Item item) throws TranslatorException { Map<String, List<String>> valueMap = createAttributeMap(item.getAttributes()); List row = new ArrayList(); for (int i = 0; i < visitor.getProjectedColumns().size(); i++) { String columnName = SimpleDBMetadataProcessor.getName(this.visitor.getProjectedColumns().get(i)); if (SimpleDBMetadataProcessor.isItemName(columnName)) { row.add(SimpleDBDataTypeManager.convertFromSimpleDBType(Arrays.asList(item.getName()), expectedColumnTypes[i])); continue; } row.add(SimpleDBDataTypeManager.convertFromSimpleDBType(valueMap.get(columnName), expectedColumnTypes[i])); } return row; }
private List<Item> mockResult() { List<Attribute> attributes = new ArrayList<Attribute>(); attributes.add(new Attribute("a1", "a1")); attributes.add(new Attribute("a2", "a2")); attributes.add(new Attribute("a2", "a22")); List<Item> items = new ArrayList<Item>(); items.add(new Item("one", attributes)); return items; }
public static String[] getItemNamesForDomain(String domainName) { SelectRequest selectRequest = new SelectRequest("select itemName() from `" + domainName + "`").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); String[] itemNames = new String[items.size()]; for (int i = 0; i < items.size(); i++) { itemNames[i] = items.get(i).getName(); } return itemNames; }
/** * Check whether the user has followed a specific user * * @param posterId id of user who posted the quote and is now being followed * @param userId id of the user who pressed the follow icon */ public static boolean isFollowedByUser(String posterId, String userId) { SelectRequest selectRequest = new SelectRequest("select itemName() from Following where followedId = '" + posterId + "' and followerId = '" + userId + "'").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); return items.size() > 0; }
/** * @param myUserId User ID to return all quotes posted by them * @return itemNames of all the Quotes created by the user */ public static List<String> getMyQuotesItemNames(String myUserId) { SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where userId = '" + myUserId + "' and timestamp is not null order by timestamp desc").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add(items.get(i).getName()); } return itemNames; }
/** * Retrieve the number of favorites a specific post has * * @param postId the post identifier to look up its number of favorites */ public static int favCount(String postId) { SelectRequest selectRequest = new SelectRequest("select favorites from Quotes where itemName() = '" + postId + "'") .withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); return Integer.parseInt(items.get(0).getAttributes().get(0).getValue()); }
/** * Check whether the user has favorited a specific post * * @param postId post identifier * @param userId user ID seeing the feed */ public static boolean isFavoritedByUser(String postId, String userId) { SelectRequest selectRequest = new SelectRequest("select itemName() from Favorites where postID = '" + postId + "' and likedBy = '" + userId + "'").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); return items.size() > 0; }
/** * Retrieve itemNames for all quotes that were not posted by the user in order of favorites */ public static List<String> getPopularFeedItemNames() { SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where favorites is not null " + "order by favorites desc").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add(((Item) items.get(i)).getName()); } return itemNames; }
/** * Get quotes by a specific user * * @param userId ID of the user whose quotes are being looked up */ public static List<String> getUserItemNames(String userId) { SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where userId = '" + userId + "' and timestamp is not null order by timestamp desc").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add(items.get(i).getName()); } return itemNames; }
/** * Get quotes favorited by a specific user * * @param userId ID of the user whose favorites are being looked up */ public static List<String> getFavoriteFeedItemNames(String userId) { SelectRequest selectRequest = new SelectRequest("select postID from Favorites where likedBy = '" + userId + "' and postID is not null order by postID asc").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add(items.get(i).getAttributes().get(0).getValue()); } return itemNames; }
/** * Get quotes by a specific category * * @param category name of the quote category */ public static List<String> getFeedItemNamesByCategory(String category) { SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where category = '" + category + "' and timestamp is not null order by timestamp desc").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add(items.get(i).getName()); } return itemNames; }
/** * Get quotes in which query term appears in the quote, author, or poster name * * @param query the term to search by */ public static List<String> getItemNamesBySearchQuery(String query) { SelectRequest selectRequest = new SelectRequest("select itemName() from Quotes where " + "(author like '%" + query + "%' or fbName like '%" + query + "%' or quoteText like '%" + query + "%') and " + "timestamp is not null order by timestamp desc limit 25").withConsistentRead(true); List<Item> items = getInstance().select(selectRequest).getItems(); List<String> itemNames = new ArrayList<String>(); for (int i = 0; i < items.size(); i++) { itemNames.add(items.get(i).getName()); } return itemNames; }
@Test public void findAll_paginated() { List<Item> items1 = createItems(0); List<Item> items2 = createItems(10); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(CaptureType.ALL); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)).times(2); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andReturn(items1); expect(result.getNextToken()).andReturn(NEXT_TOKEN); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andReturn(items2); expect(result.getNextToken()).andReturn(null); replayAll(); Set<CassandraInstance> expected = Sets.newHashSet( Iterables.concat(transform(items1), transform(items2))); assertEquals(expected, dao.findAll(RING_NAME)); List<SelectRequest> requests = requestCapture.getValues(); assertEquals(2, requests.size()); assertEquals(ALL_QUERY, requests.get(0).getSelectExpression()); assertNull(requests.get(0).getNextToken()); assertEquals(ALL_QUERY, requests.get(1).getSelectExpression()); assertEquals(NEXT_TOKEN, requests.get(1).getNextToken()); }
@Test public void findById_notFound() { List<Item> items = ImmutableList.of(); SelectResult result = createMock(SelectResult.class); Capture<SelectRequest> requestCapture = new Capture<SelectRequest>(); expect(domainFactory.createFromRing(RING_NAME)).andReturn(domain(RING_NAME)); expect(simpleDbClient.select(capture(requestCapture))).andReturn(result); expect(result.getItems()).andStubReturn(items); replayAll(); assertNull(dao.findById(RING_NAME, ID)); }
@Test public void transform() { Item item = createItem(1); replayAll(); CassandraInstance instance = SdbCassandraInstanceDao.transform(item); assertEquals(1, instance.getId()); assertEquals("dc1", instance.getDataCenter()); assertEquals("rack1", instance.getRack()); assertEquals("host1", instance.getHostName()); assertEquals("publicIpAddress1", instance.getPublicIpAddress()); }
private static Set<CassandraInstance> transform(List<Item> items) { Set<CassandraInstance> instances = Sets.newHashSet(); for (Item item : items) { instances.add(SdbCassandraInstanceDao.transform(item)); } return instances; }
public List<T> populateDomainItems(SimpleDbEntityInformation<T, ?> entityInformation, SelectResult selectResult) { final List<T> allItems = new ArrayList<T>(); for(Item item : selectResult.getItems()) { allItems.add(populateDomainItem(entityInformation, item)); } return allItems; }
private T buildDomainItem(SimpleDbEntityInformation<T, ?> entityInformation, Item item) { EntityWrapper<T, ?> entity = new EntityWrapper<T, Serializable>(entityInformation); entity.setId(item.getName()); final Map<String, String> attributes = convertSimpleDbAttributes(item.getAttributes()); entity.deserialize(attributes); return entity.getItem(); }
@Test public void populateDomainItem_should_convert_item_name() { Item sampleItem = new Item(SAMPLE_ITEM_NAME, new ArrayList<Attribute>()); SimpleDbEntityInformation<SimpleDbSampleEntity, String> entityInformation = SimpleDbSampleEntity .entityInformation(); domainItemBuilder = new DomainItemBuilder<SimpleDbSampleEntity>(); SimpleDbSampleEntity returnedDomainEntity = domainItemBuilder.populateDomainItem(entityInformation, sampleItem); assertEquals(SAMPLE_ITEM_NAME, returnedDomainEntity.getItemName()); }
@Test public void populateDomainItem_should_convert_attributes() { List<Attribute> attributeList = new ArrayList<Attribute>(); attributeList.add(new Attribute("booleanField", "" + SAMPLE_BOOLEAN_ATT_VALUE)); Item sampleItem = new Item(SAMPLE_ITEM_NAME, attributeList); SimpleDbEntityInformation<SimpleDbSampleEntity, String> entityInformation = SimpleDbSampleEntity .entityInformation(); domainItemBuilder = new DomainItemBuilder<SimpleDbSampleEntity>(); SimpleDbSampleEntity returnedDomainEntity = domainItemBuilder.populateDomainItem(entityInformation, sampleItem); assertTrue(returnedDomainEntity.getBooleanField() == SAMPLE_BOOLEAN_ATT_VALUE); }
private void domainToCSV(String domainPrefix, File csvFile) { log.info("Storing data from SDB domains starting with " + domainPrefix + " to file " + csvFile); Set<String> attributes = null; List<String> domains = getDbClient().listDomains().getDomainNames(); int c = 0; for (String domainName : domains) { if (domainName.startsWith(domainPrefix)) { c++; log.info("Exporting from " + domainName + " (" + c + "/" + domains.size() + ")"); long domainCount = getDbClient().domainMetadata( new DomainMetadataRequest(domainName)).getItemCount(); if (domainCount < MIN_RESULTS) { log.info("Ignoring " + domainName + ", less than " + MIN_RESULTS + " entries."); continue; } if (attributes == null) { attributes = getSdbAttributes(getDbClient(), domainName, MIN_RESULTS); } long total = 0; String select = "select * from `" + domainName + "` limit 2500"; String nextToken = null; SelectResult res; do { res = getDbClient().select( new SelectRequest(select).withNextToken(nextToken) .withConsistentRead(false)); for (Item i : res.getItems()) { Map<String, Object> csvEntry = new HashMap<String, Object>(); csvEntry.put("_key", i.getName()); for (String attr : attributes) { csvEntry.put(attr, ""); } for (Attribute a : i.getAttributes()) { csvEntry.put(a.getName(), a.getValue()); } CSVExport.writeToFile(csvEntry, csvFile); } nextToken = res.getNextToken(); total += res.getItems().size(); log.info("Exported " + total + " of " + domainCount); } while (nextToken != null); log.info("Finished exporting from " + domainName); } } CSVExport.closeWriter(csvFile); }
private List<Item> createItems() { return createItems(0); }
private List<Item> createItems(int offset) { return ImmutableList.of(createItem(offset + 1), createItem(offset + 2), createItem(offset + 3)); }