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

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

Introduction

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

Prototype

public SelectRequest(String selectExpression, Boolean consistentRead) 

Source Link

Document

Constructs a new SelectRequest object.

Usage

From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java

License:Apache License

/**
 * {@link SimpleDBDomain}????????//from  w  ww.  java 2  s  .  c  om
 * 
 * @param clazz
 *            {@link SimpleDBDomain}????POJO
 * @param expression
 *            where
 * @param consistentRead
 *            ??
 * 
 *            <a href=
 *            "http://docs.amazonwebservices.com/AmazonSimpleDB/latest/DeveloperGuide/ConsistencySummary.html"
 *            >AWS?</a>
 */
public <T> int count(Class<T> clazz, QueryExpression expression) {
    String whereExpression = null;
    if (expression != null) {
        whereExpression = expression.describe();
    }
    String query = createQuery(clazz, true, whereExpression, 0);
    SelectResult result = this.sdb.select(new SelectRequest(query, this.config.isConsistentRead()));
    String countValue = result.getItems().get(0).getAttributes().get(0).getValue();
    return Integer.parseInt(countValue);
}

From source file:com.dateofrock.simpledbmapper.SimpleDBMapper.java

License:Apache License

private <T> List<T> fetch(Class<T> clazz, String query) {
    SelectRequest selectRequest = new SelectRequest(query.toString(), this.config.isConsistentRead());
    if (this.selectNextToken != null) {
        selectRequest.setNextToken(this.selectNextToken);
        this.selectNextToken = null;
    }/*from ww  w  .  j a va  2  s  . co m*/

    SelectResult result = this.sdb.select(selectRequest);
    List<Item> items = result.getItems();
    if (items.isEmpty()) {
        return Collections.emptyList();
    }
    this.selectNextToken = result.getNextToken();

    List<T> objects = new ArrayList<T>();
    Field itemNameField = this.reflector.findItemNameField(clazz);
    try {
        // SDB?item?
        for (Item item : items) {
            T instance;
            instance = clazz.newInstance();

            // ItemName?
            Class<?> type = itemNameField.getType();
            String itemName = item.getName();
            itemNameField.set(instance, this.reflector.decodeItemNameFromSimpleDBFormat(type, itemName));

            // item?attributes?
            List<Attribute> attrs = item.getAttributes();
            for (Attribute attr : attrs) {
                String attributeName = attr.getName();
                Field attrField = this.reflector.findFieldByAttributeName(clazz, attributeName);
                if (attrField == null) {
                    continue;
                }
                // Blob???LazyFetch?
                SimpleDBBlob blobAnno = attrField.getAnnotation(SimpleDBBlob.class);
                if (blobAnno != null) {
                    String fieldName = attrField.getName();
                    if (this.blobEagerFetchList.contains(fieldName)) {
                        // 
                        this.reflector.setFieldValueFromAttribute(this.s3, clazz, instance, attr);
                    } else {
                        FetchType fetchType = blobAnno.fetch();
                        if (fetchType == FetchType.EAGER) {
                            // 
                            this.reflector.setFieldValueFromAttribute(this.s3, clazz, instance, attr);
                        }
                    }
                } else {
                    this.reflector.setFieldValueFromAttribute(this.s3, clazz, instance, attr);
                }

            }

            //
            objects.add(instance);
        }

    } catch (Exception e) {
        throw new SimpleDBMapperException(e);
    }

    return objects;
}

From source file:com.duboisproject.rushhour.database.SdbInterface.java

License:Open Source License

/**
 * Get the highest difficulty value in the levels table.
 * This copies all level difficulties into memory to sort.
 * It would be simpler and more performant to pad difficulties in the database and sort (lexicographically) there.
 *//* w  ww .  ja v a  2 s .  c  o  m*/
public int fetchMaxDifficulty() throws RequestException {
    // We'll cache this too.
    if (cachedMaxDifficulty == null) {
        String format = "select `%s` from `%s` limit 2500";
        String query = String.format(format, sdbEscape(LEVEL_DIFFICULTY, '`'), sdbEscape(LEVELS_DOMAIN, '`'));

        SelectRequest request = new SelectRequest(query, true);
        SelectResult result;
        try {
            result = client.select(request);
        } catch (AmazonClientException e) {
            throw new RequestException(REQUEST_FAILED_MESSAGE);
        }

        int max = Integer.MIN_VALUE;
        for (Item i : result.getItems()) {
            List<Attribute> attributes = i.getAttributes();
            if (!attributes.isEmpty()) {
                int current = Integer.parseInt(attributes.get(0).getValue());
                if (current > max) {
                    max = current;
                }
            }
        }
        cachedMaxDifficulty = max;
    }
    return cachedMaxDifficulty;
}

