Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Neo4jsource
Commits
7053ba0f
Commit
7053ba0f
authored
13 years ago
by
Andres Taylor
Browse files
Options
Download
Email Patches
Plain Diff
When working from Java, Java iterables are used, instead of Scala Seq's
parent
0fc37c71
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
community/cypher/src/docs/dev/images/friends-of-friends2.psd
+0
-0
community/cypher/src/docs/dev/images/friends-of-friends2.psd
community/cypher/src/main/scala/org/neo4j/cypher/ExecutionResult.scala
+11
-3
...her/src/main/scala/org/neo4j/cypher/ExecutionResult.scala
community/cypher/src/test/java/org/neo4j/cypher/javacompat/JavaExecutionEngineTests.java
+71
-61
...org/neo4j/cypher/javacompat/JavaExecutionEngineTests.java
with
82 additions
and
64 deletions
+82
-64
community/cypher/src/docs/dev/images/friends-of-friends2.psd
0 → 100644
+
0
-
0
View file @
7053ba0f
File added
This diff is collapsed.
Click to expand it.
community/cypher/src/main/scala/org/neo4j/cypher/ExecutionResult.scala
+
11
-
3
View file @
7053ba0f
...
...
@@ -24,6 +24,7 @@ import scala.collection.JavaConverters._
import
org.neo4j.graphdb.
{
PropertyContainer
,
Relationship
,
NotFoundException
,
Node
}
import
collection.Traversable
trait
ExecutionResult
extends
Traversable
[
Map
[
String
,
Any
]]
with
StringExtras
{
val
symbols
:
SymbolTable
...
...
@@ -31,7 +32,7 @@ trait ExecutionResult extends Traversable[Map[String, Any]] with StringExtras {
def
javaColumns
:
java.util.List
[
String
]
=
columns
.
asJava
def
javaColumnAs
[
T
](
column
:
String
)
=
columnAs
[
T
](
column
).
asJava
def
javaColumnAs
[
T
](
column
:
String
)
=
columnAs
[
T
](
column
).
map
(
x
=>
makeValueJavaCompatible
(
x
).
asInstanceOf
[
T
]).
asJava
def
columnAs
[
T
](
column
:
String
)
:
Iterator
[
T
]
=
{
this
.
map
(
m
=>
{
...
...
@@ -40,9 +41,16 @@ trait ExecutionResult extends Traversable[Map[String, Any]] with StringExtras {
}).
toIterator
}
def
javaIterator
:
java.util.Iterator
[
java.util.Map
[
String
,
Any
]]
=
this
.
map
(
m
=>
m
.
asJava
).
toIterator
.
asJava
def
makeValueJavaCompatible
(
value
:
Any
)
:
Any
=
value
match
{
case
iter
:
Seq
[
_
]
=>
iter
.
asJava
case
x
=>
x
}
def
javaIterator
:
java.util.Iterator
[
java.util.Map
[
String
,
Any
]]
=
this
.
map
(
m
=>
{
m
.
map
(
kv
=>
kv
.
_1
->
makeValueJavaCompatible
(
kv
.
_2
)).
asJava
}).
toIterator
.
asJava
def
calculateColumnSizes
(
result
:
Seq
[
Map
[
String
,
Any
]])
:
Map
[
String
,
Int
]
=
{
def
calculateColumnSizes
(
result
:
Seq
[
Map
[
String
,
Any
]])
:
Map
[
String
,
Int
]
=
{
val
columnSizes
=
new
scala
.
collection
.
mutable
.
HashMap
[
String
,
Int
]
++
columns
.
map
(
name
=>
name
->
name
.
size
)
result
.
foreach
((
m
)
=>
{
...
...
This diff is collapsed.
Click to expand it.
community/cypher/src/test/java/org/neo4j/cypher/javacompat/JavaExecutionEngineTests.java
+
71
-
61
View file @
7053ba0f
...
...
@@ -22,6 +22,7 @@ package org.neo4j.cypher.javacompat;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.neo4j.cypher.commands.Query
;
import
org.neo4j.graphdb.DynamicRelationshipType
;
import
org.neo4j.graphdb.GraphDatabaseService
;
import
org.neo4j.graphdb.Node
;
import
org.neo4j.graphdb.Transaction
;
...
...
@@ -33,12 +34,10 @@ import java.util.*;
import
static
java
.
util
.
Arrays
.
asList
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
junit
.
matchers
.
JUnitMatchers
.
containsString
;
import
static
org
.
junit
.
matchers
.
JUnitMatchers
.
hasItem
;
import
static
org
.
junit
.
matchers
.
JUnitMatchers
.*;
import
static
org
.
neo4j
.
helpers
.
collection
.
IteratorUtil
.
asIterable
;
public
class
JavaExecutionEngineTests
{
public
class
JavaExecutionEngineTests
{
private
GraphDatabaseService
db
;
private
ExecutionEngine
engine
;
...
...
@@ -46,9 +45,10 @@ public class JavaExecutionEngineTests
private
Node
johanNode
;
private
Node
michaelaNode
;
@Before
public
void
setUp
()
throws
IOException
{
@Before
public
void
setUp
()
throws
IOException
{
db
=
new
ImpermanentGraphDatabase
();
engine
=
new
ExecutionEngine
(
db
);
engine
=
new
ExecutionEngine
(
db
);
Transaction
tx
=
db
.
beginTx
();
andreasNode
=
db
.
createNode
();
johanNode
=
db
.
createNode
();
...
...
@@ -66,123 +66,126 @@ public class JavaExecutionEngineTests
tx
.
finish
();
}
private
void
index
(
Node
n
)
{
private
void
index
(
Node
n
)
{
db
.
index
().
forNodes
(
"people"
).
add
(
n
,
"name"
,
n
.
getProperty
(
"name"
));
}
@Test
public
void
exampleQuery
()
throws
Exception
{
public
void
exampleQuery
()
throws
Exception
{
// START SNIPPET: JavaQuery
ExecutionEngine
engine
=
new
ExecutionEngine
(
db
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node(0) where 1=1 return n"
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node(0) where 1=1 return n"
);
assertThat
(
result
.
columns
(),
hasItem
(
"n"
)
);
Iterator
<
Node
>
n_column
=
result
.
columnAs
(
"n"
);
assertThat
(
asIterable
(
n_column
),
hasItem
(
db
.
getNodeById
(
0
))
);
assertThat
(
result
.
toString
(),
containsString
(
"Node[0]"
)
);
assertThat
(
result
.
columns
(),
hasItem
(
"n"
)
);
Iterator
<
Node
>
n_column
=
result
.
columnAs
(
"n"
);
assertThat
(
asIterable
(
n_column
),
hasItem
(
db
.
getNodeById
(
0
)));
assertThat
(
result
.
toString
(),
containsString
(
"Node[0]"
));
// END SNIPPET: JavaQuery
}
@Test
public
void
exampleConsole
()
throws
Exception
{
public
void
shouldBeAbleToEmitJavaIterables
()
throws
Exception
{
makeFriends
(
michaelaNode
,
andreasNode
);
makeFriends
(
michaelaNode
,
johanNode
);
ExecutionEngine
engine
=
new
ExecutionEngine
(
db
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node(0) match n-->friend return collect(friend)"
);
Iterable
<
Node
>
friends
=
(
Iterable
<
Node
>)
result
.
columnAs
(
"collect(friend)"
).
next
();
assertThat
(
friends
,
hasItems
(
andreasNode
,
johanNode
));
}
@Test
public
void
exampleConsole
()
throws
Exception
{
Query
query
=
CypherParser
.
parseConsole
(
""
+
//START SNIPPET: Identifier
"start n=node(0) return n.NOT_EXISTING"
//END SNIPPET: Identifier
);
//START SNIPPET: Identifier
"start n=node(0) return n.NOT_EXISTING"
//END SNIPPET: Identifier
);
ExecutionResult
result
=
engine
.
execute
(
query
);
assertThat
(
result
.
columns
(),
hasItem
(
"n.NOT_EXISTING"
)
);
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.NOT_EXISTING"
);
assertNull
(
n_column
.
next
()
);
assertThat
(
result
.
toString
(),
containsString
(
"null"
)
);
assertThat
(
result
.
columns
(),
hasItem
(
"n.NOT_EXISTING"
)
);
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.NOT_EXISTING"
);
assertNull
(
n_column
.
next
());
assertThat
(
result
.
toString
(),
containsString
(
"null"
));
}
@Test
public
void
exampleWithParameterForNodeId
()
throws
Exception
{
// START SNIPPET: exampleWithParameterForNodeId
public
void
exampleWithParameterForNodeId
()
throws
Exception
{
// START SNIPPET: exampleWithParameterForNodeId
Map
<
String
,
Object
>
params
=
new
HashMap
<
String
,
Object
>();
params
.
put
(
"id"
,
0
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node({id}) return n.name"
,
params
);
// END SNIPPET: exampleWithParameterForNodeId
// END SNIPPET: exampleWithParameterForNodeId
assertThat
(
result
.
columns
(),
hasItem
(
"n.name"
)
);
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.name"
);
assertThat
(
result
.
columns
(),
hasItem
(
"n.name"
)
);
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.name"
);
assertEquals
(
"Michaela"
,
n_column
.
next
());
}
@Test
public
void
exampleWithParameterForMultipleNodeIds
()
throws
Exception
{
// START SNIPPET: exampleWithParameterForMultipleNodeIds
public
void
exampleWithParameterForMultipleNodeIds
()
throws
Exception
{
// START SNIPPET: exampleWithParameterForMultipleNodeIds
Map
<
String
,
Object
>
params
=
new
HashMap
<
String
,
Object
>();
params
.
put
(
"id"
,
Arrays
.
asList
(
0
,
1
,
2
));
params
.
put
(
"id"
,
Arrays
.
asList
(
0
,
1
,
2
));
ExecutionResult
result
=
engine
.
execute
(
"start n=node({id}) return n.name"
,
params
);
// END SNIPPET: exampleWithParameterForMultipleNodeIds
// END SNIPPET: exampleWithParameterForMultipleNodeIds
assertEquals
(
asList
(
"Michaela"
,
"Andreas"
,
"Johan"
),
this
.<
String
>
toList
(
result
,
"n.name"
)
);
assertEquals
(
asList
(
"Michaela"
,
"Andreas"
,
"Johan"
),
this
.<
String
>
toList
(
result
,
"n.name"
));
}
private
<
T
>
List
<
T
>
toList
(
ExecutionResult
result
,
String
column
)
{
private
<
T
>
List
<
T
>
toList
(
ExecutionResult
result
,
String
column
)
{
List
<
T
>
results
=
new
ArrayList
<
T
>();
IteratorUtil
.
addToCollection
(
result
.<
T
>
columnAs
(
column
),
results
);
IteratorUtil
.
addToCollection
(
result
.<
T
>
columnAs
(
column
),
results
);
return
results
;
}
@Test
public
void
exampleWithStringLiteralAsParameter
()
throws
Exception
{
// START SNIPPET: exampleWithStringLiteralAsParameter
public
void
exampleWithStringLiteralAsParameter
()
throws
Exception
{
// START SNIPPET: exampleWithStringLiteralAsParameter
Map
<
String
,
Object
>
params
=
new
HashMap
<
String
,
Object
>();
params
.
put
(
"name"
,
"Johan"
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node(0,1,2) where n.name = {name} return n"
,
params
);
// END SNIPPET: exampleWithStringLiteralAsParameter
// END SNIPPET: exampleWithStringLiteralAsParameter
assertEquals
(
asList
(
johanNode
),
this
.<
Node
>
toList
(
result
,
"n"
)
);
assertEquals
(
asList
(
johanNode
),
this
.<
Node
>
toList
(
result
,
"n"
));
}
@Test
public
void
exampleWithParametersForIndexKeyAndValue
()
throws
Exception
{
// START SNIPPET: exampleWithParametersForIndexKeyAndValue
public
void
exampleWithParametersForIndexKeyAndValue
()
throws
Exception
{
// START SNIPPET: exampleWithParametersForIndexKeyAndValue
Map
<
String
,
Object
>
params
=
new
HashMap
<
String
,
Object
>();
params
.
put
(
"key"
,
"name"
);
params
.
put
(
"value"
,
"Michaela"
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node:people({key} = {value}) return n"
,
params
);
// END SNIPPET: exampleWithParametersForIndexKeyAndValue
// END SNIPPET: exampleWithParametersForIndexKeyAndValue
assertEquals
(
asList
(
michaelaNode
),
this
.<
Node
>
toList
(
result
,
"n"
)
);
assertEquals
(
asList
(
michaelaNode
),
this
.<
Node
>
toList
(
result
,
"n"
));
}
@Test
public
void
exampleWithParametersForQuery
()
throws
Exception
{
// START SNIPPET: exampleWithParametersForQuery
public
void
exampleWithParametersForQuery
()
throws
Exception
{
// START SNIPPET: exampleWithParametersForQuery
Map
<
String
,
Object
>
params
=
new
HashMap
<
String
,
Object
>();
params
.
put
(
"query"
,
"name:Andreas"
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node:people({query}) return n"
,
params
);
// END SNIPPET: exampleWithParametersForQuery
// END SNIPPET: exampleWithParametersForQuery
assertEquals
(
asList
(
andreasNode
),
this
.<
Node
>
toList
(
result
,
"n"
)
);
assertEquals
(
asList
(
andreasNode
),
this
.<
Node
>
toList
(
result
,
"n"
));
}
@Test
public
void
exampleWithParameterForNode
()
throws
Exception
{
// START SNIPPET: exampleWithParameterForNode
public
void
exampleWithParameterForNode
()
throws
Exception
{
// START SNIPPET: exampleWithParameterForNode
Map
<
String
,
Object
>
params
=
new
HashMap
<
String
,
Object
>();
params
.
put
(
"node"
,
andreasNode
);
ExecutionResult
result
=
engine
.
execute
(
"start n=node({node}) return n.name"
,
params
);
// END SNIPPET: exampleWithParameterForNode
// END SNIPPET: exampleWithParameterForNode
assertThat
(
result
.
columns
(),
hasItem
(
"n.name"
)
);
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.name"
);
assertThat
(
result
.
columns
(),
hasItem
(
"n.name"
)
);
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.name"
);
assertEquals
(
"Andreas"
,
n_column
.
next
());
}
...
...
@@ -200,4 +203,11 @@ public class JavaExecutionEngineTests
Iterator
<
Object
>
n_column
=
result
.
columnAs
(
"n.name"
);
assertEquals
(
"Andreas"
,
n_column
.
next
());
}
private
void
makeFriends
(
Node
a
,
Node
b
)
{
Transaction
tx
=
db
.
beginTx
();
a
.
createRelationshipTo
(
b
,
DynamicRelationshipType
.
withName
(
"friend"
));
tx
.
success
();
tx
.
finish
();
}
}
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment
Menu
Projects
Groups
Snippets
Help