Search in sources :

Example 6 with Attribute

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

the class EntityManagerSimpleJPA method listAllObjectsRaw.

/**
 * This is mainly for debugging purposes. Will print to system out all of the items in the domain represented by the class parameter.
 *
 * @param c
 * @throws AmazonClientException
 * @throws ExecutionException
 * @throws InterruptedException
 */
public void listAllObjectsRaw(Class c) throws AmazonClientException, ExecutionException, InterruptedException {
    String domainName = factory.getDomainName(c);
    List<Item> items = DomainHelper.listAllItems(factory.getSimpleDb(), domainName, consistentRead);
    List<ItemAndAttributes> ia = ConcurrentRetriever.getAttributesFromSdb(toSdbItem(items), getExecutor(), this);
    for (ItemAndAttributes itemAndAttributes : ia) {
        System.out.println("item=" + itemAndAttributes.getItem().getIdentifier());
        List<Attribute> atts = itemAndAttributes.getAtts();
        for (Attribute att : atts) {
            System.out.println("\t=" + att.getName() + "=" + att.getValue());
        }
    }
}
Also used : Item(com.amazonaws.services.simpledb.model.Item) Attribute(com.amazonaws.services.simpledb.model.Attribute) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute)

Example 7 with Attribute

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

the class ObjectBuilder method buildObject.

public static <T> T buildObject(EntityManagerSimpleJPA em, Class<T> tClass, Object id, List<Attribute> atts) {
    T newInstance;
    /*
        Why was this here?  Should we merge if it exists though?
        newInstance = em.cacheGet(tClass, id);
        if (newInstance != null) {
            return newInstance;
        }*/
    AnnotationInfo ai = em.getFactory().getAnnotationManager().getAnnotationInfo(tClass);
    try {
        // check for DTYPE to see if it's a subclass, must be a faster way to do this you'd think?
        for (Attribute att : atts) {
            if (att.getName().equals(EntityManagerFactoryImpl.DTYPE)) {
                logger.finest("dtype=" + att.getValue());
                ai = em.getFactory().getAnnotationManager().getAnnotationInfoByDiscriminator(att.getValue());
                if (ai == null) {
                    throw new PersistenceException(new ClassNotFoundException("Could not build object with dtype = " + att.getValue() + ". Class not found or is not an @Entity."));
                }
                tClass = ai.getMainClass();
                // check cache again with new class
                newInstance = em.cacheGet(tClass, id);
                if (newInstance != null)
                    return newInstance;
                break;
            }
        }
        ObjectWithInterceptor owi = newEnancedInstance(em, tClass);
        newInstance = (T) owi.getBean();
        for (PersistentProperty field : ai.getPersistentProperties()) {
            String attName = field.getFieldName();
            String columnName = field.getColumnName();
            if (field.isForeignKeyRelationship()) {
                // lazy it up
                Set<String> keys = getForeignKeys(em, field, columnName, atts);
                logger.finest("keys=" + keys);
                if (keys == null || keys.isEmpty()) {
                    continue;
                }
                // todo: stick a cache in here and check the cache for the instance before creating the lazy loader.
                logger.finest("creating new lazy loading instance for field " + field.getFieldName() + " of class " + tClass.getSimpleName() + " with id " + id);
                // Object toSet = newLazyLoadingInstance(retType, keys);
                owi.getInterceptor().putForeignKey(attName, keys);
            } else if (field.isInverseRelationship()) {
                Class typeInList = field.getPropertyClass();
                // todo: should this return null if there are no elements??
                // LazyList lazyList = new LazyList(this, newInstance, annotation.mappedBy(), id, typeInList, factory.getAnnotationManager().getAnnotationInfo(typeInList));
                List<PersistentProperty.OrderClause> orderBy = null;
                if (List.class.isAssignableFrom(field.getRawClass())) {
                    orderBy = field.getOrderClauses();
                }
                LazyList lazyList = new LazyList(em, typeInList, oneToManyQuery(em, attName, field.getMappedBy(), id, typeInList, orderBy));
                // Class retType = field.getReturnType();
                // todo: assuming List for now, handle other collection types
                field.setProperty(newInstance, lazyList);
            } else if (field.isLob()) {
                // handled in Proxy
                String lobKeyAttributeName = field.getColumnName();
                String lobKeyVal = getValueToSet(atts, lobKeyAttributeName, columnName);
                logger.finest("lobkeyval to set on interceptor=" + lobKeyVal + " - fromatt=" + lobKeyAttributeName);
                // TODO add multivalue support for LOB keys
                if (lobKeyVal != null)
                    owi.getInterceptor().putForeignKey(attName, Collections.singleton(lobKeyVal));
            } else if (field.getEnumType() != null) {
                String val = getValueToSet(atts, attName, columnName);
                if (val != null) {
                    Object enumVal = getEnumValue(field, val);
                    field.setProperty(newInstance, enumVal);
                }
            } else if (field.isId()) {
                field.setProperty(newInstance, id);
            } else {
                Collection<String> val = getValuesToSet(atts, attName, columnName);
                if (val != null && !val.isEmpty()) {
                    em.setFieldValue(tClass, newInstance, field, val);
                }
            }
        }
    } catch (Exception e) {
        throw new PersistenceException(e);
    }
    em.cachePut(id, newInstance);
    return newInstance;
}
Also used : Attribute(com.amazonaws.services.simpledb.model.Attribute) PersistenceException(javax.persistence.PersistenceException) PersistenceException(javax.persistence.PersistenceException)

