Example usage for com.amazonaws.services.simpledb.model SelectRequest setConsistentRead

List of usage examples for com.amazonaws.services.simpledb.model SelectRequest setConsistentRead

Introduction

In this page you can find the example usage for com.amazonaws.services.simpledb.model SelectRequest setConsistentRead.

Prototype


public void setConsistentRead(Boolean consistentRead) 

Source Link

Document

Determines whether or not strong consistency should be enforced when data is read from SimpleDB.

Usage

From source file:com.netflix.simianarmy.aws.conformity.SimpleDBConformityClusterTracker.java

License:Apache License

private List<Item> querySimpleDBItems(String query) {
    Validate.notNull(query);//from  w ww .  j a v a  2 s .com
    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;
}

From source file:com.netflix.simianarmy.aws.SimpleDBRecorder.java

License:Apache License

/**
 * Find events./*from w  w w .  j ava2 s . com*/
 *
 * @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;
}

From source file:org.teiid.resource.adapter.simpledb.SimpleDBConnectionImpl.java

License:Open Source License

/**
 * Performs select expression. This expression must be in format which is understandable to SimpleDB database
 * @param selectExpression//from www.j av a  2  s  .  com
 * @param columns
 * @return Iterator of List<String> results 
 */
@Override
public SelectResult performSelect(String selectExpression, String nextToken) throws TranslatorException {
    try {
        SelectRequest selectRequest = new SelectRequest(selectExpression);
        if (nextToken != null) {
            selectRequest.setNextToken(nextToken);
        }
        selectRequest.setConsistentRead(true);
        return client.select(selectRequest);
    } catch (AmazonServiceException e) {
        throw new TranslatorException(e);
    } catch (AmazonClientException e) {
        throw new TranslatorException(e);
    }
}

From source file:siena.aws.ddb.DynamoPersistenceManager.java

License:Apache License

public <T> int count(Query<T> query) {
    StringBuffer tableBuf = new StringBuffer();
    SelectRequest req = SdbMappingUtils.buildCountQuery(query, prefix, tableBuf);

    try {//from  ww  w. ja v  a  2s.com
        checkTable(tableBuf.toString());
        req.setConsistentRead(isReadConsistent());
        SelectResult res = client.select(req);
        return SdbMappingUtils.mapSelectResultToCount(res);
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}

From source file:siena.aws.ddb.DynamoPersistenceManager.java

License:Apache License

protected <T> void doFetchList(Query<T> query, int limit, int offset, List<T> resList, int depth) {
    if (depth >= MAX_DEPTH) {
        throw new SienaException(
                "Reached maximum depth of recursion when retrieving more data (" + MAX_DEPTH + ")");
    }//  w  ww  .j  a va  2 s.co  m

    preFetch(query, limit, offset, !resList.isEmpty());

    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType) query.option(QueryOptionFetchType.ID);
    QueryOptionOffset off = (QueryOptionOffset) query.option(QueryOptionOffset.ID);

    // if previousPage has detected there is no more data, simply returns an empty list
    if (sdbCtx.noMoreDataBefore || sdbCtx.noMoreDataAfter) {
        return;
    }

    // manages cursor limitations for IN and != operators with offsets
    if (!sdbCtx.isActive()) {
        StringBuffer tableBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, tableBuf);
        req.setConsistentRead(isReadConsistent());
        checkTable(tableBuf.toString());
        SelectResult res = client.select(req);

        // activates the SdbCtx now that it is really initialised
        sdbCtx.activate();

        postFetch(query, res);

        // cursor not yet created
        switch (fetchType.fetchType) {
        case KEYS_ONLY:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(),
                        off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
            }
            break;
        case NORMAL:
        default:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
            }
            // join management
            if (!query.getJoins().isEmpty()
                    || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                mapJoins(query, resList);
        }

        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    } else {
        // we prepare the query each time
        StringBuffer tableBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, tableBuf);
        req.setConsistentRead(isReadConsistent());
        checkTable(tableBuf.toString());
        // we can't use real asynchronous function with cursors
        // so the page is extracted at once and wrapped into a SienaFuture         
        String token = sdbCtx.currentToken();
        if (token != null) {
            req.setNextToken(token);
        }
        SelectResult res = client.select(req);

        postFetch(query, res);

        switch (fetchType.fetchType) {
        case KEYS_ONLY:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(),
                        off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
            }
            break;
        case NORMAL:
        default:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
            }
            // join management
            if (!query.getJoins().isEmpty()
                    || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                mapJoins(query, resList);
        }

        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    }
}

From source file:siena.aws.ddb.DynamoPersistenceManager.java

License:Apache License

protected <T> Iterable<T> doFetchIterable(Query<T> query, int limit, int offset, boolean recursing) {
    preFetch(query, limit, offset, recursing);

    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);

    // if previousPage has detected there is no more data, simply returns an empty list
    if (sdbCtx.noMoreDataBefore || sdbCtx.noMoreDataAfter) {
        return new ArrayList<T>();
    }//www  .j a va2  s. c  o  m

    // manages cursor limitations for IN and != operators with offsets
    if (!sdbCtx.isActive()) {
        StringBuffer tableBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, tableBuf);
        req.setConsistentRead(isReadConsistent());
        checkTable(tableBuf.toString());
        SelectResult res = client.select(req);

        // activates the SdbCtx now that it is initialised
        sdbCtx.activate();

        postFetch(query, res);

        return new SdbSienaIterable<T>(this, res.getItems(), query);
    } else {
        // we prepare the query each time
        StringBuffer tableBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, tableBuf);
        req.setConsistentRead(isReadConsistent());
        checkTable(tableBuf.toString());
        // we can't use real asynchronous function with cursors
        // so the page is extracted at once and wrapped into a SienaFuture
        String token = sdbCtx.currentToken();
        if (token != null) {
            req.setNextToken(token);
        }
        SelectResult res = client.select(req);

        postFetch(query, res);

        return new SdbSienaIterable<T>(this, res.getItems(), query);
    }
}

