Skip to content

Commit

Permalink
cleanup and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalgoyal committed Oct 18, 2012
1 parent 113970b commit a2f7100
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 181 deletions.
2 changes: 0 additions & 2 deletions src/main/java/co/nubetech/crux/action/ViewReportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package co.nubetech.crux.action;

import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

Expand All @@ -26,7 +25,6 @@
import co.nubetech.crux.model.Mapping;
import co.nubetech.crux.model.Report;
import co.nubetech.crux.model.ReportDesign;
import co.nubetech.crux.model.RowAlias;
import co.nubetech.crux.model.RowAliasFilter;
import co.nubetech.crux.model.ValueType;
import co.nubetech.crux.server.CruxScanner;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/co/nubetech/crux/model/Mapping.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
package co.nubetech.crux.model;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

public class Mapping {
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/co/nubetech/crux/model/Report.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Stack;

import org.apache.log4j.Logger;

import co.nubetech.crux.server.functions.CruxFunction;
import co.nubetech.crux.util.CruxException;

public class Report {

Expand All @@ -29,6 +36,8 @@ public class Report {
private Collection<RowAliasFilter> rowAliasFilters = new ArrayList<RowAliasFilter>();
private Collection<ColumnFilter> columnFilters = new ArrayList<ColumnFilter>();
private GroupBys groupBys;

final static Logger logger = Logger.getLogger(Report.class);

public Report() {

Expand Down Expand Up @@ -260,6 +269,47 @@ public String toString() {
+ ", columnFilters=" + columnFilters + ", groupBys=" + groupBys
+ "]";
}

/*
* A report is an aggregate report
* if at least one function is an aggregate function
*/
public boolean isAggregateReport() throws CruxException{
boolean isAggregate = false;
List<Stack<CruxFunction>> functions = getFunctions();
for (Stack<CruxFunction> fnStack: functions) {
for (CruxFunction fn: fnStack) {
if (fn.isAggregate()) {
return true;
}
}
}
return false;
}

public List<Stack<CruxFunction>> getFunctions() throws CruxException{
List<Stack<CruxFunction>> aggregators = new ArrayList<Stack<CruxFunction>>();
try {
for (ReportDesign design: getDesigns()) {
logger.debug("Finding functions for design: " + design);
Collection<ReportDesignFunction> functions = design.getReportDesignFunctionList();
Stack<CruxFunction> functionStack = new Stack<CruxFunction>();
if (functions != null) {
for (ReportDesignFunction function: functions) {
logger.debug("Creating function class for " + function);
functionStack.push((CruxFunction) Class.forName(function.getFunction().getFunctionClass()).
newInstance());
}
}
aggregators.add(functionStack);
}
}
catch(Exception e) {
e.printStackTrace();
throw new CruxException("Unable to generate the functions " + e);
}
return aggregators;
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package co.nubetech.crux.server.aggregate;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand All @@ -9,6 +10,7 @@
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.coprocessor.BaseEndpointCoprocessor;
import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
import org.apache.hadoop.hbase.ipc.ProtocolSignature;
import org.apache.hadoop.hbase.regionserver.InternalScanner;
import org.apache.log4j.Logger;

Expand All @@ -17,7 +19,6 @@
import co.nubetech.crux.model.GroupBys;
import co.nubetech.crux.model.Report;
import co.nubetech.crux.model.ReportDesign;
import co.nubetech.crux.model.ReportDesignFunction;
import co.nubetech.crux.model.RowAlias;
import co.nubetech.crux.server.functions.CruxAggregator;
import co.nubetech.crux.server.functions.CruxFunction;
Expand All @@ -28,14 +29,25 @@ public class GroupingAggregationImpl extends BaseEndpointCoprocessor implements
GroupingAggregationProtocol{

private final static Logger logger = Logger.getLogger(GroupingAggregationImpl.class);
public static final long VERSION = 1L;

@Override
public ProtocolSignature getProtocolSignature(
String protocol, long version, int clientMethodsHashCode)
throws IOException {
if (GroupingAggregationImpl.class.getName().equals(protocol)) {
return new ProtocolSignature(GroupingAggregationImpl.VERSION, null);
}
throw new IOException("Unknown protocol: " + protocol);
}

/**
* We are here
* means that the caller has already decided that
* the server needs to be executed
* which means at least one of the design functions
* is an aggregate
* Thsi function is called without a group by
* This function is called without a group by
*/
@Override
public List getAggregates(Scan scan, Report report) throws CruxException{
Expand All @@ -49,7 +61,7 @@ public List getAggregates(Scan scan, Report report) throws CruxException{
//get the col or row alias and the function to be applied.
//create list of functions to be applied.
Collection<ReportDesign> designs = report.getDesigns();
List<Stack<CruxFunction>> functions = getFunctions(report);
List<Stack<CruxFunction>> functions = report.getFunctions();
GroupBys groupBys = report.getGroupBys();
//open the scan
//get values of data for each group by
Expand Down Expand Up @@ -91,6 +103,7 @@ public List getAggregates(Scan scan, Report report) throws CruxException{
}
}
catch(Exception e) {
e.printStackTrace();
throw new CruxException("Error processing aggregates " + e);
}

Expand Down Expand Up @@ -182,31 +195,7 @@ protected Alias getAlias(ReportDesign design) {
alias = design.getColumnAlias();
}
return alias;
}
}


protected List<Stack<CruxFunction>> getFunctions(Report report) throws CruxException{
List<Stack<CruxFunction>> aggregators = new ArrayList<Stack<CruxFunction>>();
try {
for (ReportDesign design: report.getDesigns()) {
logger.debug("Finding functions for design: " + design);
Collection<ReportDesignFunction> functions = design.getReportDesignFunctionList();
Stack<CruxFunction> functionStack = new Stack<CruxFunction>();
if (functions != null) {
for (ReportDesignFunction function: functions) {
logger.debug("Creating function class for " + function);
functionStack.push((CruxFunction) Class.forName(function.getFunction().getFunctionClass()).
newInstance());
}
}
aggregators.add(functionStack);
}
}
catch(Exception e) {
e.printStackTrace();
throw new CruxException("Unable to generate the functions " + e);
}
return aggregators;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import org.junit.Test;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package co.nubetech.crux.action;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import co.nubetech.crux.model.Mapping;
import co.nubetech.crux.model.RowAlias;
import co.nubetech.crux.model.ValueType;

public class TestViewReportAction {

Expand Down
57 changes: 57 additions & 0 deletions src/test/java/co/nubetech/crux/model/TestReport.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
package co.nubetech.crux.model;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.List;
import java.util.Stack;

import org.apache.log4j.Logger;
import org.junit.Test;

import co.nubetech.crux.server.functions.CruxFunction;
import co.nubetech.crux.util.CruxException;


public class TestReport {
private final static Logger logger = Logger.getLogger(TestReport.class);
@Test
public void testEquals() {
User user = new User();
Expand Down Expand Up @@ -129,4 +140,50 @@ public void testIsDashboard(){
assertTrue(!report1.onDashboard());
assertTrue(report.onDashboard());
}

@Test
public void testGetAggregators() throws CruxException{
Report report = Util.getReport();
List<Stack<CruxFunction>> functionList = report.getFunctions();
assertEquals(3, functionList.size());
Stack<CruxFunction> xFnStack = functionList.get(0);
assertEquals(2,xFnStack.size());
assertEquals(co.nubetech.crux.server.functions.SumAggregator.class, xFnStack.pop().getClass());
assertEquals(co.nubetech.crux.server.functions.Ceil.class, xFnStack.pop().getClass());
Stack<CruxFunction> yFnStack = functionList.get(1);
assertEquals(1, yFnStack.size());
assertEquals(co.nubetech.crux.server.functions.AverageAggregator.class, yFnStack.pop().getClass());
Stack<CruxFunction> yFnStack1 = functionList.get(2);
assertEquals(0, yFnStack1.size());
}

@Test
public void testGetAggregatorsNoFunctions() throws CruxException{
Report report = Util.getReport();
for (ReportDesign design: report.getDesigns()) {
design.setReportDesignFunctionList(null);
}
List<Stack<CruxFunction>> functionList = report.getFunctions();
assertEquals(3, functionList.size());
Stack<CruxFunction> xFnStack = functionList.get(0);
assertEquals(0,xFnStack.size());
Stack<CruxFunction> yFnStack = functionList.get(1);
assertEquals(0, yFnStack.size());
Stack<CruxFunction> yFnStack1 = functionList.get(2);
assertEquals(0, yFnStack1.size());
}

@Test
public void testIsAggregateTrue() throws CruxException{
Report report = Util.getReport();
assertTrue(report.isAggregateReport());
}

@Test
public void testIsAggregateFalse() throws CruxException{
Report report = Util.getReportWithoutAggregateFunctions();
assertFalse(report.isAggregateReport());
}


}
2 changes: 0 additions & 2 deletions src/test/java/co/nubetech/crux/model/TestRowAlias.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@

import org.junit.Test;

import co.nubetech.crux.action.ViewReportAction;

public class TestRowAlias {
@Test
public void testEquals() {
Expand Down
Loading

0 comments on commit a2f7100

Please sign in to comment.