Example 8 with Attribute

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

the class ConcurrentRetriever method getSerially.

private static List<ItemAndAttributes> getSerially(List<SdbItem> items) throws AmazonClientException {
    List<ItemAndAttributes> ret = new ArrayList<ItemAndAttributes>();
    for (SdbItem item : items) {
        // logger.fine("item=" + item.getIdentifier());
        List<Attribute> atts = item.getAttributes();
        ret.add(new ItemAndAttributes(item, atts));
    }
    return ret;
}
Also used : SdbItem(com.spaceprogram.simplejpa.SdbItem) Attribute(com.amazonaws.services.simpledb.model.Attribute) ArrayList(java.util.ArrayList) ItemAndAttributes(com.spaceprogram.simplejpa.ItemAndAttributes)

Example 9 with Attribute

use of com.amazonaws.services.simpledb.model.Attribute in project SimianArmy by Netflix.

the class SimpleDBRecorder method findEvents.

/**
 * Find events.
 *
 * @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;
}
Also used : MonkeyType(com.netflix.simianarmy.MonkeyType) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Attribute(com.amazonaws.services.simpledb.model.Attribute) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute) EventType(com.netflix.simianarmy.EventType) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) LinkedList(java.util.LinkedList) SelectResult(com.amazonaws.services.simpledb.model.SelectResult) Item(com.amazonaws.services.simpledb.model.Item) BasicRecorderEvent(com.netflix.simianarmy.basic.BasicRecorderEvent) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SelectRequest(com.amazonaws.services.simpledb.model.SelectRequest)

Example 10 with Attribute

use of com.amazonaws.services.simpledb.model.Attribute in project SimianArmy by Netflix.

the class SimpleDBConformityClusterTracker method parseCluster.

/**
 * Parses a SimpleDB item into a cluster.
 * @param item the item from SimpleDB
 * @return the cluster for the SimpleDB item
 */
protected Cluster parseCluster(Item item) {
    Map<String, String> fieldToValue = new HashMap<String, String>();
    for (Attribute attr : item.getAttributes()) {
        String name = attr.getName();
        String value = attr.getValue();
        if (name != null && value != null) {
            fieldToValue.put(name, value);
        }
    }
    return Cluster.parseFieldToValueMap(fieldToValue);
}
Also used : HashMap(java.util.HashMap) Attribute(com.amazonaws.services.simpledb.model.Attribute) ReplaceableAttribute(com.amazonaws.services.simpledb.model.ReplaceableAttribute)

Aggregations

Attribute (com.amazonaws.services.simpledb.model.Attribute)27 ReplaceableAttribute (com.amazonaws.services.simpledb.model.ReplaceableAttribute)18 Test (org.junit.Test)12 Item (com.amazonaws.services.simpledb.model.Item)8 UpdateCondition (com.amazonaws.services.simpledb.model.UpdateCondition)8 ArrayList (java.util.ArrayList)8 Exchange (org.apache.camel.Exchange)8 Processor (org.apache.camel.Processor)8 SelectResult (com.amazonaws.services.simpledb.model.SelectResult)5 LinkedList (java.util.LinkedList)3 DeleteAttributesRequest (com.amazonaws.services.simpledb.model.DeleteAttributesRequest)2 PutAttributesRequest (com.amazonaws.services.simpledb.model.PutAttributesRequest)2 SelectRequest (com.amazonaws.services.simpledb.model.SelectRequest)2 Field (java.lang.reflect.Field)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 List (java.util.List)2 AmazonClientException (com.amazonaws.AmazonClientException)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonSimpleDB (com.amazonaws.services.simpledb.AmazonSimpleDB)1