use of siena.ClassInfo in project siena by mandubian.
the class SdbPersistenceManager method save.
@Override
public int save(Iterable<?> objects) {
List<Object> entities2Insert = new ArrayList<Object>();
List<Object> entities2Update = new ArrayList<Object>();
for (Object obj : objects) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(clazz);
Field idField = info.getIdField();
Object idVal = Util.readField(obj, idField);
// id with null value means insert
if (idVal == null) {
entities2Insert.add(obj);
} else // id with not null value means update
{
entities2Update.add(obj);
}
}
return insert(entities2Insert) + update(entities2Update);
}
use of siena.ClassInfo in project siena by mandubian.
the class SdbPersistenceManager method get.
public void get(Object obj) {
Class<?> clazz = obj.getClass();
ClassInfo info = ClassInfo.getClassInfo(obj.getClass());
String domain = SdbMappingUtils.getDomainName(clazz, prefix);
try {
checkDomain(domain);
GetAttributesRequest req = SdbMappingUtils.createGetRequest(domain, clazz, obj);
// sets consistent read to true when reading one single object
req.setConsistentRead(isReadConsistent());
GetAttributesResult res = sdb.getAttributes(req);
if (res.getAttributes().size() == 0) {
throw new SienaException(req.getItemName() + " not found in domain " + req.getDomainName());
}
SdbMappingUtils.fillModel(req.getItemName(), res, clazz, obj);
// join management
if (!info.joinFields.isEmpty()) {
mapJoins(obj);
}
} catch (AmazonClientException ex) {
throw new SienaException(ex);
}
}
use of siena.ClassInfo in project siena by mandubian.
the class HBaseDdlGenerator method updateSchema.
public void updateSchema() throws IOException {
Configuration config = HBaseConfiguration.create();
HBaseAdmin admin = new HBaseAdmin(config);
HTableDescriptor[] descriptors = admin.listTables();
List<String> tables = new ArrayList<String>();
for (HTableDescriptor hTableDescriptor : descriptors) {
tables.add(hTableDescriptor.getNameAsString());
}
for (Class<?> clazz : classes) {
ClassInfo info = ClassInfo.getClassInfo(clazz);
String tableName = info.tableName;
if (!tables.contains(tableName)) {
// create table
HTableDescriptor descriptor = createTable(tableName);
admin.createTable(descriptor);
}
tables.remove(tableName);
}
for (String table : tables) {
admin.disableTable(table);
admin.deleteTable(table);
}
}
use of siena.ClassInfo in project siena by mandubian.
the class H2PersistenceManager method doSearchCount.
protected <T> int doSearchCount(Query<T> query) {
// TODO this is a very raw impl: need some work certainly
try {
Connection conn = this.getConnection();
ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
// doesn't index a table that has already been indexed
if (!tableIndexMap.containsKey(ci.tableName)) {
List<String> colList = ci.getUpdateFieldsColumnNames();
String cols = null;
if (!colList.isEmpty()) {
cols = "";
// removes auto generated IDs from index
int sz = colList.size();
for (int i = 0; i < sz; i++) {
if ("h2".equals(dbMode))
cols += colList.get(i).toUpperCase();
else // !!! mysql mode means case INsensitive to lowercase !!!!
if ("mysql".equals(dbMode))
cols += colList.get(i).toLowerCase();
else
cols += colList.get(i).toUpperCase();
if (i < sz - 1)
cols += ",";
}
}
// creates the index
FullText.createIndex(conn, "PUBLIC", ci.tableName.toUpperCase(), cols);
tableIndexMap.put(ci.tableName, true);
}
String searchString = "";
Iterator<QueryFilterSearch> it = query.getSearches().iterator();
boolean first = true;
while (it.hasNext()) {
if (!first) {
searchString += " ";
} else {
first = false;
}
searchString += it.next().match;
}
ResultSet rs = FullText.searchData(conn, searchString, 0, 0);
int count = 0;
while (rs.next()) {
//String queryStr = rs.getString("QUERY");
//String score = rs.getString("SCORE");
//Array columns = rs.getArray("COLUMNS");
Object[] keys = (Object[]) rs.getArray("KEYS").getArray();
count += keys.length;
}
return count;
} catch (SQLException e) {
throw new SienaException(e);
}
}
use of siena.ClassInfo in project siena by mandubian.
the class H2PersistenceManager method doSearchKeys.
protected <T> List<T> doSearchKeys(Query<T> query, int limit, int offset) {
// TODO this is a very raw impl: need some work certainly
try {
Connection conn = this.getConnection();
ClassInfo ci = ClassInfo.getClassInfo(query.getQueriedClass());
// doesn't index a table that has already been indexed
if (!tableIndexMap.containsKey(ci.tableName)) {
List<String> colList = ci.getUpdateFieldsColumnNames();
String cols = null;
if (!colList.isEmpty()) {
cols = "";
// removes auto generated IDs from index
int sz = colList.size();
for (int i = 0; i < sz; i++) {
if ("h2".equals(dbMode))
cols += colList.get(i).toUpperCase();
else // !!! mysql mode means case INsensitive to lowercase !!!!
if ("mysql".equals(dbMode))
cols += colList.get(i).toLowerCase();
else
cols += colList.get(i).toUpperCase();
if (i < sz - 1)
cols += ",";
}
}
// creates the index
FullText.createIndex(conn, "PUBLIC", ci.tableName.toUpperCase(), cols);
tableIndexMap.put(ci.tableName, true);
}
String searchString = "";
Iterator<QueryFilterSearch> it = query.getSearches().iterator();
boolean first = true;
while (it.hasNext()) {
if (!first) {
searchString += " ";
} else {
first = false;
}
searchString += it.next().match;
}
ResultSet rs = FullText.searchData(conn, searchString, limit, offset);
List<T> res = new ArrayList<T>();
Class<T> clazz = query.getQueriedClass();
while (rs.next()) {
//String queryStr = rs.getString("QUERY");
//String score = rs.getString("SCORE");
//Array columns = rs.getArray("COLUMNS");
Object[] keys = (Object[]) rs.getArray("KEYS").getArray();
for (Object key : keys) {
T obj = Util.createObjectInstance(clazz);
for (Field field : JdbcClassInfo.getClassInfo(clazz).keys) {
JdbcMappingUtils.setFromObject(obj, field, key);
}
res.add(obj);
}
}
return res;
} catch (SQLException e) {
throw new SienaException(e);
} catch (Exception e) {
throw new SienaException(e);
}
}
Aggregations