Skip to content

Commit

Permalink
mutable map as context, cached-expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
jexp authored and systay committed Mar 19, 2012
1 parent 7e88fe0 commit 022994d
Show file tree
Hide file tree
Showing 50 changed files with 260 additions and 118 deletions.
15 changes: 8 additions & 7 deletions cypher/src/main/scala/org/neo4j/cypher/PipeExecutionResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import org.neo4j.graphdb.{PropertyContainer, Relationship, NotFoundException, No
import java.io.{StringWriter, PrintWriter}
import java.lang.String
import internal.symbols.SymbolTable


class PipeExecutionResult(result: Traversable[Map[String, Any]], val symbols: SymbolTable, val columns: List[String], val timeTaken: Long)
import collection.Map
import collection.immutable.{Map => ImmutableMap}
class PipeExecutionResult(r: Traversable[Map[String, Any]], val symbols: SymbolTable, val columns: List[String], val timeTaken: Long)
extends ExecutionResult
with StringExtras {


lazy val immutableResult = r.map( m => m.toMap )
def javaColumns: java.util.List[String] = columns.asJava

def javaColumnAs[T](column: String): java.util.Iterator[T] = columnAs[T](column).map(x => makeValueJavaCompatible(x).asInstanceOf[T]).asJava
Expand Down Expand Up @@ -66,7 +67,7 @@ class PipeExecutionResult(result: Traversable[Map[String, Any]], val symbols: Sy
}

def dumpToString(writer: PrintWriter) {
val eagerResult = result.toList
val eagerResult = r.toList

val columnSizes = calculateColumnSizes(eagerResult)

Expand Down Expand Up @@ -121,10 +122,10 @@ class PipeExecutionResult(result: Traversable[Map[String, Any]], val symbols: Sy
}).mkString("| ", " | ", " |")
}

val iterator = result.toIterator
lazy val iterator = immutableResult.toIterator

def hasNext: Boolean = iterator.hasNext

def next(): Map[String, Any] = iterator.next()
def next(): ImmutableMap[String, Any] = iterator.next()
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ import collection.Seq
import org.neo4j.cypher.internal.pipes.aggregation._
import org.neo4j.cypher.internal.symbols._
import org.neo4j.cypher.SyntaxException

import collection.Map
abstract class AggregationExpression extends Expression {
def apply(m: Map[String, Any]) = m.get(name) match {
def compute(m: Map[String, Any]) = m.get(name) match {
case None => null
case Some(x) => x
}

override def identifier = Identifier(name, typ)
override val identifier = Identifier(name, typ)

def name: String

Expand Down Expand Up @@ -65,7 +65,7 @@ abstract class AggregationWithInnerExpression(inner:Expression) extends Aggregat
def declareDependencies(extectedType: AnyType): Seq[Identifier] = inner.dependencies(expectedInnerType)
def expectedInnerType: AnyType

override def identifier = Identifier("%s(%s)".format(name, inner.identifier.name), typ)
override val identifier = Identifier("%s(%s)".format(name, inner.identifier.name), typ)

def filter(f: (Expression) => Boolean) = if (f(this))
Seq(this) ++ inner.filter(f)
Expand All @@ -76,7 +76,7 @@ abstract class AggregationWithInnerExpression(inner:Expression) extends Aggregat
case class Distinct(innerAggregator: AggregationExpression, expression: Expression) extends AggregationWithInnerExpression(expression) {
def typ = innerAggregator.identifier.typ

override def identifier = Identifier("%s(distinct %s)".format(innerAggregator.name, expression.identifier.name), innerAggregator.identifier.typ)
override val identifier = Identifier("%s(distinct %s)".format(innerAggregator.name, expression.identifier.name), innerAggregator.identifier.typ)

def expectedInnerType: AnyType = AnyType()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import collection.Seq
import org.neo4j.cypher.internal.Comparer
import java.lang.String
import org.neo4j.cypher.internal.symbols.{AnyType, ScalarType, Identifier}
import collection.Map

abstract sealed class ComparablePredicate(left: Expression, right: Expression) extends Predicate with Comparer {
def compare(comparisonResult: Int): Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ import java.lang.String
import org.neo4j.cypher._
import internal.symbols._
import org.neo4j.graphdb.{NotFoundException, PropertyContainer}
import collection.Seq
import collection.Map

abstract class Expression extends (Map[String, Any] => Any) {
def identifier: Identifier

protected def compute(v1: Map[String, Any]) : Any
def apply(m: Map[String, Any]) = m.getOrElse(identifier.name, compute(m))

val identifier: Identifier

def declareDependencies(extectedType: AnyType): Seq[Identifier]

Expand All @@ -48,10 +52,22 @@ abstract class Expression extends (Map[String, Any] => Any) {
override def toString() = identifier.name
}

case class CachedExpression(key:String, identifier:Identifier) extends Expression {
override def apply(m: Map[String, Any]) = m(key)

protected def compute(v1: Map[String, Any]) = null

def declareDependencies(extectedType: AnyType) = Seq()

def rewrite(f: (Expression) => Expression) = f(this)

def filter(f: (Expression) => Boolean) = if(f(this)) Seq(this) else Seq()
}

case class Add(a: Expression, b: Expression) extends Expression {
def identifier = Identifier(a.identifier.name + " + " + b.identifier.name, ScalarType())
val identifier = Identifier(a.identifier.name + " + " + b.identifier.name, ScalarType())

def apply(m: Map[String, Any]) = {
def compute(m: Map[String, Any]) = {
val aVal = a(m)
val bVal = b(m)

Expand Down Expand Up @@ -134,7 +150,7 @@ case class Divide(a: Expression, b: Expression) extends Arithmetics(a, b) {
}

abstract class Arithmetics(left: Expression, right: Expression) extends Expression {
def identifier = Identifier("%s %s %s".format(left.identifier.name, operand, right.identifier.name), ScalarType())
val identifier = Identifier("%s %s %s".format(left.identifier.name, operand, right.identifier.name), ScalarType())

def operand: String

Expand All @@ -149,7 +165,7 @@ abstract class Arithmetics(left: Expression, right: Expression) extends Expressi
case _ => x.toString
}

def apply(m: Map[String, Any]) = {
def compute(m: Map[String, Any]) = {
val aVal = left(m)
val bVal = right(m)

Expand All @@ -176,9 +192,9 @@ abstract class Arithmetics(left: Expression, right: Expression) extends Expressi
}

case class Literal(v: Any) extends Expression {
def apply(m: Map[String, Any]) = v
def compute(m: Map[String, Any]) = v

def identifier = Identifier(v.toString, AnyType.fromJava(v))
val identifier = Identifier(v.toString, AnyType.fromJava(v))

def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq()

Expand All @@ -195,9 +211,9 @@ abstract class CastableExpression extends Expression {
}

case class Nullable(expression: Expression) extends Expression {
def identifier = Identifier(expression.identifier.name + "?", expression.identifier.typ)
val identifier = Identifier(expression.identifier.name + "?", expression.identifier.typ)

def apply(m: Map[String, Any]) = try {
def compute(m: Map[String, Any]) = try {
expression.apply(m)
} catch {
case x: EntityNotFoundException => null
Expand All @@ -216,7 +232,7 @@ case class Nullable(expression: Expression) extends Expression {
}

case class Property(entity: String, property: String) extends CastableExpression {
def apply(m: Map[String, Any]): Any = {
def compute(m: Map[String, Any]): Any = {
m(entity).asInstanceOf[PropertyContainer] match {
case null => null
case propertyContainer => try {
Expand All @@ -227,7 +243,7 @@ case class Property(entity: String, property: String) extends CastableExpression
}
}

def identifier: Identifier = Identifier(entity + "." + property, ScalarType())
val identifier: Identifier = Identifier(entity + "." + property, ScalarType())

def declareDependencies(extectedType: AnyType): Seq[Identifier] = Seq(Identifier(entity, MapType()))

Expand All @@ -240,9 +256,9 @@ case class Property(entity: String, property: String) extends CastableExpression
}

case class Entity(entityName: String) extends CastableExpression {
def apply(m: Map[String, Any]): Any = m.getOrElse(entityName, throw new NotFoundException)
def compute(m: Map[String, Any]): Any = m.getOrElse(entityName, throw new NotFoundException)

def identifier: Identifier = Identifier(entityName, AnyType())
val identifier: Identifier = Identifier(entityName, AnyType())

override def toString(): String = entityName

Expand All @@ -257,9 +273,9 @@ case class Entity(entityName: String) extends CastableExpression {
}

case class Collection(expressions:Expression*) extends CastableExpression {
def apply(m: Map[String, Any]): Any = expressions.map(e=>e(m))
def compute(m: Map[String, Any]): Any = expressions.map(e=>e(m))

def identifier: Identifier = Identifier(name, AnyIterableType())
val identifier: Identifier = Identifier(name, AnyIterableType())

private def name = expressions.map(_.identifier.name).mkString("[", ", ", "]")

Expand All @@ -274,12 +290,12 @@ case class Collection(expressions:Expression*) extends CastableExpression {
}

case class Parameter(parameterName: String) extends CastableExpression {
def apply(m: Map[String, Any]): Any = m.getOrElse("-=PARAMETER=-" + parameterName + "-=PARAMETER=-", throw new ParameterNotFoundException("Expected a parameter named " + parameterName)) match {
def compute(m: Map[String, Any]): Any = m.getOrElse("-=PARAMETER=-" + parameterName + "-=PARAMETER=-", throw new ParameterNotFoundException("Expected a parameter named " + parameterName)) match {
case ParameterValue(x) => x
case _ => throw new ParameterNotFoundException("Expected a parameter named " + parameterName)
}

def identifier: Identifier = Identifier(parameterName, AnyType())
val identifier: Identifier = Identifier(parameterName, AnyType())

override def toString(): String = "{" + parameterName + "}"

Expand Down
Loading

0 comments on commit 022994d

Please sign in to comment.