Skip to content

Commit

Permalink
Refactor result set and execution plan interface hierarchies
Browse files Browse the repository at this point in the history
  • Loading branch information
luigidellaquila committed Jul 21, 2016
1 parent 8184b8f commit 2298d3d
Show file tree
Hide file tree
Showing 28 changed files with 222 additions and 124 deletions.
Expand Up @@ -7,34 +7,34 @@
/** /**
* @author Luigi Dell'Aquila * @author Luigi Dell'Aquila
*/ */
public abstract class AbstractExecutionStep implements OExecutionStep { public abstract class AbstractExecutionStep implements OExecutionStepInternal {


protected final OCommandContext ctx; protected final OCommandContext ctx;
protected Optional<OExecutionStep> prev = Optional.empty(); protected Optional<OExecutionStepInternal> prev = Optional.empty();
protected Optional<OExecutionStep> next = Optional.empty(); protected Optional<OExecutionStepInternal> next = Optional.empty();
protected boolean timedOut = false; protected boolean timedOut = false;


public AbstractExecutionStep(OCommandContext ctx) { public AbstractExecutionStep(OCommandContext ctx) {
this.ctx = ctx; this.ctx = ctx;
} }


@Override public void setPrevious(OExecutionStep step) { @Override public void setPrevious(OExecutionStepInternal step) {
this.prev = Optional.ofNullable(step); this.prev = Optional.ofNullable(step);
} }


@Override public void setNext(OExecutionStep step) { @Override public void setNext(OExecutionStepInternal step) {
this.next = Optional.ofNullable(step); this.next = Optional.ofNullable(step);
} }


public OCommandContext getContext() { public OCommandContext getContext() {
return ctx; return ctx;
} }


public Optional<OExecutionStep> getPrev() { public Optional<OExecutionStepInternal> getPrev() {
return prev; return prev;
} }


public Optional<OExecutionStep> getNext() { public Optional<OExecutionStepInternal> getNext() {
return next; return next;
} }


Expand Down
Expand Up @@ -18,8 +18,8 @@ public class AggregateProjectionCalculationStep extends ProjectionCalculationSte
private final OGroupBy groupBy; private final OGroupBy groupBy;


//the key is the GROUP BY key, the value is the (partially) aggregated value //the key is the GROUP BY key, the value is the (partially) aggregated value
private Map<List, OResult> aggregateResults = new LinkedHashMap<>(); private Map<List, OResultInternal> aggregateResults = new LinkedHashMap<>();
private List<OResult> finalResults = null; private List<OResultInternal> finalResults = null;


private int nextItem = 0; private int nextItem = 0;


Expand Down Expand Up @@ -71,7 +71,7 @@ private void executeAggregation(OCommandContext ctx, int nRecords) {
if (!prev.isPresent()) { if (!prev.isPresent()) {
throw new OCommandExecutionException("Cannot execute an aggregation or a GROUP BY without a previous result"); throw new OCommandExecutionException("Cannot execute an aggregation or a GROUP BY without a previous result");
} }
OExecutionStep prevStep = prev.get(); OExecutionStepInternal prevStep = prev.get();
OTodoResultSet lastRs = prevStep.syncPull(ctx, nRecords); OTodoResultSet lastRs = prevStep.syncPull(ctx, nRecords);
while (lastRs.hasNext()) { while (lastRs.hasNext()) {
aggregate(lastRs.next(), ctx); aggregate(lastRs.next(), ctx);
Expand All @@ -82,7 +82,7 @@ private void executeAggregation(OCommandContext ctx, int nRecords) {
finalResults = new ArrayList<>(); finalResults = new ArrayList<>();
finalResults.addAll(aggregateResults.values()); finalResults.addAll(aggregateResults.values());
aggregateResults.clear(); aggregateResults.clear();
for (OResult item : finalResults) { for (OResultInternal item : finalResults) {
for (String name : item.getPropertyNames()) { for (String name : item.getPropertyNames()) {
Object prevVal = item.getProperty(name); Object prevVal = item.getProperty(name);
if (prevVal instanceof AggregationContext) { if (prevVal instanceof AggregationContext) {
Expand All @@ -100,9 +100,9 @@ private void aggregate(OResult next, OCommandContext ctx) {
key.add(val); key.add(val);
} }
} }
OResult preAggr = aggregateResults.get(key); OResultInternal preAggr = aggregateResults.get(key);
if (preAggr == null) { if (preAggr == null) {
preAggr = new OResult(); preAggr = new OResultInternal();
aggregateResults.put(key, preAggr); aggregateResults.put(key, preAggr);
} }


Expand All @@ -122,7 +122,7 @@ private void aggregate(OResult next, OCommandContext ctx) {
} }


@Override public String prettyPrint(int depth, int indent) { @Override public String prettyPrint(int depth, int indent) {
String spaces = OExecutionStep.getIndent(depth, indent); String spaces = OExecutionStepInternal.getIndent(depth, indent);
return spaces + "+ CALCULATE AGGREGATE PROJECTIONS\n" + return spaces + "+ CALCULATE AGGREGATE PROJECTIONS\n" +
spaces + " " + projection.toString() + "" + spaces + " " + projection.toString() + "" +
(groupBy == null ? "" : (spaces + "\n " + groupBy.toString())); (groupBy == null ? "" : (spaces + "\n " + groupBy.toString()));
Expand Down
Expand Up @@ -6,7 +6,7 @@
/** /**
* Created by luigidellaquila on 08/07/16. * Created by luigidellaquila on 08/07/16.
*/ */
public class EmptyStep implements OExecutionStep { public class EmptyStep implements OExecutionStepInternal {
@Override public OTodoResultSet syncPull(OCommandContext ctx, int nRecords) throws OTimeoutException { @Override public OTodoResultSet syncPull(OCommandContext ctx, int nRecords) throws OTimeoutException {
return new OInternalResultSet(); return new OInternalResultSet();
} }
Expand All @@ -19,11 +19,11 @@ public class EmptyStep implements OExecutionStep {


} }


@Override public void setPrevious(OExecutionStep step) { @Override public void setPrevious(OExecutionStepInternal step) {


} }


@Override public void setNext(OExecutionStep step) { @Override public void setNext(OExecutionStepInternal step) {


} }


Expand Down
Expand Up @@ -142,14 +142,14 @@ private void sortClusers(int[] clusterIds) {
} }


@Override public void sendTimeout() { @Override public void sendTimeout() {
for (OExecutionStep step : subSteps) { for (OExecutionStepInternal step : subSteps) {
step.sendTimeout(); step.sendTimeout();
} }
prev.ifPresent(p -> p.sendTimeout()); prev.ifPresent(p -> p.sendTimeout());
} }


@Override public void close() { @Override public void close() {
for (OExecutionStep step : subSteps) { for (OExecutionStepInternal step : subSteps) {
step.close(); step.close();
} }
prev.ifPresent(p -> p.close()); prev.ifPresent(p -> p.close());
Expand All @@ -161,11 +161,11 @@ private void sortClusers(int[] clusterIds) {


@Override public String prettyPrint(int depth, int indent) { @Override public String prettyPrint(int depth, int indent) {
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
String ind = OExecutionStep.getIndent(depth, indent); String ind = OExecutionStepInternal.getIndent(depth, indent);
builder.append(ind); builder.append(ind);
builder.append("+ FETCH FROM CLASS " + className + "\n"); builder.append("+ FETCH FROM CLASS " + className + "\n");
for (int i = 0; i < subSteps.length; i++) { for (int i = 0; i < subSteps.length; i++) {
OExecutionStep step = subSteps[i]; OExecutionStepInternal step = subSteps[i];
builder.append(step.prettyPrint(depth + 1, indent)); builder.append(step.prettyPrint(depth + 1, indent));
if (i < subSteps.length - 1) { if (i < subSteps.length - 1) {
builder.append("\n"); builder.append("\n");
Expand Down
Expand Up @@ -66,7 +66,7 @@ record = iterator.previous();
record = iterator.next(); record = iterator.next();
} }
nFetched++; nFetched++;
OResult result = new OResult(); OResultInternal result = new OResultInternal();
result.element = record; result.element = record;
return result; return result;
} }
Expand Down Expand Up @@ -103,7 +103,9 @@ record = iterator.next();
} }


@Override public String prettyPrint(int depth, int indent) { @Override public String prettyPrint(int depth, int indent) {
return OExecutionStep.getIndent(depth, indent) + "+ FETCH FROM CLUSTER " + clusterId; return OExecutionStepInternal.getIndent(depth, indent) + "+ FETCH FROM CLUSTER " + clusterId + " " + (ORDER_DESC.equals(order) ?
"DESC" :
"ASC");
} }


public void setOrder(Object order) { public void setOrder(Object order) {
Expand Down
Expand Up @@ -22,7 +22,7 @@ public FilterStep(OWhereClause whereClause, OCommandContext ctx) {
if (!prev.isPresent()) { if (!prev.isPresent()) {
throw new IllegalStateException("filter step requires a previous step"); throw new IllegalStateException("filter step requires a previous step");
} }
OExecutionStep prevStep = prev.get(); OExecutionStepInternal prevStep = prev.get();


return new OTodoResultSet() { return new OTodoResultSet() {
public boolean finished = false; public boolean finished = false;
Expand Down Expand Up @@ -114,7 +114,7 @@ private void fetchNextItem() {
} }


@Override public String prettyPrint(int depth, int indent) { @Override public String prettyPrint(int depth, int indent) {
return OExecutionStep.getIndent(depth, indent) + "+ CALCULATE WHERE CONDITION : " + whereClause.toString(); return OExecutionStepInternal.getIndent(depth, indent) + "+ CALCULATE WHERE CONDITION : " + whereClause.toString();
} }


} }
Expand Up @@ -45,7 +45,7 @@ public LimitExecutionStep(OLimit limit, OCommandContext ctx) {
} }


@Override public String prettyPrint(int depth, int indent) { @Override public String prettyPrint(int depth, int indent) {
return OExecutionStep.getIndent(depth, indent) + "+ LIMIT (" + limit.toString() + ")"; return OExecutionStepInternal.getIndent(depth, indent) + "+ LIMIT (" + limit.toString() + ")";
} }


} }
@@ -1,13 +1,16 @@
package com.orientechnologies.orient.core.sql.executor; package com.orientechnologies.orient.core.sql.executor;


import java.io.Serializable; import java.io.Serializable;
import java.util.List;


/** /**
* Created by luigidellaquila on 06/07/16. * Created by luigidellaquila on 06/07/16.
*/ */
public interface OExecutionPlan extends Serializable{ public interface OExecutionPlan extends Serializable{


List<OExecutionStep> getSteps();


public String prettyPrint(int indent); String prettyPrint(int indent);


OResult toResult();
} }
@@ -1,37 +1,19 @@
package com.orientechnologies.orient.core.sql.executor; package com.orientechnologies.orient.core.sql.executor;


import com.orientechnologies.common.concur.OTimeoutException; import java.util.List;
import com.orientechnologies.orient.core.command.OCommandContext;


/** /**
* Created by luigidellaquila on 06/07/16. * Created by luigidellaquila on 20/07/16.
*/ */
public interface OExecutionStep extends OExecutionCallback { public interface OExecutionStep {


OTodoResultSet syncPull(OCommandContext ctx, int nRecords) throws OTimeoutException; String getName();


void asyncPull(OCommandContext ctx, int nRecords, OExecutionCallback callback) throws OTimeoutException; String getType();


void sendTimeout(); String getTargetNode();


void setPrevious(OExecutionStep step); String getDescription();


void setNext(OExecutionStep step); List<OExecutionStep> getSubSteps();

void close();

static String getIndent(int depth, int indent) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < depth; i++) {
for (int j = 0; j < indent; j++) {
result.append(" ");
}
}
return result.toString();
}

default String prettyPrint(int depth, int indent) {
String spaces = getIndent(depth, indent);
return spaces + getClass().getSimpleName();
}
} }
@@ -0,0 +1,60 @@
package com.orientechnologies.orient.core.sql.executor;

import com.orientechnologies.common.concur.OTimeoutException;
import com.orientechnologies.orient.core.command.OCommandContext;

import java.util.Collections;
import java.util.List;

/**
* Created by luigidellaquila on 06/07/16.
*/
public interface OExecutionStepInternal extends OExecutionStep, OExecutionCallback {

OTodoResultSet syncPull(OCommandContext ctx, int nRecords) throws OTimeoutException;

void asyncPull(OCommandContext ctx, int nRecords, OExecutionCallback callback) throws OTimeoutException;

void sendTimeout();

void setPrevious(OExecutionStepInternal step);

void setNext(OExecutionStepInternal step);

void close();

static String getIndent(int depth, int indent) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < depth; i++) {
for (int j = 0; j < indent; j++) {
result.append(" ");
}
}
return result.toString();
}

default String prettyPrint(int depth, int indent) {
String spaces = getIndent(depth, indent);
return spaces + getClass().getSimpleName();
}

default String getName() {
return getClass().getSimpleName();
}

default String getType() {
return getClass().getSimpleName();
}

default String getDescription() {
return prettyPrint(0, 3);
}

default String getTargetNode() {
return "<local>";
}

default List<OExecutionStep> getSubSteps() {
return Collections.EMPTY_LIST;
}
}
Expand Up @@ -6,9 +6,15 @@
* Created by luigidellaquila on 06/07/16. * Created by luigidellaquila on 06/07/16.
*/ */
public interface OInternalExecutionPlan extends OExecutionPlan{ public interface OInternalExecutionPlan extends OExecutionPlan{
public void close(); void close();


public OTodoResultSet fetchNext(int n); /**
* if the execution can still return N elements, then the result will contain them all.
* If the execution contains less than N elements, then the result will contain them all, next result(s) will contain zero elements
* @param n
* @return
*/
OTodoResultSet fetchNext(int n);


public void reset(OCommandContext ctx); void reset(OCommandContext ctx);
} }
Expand Up @@ -7,7 +7,7 @@
*/ */
public class OInternalResultSet implements OTodoResultSet { public class OInternalResultSet implements OTodoResultSet {
List<OResult> content = new ArrayList<>(); List<OResult> content = new ArrayList<>();
int next = 0; int next = 0;


@Override public boolean hasNext() { @Override public boolean hasNext() {
return content.size() > next; return content.size() > next;
Expand Down
Expand Up @@ -27,7 +27,7 @@ public OIteratorResultSet(Iterator iter) {
return (OResult) val; return (OResult) val;
} }


OResult result = new OResult(); OResultInternal result = new OResultInternal();
if (val instanceof OIdentifiable) { if (val instanceof OIdentifiable) {
result.setElement((OIdentifiable) val); result.setElement((OIdentifiable) val);
} else { } else {
Expand Down

0 comments on commit 2298d3d

Please sign in to comment.