Skip to content

Commit

Permalink
Only continue if possible, otherwise do nothing
Browse files Browse the repository at this point in the history
This is needed because some plans do not use loops for find entities.
  • Loading branch information
fickludd committed Nov 29, 2018
1 parent f28f49b commit 076d2de
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
17 changes: 14 additions & 3 deletions community/codegen/src/main/java/org/neo4j/codegen/CodeBlock.java
Expand Up @@ -33,24 +33,32 @@ public class CodeBlock implements AutoCloseable
private MethodEmitter emitter;
private final CodeBlock parent;
private boolean done;
private boolean continuableBlock;

protected LocalVariables localVariables = new LocalVariables();

CodeBlock( CodeBlock parent )
{
this( parent, parent.continuableBlock );
}

CodeBlock( CodeBlock parent, boolean continuableBlock )
{
this.clazz = parent.clazz;
this.emitter = parent.emitter;
parent.emitter = InvalidState.IN_SUB_BLOCK;
this.parent = parent;
//copy over local variables from parent
this.localVariables = copy( parent.localVariables );
this.continuableBlock = continuableBlock;
}

CodeBlock( ClassGenerator clazz, MethodEmitter emitter, Parameter... parameters )
{
this.clazz = clazz;
this.emitter = emitter;
this.parent = null;
this.continuableBlock = false;
localVariables.createNew( clazz.handle(), "this" );
for ( Parameter parameter : parameters )
{
Expand Down Expand Up @@ -162,7 +170,7 @@ public CodeBlock forEach( Parameter local, Expression iterable )
public CodeBlock whileLoop( Expression test )
{
emitter.beginWhile( test );
return new CodeBlock( this );
return new CodeBlock( this, true );
}

public CodeBlock ifStatement( Expression test )
Expand Down Expand Up @@ -192,9 +200,12 @@ public void returns( Expression value )
emitter.returns( value );
}

public void continues()
public void continueIfPossible()
{
emitter.continues();
if ( continuableBlock )
{
emitter.continues();
}
}

public void throwException( Expression exception )
Expand Down
Expand Up @@ -605,7 +605,7 @@ public void shouldGenerateWhileLoopContinue() throws Throwable

try ( CodeBlock ifBlock = loop.ifStatement( loop.load( "skip" ) ) )
{
ifBlock.continues();
ifBlock.continueIfPossible();
}

loop.expression( invoke(
Expand Down Expand Up @@ -667,7 +667,7 @@ public void shouldGenerateNestedWhileLoopInnerContinue() throws Throwable

try ( CodeBlock ifBlock = inner.ifStatement( inner.load( "skip" ) ) )
{
ifBlock.continues();
ifBlock.continueIfPossible();
}

inner.expression( invoke(
Expand Down Expand Up @@ -736,7 +736,7 @@ public void shouldGenerateNestedWhileLoopDoubleContinue() throws Throwable

try ( CodeBlock ifBlock = outer.ifStatement( outer.load( "skipOuter" ) ) )
{
ifBlock.continues();
ifBlock.continueIfPossible();
}

try ( CodeBlock inner = outer.whileLoop( invoke( outer.load( "targets" ),
Expand All @@ -756,7 +756,7 @@ public void shouldGenerateNestedWhileLoopDoubleContinue() throws Throwable

try ( CodeBlock ifBlock = inner.ifStatement( inner.load( "skipInner" ) ) )
{
ifBlock.continues();
ifBlock.continueIfPossible();
}

inner.expression( invoke(
Expand Down
Expand Up @@ -120,7 +120,7 @@ object Templates {
}, new Consumer[CodeBlock] {
override def accept(innerError: CodeBlock): Unit = {
result = onFailure(innerError)
innerError.continues()
innerError.continueIfPossible()
}
}, param[EntityNotFoundException]("enf"))
result
Expand Down

0 comments on commit 076d2de

Please sign in to comment.