From source file:com.duboisproject.rushhour.database.SdbInterface.java

License:Open Source License

/**
 * Fetch the stats for a mathlete's most recent play.
 *
 * Uses at most one query, and sorts on the database side.
 *
 * @return stats for the last play, or <code>null</code> if no plays exist
 *///from   www  . ja v  a2 s  .  co  m
public GameStatistics fetchLastPlay(Mathlete mathlete) throws RequestException {
    String format = "select * from `%s` where `%s` = \"%s\" and `%s` is not null "
            + "order by `%s` desc limit 1";
    String query = String.format(format, sdbEscape(PLAYS_DOMAIN, '`'), sdbEscape(PLAYS_MATHLETE, '`'),
            sdbEscape(mathlete.id, '"'), sdbEscape(PLAYS_START, '`'), sdbEscape(PLAYS_START, '`'));

    SelectRequest request = new SelectRequest(query, true);
    SelectResult result;
    try {
        result = client.select(request);
    } catch (AmazonClientException e) {
        throw new RequestException(REQUEST_FAILED_MESSAGE);
    }

    List<Item> items = result.getItems();
    if (items.isEmpty()) {
        return null;
    } else {
        return parseStats(items.get(0));
    }
}

From source file:com.duboisproject.rushhour.database.SdbInterface.java

License:Open Source License

/**
 * Get the stats for all levels a mathlete has played.
 *//*from  w w w.  ja v a 2 s. c o m*/
public Set<GameStatistics> fetchAllPlays(Mathlete mathlete) throws RequestException {
    String format = "select * from `%s` where `%s` = \"%s\" limit 2500";
    String query = String.format(format, sdbEscape(PLAYS_DOMAIN, '`'), sdbEscape(PLAYS_MATHLETE, '`'),
            sdbEscape(mathlete.id, '"'));

    Set<GameStatistics> plays = new HashSet<GameStatistics>();

    SelectRequest request = new SelectRequest(query, true);
    String nextToken = null;
    do {
        request.setNextToken(nextToken);
        SelectResult result = client.select(request);
        for (Item item : result.getItems()) {
            plays.add(parseStats(item));
        }
        nextToken = result.getNextToken();
    } while (nextToken != null);

    return plays;
}

From source file:com.duboisproject.rushhour.database.SdbInterface.java

License:Open Source License

/**
 * Get the stats for all plays by a mathlete at a given difficulty.
 *
 * May be useful for determining which level in a certain difficulty should be played next.
 *///from  w  w w  . ja  v  a  2 s.  c o m
public Map<Integer, GameStatistics[]> fetchStatsAtDifficulty(Mathlete player, int difficulty)
        throws RequestException {
    Map<Integer, GameStatistics[]> levelStats = new HashMap<Integer, GameStatistics[]>();

    // Select all levels of the specified difficulty
    String format = "select `%s` from `%s` where `%s` = \"%s\"";
    String query = String.format(format, sdbEscape(PRIMARY_KEY, '`'), sdbEscape(LEVELS_DOMAIN, '`'),
            sdbEscape(LEVEL_DIFFICULTY, '`'), sdbEscape(Integer.toString(difficulty), '"'));

    SelectRequest request = new SelectRequest(query, true);
    SelectResult result;
    try {
        result = client.select(request);
    } catch (AmazonClientException e) {
        throw new RequestException(REQUEST_FAILED_MESSAGE);
    }

    // Get all stats for each such level
    format = "select * from `%s` where `%s` = \"%s\" and `%s` = \"%s\"";
    for (Item item : result.getItems()) {
        int id = Integer.parseInt(item.getName());
        query = String.format(format, sdbEscape(PLAYS_DOMAIN, '`'), sdbEscape(PLAYS_LEVEL, '`'),
                sdbEscape(Integer.toString(id), '"'), sdbEscape(PLAYS_MATHLETE, '`'),
                sdbEscape(player.id, '"'));

        SelectRequest playsRequest = new SelectRequest(query, true);
        SelectResult playsResult;
        try {
            playsResult = client.select(playsRequest);
        } catch (AmazonClientException e) {
            throw new RequestException(REQUEST_FAILED_MESSAGE);
        }

        List<Item> items = playsResult.getItems();
        GameStatistics[] stats = new GameStatistics[items.size()];
        for (int i = 0; i < items.size(); i += 1) {
            stats[i] = parseStats(items.get(i));
        }
        levelStats.put(id, stats);
    }
    return levelStats;
}

