Skip to content
feiben edited this page Jan 8, 2016 · 4 revisions

For complex db queries, we need to manipulate every single query and put the results together. Most popular operations are AND, OR. We need to know all the records satisfying "age=25" AND "payment=600" OR .....

It is an interpreter pattern that we need. The following example is an query with multiple conditions select where age=36 AND payment=600 AND name=Rafael8

/*
 * Step1: construct a batch of queries,  
 * each of them returns an array of records ids for quick processing,   
 * as we know that fetching the whole long records are time consuming.  
 *   
 * It is executed multi-threaded.    
 */  
 	 		 
 /*  
  * the query is a combination of multiple conditions:  
  * select where age=36 AND payment=600 AND name=Rafael8  
  */  
 QuerySimpleIDs sq1 = new QuerySimpleIDs("age", "36", 0);  
 QuerySimpleIDs sq2 = new QuerySimpleIDs("payment", "600", 0);  
 QuerySimpleIDs sq3 = new QuerySimpleIDs("name", "Rafael8", 0);  
		
 LFuture<int[]> ids1 = tc.dispatch(sq1);  
 LFuture<int[]> ids2 = tc.dispatch(sq2);  
 LFuture<int[]> ids3 = tc.dispatch(sq3);   

LFuture is an internal generic class which is convenient to accept various values returned by task center. In this example it is an array of ids.

 OpsStack os = new OpsStack();  
/*   
 * add an nothing-operator just for keeping   
 * the lengths of ops and candidates the same.  
 */  
 os.addOps(ids1.get(), Ops.NOTHING);  
 os.addOps(ids2.get(), Ops.AND);  
 os.addOps(ids3.get(), Ops.AND);  
		
 int[] result_ids = os.execute();  
/*
 * again we use LFuture to accept the return result.  
 * In this case, it is an array of records.  
 */  
 LFuture<ArrayList<Record32KBytes>> results = tc.dispatch(new QueryRecs(result_ids));

OpsStack pushes data and operations into a stack, and execute them one by one. It is a very simple interpreter. The complete example is Interpreter.

For very big data sets, say 10 million records ids need to computer AND, OR or something else, we have to introduce map-reduce methodology to speed up the procedure. We split 10 million ids to 10 cpu cores, if has, sort them and put the results together. Our advanced topics will do this for you.

Clone this wiki locally