Search in sources :

Example 56 with ClassInfo

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);
}
Also used : Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) ClassInfo(siena.ClassInfo)

Example 57 with ClassInfo

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);
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) GetAttributesRequest(com.amazonaws.services.simpledb.model.GetAttributesRequest) SienaException(siena.SienaException) GetAttributesResult(com.amazonaws.services.simpledb.model.GetAttributesResult) ClassInfo(siena.ClassInfo)

Example 58 with ClassInfo

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);
    }
}
Also used : HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) Configuration(org.apache.hadoop.conf.Configuration) ArrayList(java.util.ArrayList) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) ClassInfo(siena.ClassInfo)

Example 59 with ClassInfo

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) QueryFilterSearch(siena.QueryFilterSearch) ResultSet(java.sql.ResultSet) SienaException(siena.SienaException) ClassInfo(siena.ClassInfo)

Example 60 with ClassInfo

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);
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) SienaException(siena.SienaException) QueryFilterSearch(siena.QueryFilterSearch) Field(java.lang.reflect.Field) ResultSet(java.sql.ResultSet) SienaException(siena.SienaException) 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