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