Java Code Examples for javax.jdo.Query#executeWithArray()

The following examples show how to use javax.jdo.Query#executeWithArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ServerUtils.java    From sc2gears with Apache License 2.0 6 votes vote down vote up
/**
 * Counts the entities returned by the specified query.
 * @param query  query whose results to count
 * @param params optional parameters of the query
 * @return the number of entities returned by the query
 * 
 * @deprecated Use of this method does not count toward <b>"Datastore Small Ops"</b> but toward <b>"Datastore Read Ops"</b>. Use {@link #countEntities(DatastoreService, com.google.appengine.api.datastore.Query)} instead.
 */
public static int countEntitiesJdo( final Query query, final Object... params ) {
	int count = 0;
	
	query.setRange( 0, 1000 );
	
	while ( true ) {
		final List< ? > resultList = (List< ? >) query.executeWithArray( params );
		count += resultList.size();
		
		if ( resultList.size() < 1000 )
			return count;
		
		setQueryCursor( query, resultList );
	}
}
 
Example 2
Source File: JdoClusterCountStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getTopSignificantCountsByDimension(int clusterId,
		Set<Integer> dimensions, long timestamp, int limit, double decay)
		throws ClusterCountNoImplementationException {
	final PersistenceManager pm = getPM();
	Map<Long,Double> map = new HashMap<>();
	String dimensionsStr = StringUtils.join(dimensions, ",");
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select item_id,r.v*r.count as score from (select item_id,(count/sl-s/sg)/greatest(count/sl,s/sg) as v,count from (select exp(-(greatest(unix_timestamp()-c.t,0)/?))*c.count as count,cit.total as s,sl,cct.total as sg,c.item_id from cluster_counts c join (select sum(exp(-(greatest(unix_timestamp()-c.t,0)/?))) sl from cluster_counts c where id=?) t1 join cluster_counts_total cct join cluster_counts_item_total cit on (c.item_id=cit.item_id) where id=?) r1) r natural join item_map_enum natural join dimension where dim_id in ("+dimensionsStr+") order by score desc limit "+limit );
	ArrayList<Object> args = new ArrayList<>();
	args.add(decay);
	args.add(decay);
	args.add(clusterId);
	args.add(clusterId);
	Collection<Object[]> res = (Collection<Object[]>)  query.executeWithArray(args.toArray());
	for(Object[] r : res)
	{
		Long itemId = (Long) r[0];
		Double count = (Double) r[1];
		map.put(itemId, count);
	}
	return map;
}
 
Example 3
Source File: JdoClusterCountStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getTopCountsByTwoDimensions(Set<Integer> dimensions,
		int dimension2, int limit, double decay)
		throws ClusterCountNoImplementationException {
	final PersistenceManager pm = getPM();
	Map<Long,Double> map = new HashMap<>();
	String dimensionsStr = StringUtils.join(dimensions, ",");
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select c.item_id,sum(exp(-(greatest(unix_timestamp()-t,0)/?))*count) as decayedCount from cluster_counts c natural join item_map_enum ime1 join dimension d1 on (d1.attr_id=ime1.attr_id and ime1.value_id=d1.value_id) join item_map_enum ime2 on (c.item_id=ime2.item_id) join dimension d2 on (d2.attr_id=ime2.attr_id and ime2.value_id=d2.value_id) where d1.dim_id in ("+dimensionsStr+") and d2.dim_id = ?  group by item_id order by decayedcount desc limit "+limit );
	ArrayList<Object> args = new ArrayList<>();
	args.add(decay);
	args.add(dimension2);
	Collection<Object[]> res = (Collection<Object[]>)  query.executeWithArray(args.toArray());
	for(Object[] r : res)
	{
		Long itemId = (Long) r[0];
		Double count = (Double) r[1];
		map.put(itemId, count);
	}
	return map;
}
 
Example 4
Source File: JdoClusterCountStore.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getTopCountsByTagAndTwoDimensions(String tag,
		int tagAttrId, Set<Integer> dimensions, int dimension2, int limit,
		double decay) throws ClusterCountNoImplementationException {
	final PersistenceManager pm = getPM();
	Map<Long,Double> map = new HashMap<>();
	String dimensionsStr = StringUtils.join(dimensions, ",");
	Query query = pm.newQuery( "javax.jdo.query.SQL", "select c.item_id,sum(exp(-(greatest(unix_timestamp()-t,0)/?))*count) as decayedCount from cluster_counts c natural join item_map_enum ime1 join dimension d1 on (d1.attr_id=ime1.attr_id and ime1.value_id=d1.value_id) join item_map_enum ime2 on (c.item_id=ime2.item_id) join dimension d2 on (d2.attr_id=ime2.attr_id and ime2.value_id=d2.value_id) join item_map_varchar on (c.item_id=item_map_varchar.item_id and item_map_varchar.attr_id=?) where d1.dim_id in ("+dimensionsStr+") and d2.dim_id = ? and value regexp \"(^|,)[ ]*"+tag+"[ ]*(,|$)\" group by item_id order by decayedcount desc limit "+limit );
	ArrayList<Object> args = new ArrayList<>();
	args.add(decay);
	args.add(tagAttrId);
	args.add(dimension2);
	Collection<Object[]> res = (Collection<Object[]>)  query.executeWithArray(args.toArray());
	for(Object[] r : res)
	{
		Long itemId = (Long) r[0];
		Double count = (Double) r[1];
		map.put(itemId, count);
	}
	logger.info("getTopCountsByTagAndTwoDimensions "+tag+" tagATtrId "+tagAttrId+" dimension "+dimensionsStr+" dimension2 "+dimension2+" decay "+decay+ " limit "+limit+ " results "+map.size());
	return map;
}
 