From source file:com.duboisproject.rushhour.database.SdbInterface.java

License:Open Source License

/**
 * Get the IDs for all levels at a certain difficulty.
 *///from   w  w  w.  j a va  2  s. c om
public int[] fetchLevelsAtDifficulty(int difficulty) throws RequestException {
    String format = "select `%s` from `%s` where `%s` = \"%s\" limit 2500";
    String query = String.format(format, sdbEscape(PRIMARY_KEY, '`'), sdbEscape(LEVELS_DOMAIN, '`'),
            sdbEscape(LEVEL_DIFFICULTY, '`'), sdbEscape(Integer.toString(difficulty), '"'));

    SelectRequest request = new SelectRequest(query, true);
    SelectResult result;
    try {
        result = client.select(request);
    } catch (AmazonClientException e) {
        throw new RequestException(REQUEST_FAILED_MESSAGE);
    }

    List<Item> items = result.getItems();
    int[] levelIds = new int[items.size()];
    for (int i = 0; i < items.size(); i += 1) {
        levelIds[i] = Integer.parseInt(items.get(i).getName());
    }
    return levelIds;
}

From source file:com.shelfmap.simplequery.expression.impl.BaseExpression.java

License:Apache License

@Override
public T getSingleResult(boolean consistent) throws SimpleQueryException, MultipleResultsExistException {
    createRemoteDomainIfNeed(getDomain());
    String expression = describe();
    SelectRequest selectReq = new SelectRequest(expression, consistent);
    SelectResult result = context.getSimpleDB().select(selectReq);
    List<Item> items = result.getItems();
    if (items.size() > 1)
        throw new MultipleResultsExistException(
                "more than 1 results returned by the expression: " + expression);
    if (items.isEmpty())
        return null;

    Item first = items.get(0);//ww  w.  j a  v a2 s . c o  m
    try {
        return getContext().getItemConverterFactory().create(getDomain()).convertToInstance(first);
    } catch (CanNotConvertItemException ex) {
        throw new SimpleQueryException("Can not convert an item", ex);
    }
}

From source file:com.shelfmap.simplequery.expression.impl.BaseExpression.java

License:Apache License

@Override
public QueryResults<T> getResults(boolean consistent) throws SimpleQueryException {
    createRemoteDomainIfNeed(getDomain());
    SelectRequest selectReq = new SelectRequest(describe(), consistent);
    SelectResult result = context.getSimpleDB().select(selectReq);
    return new DefaultQueryResult<T>(getContext(), getDomain(), this, result);
}

From source file:com.threepillar.labs.quartz.simpledb.SimpleDbJobStore.java

License:Apache License

/**
 * <p>/*w  w w. ja  va 2  s . c o m*/
 * Get a handle to the next trigger to be fired, and mark it as 'reserved'
 * by the calling scheduler.
 * </p>
 *
 * @see #releaseAcquiredTrigger(SchedulingContext, Trigger)
 */
@Override
public Trigger acquireNextTrigger(SchedulingContext ctxt, long noLaterThan) {
    log.info("Acquiring next trigger: " + query.acquireTrigger(dateFormat.format(new Date(noLaterThan))));
    SelectResult result = amazonSimpleDb.select(new SelectRequest(
            query.acquireTrigger(dateFormat.format(new Date(noLaterThan))), isConsistentPoll()));
    List<Item> items = result.getItems();

    if (items.size() == 1) {
        try {
            TriggerWrapper tw = triggerFromAttributes(items.get(0).getAttributes());
            logDebug("Acquired next Trigger: ", tw.trigger.getFullName());
            if (tw.trigger.getNextFireTime() != null) {
                tw.state = TriggerWrapper.STATE_ACQUIRED;
                updateState(tw);
                return tw.trigger;
            } else {
                removeTrigger(ctxt, tw.trigger.getName(), tw.trigger.getGroup());
            }
        } catch (IOException e) {
            log.error("Could not acquire trigger", e);
        }
    }
    return null;
}