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();
}
}
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);
}
}
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();
}
}
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);
}
}
}
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;
}
Aggregations