Search in sources :

Example 6 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method deleteByKeys.

@Override
public <T> int deleteByKeys(Class<T> clazz, Iterable<?> keys) {
    List<DeletableItem> doList = new ArrayList<DeletableItem>();
    int nb = 0;
    String domain = SdbMappingUtils.getDomainName(clazz, prefix);
    for (Object key : keys) {
        doList.add(SdbMappingUtils.createDeletableItemFromKey(clazz, key));
        nb++;
    }
    try {
        checkDomain(domain);
        int len = doList.size() > MAX_ITEMS_PER_CALL ? MAX_ITEMS_PER_CALL : doList.size();
        for (int i = 0; i < doList.size(); i += len) {
            int sz = i + len;
            if (sz > doList.size()) {
                sz = doList.size();
            }
            sdb.batchDeleteAttributes(new BatchDeleteAttributesRequest(domain, doList.subList(i, sz)));
        }
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
    return nb;
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) BatchDeleteAttributesRequest(com.amazonaws.services.simpledb.model.BatchDeleteAttributesRequest) DeletableItem(com.amazonaws.services.simpledb.model.DeletableItem) SienaException(siena.SienaException)

Example 7 with SienaException

use of siena.SienaException 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 8 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method update.

public <T> int update(Iterable<T> models) {
    Map<String, List<ReplaceableItem>> doMap = new HashMap<String, List<ReplaceableItem>>();
    int nb = 0;
    for (Object obj : models) {
        Class<?> clazz = obj.getClass();
        String domain = SdbMappingUtils.getDomainName(clazz, prefix);
        List<ReplaceableItem> doList = doMap.get(domain);
        if (doList == null) {
            doList = new ArrayList<ReplaceableItem>();
            doMap.put(domain, doList);
        }
        doList.add(SdbMappingUtils.createItem(obj));
        nb++;
    }
    try {
        for (String domain : doMap.keySet()) {
            checkDomain(domain);
            List<ReplaceableItem> doList = doMap.get(domain);
            int len = doList.size() > MAX_ITEMS_PER_CALL ? MAX_ITEMS_PER_CALL : doList.size();
            for (int i = 0; i < doList.size(); i += len) {
                int sz = i + len;
                if (sz > doList.size()) {
                    sz = doList.size();
                }
                sdb.batchPutAttributes(new BatchPutAttributesRequest(domain, doList.subList(i, sz)));
            }
        }
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
    return nb;
}
Also used : HashMap(java.util.HashMap) AmazonClientException(com.amazonaws.AmazonClientException) ReplaceableItem(com.amazonaws.services.simpledb.model.ReplaceableItem) BatchPutAttributesRequest(com.amazonaws.services.simpledb.model.BatchPutAttributesRequest) ArrayList(java.util.ArrayList) List(java.util.List) SienaException(siena.SienaException)

Example 9 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbPersistenceManager method insert.

@Override
public int insert(Iterable<?> objects) {
    Map<String, List<ReplaceableItem>> doMap = new HashMap<String, List<ReplaceableItem>>();
    int nb = 0;
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        String domain = SdbMappingUtils.getDomainName(clazz, prefix);
        List<ReplaceableItem> doList = doMap.get(domain);
        if (doList == null) {
            doList = new ArrayList<ReplaceableItem>();
            doMap.put(domain, doList);
        }
        doList.add(SdbMappingUtils.createItem(obj));
        nb++;
    }
    try {
        for (String domain : doMap.keySet()) {
            checkDomain(domain);
            List<ReplaceableItem> doList = doMap.get(domain);
            int len = doList.size() > MAX_ITEMS_PER_CALL ? MAX_ITEMS_PER_CALL : doList.size();
            for (int i = 0; i < doList.size(); i += len) {
                int sz = i + len;
                if (sz > doList.size()) {
                    sz = doList.size();
                }
                sdb.batchPutAttributes(new BatchPutAttributesRequest(domain, doList.subList(i, sz)));
            }
        }
    } catch (AmazonClientException ex) {
        throw new SienaException(ex);
    }
    return nb;
}
Also used : HashMap(java.util.HashMap) AmazonClientException(com.amazonaws.AmazonClientException) ReplaceableItem(com.amazonaws.services.simpledb.model.ReplaceableItem) BatchPutAttributesRequest(com.amazonaws.services.simpledb.model.BatchPutAttributesRequest) ArrayList(java.util.ArrayList) List(java.util.List) SienaException(siena.SienaException)

Example 10 with SienaException

use of siena.SienaException in project siena by mandubian.

the class SdbNativeSerializer method embed.

public static void embed(PutAttributesRequest req, String embeddingColumnName, Object embeddedObj) {
    Class<?> clazz = embeddedObj.getClass();
    if (clazz.isArray() || Collection.class.isAssignableFrom(clazz)) {
        throw new SienaException("can't serializer Array/Collection in native mode");
    }
    for (Field f : ClassInfo.getClassInfo(clazz).updateFields) {
        String propValue = SdbMappingUtils.objectFieldToString(embeddedObj, f);
        if (propValue != null) {
            ReplaceableAttribute attr = new ReplaceableAttribute(getEmbeddedAttributeName(embeddingColumnName, f), propValue, true);
            req.withAttributes(attr);
        } else {
            if (ClassInfo.isEmbeddedNative(f)) {
                SdbNativeSerializer.embed(req, getEmbeddedAttributeName(embeddingColumnName, f), Util.readField(embeddedObj, f));
            }
        }
    }
}
Also used : Field(java.lang.reflect.Field) Collection(java.util.Collection) SienaException(siena.SienaException) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute)

Aggregations

SienaException (siena.SienaException)214 Field (java.lang.reflect.Field)65 TransactionAccountFrom (siena.base.test.model.TransactionAccountFrom)52 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)40 SQLException (java.sql.SQLException)38 TransactionAccountTo (siena.base.test.model.TransactionAccountTo)34 SienaRestrictedApiException (siena.SienaRestrictedApiException)33 PreparedStatement (java.sql.PreparedStatement)25 ClassInfo (siena.ClassInfo)21 HashMap (java.util.HashMap)19 List (java.util.List)18 Discovery (siena.base.test.model.Discovery)18 Entity (com.google.appengine.api.datastore.Entity)15 Key (com.google.appengine.api.datastore.Key)14 ResultSet (java.sql.ResultSet)14 Id (siena.Id)14 AmazonClientException (com.amazonaws.AmazonClientException)12 Collection (java.util.Collection)10 DiscoveryStringId (siena.base.test.model.DiscoveryStringId)9