From source file:siena.sdb.SdbPersistenceManager.java

License:Apache License

protected <T> int rawGet(Iterable<T> models) {
    StringBuffer domainBuf = new StringBuffer();
    SelectRequest req = SdbMappingUtils.buildBatchGetQuery(models, prefix, domainBuf);
    req.setConsistentRead(isReadConsistent());
    try {//from   w  w  w .ja v a  2 s.c o  m
        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);
    }
}

From source file:siena.sdb.SdbPersistenceManager.java

License:Apache License

protected <T> List<T> rawGetByKeys(Class<T> clazz, Iterable<?> keys) {
    try {//from   ww w  .  j  av  a 2 s  . c  om
        StringBuffer domainBuf = new StringBuffer();

        SelectRequest req = SdbMappingUtils.buildBatchGetQueryByKeys(clazz, keys, prefix, domainBuf);
        checkDomain(domainBuf.toString());
        req.setConsistentRead(isReadConsistent());
        SelectResult res = sdb.select(req);
        List<T> models = new ArrayList<T>();

        SdbMappingUtils.mapSelectResultToListOrderedFromKeys(res, models, clazz, keys);

        // join management
        if (!ClassInfo.getClassInfo(clazz).joinFields.isEmpty()) {
            mapJoins(models);
        }

        return models;
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}

From source file:siena.sdb.SdbPersistenceManager.java

License:Apache License

public <T> int count(Query<T> query) {
    StringBuffer domainBuf = new StringBuffer();
    SelectRequest req = SdbMappingUtils.buildCountQuery(query, prefix, domainBuf);

    try {/*from  ww  w .j  a  v  a  2s  .  com*/
        checkDomain(domainBuf.toString());
        req.setConsistentRead(isReadConsistent());
        SelectResult res = sdb.select(req);
        return SdbMappingUtils.mapSelectResultToCount(res);
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
}

From source file:siena.sdb.SdbPersistenceManager.java

License:Apache License

protected <T> void doFetchList(Query<T> query, int limit, int offset, List<T> resList, int depth) {
    if (depth >= MAX_DEPTH) {
        throw new SienaException(
                "Reached maximum depth of recursion when retrieving more data (" + MAX_DEPTH + ")");
    }/*  www. ja  va2  s  .c  o  m*/

    preFetch(query, limit, offset, !resList.isEmpty());

    QueryOptionSdbContext sdbCtx = (QueryOptionSdbContext) query.option(QueryOptionSdbContext.ID);
    QueryOptionFetchType fetchType = (QueryOptionFetchType) query.option(QueryOptionFetchType.ID);
    QueryOptionOffset off = (QueryOptionOffset) query.option(QueryOptionOffset.ID);

    // if previousPage has detected there is no more data, simply returns an empty list
    if (sdbCtx.noMoreDataBefore || sdbCtx.noMoreDataAfter) {
        return;
    }

    // manages cursor limitations for IN and != operators with offsets
    if (!sdbCtx.isActive()) {
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        SelectResult res = sdb.select(req);

        // activates the SdbCtx now that it is really initialised
        sdbCtx.activate();

        postFetch(query, res);

        // cursor not yet created
        switch (fetchType.fetchType) {
        case KEYS_ONLY:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(),
                        off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
            }
            break;
        case NORMAL:
        default:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
            }
            // join management
            if (!query.getJoins().isEmpty()
                    || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                mapJoins(query, resList);
        }

        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    } else {
        // we prepare the query each time
        StringBuffer domainBuf = new StringBuffer();
        SelectRequest req = SdbMappingUtils.buildQuery(query, prefix, domainBuf);
        req.setConsistentRead(isReadConsistent());
        checkDomain(domainBuf.toString());
        // we can't use real asynchronous function with cursors
        // so the page is extracted at once and wrapped into a SienaFuture         
        String token = sdbCtx.currentToken();
        if (token != null) {
            req.setNextToken(token);
        }
        SelectResult res = sdb.select(req);

        postFetch(query, res);

        switch (fetchType.fetchType) {
        case KEYS_ONLY:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass(),
                        off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToListKeysOnly(res, resList, query.getQueriedClass());
            }
            break;
        case NORMAL:
        default:
            if (off.isActive()) {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass(), off.offset);
            } else {
                SdbMappingUtils.mapSelectResultToList(res, resList, query.getQueriedClass());
            }
            // join management
            if (!query.getJoins().isEmpty()
                    || !ClassInfo.getClassInfo(query.getQueriedClass()).joinFields.isEmpty())
                mapJoins(query, resList);
        }

        continueFetchNextToken(query, resList, depth);
        postMapping(query);
    }
}