Search in sources :

Example 6 with SelectRequest

use of com.amazonaws.services.simpledb.model.SelectRequest in project siena by mandubian.

the class SdbPersistenceManager method rawGet.

protected <T> int rawGet(Iterable<T> models) {
    StringBuffer domainBuf = new StringBuffer();
    SelectRequest req = SdbMappingUtils.buildBatchGetQuery(models, prefix, domainBuf);
    req.setConsistentRead(isReadConsistent());
    try {
        checkDomain(domainBuf.toString());
        SelectResult res = sdb.select(req);
        int nb = SdbMappingUtils.mapSelectResult(res, models);
        // join management
        // gets class
        Class<?> clazz = null;
        for (T obj : models) {
            if (clazz == null) {
                clazz = obj.getClass();
                break;
            }
        }
        if (!ClassInfo.getClassInfo(clazz).joinFields.isEmpty()) {
            mapJoins(models);
        }
        return nb;
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) AmazonClientException(com.amazonaws.AmazonClientException) SienaException(siena.SienaException) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 7 with SelectRequest

use of com.amazonaws.services.simpledb.model.SelectRequest in project SimianArmy by Netflix.

the class SimpleDBRecorder method findEvents.

/**
 * Find events.
 *
 * @param queryMap
 *            the query map
 * @param after
 *            the start time to query for all events after
 * @return the list
 */
protected List<Event> findEvents(Map<String, String> queryMap, long after) {
    StringBuilder query = new StringBuilder(String.format("select * from `%s` where region = '%s'", domain, region));
    for (Map.Entry<String, String> pair : queryMap.entrySet()) {
        query.append(String.format(" and %s = '%s'", pair.getKey(), pair.getValue()));
    }
    query.append(String.format(" and eventTime > '%d'", after));
    // always return with most recent record first
    query.append(" order by eventTime desc");
    List<Event> list = new LinkedList<Event>();
    SelectRequest request = new SelectRequest(query.toString());
    request.setConsistentRead(Boolean.TRUE);
    SelectResult result = new SelectResult();
    do {
        result = sdbClient().select(request.withNextToken(result.getNextToken()));
        for (Item item : result.getItems()) {
            Map<String, String> fields = new HashMap<String, String>();
            Map<String, String> res = new HashMap<String, String>();
            for (Attribute attr : item.getAttributes()) {
                if (Keys.KEYSET.contains(attr.getName())) {
                    res.put(attr.getName(), attr.getValue());
                } else {
                    fields.put(attr.getName(), attr.getValue());
                }
            }
            String eid = res.get(Keys.id.name());
            String ereg = res.get(Keys.region.name());
            MonkeyType monkeyType = valueToEnum(MonkeyType.class, res.get(Keys.monkeyType.name()));
            EventType eventType = valueToEnum(EventType.class, res.get(Keys.eventType.name()));
            long eventTime = Long.parseLong(res.get(Keys.eventTime.name()));
            list.add(new BasicRecorderEvent(monkeyType, eventType, ereg, eid, eventTime).addFields(fields));
        }
    } while (result.getNextToken() != null);
    return list;
}
Also used : MonkeyType(com.netflix.simianarmy.MonkeyType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(com.amazonaws.services.simpledb.model.Attribute) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute) EventType(com.netflix.simianarmy.EventType) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) LinkedList(java.util.LinkedList) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 8 with SelectRequest

use of com.amazonaws.services.simpledb.model.SelectRequest in project SimianArmy by Netflix.

the class TestSimpleDBJanitorResourceTracker method testGetResources.

@Test
public void testGetResources() {
    String id1 = "id-1";
    String id2 = "id-2";
    AWSResourceType resourceType = AWSResourceType.INSTANCE;
    Resource.CleanupState state = Resource.CleanupState.MARKED;
    String description = "This is a test resource.";
    String ownerEmail = "owner@test.com";
    String region = "us-east-1";
    String terminationReason = "This is a test termination reason.";
    DateTime now = DateTime.now();
    Date expectedTerminationTime = new Date(now.plusDays(10).getMillis());
    Date markTime = new Date(now.getMillis());
    String fieldName = "fieldName123";
    String fieldValue = "fieldValue456";
    SelectResult result1 = mkSelectResult(id1, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, false, fieldName, fieldValue);
    result1.setNextToken("nextToken");
    SelectResult result2 = mkSelectResult(id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, true, fieldName, fieldValue);
    ArgumentCaptor<SelectRequest> arg = ArgumentCaptor.forClass(SelectRequest.class);
    TestSimpleDBJanitorResourceTracker tracker = new TestSimpleDBJanitorResourceTracker();
    when(tracker.sdbMock.select(any(SelectRequest.class))).thenReturn(result1).thenReturn(result2);
    verifyResources(tracker.getResources(resourceType, state, region), id1, id2, resourceType, state, description, ownerEmail, region, terminationReason, expectedTerminationTime, markTime, fieldName, fieldValue);
    verify(tracker.sdbMock, times(2)).select(arg.capture());
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) AWSResourceType(com.netflix.simianarmy.aws.AWSResourceType) AWSResource(com.netflix.simianarmy.aws.AWSResource) Resource(com.netflix.simianarmy.Resource) DateTime(org.joda.time.DateTime) Date(java.util.Date) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest) Test(org.testng.annotations.Test)