Example 5
Source File: SqlBaselineRecommenderUtils.java    From seldon-server with Apache License 2.0 6 votes vote down vote up
@Override
public Map<Long, Double> getPopularItems(int dimension, int numRecommendations) {
	
	String sql = "select ip.item_id,opsum from items_popular ip join item_map_enum ime on (ip.item_id=ime.item_id) join dimension d on (ime.attr_id=d.attr_id and ime.value_id=d.value_id) where dim_id=? order by opsum desc limit "+numRecommendations;
	Query query = getPM().newQuery( "javax.jdo.query.SQL", sql);
	ArrayList<Object> args = new ArrayList<>();
	args.add(dimension);
	Collection<Object[]> results = (Collection<Object[]>) query.executeWithArray(args.toArray());
	Map<Long,Double> map = new HashMap<>();
	Double topScore = null;
	for(Object[] res : results)
	{
		if (topScore == null)
			topScore = (Double) res[1];
		Long item = (Long) res[0];
		Double score = (Double) res[1];
		map.put(item, score/topScore);
	}
	query.closeAll();
	return map;
}
 
Example 6
Source File: SqlExplanationProvider.java    From seldon-server with Apache License 2.0 5 votes vote down vote up
@Override
public String getExplanation(String recommender, String locale) {
    logger.debug(String.format("Gettting explanation from db for recommender[%s] locale[%s]", recommender, locale));

    String retVal = null;
    {
        String sql = "select re1.explanation as default_explanation,re2.explanation as explanation from recommendation_explanation re1 LEFT join recommendation_explanation re2 on re1.locale=re2.locale and re2.recommender=? where re1.locale=? and re1.recommender ='_DEFAULT_'";
        Query query = getPM().newQuery("javax.jdo.query.SQL", sql);
        List<Object> args = new ArrayList<>();
        args.add(recommender);
        args.add(locale);
        Collection<Object[]> results = (Collection<Object[]>) query.executeWithArray(args.toArray());
        if (results.size() != 1) {
            // Will return null
            logger.debug(String.format("Failed explanation from db for recommender[%s] locale[%s]", recommender, locale));
        } else {
            for (Object[] r : results) { // we just want the first item (there should be only 1)
                String default_explanation = (String) r[0];
                String explanation = (String) r[1];

                if (explanation != null) {
                    retVal = explanation;
                    logger.debug(String.format("Retrieved explanation from db for recommender[%s] locale[%s] as[%s]", recommender, locale, retVal));
                } else {
                    retVal = default_explanation;
                    logger.debug(String.format("Retrieved default_explanation from db for recommender[%s] locale[%s] as[%s]", recommender, locale, retVal));
                }
                break;
            }
        }
    }

    return retVal;
}
 
Example 7
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a List of all CPE objects that match the specified vendor/product/version.
 * @return a List of matching CPE objects
 */
@SuppressWarnings("unchecked")
public List<Cpe> getCpes(final String part, final String vendor, final String product, final String version) {
    final Query query = pm.newQuery(Cpe.class);
    query.setFilter("part == :part && vendor == :vendor && product == :product && version == :version");
    return (List<Cpe>)query.executeWithArray(part, vendor, product, version);
}
 
Example 8
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a List of all VulnerableSoftware objects that match the specified vendor/product/version.
 * @return a List of matching VulnerableSoftware objects
 */
@SuppressWarnings("unchecked")
public List<VulnerableSoftware> getAllVulnerableSoftware(final String part, final String vendor, final String product, final String version) {
    final Query query = pm.newQuery(VulnerableSoftware.class);
    query.setFilter("part == :part && vendor == :vendor && product == :product && version == :version");
    return (List<VulnerableSoftware>)query.executeWithArray(part, vendor, product, version);
}
 
Example 9
Source File: QueryManager.java    From dependency-track with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a List of all VulnerableSoftware objects that match the specified vendor/product.
 * @return a List of matching VulnerableSoftware objects
 */
@SuppressWarnings("unchecked")
public List<VulnerableSoftware> getAllVulnerableSoftware(final String part, final String vendor, final String product) {
    final Query query = pm.newQuery(VulnerableSoftware.class);
    query.setFilter("part == :part && vendor == :vendor && product == :product");
    return (List<VulnerableSoftware>)query.executeWithArray(part, vendor, product);
}
 
Example 10
Source File: AbstractAlpineQueryManager.java    From Alpine with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the number of items that would have resulted from returning all object.
 * This method is performant in that the objects are not actually retrieved, only
 * the count.
 * @param query the query to return a count from
 * @param parameters the <code>Object</code> array with all of the parameters
 * @return the number of items
 * @since 1.0.0
 */
public long getCount(final Query query, final Object... parameters) {
    final String ordering = ((JDOQuery) query).getInternalQuery().getOrdering();
    query.setResult("count(id)");
    query.setOrdering(null);
    final long count = (Long) query.executeWithArray(parameters);
    query.setOrdering(ordering);
    return count;
}