Skip to content
feiben edited this page Oct 5, 2016 · 14 revisions

Another kind of common query is range query. We want to know all our users that aging from 25 to 40, than we construct a QueryRange object, send it to DBTaskCenter, and get the results.

To perform range query, user must turn on LunarMax real-time module. Consult LunarMax for how to do it in detail.

/*  
 * Step 1: construct a new range query   
 */   

QueryRange sq = new QueryRange(table, "age", 25, 38);
LFuture<RGQueryResult> results = tc.dispatch(sq);

QueryRange(table, "age", 25, 38) is a LunarBase object consists of a name of column, the starting value and the end value user wants to search. In this example, the column is age, from 25 to 38. As all kinds of queries, send QueryRange to DBTaskCenter, then we have the RGQueryResult of all the satisfying records.

/*
 * Step 2: we have a result of type RGQueryResult, which keeps the records  
 * that drop in the range 25 to 38.  
 * What we have to do is call its interface fetchRecords(),  
 * then returns a list of results.
 */
if(results.get().resultCount() >= 0)
		{
    			ArrayList<Record32KBytes> recs = results.get().fetchRecords();	
    			tc.dispatch(new QueryResult(recs));
		}

The results is kept in an object of RGQueryResult, whose major interfaces are:

resultCount(): how many results falling into the range;  
fetchRecords(): get all the results as a list;
fetchRecords(int top_n): get the top n results;  
fetchRecords(int from, int count): paging the results, for rendering them on each page of screen;  

This source code is under the package of LCG.Examples, the seventh example. Range query and point query combined together will meet the most of data retrieval requirements. Results get from db storage to main memory, then we can do further analysis on it. To learn how to construct a complex multi-dimensional query, please check out previous sector interpreter pattern.

Clone this wiki locally