cadet 0.0.7-java → 0.0.8-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -0
- data/README.md +18 -41
- data/lib/cadet/batch_inserter/node.rb +18 -4
- data/lib/cadet/batch_inserter/session.rb +14 -17
- data/lib/cadet/batch_inserter/transaction.rb +16 -0
- data/lib/cadet/cadet_index/index.rb +5 -1
- data/lib/cadet/cadet_index/index_provider.rb +3 -3
- data/lib/cadet/direction.rb +6 -0
- data/lib/cadet/dsl.rb +36 -0
- data/lib/cadet/dynamic_label.rb +2 -2
- data/lib/cadet/dynamic_relationshiptype.rb +1 -1
- data/lib/cadet/helpers.rb +15 -0
- data/lib/cadet/node.rb +29 -8
- data/lib/cadet/node_relationships.rb +20 -0
- data/lib/cadet/node_traverser.rb +33 -0
- data/lib/cadet/path_traverser.rb +38 -0
- data/lib/cadet/relationship.rb +17 -0
- data/lib/cadet/session.rb +18 -17
- data/lib/cadet/test/session.rb +1 -12
- data/lib/cadet/transaction.rb +27 -0
- data/lib/cadet/version.rb +1 -1
- data/lib/cadet.rb +15 -6
- data/spec/spec_helper.rb +45 -2
- data/spec/unit/batch_insert_spec.rb +144 -0
- data/spec/unit/cadet_spec.rb +76 -6
- metadata +13 -3
- data/lib/cadet/relationship_traverser.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a1efba14fa6c1668a60ffd431f8c37d17cf22027
|
4
|
+
data.tar.gz: 40606929349bf698a9a7fde8710d7bbc48dc97b2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 234bfad83bb8ab3d621ef663c7f8ee2324b0f6ab2868de827f52b979fc117819d09cdf071c84f0176d40286bd7250205faeedbc4566f1dbf79554a6457f4ba06
|
7
|
+
data.tar.gz: 4ec3008adb3781e3b76ba70ed3c9038ad60da616218e9362d1602dcb257c4637212d1daa2a32a1b5a9179d3242843d113717d2a1383506bbee0d47af5ea21dc4
|
data/.travis.yml
ADDED
data/README.md
CHANGED
@@ -1,52 +1,29 @@
|
|
1
|
-
-
|
2
|
-
- support for batchinsert mode!
|
3
|
-
just open the database with Cadet::BatchInserter::Session.open() instead of Cadet::Session.open()!
|
4
|
-
this no-ops transaction, simply yielding to the supplied block.
|
1
|
+
[![Build Status](https://travis-ci.org/karabijavad/cadet.png?branch=master)](https://travis-ci.org/karabijavad/cadet)
|
5
2
|
|
6
|
-
|
7
|
-
|
8
|
-
1. Gemfile
|
9
|
-
|
10
|
-
```ruby
|
11
|
-
gem 'cadet'
|
12
|
-
```
|
13
|
-
2. ``` bundle install ```
|
14
|
-
3. cadet-example.rb
|
3
|
+
Use neo4j via jruby! Nothing else needed, simply add this gem to get the power of embedded neo4j!
|
15
4
|
|
16
|
-
|
5
|
+
* Batchinsert mode supported.
|
17
6
|
|
18
|
-
require 'cadet'
|
19
|
-
require 'yaml'
|
20
7
|
|
21
|
-
|
22
|
-
|
23
|
-
#open the database, db is now used to interact with the database
|
24
|
-
db = Cadet::Session.open("neo4j-community-2.0.0/data/graph.db")
|
25
|
-
|
26
|
-
db.transaction do
|
27
|
-
["Legislator", "Party", "Gender", "State"].each {|v| db.constraint v, "name"}
|
28
|
-
end
|
8
|
+
super simple. you dont even need to download neo4j.
|
29
9
|
|
30
|
-
data.each do |leg|
|
31
|
-
#begin a transaction. the transaction will automatically finish at the end of the provided block
|
32
|
-
db.transaction do
|
33
10
|
|
34
|
-
|
35
|
-
l = db.get_a_Legislator_by_name leg["name"]["official_full"]
|
36
|
-
p = db.get_a_Party_by_name leg["terms"].first["party"]
|
37
|
-
g = db.get_a_Gender_by_name leg["bio"]["gender"]
|
38
|
-
s = db.get_a_State_by_name leg["terms"].first["state"]
|
11
|
+
```ruby
|
39
12
|
|
40
|
-
|
41
|
-
l.outgoing(:gender) << g
|
42
|
-
l.outgoing(:represents) << s
|
43
|
-
end
|
44
|
-
end
|
13
|
+
require 'cadet'
|
45
14
|
|
46
|
-
|
47
|
-
|
15
|
+
#open the database
|
16
|
+
db = Cadet::Session.open("neo4j-community-2.0.0/data/graph.db").dsl do
|
17
|
+
#begin a transaction. the transaction will automatically finish at the end of the provided block
|
18
|
+
transaction do
|
19
|
+
Person_by_name("Javad").lives_in_to City_by_name("Chicago")
|
20
|
+
end
|
21
|
+
end
|
48
22
|
|
23
|
+
#close the database
|
24
|
+
db.close
|
49
25
|
|
26
|
+
```
|
50
27
|
|
51
|
-
|
52
|
-
|
28
|
+
Batch insert mode can be used by simply using Cadet::BatchInserter::Session instead of Cadet::Session!
|
29
|
+
None of your code needs to change.
|
@@ -1,18 +1,32 @@
|
|
1
1
|
module Cadet
|
2
2
|
module BatchInserter
|
3
3
|
class Node < Cadet::Node
|
4
|
-
include_package "org.neo4j.graphdb"
|
5
4
|
|
6
5
|
def create_outgoing(to, type, properties = {})
|
7
|
-
@db.createRelationship(@underlying, to.underlying, DynamicRelationshipType.withName(type), properties)
|
6
|
+
Relationship.new @db.createRelationship(@underlying, to.underlying, DynamicRelationshipType.withName(type), properties)
|
8
7
|
end
|
9
8
|
|
10
9
|
def []= (property, value)
|
11
|
-
@db.setNodeProperty @underlying, property.
|
10
|
+
@db.setNodeProperty @underlying, property.to_java_string, value
|
12
11
|
end
|
13
12
|
|
14
13
|
def [] (property)
|
15
|
-
@db.getNodeProperties(@underlying)[property.
|
14
|
+
@db.getNodeProperties(@underlying)[property.to_java_string]
|
15
|
+
end
|
16
|
+
|
17
|
+
def == other_node
|
18
|
+
@underlying == other_node
|
19
|
+
end
|
20
|
+
|
21
|
+
def outgoing(type)
|
22
|
+
NodeTraverser.new(self, :outgoing, type)
|
23
|
+
end
|
24
|
+
|
25
|
+
def each_relationship(direction, type)
|
26
|
+
# not implemented in batch inserter mode. though, it could be done.
|
27
|
+
# the assumption is that this shouldnt be necessary, as batch inserter mode
|
28
|
+
# should be about inserting data, not querying data
|
29
|
+
raise NotImplementedError
|
16
30
|
end
|
17
31
|
|
18
32
|
end
|
@@ -1,14 +1,10 @@
|
|
1
1
|
module Cadet
|
2
2
|
module BatchInserter
|
3
3
|
class Session < Cadet::Session
|
4
|
-
include_package "org.neo4j.graphdb"
|
5
|
-
include_package "org.neo4j.unsafe.batchinsert"
|
6
|
-
include_package "org.neo4j.index.impl.lucene"
|
7
|
-
include_package "org.neo4j.helpers.collection"
|
8
4
|
|
9
5
|
def initialize(db)
|
10
|
-
@
|
11
|
-
|
6
|
+
@index_provider = CadetIndex::IndexProvider.new(db)
|
7
|
+
super db
|
12
8
|
end
|
13
9
|
|
14
10
|
def close
|
@@ -16,32 +12,33 @@ module Cadet
|
|
16
12
|
super
|
17
13
|
end
|
18
14
|
|
19
|
-
def self.open(location)
|
20
|
-
new BatchInserters.inserter(location)
|
21
|
-
end
|
22
|
-
|
23
|
-
def transaction
|
24
|
-
yield
|
15
|
+
def self.open(location, config = {})
|
16
|
+
new org.neo4j.unsafe.batchinsert.BatchInserters.inserter(location, config)
|
25
17
|
end
|
26
18
|
|
27
19
|
def constraint(label, property)
|
28
20
|
@db.createDeferredConstraint(DynamicLabel.label(label))
|
29
|
-
.assertPropertyIsUnique(property
|
21
|
+
.assertPropertyIsUnique(property)
|
30
22
|
.create()
|
31
23
|
end
|
32
24
|
|
33
25
|
def find_node(label, property, value)
|
34
26
|
index = @index_provider.nodeIndex(label)
|
35
27
|
|
36
|
-
(
|
28
|
+
(node = index.get(property.to_sym, value).first) ?
|
37
29
|
Node.new(node, @db) : nil
|
38
30
|
end
|
39
31
|
|
40
|
-
def create_node(label,
|
41
|
-
Node.new(@db.createNode({
|
42
|
-
|
32
|
+
def create_node(label, properties, indexing_property = nil)
|
33
|
+
Node.new(@db.createNode(properties.inject({}){|result,(k,v)| result[k.to_java_string] = v; result}, DynamicLabel.label(label)), @db).tap do |n|
|
34
|
+
@index_provider.nodeIndex(label).add(n.underlying, indexing_property.to_sym, properties[indexing_property]) if indexing_property
|
43
35
|
end
|
44
36
|
end
|
37
|
+
|
38
|
+
def get_transaction
|
39
|
+
Cadet::BatchInserter::Transaction.new(self)
|
40
|
+
end
|
41
|
+
|
45
42
|
end
|
46
43
|
end
|
47
44
|
end
|
@@ -7,23 +7,27 @@ module Cadet
|
|
7
7
|
@index = {}
|
8
8
|
@lucene_index = lucene_index
|
9
9
|
end
|
10
|
+
|
10
11
|
def add(node, property, value)
|
11
12
|
@index[property] ||= {}
|
12
13
|
@index[property][value] = node
|
13
14
|
end
|
15
|
+
|
14
16
|
def get(property, value)
|
15
17
|
@index[property] ||= {}
|
16
18
|
[@index[property][value]]
|
17
19
|
end
|
20
|
+
|
18
21
|
def flush
|
19
22
|
index = @lucene_index.nodeIndex(@name, @type)
|
20
23
|
|
21
24
|
@index.each do |property, mappings|
|
22
25
|
mappings.each do |value, node|
|
23
|
-
index.add(node, {property.
|
26
|
+
index.add(node, {property.to_java_string => value})
|
24
27
|
end
|
25
28
|
end
|
26
29
|
end
|
30
|
+
|
27
31
|
end
|
28
32
|
end
|
29
33
|
end
|
@@ -1,14 +1,14 @@
|
|
1
1
|
module Cadet
|
2
2
|
module CadetIndex
|
3
3
|
class IndexProvider
|
4
|
-
|
4
|
+
|
5
5
|
def initialize(db)
|
6
6
|
@db = db
|
7
7
|
@indexes = {}
|
8
|
-
@lucene_index = LuceneBatchInserterIndexProviderNewImpl.new(db)
|
8
|
+
@lucene_index = org.neo4j.index.impl.lucene.LuceneBatchInserterIndexProviderNewImpl.new(db)
|
9
9
|
end
|
10
10
|
def nodeIndex(name, type = {"type" => "exact"})
|
11
|
-
@indexes[name] ||=
|
11
|
+
@indexes[name.to_sym] ||= CadetIndex::Index.new(@lucene_index, name.to_sym, type)
|
12
12
|
end
|
13
13
|
def shutdown
|
14
14
|
@indexes.each do |name, index|
|
data/lib/cadet/dsl.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Cadet
|
2
|
+
class DSL
|
3
|
+
def initialize(db)
|
4
|
+
@db = db
|
5
|
+
end
|
6
|
+
|
7
|
+
def method_missing(name, *args, &block)
|
8
|
+
case name
|
9
|
+
when /^([A-z_]*)_by_([A-z_]*)/
|
10
|
+
self.class.class_eval "
|
11
|
+
def #{name}(value)
|
12
|
+
@db.get_node :#{$1}, :#{$2}, value
|
13
|
+
end"
|
14
|
+
return self.send(name, *args, &block)
|
15
|
+
|
16
|
+
when /^create_([A-z_]*)_on_([A-z_]*)/.match(name)
|
17
|
+
self.class.class_eval "
|
18
|
+
def #{name}(value)
|
19
|
+
@db.create_node :#{$1}, value, :#{$2}
|
20
|
+
end"
|
21
|
+
return self.send(name, *args, &block)
|
22
|
+
|
23
|
+
when /^create_([A-z_]*)/
|
24
|
+
self.class.class_eval "
|
25
|
+
def #{name}(value = {})
|
26
|
+
@db.create_node :#{$1}, value
|
27
|
+
end"
|
28
|
+
return self.send(name, *args, &block)
|
29
|
+
|
30
|
+
else
|
31
|
+
return @db.send(name, *args, &block)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
data/lib/cadet/dynamic_label.rb
CHANGED
@@ -3,7 +3,7 @@ module Cadet
|
|
3
3
|
@dynamic_relationship_types = {}
|
4
4
|
|
5
5
|
def self.withName(name)
|
6
|
-
@dynamic_relationship_types[name] ||= org.neo4j.graphdb.DynamicRelationshipType.withName(name
|
6
|
+
@dynamic_relationship_types[name.to_sym] ||= org.neo4j.graphdb.DynamicRelationshipType.withName(name)
|
7
7
|
end
|
8
8
|
end
|
9
9
|
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# This is to fix a (bug?) in jruby.
|
2
|
+
# typically, when a ruby symbol is passed into a native java function,
|
3
|
+
# it should be converted to a java string by jruby.
|
4
|
+
# unfortunately, this conversion does not seem to take place when the native
|
5
|
+
# java function is overloaded.
|
6
|
+
# This method allows me to specify :symbol.to_java_string,
|
7
|
+
# so that I do not need to create a Ruby string from a symbol,
|
8
|
+
# just so that i can get a native java string
|
9
|
+
|
10
|
+
class Symbol
|
11
|
+
JString = java.lang.String
|
12
|
+
def to_java_string
|
13
|
+
to_java(JString)
|
14
|
+
end
|
15
|
+
end
|
data/lib/cadet/node.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Cadet
|
2
2
|
class Node
|
3
3
|
attr_accessor :underlying
|
4
|
-
include_package "org.neo4j.graphdb"
|
5
4
|
|
6
5
|
def initialize(node, db = nil)
|
7
6
|
@db = db
|
@@ -17,28 +16,50 @@ module Cadet
|
|
17
16
|
end
|
18
17
|
|
19
18
|
def []= (property, value)
|
20
|
-
@underlying.setProperty(property.
|
19
|
+
@underlying.setProperty(property.to_java_string, value)
|
21
20
|
end
|
22
21
|
|
23
22
|
def [] (property)
|
24
|
-
@underlying.getProperty(property.
|
23
|
+
@underlying.getProperty(property.to_java_string)
|
25
24
|
end
|
26
25
|
|
27
|
-
def
|
28
|
-
@underlying.getRelationships(direction, DynamicRelationshipType.withName(type))
|
26
|
+
def each_relationship(direction, type)
|
27
|
+
@underlying.getRelationships(Directions[direction], DynamicRelationshipType.withName(type)).each do |rel|
|
28
|
+
yield Relationship.new(rel)
|
29
|
+
end
|
29
30
|
end
|
31
|
+
|
30
32
|
def create_outgoing(to, type)
|
31
|
-
@underlying.createRelationshipTo(to.underlying, DynamicRelationshipType.withName(type))
|
33
|
+
Relationship.new @underlying.createRelationshipTo(to.underlying, DynamicRelationshipType.withName(type))
|
32
34
|
end
|
33
35
|
def outgoing(type)
|
34
|
-
|
36
|
+
NodeTraverser.new(self, :outgoing, type)
|
35
37
|
end
|
36
38
|
def incoming(type)
|
37
|
-
|
39
|
+
NodeTraverser.new(self, :incoming, type)
|
40
|
+
end
|
41
|
+
|
42
|
+
def outgoing_rels(type)
|
43
|
+
NodeRelationships.new(self, :outgoing, type)
|
44
|
+
end
|
45
|
+
def incoming_rels(type)
|
46
|
+
NodeRelationships.new(self, :incoming, type)
|
38
47
|
end
|
39
48
|
|
40
49
|
def == other_node
|
41
50
|
@underlying.getId == other_node.underlying.getId
|
42
51
|
end
|
52
|
+
|
53
|
+
def method_missing(name, *args, &block)
|
54
|
+
if match = /([A-z_]*)_to$/.match(name)
|
55
|
+
self.class.class_eval "
|
56
|
+
def #{name}(value)
|
57
|
+
create_outgoing(value, :#{match.captures[0]})
|
58
|
+
end
|
59
|
+
"
|
60
|
+
self.send(name, *args, &block)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
43
64
|
end
|
44
65
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Cadet
|
2
|
+
class NodeRelationships
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(node, direction, type)
|
6
|
+
@node = node
|
7
|
+
@type = type
|
8
|
+
@direction = direction
|
9
|
+
end
|
10
|
+
|
11
|
+
def each &block
|
12
|
+
@node.each_relationship(@direction, @type, &block)
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
self.to_a == other.to_a
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Cadet
|
2
|
+
class NodeTraverser
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(node, direction, type)
|
6
|
+
@node = node
|
7
|
+
@type = type
|
8
|
+
@direction = direction
|
9
|
+
end
|
10
|
+
|
11
|
+
def each
|
12
|
+
@node.each_relationship(@direction, @type) do |rel|
|
13
|
+
yield rel.get_other_node(@node)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def << (othernode)
|
18
|
+
@direction == :outgoing ? @node.create_outgoing(othernode, @type) : othernode.create_outgoing(@node, @type)
|
19
|
+
end
|
20
|
+
|
21
|
+
def outgoing(type)
|
22
|
+
PathTraverser.new to_a, :outgoing, type
|
23
|
+
end
|
24
|
+
def incoming(type)
|
25
|
+
PathTraverser.new to_a, :incoming, type
|
26
|
+
end
|
27
|
+
|
28
|
+
def ==(other)
|
29
|
+
self.to_a == other.to_a
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Cadet
|
2
|
+
class PathTraverser
|
3
|
+
include Enumerable
|
4
|
+
|
5
|
+
def initialize(nodes, direction, type)
|
6
|
+
@nodes = nodes
|
7
|
+
@direction = direction
|
8
|
+
@type = type
|
9
|
+
end
|
10
|
+
|
11
|
+
def each
|
12
|
+
if @direction == :outgoing
|
13
|
+
@nodes.each do |n|
|
14
|
+
n.outgoing(@type).each do |o|
|
15
|
+
yield o
|
16
|
+
end
|
17
|
+
end
|
18
|
+
else
|
19
|
+
@nodes.each do |n|
|
20
|
+
n.incoming(@type).each do |o|
|
21
|
+
yield o
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def outgoing(type)
|
28
|
+
PathTraverser.new(to_a, :outgoing, type)
|
29
|
+
end
|
30
|
+
def incoming(type)
|
31
|
+
PathTraverser.new(to_a, :incoming, type)
|
32
|
+
end
|
33
|
+
|
34
|
+
def ==(other)
|
35
|
+
self.to_a == other.to_a
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module Cadet
|
2
|
+
class Relationship
|
3
|
+
attr_accessor :underlying
|
4
|
+
|
5
|
+
def initialize(relationship)
|
6
|
+
@underlying = relationship
|
7
|
+
end
|
8
|
+
|
9
|
+
def == other_rel
|
10
|
+
@underlying.getId == other_rel.underlying.getId
|
11
|
+
end
|
12
|
+
|
13
|
+
def get_other_node(node)
|
14
|
+
Node.new @underlying.getOtherNode(node.underlying)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/lib/cadet/session.rb
CHANGED
@@ -1,39 +1,43 @@
|
|
1
1
|
module Cadet
|
2
2
|
class Session
|
3
|
-
include_package "org.neo4j.graphdb"
|
4
|
-
include_package "org.neo4j.graphdb.factory"
|
5
|
-
include_package "org.neo4j.unsafe.batchinsert"
|
6
|
-
include_package "org.neo4j.helpers.collection"
|
7
|
-
|
8
3
|
def initialize(db)
|
9
4
|
@db = db
|
10
5
|
end
|
11
6
|
|
12
7
|
def self.open(location)
|
13
|
-
new GraphDatabaseFactory.new.newEmbeddedDatabase(location)
|
8
|
+
new org.neo4j.graphdb.factory.GraphDatabaseFactory.new.newEmbeddedDatabase(location)
|
14
9
|
end
|
15
10
|
def close
|
16
11
|
@db.shutdown
|
17
12
|
end
|
18
13
|
|
19
|
-
def create_node(label,
|
14
|
+
def create_node(label, properties, indexing_property = nil)
|
20
15
|
Node.new(@db.createNode).tap do |n|
|
21
16
|
n.add_label label
|
22
|
-
n[
|
17
|
+
properties.each { |prop, val| n[prop] = val }
|
23
18
|
end
|
24
19
|
end
|
25
20
|
|
26
21
|
def find_node(label, property, value)
|
27
|
-
( node = IteratorUtil.firstOrNull(@db.findNodesByLabelAndProperty(DynamicLabel.label(label), property, value)) ) ?
|
22
|
+
( node = org.neo4j.helpers.collection.IteratorUtil.firstOrNull(@db.findNodesByLabelAndProperty(DynamicLabel.label(label), property, value)) ) ?
|
28
23
|
Node.new(node) : nil
|
29
24
|
end
|
30
25
|
|
31
26
|
def get_node(label, property, value)
|
32
|
-
find_node(label, property, value) || create_node(label, property,
|
27
|
+
find_node(label, property, value) || create_node(label, {property.to_sym => value}, property)
|
28
|
+
end
|
29
|
+
|
30
|
+
def get_transaction
|
31
|
+
Transaction.new(self)
|
32
|
+
end
|
33
|
+
|
34
|
+
def dsl(&block)
|
35
|
+
DSL.new(self).instance_exec(self, &block)
|
36
|
+
self
|
33
37
|
end
|
34
38
|
|
35
39
|
def transaction
|
36
|
-
tx =
|
40
|
+
tx = get_transaction
|
37
41
|
begin
|
38
42
|
yield tx
|
39
43
|
tx.success
|
@@ -45,15 +49,12 @@ module Cadet
|
|
45
49
|
def constraint(label, property)
|
46
50
|
@db.schema
|
47
51
|
.constraintFor(DynamicLabel.label(label))
|
48
|
-
.assertPropertyIsUnique(property
|
52
|
+
.assertPropertyIsUnique(property)
|
49
53
|
.create()
|
50
54
|
end
|
51
55
|
|
52
|
-
def
|
53
|
-
|
54
|
-
return get_node match.captures[0], match.captures[1], args[0]
|
55
|
-
end
|
56
|
+
def begin_tx
|
57
|
+
@db.beginTx
|
56
58
|
end
|
57
|
-
|
58
59
|
end
|
59
60
|
end
|
data/lib/cadet/test/session.rb
CHANGED
@@ -1,20 +1,9 @@
|
|
1
1
|
module Cadet
|
2
2
|
module Test
|
3
3
|
class Session < Cadet::Session
|
4
|
-
include_package "org.neo4j.graphdb"
|
5
|
-
include_package "org.neo4j.unsafe.batchinsert"
|
6
|
-
include_package "org.neo4j.index.impl.lucene"
|
7
|
-
include_package "org.neo4j.helpers.collection"
|
8
|
-
include_package "org.neo4j.graphdb.GraphDatabaseService"
|
9
|
-
include_package "org.neo4j.graphdb.Node"
|
10
|
-
include_package "org.neo4j.graphdb.Transaction"
|
11
|
-
include_package "org.neo4j.graphdb.factory.GraphDatabaseSettings"
|
12
|
-
include_package "org.neo4j.test"
|
13
|
-
|
14
|
-
|
15
4
|
|
16
5
|
def self.open
|
17
|
-
new TestGraphDatabaseFactory.new.newImpermanentDatabase
|
6
|
+
new org.neo4j.test.TestGraphDatabaseFactory.new.newImpermanentDatabase
|
18
7
|
end
|
19
8
|
|
20
9
|
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Cadet
|
2
|
+
class Transaction
|
3
|
+
attr_accessor :underlying
|
4
|
+
|
5
|
+
def initialize(session)
|
6
|
+
@session = session
|
7
|
+
@underlying = @session.begin_tx
|
8
|
+
end
|
9
|
+
|
10
|
+
def method_missing(name, *args, &block)
|
11
|
+
# for the "dsl".
|
12
|
+
# the transaction block is instance_eval'd by this class,
|
13
|
+
# so any missing methods are then sent to the session
|
14
|
+
# essentially means that session.blah can then be writen blah
|
15
|
+
@session.send(name, *args, &block)
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
def success
|
20
|
+
@underlying.success
|
21
|
+
end
|
22
|
+
def close
|
23
|
+
@underlying.close
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
data/lib/cadet/version.rb
CHANGED
data/lib/cadet.rb
CHANGED
@@ -1,20 +1,29 @@
|
|
1
1
|
require 'java'
|
2
2
|
|
3
|
-
require "neo4j/concurrentlinkedhashmap-lru-1.3.1.jar"
|
4
|
-
require "neo4j/lucene-core-3.6.2.jar"
|
5
|
-
require "neo4j/neo4j-kernel-2.0.1-tests.jar"
|
6
|
-
require "neo4j/neo4j-kernel-2.0.1.jar"
|
7
3
|
require "neo4j/neo4j-lucene-index-2.0.1.jar"
|
8
4
|
require "neo4j/neo4j-shell-2.0.1.jar"
|
9
5
|
require "neo4j/neo4j-kernel-2.0.1-tests.jar"
|
10
6
|
|
7
|
+
require 'cadet/helpers'
|
11
8
|
require 'cadet/session'
|
12
9
|
require 'cadet/node'
|
13
|
-
require 'cadet/
|
10
|
+
require 'cadet/relationship'
|
11
|
+
require 'cadet/direction'
|
12
|
+
require 'cadet/node_traverser'
|
13
|
+
require 'cadet/path_traverser'
|
14
|
+
require 'cadet/node_relationships'
|
15
|
+
require 'cadet/transaction'
|
16
|
+
|
17
|
+
require 'cadet/dsl'
|
18
|
+
|
14
19
|
require 'cadet/batch_inserter/session'
|
15
20
|
require 'cadet/batch_inserter/node'
|
21
|
+
require 'cadet/batch_inserter/transaction'
|
22
|
+
|
23
|
+
require 'cadet/test/session'
|
24
|
+
|
16
25
|
require 'cadet/cadet_index/index_provider'
|
17
26
|
require 'cadet/cadet_index/index'
|
27
|
+
|
18
28
|
require 'cadet/dynamic_relationshiptype'
|
19
29
|
require 'cadet/dynamic_label'
|
20
|
-
require 'cadet/test/session'
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,52 @@
|
|
1
1
|
require 'cadet'
|
2
|
+
require 'tmpdir'
|
2
3
|
|
3
|
-
def
|
4
|
-
|
4
|
+
def quick_test_neo4j
|
5
|
+
test_neo4j do |db|
|
5
6
|
db.transaction do |tx|
|
6
7
|
yield db, tx
|
7
8
|
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_neo4j
|
13
|
+
db = Cadet::Test::Session.open
|
14
|
+
yield db
|
8
15
|
db.close
|
9
16
|
end
|
17
|
+
|
18
|
+
def quick_normal_neo4j(at = nil)
|
19
|
+
at ||= Dir.mktmpdir
|
20
|
+
|
21
|
+
normal_neo4j(at) do |db|
|
22
|
+
db.transaction do
|
23
|
+
yield db
|
24
|
+
end
|
25
|
+
end
|
26
|
+
at
|
27
|
+
end
|
28
|
+
|
29
|
+
def normal_neo4j(at)
|
30
|
+
db = Cadet::Session.open(at)
|
31
|
+
yield db
|
32
|
+
db.close
|
33
|
+
end
|
34
|
+
|
35
|
+
def quick_batch_neo4j(at = nil)
|
36
|
+
at ||= Dir.mktmpdir
|
37
|
+
|
38
|
+
batch_neo4j(at) do |db|
|
39
|
+
yield db
|
40
|
+
end
|
41
|
+
at
|
42
|
+
end
|
43
|
+
|
44
|
+
def batch_neo4j(at)
|
45
|
+
db = Cadet::BatchInserter::Session.open(at)
|
46
|
+
yield db
|
47
|
+
db.close
|
48
|
+
end
|
49
|
+
|
50
|
+
def quick_batch_dsl_neo4j
|
51
|
+
yield quick_batch_neo4j.dsl
|
52
|
+
end
|
@@ -0,0 +1,144 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'tmpdir'
|
3
|
+
|
4
|
+
describe Cadet::BatchInserter do
|
5
|
+
it "should set a nodes property" do
|
6
|
+
tmpdir = quick_batch_neo4j do |db|
|
7
|
+
javad = db.get_node(:Person, :name, "Javad")
|
8
|
+
javad[:age] = 25
|
9
|
+
end
|
10
|
+
|
11
|
+
quick_normal_neo4j(tmpdir) do |db|
|
12
|
+
javad = db.get_node(:Person, :name, "Javad")
|
13
|
+
javad[:age].should == 25
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should create a relationship" do
|
18
|
+
tmpdir = quick_batch_neo4j do |db|
|
19
|
+
javad = db.get_node(:Person, :name, "Javad")
|
20
|
+
ellen = db.get_node(:Person, :name, "Ellen")
|
21
|
+
|
22
|
+
javad.outgoing(:knows) << ellen
|
23
|
+
end
|
24
|
+
|
25
|
+
quick_normal_neo4j(tmpdir) do |db|
|
26
|
+
javad = db.get_node(:Person, :name, "Javad")
|
27
|
+
ellen = db.get_node(:Person, :name, "Ellen")
|
28
|
+
|
29
|
+
javad.outgoing(:knows).should == [ellen]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should create a node with a set of properties, and index that node on a specified property" do
|
34
|
+
tmpdir = quick_batch_neo4j do |db|
|
35
|
+
javad = db.create_node(:Person, {name: "Javad", age: 25})
|
36
|
+
end
|
37
|
+
|
38
|
+
quick_normal_neo4j(tmpdir) do |db|
|
39
|
+
javad = db.get_node(:Person, :name, "Javad")
|
40
|
+
|
41
|
+
javad[:name].should == "Javad"
|
42
|
+
javad[:age].should == 25
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should retrieve the same node, for the same label-key-value" do
|
47
|
+
tmpdir = quick_batch_neo4j do |db|
|
48
|
+
javad = db.get_node(:Person, :name, "Javad")
|
49
|
+
ellen = db.get_node(:Person, :name, "Ellen")
|
50
|
+
|
51
|
+
javad.outgoing(:lives_in) << db.get_node(:City, :name, "Chicago")
|
52
|
+
ellen.outgoing(:lives_in) << db.get_node(:City, :name, "Chicago")
|
53
|
+
end
|
54
|
+
|
55
|
+
quick_normal_neo4j(tmpdir) do |db|
|
56
|
+
javad = db.get_node(:Person, :name, "Javad")
|
57
|
+
ellen = db.get_node(:Person, :name, "Ellen")
|
58
|
+
|
59
|
+
javad.outgoing(:lives_in).should == ellen.outgoing(:lives_in)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it "should work" do
|
64
|
+
tmpdir = quick_batch_neo4j do |db|
|
65
|
+
javad = db.get_node :Person, :name, "Javad"
|
66
|
+
ellen = db.get_node :Person, :name, "Ellen"
|
67
|
+
trunkclub = db.get_node :Company, :name, "Trunkclub"
|
68
|
+
chicago = db.get_node :City, :name, "Chicago"
|
69
|
+
us = db.get_node :Country, :name, "United States"
|
70
|
+
|
71
|
+
javad.outgoing(:works_at) << trunkclub
|
72
|
+
trunkclub.outgoing(:located_in) << chicago
|
73
|
+
ellen.outgoing(:lives_in) << chicago
|
74
|
+
chicago.outgoing(:country) << us
|
75
|
+
end
|
76
|
+
|
77
|
+
quick_normal_neo4j(tmpdir) do |db|
|
78
|
+
javad = db.get_node :Person, :name, "Javad"
|
79
|
+
ellen = db.get_node :Person, :name, "Ellen"
|
80
|
+
trunkclub = db.get_node :Company, :name, "Trunkclub"
|
81
|
+
chicago = db.get_node :City, :name, "Chicago"
|
82
|
+
us = db.get_node :Country, :name, "United States"
|
83
|
+
|
84
|
+
javad.outgoing(:works_at).should == [trunkclub]
|
85
|
+
end
|
86
|
+
|
87
|
+
end
|
88
|
+
|
89
|
+
it "should work" do
|
90
|
+
tmpdir = quick_batch_neo4j do |db|
|
91
|
+
javad = db.get_node :Person, :name, "Javad"
|
92
|
+
ellen = db.get_node :Person, :name, "Ellen"
|
93
|
+
trunkclub = db.get_node :Company, :name, "Trunkclub"
|
94
|
+
chicago = db.get_node :City, :name, "Chicago"
|
95
|
+
us = db.get_node :Country, :name, "United States"
|
96
|
+
|
97
|
+
javad.outgoing(:lives_in) << chicago
|
98
|
+
ellen.outgoing(:lives_in) << chicago
|
99
|
+
end
|
100
|
+
|
101
|
+
quick_normal_neo4j(tmpdir) do |db|
|
102
|
+
javad = db.get_node :Person, :name, "Javad"
|
103
|
+
ellen = db.get_node :Person, :name, "Ellen"
|
104
|
+
trunkclub = db.get_node :Company, :name, "Trunkclub"
|
105
|
+
chicago = db.get_node :City, :name, "Chicago"
|
106
|
+
us = db.get_node :Country, :name, "United States"
|
107
|
+
|
108
|
+
javad.outgoing(:lives_in).should == [chicago]
|
109
|
+
ellen.outgoing(:lives_in).should == [chicago]
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should not allow for get_relationships" do
|
114
|
+
tmpdir = quick_batch_neo4j do |db|
|
115
|
+
javad = db.get_node :Person, :name, "Javad"
|
116
|
+
ellen = db.get_node :Person, :name, "Ellen"
|
117
|
+
trunkclub = db.get_node :Company, :name, "Trunkclub"
|
118
|
+
chicago = db.get_node :City, :name, "Chicago"
|
119
|
+
us = db.get_node :Country, :name, "United States"
|
120
|
+
|
121
|
+
javad.outgoing(:lives_in) << chicago
|
122
|
+
chicago.outgoing(:country) << us
|
123
|
+
|
124
|
+
expect {javad.outgoing(:lives_in).outgoing(:country)}.to raise_error(NotImplementedError)
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
it "should accept database configs" do
|
129
|
+
db = Cadet::BatchInserter::Session.open(Dir.mktmpdir, {"use_memory_mapped_buffers" => "true"})
|
130
|
+
db.close
|
131
|
+
end
|
132
|
+
|
133
|
+
xit "should not allow for get_relationships" do
|
134
|
+
quick_batch_dsl_neo4j do
|
135
|
+
transaction do
|
136
|
+
Person_by_name("Javad").lives_in_to City_by_name("Chicago")
|
137
|
+
|
138
|
+
Person_by_name("Javad").outgoing(:lives_in).should == [City_by_name("Chicago")]
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
|
144
|
+
end
|
data/spec/unit/cadet_spec.rb
CHANGED
@@ -1,31 +1,101 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
-
require 'tmpdir'
|
3
2
|
|
4
3
|
describe Cadet do
|
5
4
|
|
6
5
|
it "should set a node's property" do
|
7
|
-
|
6
|
+
quick_normal_neo4j do |db|
|
8
7
|
javad = db.get_node :Person, :name, "Javad"
|
9
8
|
javad[:name].should == "Javad"
|
10
9
|
end
|
11
10
|
end
|
12
11
|
|
13
12
|
it "should set a node's label" do
|
14
|
-
|
13
|
+
quick_normal_neo4j do |db|
|
15
14
|
javad = db.get_node :Person, :name, "Javad"
|
16
|
-
javad.add_label
|
15
|
+
javad.add_label :Member
|
17
16
|
javad.labels.should == ["Person", "Member"]
|
18
17
|
end
|
19
18
|
end
|
20
19
|
|
21
20
|
it "should add outgoing relationship's to a node" do
|
22
|
-
|
21
|
+
quick_normal_neo4j do |db|
|
23
22
|
javad = db.get_node :Person, :name, "Javad"
|
24
23
|
ellen = db.get_node :Person, :name, "Ellen"
|
25
24
|
|
26
25
|
javad.outgoing(:knows) << ellen
|
27
26
|
|
28
|
-
javad.outgoing(:knows).
|
27
|
+
javad.outgoing(:knows).should == [ellen]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
it "it should accept multiple relationships" do
|
32
|
+
quick_normal_neo4j do |db|
|
33
|
+
javad = db.get_node(:Person, :name, "Javad")
|
34
|
+
javad.outgoing(:lives_in) << db.get_node(:City, :name, "Chicago")
|
35
|
+
javad.outgoing(:lives_in) << db.get_node(:City, :name, "Houston")
|
36
|
+
javad.outgoing(:lives_in).should == [db.get_node(:City, :name, "Chicago"), db.get_node(:City, :name, "Houston")]
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should allow for outgoing to be chained" do
|
41
|
+
quick_test_neo4j do |db|
|
42
|
+
db.dsl do |db|
|
43
|
+
javad = Person_by_name "Javad"
|
44
|
+
ellen = Person_by_name "Ellen"
|
45
|
+
trunkclub = Company_by_name "Trunkclub"
|
46
|
+
chicago = City_by_name "Chicago"
|
47
|
+
us = Country_by_name "United States"
|
48
|
+
springfield = City_by_name "Springfield"
|
49
|
+
|
50
|
+
|
51
|
+
javad.outgoing(:works_at) << trunkclub
|
52
|
+
trunkclub.outgoing(:located_in) << chicago
|
53
|
+
javad.outgoing(:lives_in) << chicago
|
54
|
+
ellen.outgoing(:lives_in) << chicago
|
55
|
+
chicago.outgoing(:country) << us
|
56
|
+
|
57
|
+
javad.outgoing(:works_at).outgoing(:located_in).outgoing(:country).should == [us]
|
58
|
+
chicago.incoming(:located_in).incoming(:works_at).should == [javad]
|
59
|
+
javad.outgoing(:works_at).outgoing(:located_in).incoming(:lives_in).should == [javad, ellen]
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should allow for node relationship's to be accessed" do
|
65
|
+
quick_test_neo4j do |db|
|
66
|
+
db.dsl do |db|
|
67
|
+
javad = Person_by_name "Javad"
|
68
|
+
ellen = Person_by_name "Ellen"
|
69
|
+
javad.outgoing(:knows) << ellen
|
70
|
+
javad.incoming(:also_knows) << ellen
|
71
|
+
|
72
|
+
javad.outgoing_rels(:knows).map{ |rel| rel.get_other_node(javad)}.should == [ellen]
|
73
|
+
javad.incoming_rels(:also_knows).map{ |rel| rel.get_other_node(javad)}.should == [ellen]
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
xit "should enforce unique constraints" do
|
80
|
+
test_neo4j do |db|
|
81
|
+
db.transaction do
|
82
|
+
db.constraint :Person, :name
|
83
|
+
end
|
84
|
+
db.transaction do
|
85
|
+
db.create_node :Person, :name, "Javad"
|
86
|
+
|
87
|
+
expect { db.create_node :Person, :name, "Javad" }.to raise_error(org.neo4j.graphdb.ConstraintViolationException)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'should have a working dsl' do
|
93
|
+
db = Cadet::Test::Session.open.dsl do
|
94
|
+
transaction do |tx|
|
95
|
+
Person_by_name("Javad").lives_in_to City_by_name("Chicago")
|
96
|
+
|
97
|
+
Person_by_name("Javad").outgoing(:lives_in).should == [City_by_name("Chicago")]
|
98
|
+
end
|
29
99
|
end
|
30
100
|
end
|
31
101
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cadet
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.8
|
5
5
|
platform: java
|
6
6
|
authors:
|
7
7
|
- Javad Karabi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|
@@ -45,6 +45,7 @@ extensions: []
|
|
45
45
|
extra_rdoc_files: []
|
46
46
|
files:
|
47
47
|
- .gitignore
|
48
|
+
- .travis.yml
|
48
49
|
- Gemfile
|
49
50
|
- Gemfile.lock
|
50
51
|
- README.md
|
@@ -53,14 +54,22 @@ files:
|
|
53
54
|
- lib/cadet.rb
|
54
55
|
- lib/cadet/batch_inserter/node.rb
|
55
56
|
- lib/cadet/batch_inserter/session.rb
|
57
|
+
- lib/cadet/batch_inserter/transaction.rb
|
56
58
|
- lib/cadet/cadet_index/index.rb
|
57
59
|
- lib/cadet/cadet_index/index_provider.rb
|
60
|
+
- lib/cadet/direction.rb
|
61
|
+
- lib/cadet/dsl.rb
|
58
62
|
- lib/cadet/dynamic_label.rb
|
59
63
|
- lib/cadet/dynamic_relationshiptype.rb
|
64
|
+
- lib/cadet/helpers.rb
|
60
65
|
- lib/cadet/node.rb
|
61
|
-
- lib/cadet/
|
66
|
+
- lib/cadet/node_relationships.rb
|
67
|
+
- lib/cadet/node_traverser.rb
|
68
|
+
- lib/cadet/path_traverser.rb
|
69
|
+
- lib/cadet/relationship.rb
|
62
70
|
- lib/cadet/session.rb
|
63
71
|
- lib/cadet/test/session.rb
|
72
|
+
- lib/cadet/transaction.rb
|
64
73
|
- lib/cadet/version.rb
|
65
74
|
- lib/neo4j/README.txt
|
66
75
|
- lib/neo4j/concurrentlinkedhashmap-lru-1.3.1.jar
|
@@ -71,6 +80,7 @@ files:
|
|
71
80
|
- lib/neo4j/neo4j-lucene-index-2.0.1.jar
|
72
81
|
- lib/neo4j/neo4j-shell-2.0.1.jar
|
73
82
|
- spec/spec_helper.rb
|
83
|
+
- spec/unit/batch_insert_spec.rb
|
74
84
|
- spec/unit/cadet_spec.rb
|
75
85
|
homepage: https://github.com/karabijavad/cadet
|
76
86
|
licenses:
|
@@ -1,22 +0,0 @@
|
|
1
|
-
module Cadet
|
2
|
-
class RelationshipTraverser
|
3
|
-
include Enumerable
|
4
|
-
include_package "org.neo4j.graphdb"
|
5
|
-
|
6
|
-
def initialize(node, direction, type)
|
7
|
-
@node = node
|
8
|
-
@type = type
|
9
|
-
@direction = direction
|
10
|
-
end
|
11
|
-
|
12
|
-
def each
|
13
|
-
@node.get_relationships(@direction, @type).each do |rel|
|
14
|
-
yield Node.new(rel.getOtherNode(@node.underlying))
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def << (othernode)
|
19
|
-
@direction == Direction::OUTGOING ? @node.create_outgoing(othernode, @type) : othernode.create_outgoing(@node, @type)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|