Search in sources :

Example 11 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project simplejpa by appoxy.

the class EntityManagerSimpleJPA method renameSubclass.

public void renameSubclass(String oldClassName, Class newClass) {
    logger.info("Renaming DTYPE for " + oldClassName + " to " + newClass.getSimpleName());
    try {
        String newClassName = newClass.getSimpleName();
        String domainName = factory.getDomainName(newClass);
        SelectResult result;
        List<Item> items;
        int i = 0;
        String nextToken = null;
        while (i == 0 || nextToken != null) {
            result = executeQueryForRenameSubclass(oldClassName, newClass, domainName, nextToken);
            items = result.getItems();
            putNewValue(domainName, items, EntityManagerFactoryImpl.DTYPE, newClassName);
            nextToken = result.getNextToken();
            i++;
            if (i % 100 == 0) {
                System.out.println("Renamed " + i + " subclassed objects so far...");
            }
        }
    } catch (AmazonClientException e) {
        e.printStackTrace();
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) AmazonClientException(com.amazonaws.AmazonClientException)

Example 12 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project simplejpa by appoxy.

the class AbstractQuery method getResultList.

public List getResultList() {
    // convert to amazon query
    AmazonQueryString amazonQuery;
    try {
        amazonQuery = createAmazonQuery();
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (amazonQuery == null) {
        return new ArrayList();
    }
    try {
        // String qToSend = amazonQuery != null ? amazonQuery.toString() : null;
        em.incrementQueryCount();
        if (amazonQuery.isCount()) {
            // String domainName = em.getDomainName(tClass);
            String nextToken = null;
            SelectResult qr;
            long count = 0;
            while ((qr = DomainHelper.selectItems(this.em.getSimpleDb(), amazonQuery.getValue(), nextToken)) != null) {
                Map<String, List<Attribute>> itemMap = new HashMap<String, List<Attribute>>();
                for (Item item : qr.getItems()) {
                    itemMap.put(item.getName(), item.getAttributes());
                }
                for (String id : itemMap.keySet()) {
                    List<Attribute> list = itemMap.get(id);
                    for (Attribute itemAttribute : list) {
                        if (itemAttribute.getName().equals("Count")) {
                            count += Long.parseLong(itemAttribute.getValue());
                        }
                    }
                }
                nextToken = qr.getNextToken();
                if (nextToken == null) {
                    break;
                }
            }
            return Arrays.asList(count);
        } else {
            LazyList ret = new LazyList(em, tClass, this);
            return ret;
        }
    } catch (NoSuchDomainException e) {
        // no need to throw here
        return new ArrayList();
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
}
Also used : LazyList(com.spaceprogram.simplejpa.LazyList) Attribute(com.amazonaws.services.simpledb.model.Attribute) NoSuchDomainException(com.amazonaws.services.simpledb.model.NoSuchDomainException) NotImplementedException(org.apache.commons.lang.NotImplementedException) AmazonClientException(com.amazonaws.AmazonClientException) NoSuchDomainException(com.amazonaws.services.simpledb.model.NoSuchDomainException) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) LazyList(com.spaceprogram.simplejpa.LazyList)

Example 13 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project simplejpa by appoxy.

the class EntityManagerSimpleJPA method renameField.

public void renameField(Class tClass, String oldAttributeName, String newAttributeName) {
    // get list of all items in the domain
    try {
        String domainName = getDomainName(tClass);
        SelectResult result;
        List<Item> items;
        int i = 0;
        String nextToken = null;
        while (i == 0 || nextToken != null) {
            result = executeQueryForRename(oldAttributeName, newAttributeName, domainName, nextToken);
            items = result.getItems();
            putAndDelete(domainName, oldAttributeName, newAttributeName, items);
            nextToken = result.getNextToken();
            i++;
            if (i % 100 == 0) {
                System.out.println("Renamed " + i + " fields so far...");
            }
        }
    } catch (AmazonClientException e) {
        e.printStackTrace();
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) AmazonClientException(com.amazonaws.AmazonClientException)

Example 14 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project simplejpa by appoxy.

the class LazyList method loadAtleastItems.

private synchronized void loadAtleastItems(int index) {
    if ((backingList != null && nextToken == null) || (!noLimit() && index >= maxResults)) {
        return;
    }
    if (backingList == null) {
        backingList = new GrowthList();
    }
    while (backingList.size() <= index) {
        SelectResult qr;
        try {
            if (logger.isLoggable(Level.FINER))
                logger.finer("query for lazylist=" + origQuery);
            int limit = maxResults - backingList.size();
            String limitQuery = realQuery + " limit " + (noLimit() ? maxResultsPerToken : Math.min(maxResultsPerToken, limit));
            if (em.getFactory().isPrintQueries())
                System.out.println("query in lazylist=" + limitQuery);
            qr = DomainHelper.selectItems(this.em.getSimpleDb(), limitQuery, nextToken, isConsistentRead());
            if (logger.isLoggable(Level.FINER))
                logger.finer("got items for lazylist=" + qr.getItems().size());
            for (Item item : qr.getItems()) {
                backingList.add((E) em.buildObject(genericReturnType, item.getName(), item.getAttributes()));
            }
            if (qr.getNextToken() == null || (!noLimit() && qr.getItems().size() == limit)) {
                nextToken = null;
                break;
            }
            if (!noLimit() && qr.getItems().size() > limit) {
                throw new PersistenceException("Got more results than the limit.");
            }
            nextToken = qr.getNextToken();
        } catch (AmazonClientException e) {
            throw new PersistenceException("Query failed: Domain=" + domainName + " -> " + origQuery, e);
        }
    }
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) AmazonClientException(com.amazonaws.AmazonClientException) PersistenceException(javax.persistence.PersistenceException) GrowthList(org.apache.commons.collections.list.GrowthList)

Example 15 with SelectResult

use of com.amazonaws.services.simpledb.model.SelectResult in project simplejpa by appoxy.

the class DomainHelper method selectItems.

/**
     * Runs a query on the passed in domain with the passed in whereClause.  If the nextToken is included for pagination.
     * 
     * @param db
     * @param domainName
     * @param whereClause
     * @param nextToken
     * @param consistentRead true to read consistently, false to use eventual consistency 
     * @return
     * @throws AmazonClientException
     */
public static SelectResult selectItems(AmazonSimpleDB db, String domainName, String whereClause, String nextToken, boolean consistentRead) throws AmazonClientException {
    String selectExpression = "select * from `" + domainName + "`";
    if (whereClause != null) {
        selectExpression += " where " + whereClause;
    }
    SelectResult results = db.select(new SelectRequest().withConsistentRead(consistentRead).withSelectExpression(selectExpression).withNextToken(nextToken));
    return results;
}
Also used : SelectResult(com.amazonaws.services.simpledb.model.SelectResult) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Aggregations

SelectResult (com.amazonaws.services.simpledb.model.SelectResult)22 SelectRequest (com.amazonaws.services.simpledb.model.SelectRequest)12 Item (com.amazonaws.services.simpledb.model.Item)11 AmazonClientException (com.amazonaws.AmazonClientException)7 ReplaceableAttribute (com.amazonaws.services.simpledb.model.ReplaceableAttribute)5 ArrayList (java.util.ArrayList)5 Attribute (com.amazonaws.services.simpledb.model.Attribute)4 SienaException (siena.SienaException)4 AmazonSimpleDB (com.amazonaws.services.simpledb.AmazonSimpleDB)3 LinkedList (java.util.LinkedList)3 CreateDomainRequest (com.amazonaws.services.simpledb.model.CreateDomainRequest)2 DeleteDomainRequest (com.amazonaws.services.simpledb.model.DeleteDomainRequest)2 PutAttributesRequest (com.amazonaws.services.simpledb.model.PutAttributesRequest)2 Date (java.util.Date)2 LinkedHashMap (java.util.LinkedHashMap)2 Test (org.junit.Test)2 Test (org.testng.annotations.Test)2 NoSuchDomainException (com.amazonaws.services.simpledb.model.NoSuchDomainException)1 EventType (com.netflix.simianarmy.EventType)1 MonkeyType (com.netflix.simianarmy.MonkeyType)1