Skip to content

Commit

Permalink
Fixed issue #2309 wit new functions to get traversed elements
Browse files Browse the repository at this point in the history
We'd need 3 new functions:

traversedVertex, to return the traversed vertex
traversedEdge, to return the traversed edge
traversedElement, to return any of the traversed element
All functions should get 2 params:

index, as starting index. negative numbers means from the end, positive
from the beginning of the stack. Example 0 = absolute first traversed
vertex, -1 = relative last one
items, as optional, by default is 1, but specifying >1 collects the
elements in a collection.
Example:

select $path, traversedVertex(-1) from ( traverse out() from #9:1 while
$depth < 10 )
  • Loading branch information
lvca committed May 2, 2014
1 parent c261173 commit 40f9d6f
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package com.orientechnologies.orient.core.sql.functions;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.sql.functions.coll.*;
import com.orientechnologies.orient.core.sql.functions.geo.OSQLFunctionDistance;
Expand All @@ -33,10 +37,6 @@
import com.orientechnologies.orient.core.sql.functions.misc.OSQLFunctionSysdate;
import com.orientechnologies.orient.core.sql.functions.text.OSQLFunctionFormat;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
* Default set of SQL function.
*
Expand All @@ -60,6 +60,9 @@ public final class ODefaultSQLFunctionFactory implements OSQLFunctionFactory {
register(OSQLFunctionEval.NAME, OSQLFunctionEval.class);
register(OSQLFunctionFirst.NAME, OSQLFunctionFirst.class);
register(OSQLFunctionFormat.NAME, new OSQLFunctionFormat());
register(OSQLFunctionTraversedEdge.NAME, OSQLFunctionTraversedEdge.class);
register(OSQLFunctionTraversedElement.NAME, OSQLFunctionTraversedElement.class);
register(OSQLFunctionTraversedVertex.NAME, OSQLFunctionTraversedVertex.class);
register(OSQLFunctionIf.NAME, new OSQLFunctionIf());
register(OSQLFunctionIfNull.NAME, new OSQLFunctionIfNull());
register(OSQLFunctionIntersect.NAME, OSQLFunctionIntersect.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object i
}

public boolean aggregateResults() {
return configuredParameters.length == 1;
return false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orientechnologies.orient.core.sql.functions.coll;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;

/**
* Returns a traversed element from the stack. Use it with SQL traverse only.
*
* @author Luca Garulli (l.garulli--at--orientechnologies.com)
*
*/
public class OSQLFunctionTraversedEdge extends OSQLFunctionTraversedElement {
public static final String NAME = "traversedEdge";

public OSQLFunctionTraversedEdge() {
super(NAME);
}

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams,
final OCommandContext iContext) {
return evaluate( iParams, iContext, "E" );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orientechnologies.orient.core.sql.functions.coll;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.command.traverse.OTraverseRecordProcess;
import com.orientechnologies.orient.core.db.record.OIdentifiable;
import com.orientechnologies.orient.core.exception.OCommandExecutionException;
import com.orientechnologies.orient.core.record.impl.ODocument;
import com.orientechnologies.orient.core.sql.functions.OSQLFunctionConfigurableAbstract;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
* Returns a traversed element from the stack. Use it with SQL traverse only.
*
* @author Luca Garulli (l.garulli--at--orientechnologies.com)
*
*/
public class OSQLFunctionTraversedElement extends OSQLFunctionConfigurableAbstract {
public static final String NAME = "traversedElement";

public OSQLFunctionTraversedElement(final String name) {
super(name, 1, 2);
}

public boolean aggregateResults() {
return false;
}

@Override
public Object getResult() {
return null;
}

@Override
public boolean filterResult() {
return true;
}

public String getSyntax() {
return getName() + "(<beginIndex> [,<items>])";
}

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams,
final OCommandContext iContext) {
return evaluate(iParams, iContext, null);
}

protected Object evaluate(final Object[] iParams, final OCommandContext iContext, final String iClassName) {
final int beginIndex = (Integer) iParams[0];
final int items = iParams.length > 1 ? (Integer) iParams[1] : 1;

final ArrayDeque stack = (ArrayDeque) iContext.getVariable("stack");
if (stack == null)
throw new OCommandExecutionException("Cannot invoke " + getName() + "() against non traverse command");

final List<OIdentifiable> result = items > 1 ? new ArrayList<OIdentifiable>(items) : null;

if (beginIndex < 0) {
int i = -1;
for (Iterator it = stack.iterator(); it.hasNext();) {
final Object o = it.next();
if (o instanceof OTraverseRecordProcess) {
final ODocument record = ((OTraverseRecordProcess) o).getTarget();

if (iClassName == null || record.getSchemaClass().isSubClassOf(iClassName)) {
if (i <= beginIndex) {
if (items == 1)
return record;
else {
result.add(record);
if (result.size() >= items)
break;
}
}
i--;
}
}
}
} else {
int i = 0;
for (Iterator it = stack.descendingIterator(); it.hasNext();) {
final Object o = it.next();
if (o instanceof OTraverseRecordProcess) {
final ODocument record = ((OTraverseRecordProcess) o).getTarget();

if (iClassName == null || record.getSchemaClass().isSubClassOf(iClassName)) {
if (i >= beginIndex) {
if (items == 1)
return record;
else {
result.add(record);
if (result.size() >= items)
break;
}
}
i++;
}
}
}
}

if (items > 0 && result != null && !result.isEmpty())
return result;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orientechnologies.orient.core.sql.functions.coll;

import com.orientechnologies.orient.core.command.OCommandContext;
import com.orientechnologies.orient.core.db.record.OIdentifiable;

/**
* Returns a traversed element from the stack. Use it with SQL traverse only.
*
* @author Luca Garulli (l.garulli--at--orientechnologies.com)
*
*/
public class OSQLFunctionTraversedVertex extends OSQLFunctionTraversedElement {
public static final String NAME = "traversedVertex";

public OSQLFunctionTraversedVertex() {
super(NAME);
}

public Object execute(Object iThis, final OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams,
final OCommandContext iContext) {
return evaluate( iParams, iContext, "V" );
}
}

0 comments on commit 40f9d6f

Please sign in to comment.