Search in sources :

Example 61 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class HBasePersistenceManager method insert.

public void insert(Object obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    try {
        HTable table = new HTable(config, info.tableName);
        Field id = ClassInfo.getIdField(clazz);
        id.setAccessible(true);
        Put p = new Put(Bytes.toBytes(id.get(obj).toString()));
        List<Field> fields = info.insertFields;
        for (Field field : fields) {
            p.add(Bytes.toBytes("string"), Bytes.toBytes(ClassInfo.getColumnNames(field)[0]), Bytes.toBytes(field.get(obj).toString()));
        }
        table.put(p);
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Field(java.lang.reflect.Field) SienaException(siena.SienaException) HTable(org.apache.hadoop.hbase.client.HTable) Put(org.apache.hadoop.hbase.client.Put) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 62 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class HBasePersistenceManager method delete.

public void delete(Object obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    try {
        HTable table = new HTable(config, info.tableName);
        Field id = ClassInfo.getIdField(clazz);
        id.setAccessible(true);
        Delete d = new Delete(Bytes.toBytes(id.get(obj).toString()));
        table.delete(d);
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) Field(java.lang.reflect.Field) SienaException(siena.SienaException) HTable(org.apache.hadoop.hbase.client.HTable) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 63 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class HBasePersistenceManager method get.

public void get(Object obj) {
    Class<?> clazz = obj.getClass();
    ClassInfo info = ClassInfo.getClassInfo(clazz);
    try {
        HTable table = new HTable(config, info.tableName);
        Field id = ClassInfo.getIdField(clazz);
        id.setAccessible(true);
        Get g = new Get(Bytes.toBytes(id.get(obj).toString()));
        Result rowResult = table.get(g);
        if (rowResult.isEmpty())
            throw new SienaException("No such object");
        mapObject(clazz, obj, rowResult);
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Field(java.lang.reflect.Field) Get(org.apache.hadoop.hbase.client.Get) SienaException(siena.SienaException) HTable(org.apache.hadoop.hbase.client.HTable) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo) Result(org.apache.hadoop.hbase.client.Result)

Example 64 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class HBasePersistenceManager method mapObject.

private <T> void mapObject(Class<T> clazz, Object obj, Result result) {
    try {
        String id = Bytes.toString(result.getRow());
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        for (Field field : info.insertFields) {
            String column = "string:" + ClassInfo.getColumnNames(field)[0];
            String value = Bytes.toString(result.getValue(Bytes.toBytes("string"), Bytes.toBytes(ClassInfo.getColumnNames(field)[0])));
            field.setAccessible(true);
            field.set(obj, value);
        }
        ClassInfo.getIdField(clazz).set(obj, id);
    } catch (Exception e) {
        throw new SienaException(e);
    }
}
Also used : Field(java.lang.reflect.Field) SienaException(siena.SienaException) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 65 with ClassInfo

use of siena.ClassInfo in project siena by mandubian.

the class GaePersistenceManagerAsync method save.

public SienaFuture<Integer> save(final Iterable<?> objects) {
    List<Entity> entities = new ArrayList<Entity>();
    for (Object obj : objects) {
        Class<?> clazz = obj.getClass();
        ClassInfo info = ClassInfo.getClassInfo(clazz);
        Field idField = info.getIdField();
        Entity entity;
        Object idVal = Util.readField(obj, idField);
        // id with null value means insert
        if (idVal == null) {
            entity = GaeMappingUtils.createEntityInstance(idField, info, obj);
        } else // id with not null value means update
        {
            entity = GaeMappingUtils.createEntityInstanceForUpdate(info, obj);
        }
        GaeMappingUtils.fillEntity(obj, entity);
        entities.add(entity);
    }
    Future<List<Key>> future = ds.put(entities);
    Future<Integer> wrapped = new SienaFutureWrapper<List<Key>, Integer>(future) {

        @Override
        protected Integer wrap(List<Key> keys) throws Exception {
            int i = 0;
            for (Object obj : objects) {
                Class<?> clazz = obj.getClass();
                ClassInfo info = ClassInfo.getClassInfo(clazz);
                Field idField = info.getIdField();
                Object idVal = Util.readField(obj, idField);
                if (idVal == null) {
                    GaeMappingUtils.setIdFromKey(idField, obj, keys.get(i++));
                }
            }
            return keys.size();
        }
    };
    return new SienaFutureContainer<Integer>(wrapped);
}
Also used : SienaFutureWrapper(siena.core.async.SienaFutureWrapper) Entity(com.google.appengine.api.datastore.Entity) SienaFutureContainer(siena.core.async.SienaFutureContainer) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) QueryResultList(com.google.appengine.api.datastore.QueryResultList) ArrayList(java.util.ArrayList) List(java.util.List) Key(com.google.appengine.api.datastore.Key) ClassInfo(siena.ClassInfo)

Aggregations

ClassInfo (siena.ClassInfo)68 Field (java.lang.reflect.Field)33 ArrayList (java.util.ArrayList)24 Key (com.google.appengine.api.datastore.Key)23 SienaException (siena.SienaException)21 Entity (com.google.appengine.api.datastore.Entity)17 QueryResultList (com.google.appengine.api.datastore.QueryResultList)10 List (java.util.List)10 DeletableItem (com.amazonaws.services.simpledb.model.DeletableItem)6 Item (com.amazonaws.services.simpledb.model.Item)6 ReplaceableItem (com.amazonaws.services.simpledb.model.ReplaceableItem)6 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)5 HashMap (java.util.HashMap)5 SienaRestrictedApiException (siena.SienaRestrictedApiException)5 Relation (siena.core.Relation)5 IOException (java.io.IOException)4 QueryFilterSearch (siena.QueryFilterSearch)4 Many4PM (siena.core.Many4PM)4 SienaFutureContainer (siena.core.async.SienaFutureContainer)4