Skip to content
feiben edited this page Feb 3, 2016 · 5 revisions

LunarBase has an event driven framework(EDF) to control the data flow. In case we need to do something with the query result, say sort them by payment, date, or something else, we shall implement a new result handler and register it to EDF, then the engine knows how to execute it.

Here is an OrderBy:

public class ResultOrderBy implements LHandler<Event, Void> {  
	 
		public final String property;  
		public final boolean inverted;  
	  
		public ResultOrderBy(String _property, boolean _inverted) {  
			this.property = _property;  
			this.inverted = _inverted;   
		}   
	  
		public Void execute(Event evt) {  
			if (evt.getClass() == QueryResult.class) {  
				QueryResult recs = (QueryResult) evt;  
				internalExecute( recs._results, this.property, this.inverted);  
				return null;  
			}  
			return null;  
		}   

Any customized handler must implement the LHandler interface execute(Event evt). Here this example deal with the event QueryResult, and returns null. We use Void as the return value for simplicity.

Then we register it, of course the original one will be replaced. The complete code is SortByComplete:

 String db_root = "/home/feiben/DBTest/SeventhDB";  
 DBTaskCenter tc = new DBTaskCenter(db_root);  
 String table = "order";		
 /*    
  * Step1: create an instance of result handler,   
  * in which we process the query results.   
  * Tasks like ranking, calculation, or any thing you want to deal with.  
  */  
 ResultOrderBy my_handler = new ResultOrderBy("payment", false);
		 
 /*  
  * Step2: The DBTaskCenter has default handler, which just   
  * simply print all the query results from the latest to the eldest.   
  */  
 if(tc.hasHandler(QueryResult.class))    
 {  
    tc.replaceHandler(QueryResult.class, my_handler);  
 }   
		
 /*
  * Step3: then construct a new query, see if my new handler works :-)  
  */  
 QuerySimple sq = new QuerySimple(table, "age", "36", 1000);  
 tc.dispatch(sq);   
		
 tc.shutdownDB();

Clone this wiki locally