Example 9 with SelectRequest

use of com.amazonaws.services.simpledb.model.SelectRequest in project SimianArmy by Netflix.

the class TestSimpleDBRecorder method testFindEvent.

@Test
public void testFindEvent() {
    SelectResult result1 = mkSelectResult("testId1");
    result1.setNextToken("nextToken");
    SelectResult result2 = mkSelectResult("testId2");
    ArgumentCaptor<SelectRequest> arg = ArgumentCaptor.forClass(SelectRequest.class);
    when(sdbMock.select(any(SelectRequest.class))).thenReturn(result1).thenReturn(result2);
    Map<String, String> query = new LinkedHashMap<String, String>();
    query.put("instanceId", "testId1");
    verifyEvents(findEvents(query, new Date(0)));
    verify(sdbMock, times(2)).select(arg.capture());
    SelectRequest req = arg.getValue();
    StringBuilder sb = new StringBuilder();
    sb.append("select * from `DOMAIN` where region = 'region'");
    sb.append(" and instanceId = 'testId1'");
    Assert.assertEquals(req.getSelectExpression(), sb.toString() + " and eventTime > '0' order by eventTime desc");
    // reset for next test
    when(sdbMock.select(any(SelectRequest.class))).thenReturn(result1).thenReturn(result2);
    verifyEvents(findEvents(Type.MONKEY, query, new Date(0)));
    verify(sdbMock, times(4)).select(arg.capture());
    req = arg.getValue();
    sb.append(" and monkeyType = 'MONKEY|com.netflix.simianarmy.aws.TestSimpleDBRecorder$Type'");
    Assert.assertEquals(req.getSelectExpression(), sb.toString() + " and eventTime > '0' order by eventTime desc");
    // reset for next test
    when(sdbMock.select(any(SelectRequest.class))).thenReturn(result1).thenReturn(result2);
    verifyEvents(findEvents(Type.MONKEY, EventTypes.EVENT, query, new Date(0)));
    verify(sdbMock, times(6)).select(arg.capture());
    req = arg.getValue();
    sb.append(" and eventType = 'EVENT|com.netflix.simianarmy.aws.TestSimpleDBRecorder$EventTypes'");
    Assert.assertEquals(req.getSelectExpression(), sb.toString() + " and eventTime > '0' order by eventTime desc");
    // reset for next test
    when(sdbMock.select(any(SelectRequest.class))).thenReturn(result1).thenReturn(result2);
    verifyEvents(findEvents(Type.MONKEY, EventTypes.EVENT, query, new Date(1330538400000L)));
    verify(sdbMock, times(8)).select(arg.capture());
    req = arg.getValue();
    sb.append(" and eventTime > '1330538400000' order by eventTime desc");
    Assert.assertEquals(req.getSelectExpression(), sb.toString());
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.testng.annotations.Test)

Example 10 with SelectRequest

use of com.amazonaws.services.simpledb.model.SelectRequest in project SimianArmy by Netflix.

the class SimpleDBConformityClusterTracker method querySimpleDBItems.

private List<Item> querySimpleDBItems(String query) {
    Validate.notNull(query);
    String nextToken = null;
    List<Item> items = new ArrayList<Item>();
    do {
        SelectRequest request = new SelectRequest(query);
        request.setNextToken(nextToken);
        request.setConsistentRead(Boolean.TRUE);
        SelectResult result = this.simpleDBClient.select(request);
        items.addAll(result.getItems());
        nextToken = result.getNextToken();
    } while (nextToken != null);
    return items;
}
Also used : Item(com.amazonaws.services.simpledb.model.Item) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) ArrayList(java.util.ArrayList) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Aggregations

SelectRequest (com.amazonaws.services.simpledb.model.SelectRequest)13 SelectResult (com.amazonaws.services.simpledb.model.SelectResult)12 ArrayList (java.util.ArrayList)4 SienaException (siena.SienaException)4 AmazonClientException (com.amazonaws.AmazonClientException)3 Item (com.amazonaws.services.simpledb.model.Item)3 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.testng.annotations.Test)2 QueryOptionFetchType (siena.core.options.QueryOptionFetchType)2 Attribute (com.amazonaws.services.simpledb.model.Attribute)1 ReplaceableAttribute (com.amazonaws.services.simpledb.model.ReplaceableAttribute)1 EventType (com.netflix.simianarmy.EventType)1 MonkeyType (com.netflix.simianarmy.MonkeyType)1 Resource (com.netflix.simianarmy.Resource)1 AWSResource (com.netflix.simianarmy.aws.AWSResource)1 AWSResourceType (com.netflix.simianarmy.aws.AWSResourceType)1 BasicRecorderEvent (com.netflix.simianarmy.basic.BasicRecorderEvent)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1