cadet 0.1.0-java → 0.1.1-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.ruby-version +1 -0
- data/README.md +22 -3
- data/lib/cadet/batch_inserter/cadet_index/index.rb +2 -3
- data/lib/cadet/batch_inserter/cadet_index/index_provider.rb +6 -3
- data/lib/cadet/batch_inserter/node.rb +3 -3
- data/lib/cadet/batch_inserter/relationship.rb +20 -0
- data/lib/cadet/batch_inserter/session.rb +35 -11
- data/lib/cadet/node.rb +1 -2
- data/lib/cadet/session.rb +17 -10
- data/lib/cadet/version.rb +1 -1
- data/lib/cadet.rb +1 -0
- data/spec/unit/batch_insert_spec.rb +22 -6
- data/spec/unit/cadet_spec.rb +8 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ca887c03dce61ba0ba05011ce4693ff9b08c19fc
|
4
|
+
data.tar.gz: b028f3f77bc89d1beaa4be1f234ecb205f5abb7e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e43b1a700ed9798678ce7f715c6dd4b2f4d7c54a414c150c48eecf641ec22f6e1a3ef489f813ed6f15b3bc5d4ddb0294795fb2f31a33e711b3596f87e8afe81
|
7
|
+
data.tar.gz: 868a047761af51e5e257d3a38b9f757b5fc166ae20bae08e88559cbdf987884e5e209bcf86df8039a1b5d5ea88f5e0b35a0ad11c21cbc96287f5241865faff2f
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
jruby
|
data/README.md
CHANGED
@@ -7,19 +7,29 @@ Use neo4j via jruby! Nothing else needed, simply add this gem to get the power o
|
|
7
7
|
|
8
8
|
super simple. you dont even need to download neo4j.
|
9
9
|
|
10
|
-
|
10
|
+
##Open a session
|
11
11
|
```ruby
|
12
12
|
|
13
13
|
require 'cadet'
|
14
14
|
|
15
15
|
Cadet::Session.open "path/to/graph.db/" do
|
16
|
+
#for a batch inserter session:
|
17
|
+
#Cadet::BatchInserter::Session.open "path/to/graph.db/" do
|
18
|
+
#bear in mind that, the database directory needs to be clean before establishing a BatchInserter session.
|
16
19
|
transaction do
|
17
20
|
Person_by_name("Javad").lives_in_to City_by_name("Chicago")
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
24
|
+
```
|
21
25
|
|
26
|
+
## beginning a transaction
|
27
|
+
```ruby
|
28
|
+
transaction do
|
29
|
+
#...
|
30
|
+
end
|
22
31
|
```
|
32
|
+
Note: transaction effictively does nothing in a BatchInserter session, as transactions are not supported in neo4j's BatchInserter (for performance reasons)
|
23
33
|
|
24
34
|
## Getting/creating a node, using label-property-value
|
25
35
|
A node can be retrieved (and implicitly created if it does not exist) via the following syntax:
|
@@ -53,6 +63,15 @@ Relationship creation can also be chained:
|
|
53
63
|
Person_by_name("Javad").lives_in_to(City_by_name("Chicago")).city_of_to(State_by_name("Illinois")).state_of_to(Country_by_name("United States"))
|
54
64
|
```
|
55
65
|
|
66
|
+
A node's relationships can be iterated over via:
|
67
|
+
```ruby
|
68
|
+
javad.outgoing(:lives_in).each do |rel|
|
69
|
+
#rel is a Cadet#Relationship
|
70
|
+
end
|
56
71
|
|
57
|
-
|
58
|
-
|
72
|
+
chicago.incoming(:lives_in).each do |rel|
|
73
|
+
#rel is a Cadet#Relationship
|
74
|
+
end
|
75
|
+
```
|
76
|
+
Note: this (relationship traversal) does not work in a batch inserter session, atleast not yet.
|
77
|
+
The idea is a batch inserter session is used for writing data, as opposed to reading data
|
@@ -2,9 +2,8 @@ module Cadet
|
|
2
2
|
module BatchInserter
|
3
3
|
module CadetIndex
|
4
4
|
class Index
|
5
|
-
def initialize(lucene_index, name
|
5
|
+
def initialize(lucene_index, name)
|
6
6
|
@name = name.to_sym
|
7
|
-
@type = type
|
8
7
|
@property_index = {}
|
9
8
|
@lucene_index = lucene_index
|
10
9
|
end
|
@@ -21,7 +20,7 @@ module Cadet
|
|
21
20
|
end
|
22
21
|
|
23
22
|
def flush
|
24
|
-
lucene_node_index = @lucene_index.nodeIndex(@name,
|
23
|
+
lucene_node_index = @lucene_index.nodeIndex(@name, {"type" => "exact"})
|
25
24
|
|
26
25
|
@property_index.each do |property, propval_to_node_mappings|
|
27
26
|
propval_to_node_mappings.each do |value, nodes|
|
@@ -7,11 +7,14 @@ module Cadet
|
|
7
7
|
@indexes = {}
|
8
8
|
@lucene_index = org.neo4j.index.impl.lucene.LuceneBatchInserterIndexProviderNewImpl.new(db)
|
9
9
|
end
|
10
|
-
|
11
|
-
|
10
|
+
|
11
|
+
def [](label)
|
12
|
+
@indexes[label.to_sym] ||= CadetIndex::Index.new(@lucene_index, label.to_sym)
|
12
13
|
end
|
13
14
|
def shutdown
|
14
|
-
@indexes.each
|
15
|
+
@indexes.each do |label, index|
|
16
|
+
index.flush
|
17
|
+
end
|
15
18
|
end
|
16
19
|
end
|
17
20
|
end
|
@@ -3,15 +3,15 @@ module Cadet
|
|
3
3
|
class Node < Cadet::Node
|
4
4
|
|
5
5
|
def create_outgoing(to, type, properties = {})
|
6
|
-
|
6
|
+
Cadet::BatchInserter::Session.current_session.create_relationship(self, to, type, properties)
|
7
7
|
end
|
8
8
|
|
9
9
|
def []= (property, value)
|
10
|
-
|
10
|
+
Cadet::BatchInserter::Session.current_session.set_node_property(self, property, value)
|
11
11
|
end
|
12
12
|
|
13
13
|
def [] (property)
|
14
|
-
|
14
|
+
Cadet::BatchInserter::Session.current_session.get_node_properties(self)[property.to_s]
|
15
15
|
end
|
16
16
|
|
17
17
|
def == other_node
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Cadet
|
2
|
+
module BatchInserter
|
3
|
+
class Relationship < Cadet::Relationship
|
4
|
+
attr_accessor :underlying
|
5
|
+
|
6
|
+
def == other_rel
|
7
|
+
@underlying == other_rel
|
8
|
+
end
|
9
|
+
|
10
|
+
def []= (property, value)
|
11
|
+
Cadet::BatchInserter::Session.current_session.set_relationship_property(self, property, value)
|
12
|
+
end
|
13
|
+
|
14
|
+
def [] (property)
|
15
|
+
Cadet::BatchInserter::Session.current_session.get_relationship_properties(self)[property]
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -2,9 +2,13 @@ module Cadet
|
|
2
2
|
module BatchInserter
|
3
3
|
class Session < Cadet::Session
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@index_provider = CadetIndex::IndexProvider.new(
|
7
|
-
@
|
5
|
+
def initialize(underlying)
|
6
|
+
@index_provider = CadetIndex::IndexProvider.new(underlying)
|
7
|
+
@underlying = underlying
|
8
|
+
end
|
9
|
+
|
10
|
+
def index_on(label, property, node)
|
11
|
+
@index_provider[label].add(node.underlying, property, node[property])
|
8
12
|
end
|
9
13
|
|
10
14
|
def close
|
@@ -14,6 +18,7 @@ module Cadet
|
|
14
18
|
|
15
19
|
def self.open(location, &block)
|
16
20
|
new(org.neo4j.unsafe.batchinsert.BatchInserters.inserter(location)).tap do |session|
|
21
|
+
@@current_session = session
|
17
22
|
if block_given?
|
18
23
|
session.instance_exec(session, &block)
|
19
24
|
session.close
|
@@ -22,28 +27,47 @@ module Cadet
|
|
22
27
|
end
|
23
28
|
|
24
29
|
def constraint(label, property)
|
25
|
-
@
|
30
|
+
@underlying.createDeferredConstraint(DynamicLabel.label(label))
|
26
31
|
.assertPropertyIsUnique(property)
|
27
32
|
.create()
|
28
33
|
end
|
29
34
|
|
30
35
|
def find_node(label, property, value)
|
31
|
-
|
36
|
+
(node = @index_provider[label].get(property.to_sym, value).first) ?
|
37
|
+
Node.new(node) : nil
|
38
|
+
end
|
32
39
|
|
33
|
-
|
34
|
-
|
40
|
+
def create_node(label, properties)
|
41
|
+
Node.new(@underlying.createNode(Hash[properties.map { |k,v| [k.to_java_string, v] }], DynamicLabel.label(label)))
|
35
42
|
end
|
36
43
|
|
37
|
-
def
|
38
|
-
|
39
|
-
@index_provider.nodeIndex(label).add(n.underlying, indexing_property.to_sym, properties[indexing_property]) if indexing_property
|
40
|
-
end
|
44
|
+
def get_node(label, property, value)
|
45
|
+
find_node(label, property, value) || create_node(label, {property.to_sym => value}).tap { |n| index_on(label, property, n); }
|
41
46
|
end
|
42
47
|
|
43
48
|
def get_transaction
|
44
49
|
Transaction.new(self)
|
45
50
|
end
|
46
51
|
|
52
|
+
def create_relationship(from, to, type, properties)
|
53
|
+
Relationship.new @underlying.createRelationship(from.underlying, to.underlying, DynamicRelationshipType.withName(type), properties)
|
54
|
+
end
|
55
|
+
|
56
|
+
def get_node_properties(node)
|
57
|
+
@underlying.getNodeProperties(node.underlying)
|
58
|
+
end
|
59
|
+
|
60
|
+
def set_node_property(node, property, value)
|
61
|
+
@underlying.setNodeProperty(node.underlying, property.to_java_string, value)
|
62
|
+
end
|
63
|
+
|
64
|
+
def get_relationship_properties(relationship)
|
65
|
+
@underlying.getRelationshipProperties(relationship.underlying)
|
66
|
+
end
|
67
|
+
|
68
|
+
def set_relationship_property(relationship, property, value)
|
69
|
+
@underlying.setRelationshipProperty(relationship.underlying, property.to_java_string, value)
|
70
|
+
end
|
47
71
|
end
|
48
72
|
end
|
49
73
|
end
|
data/lib/cadet/node.rb
CHANGED
data/lib/cadet/session.rb
CHANGED
@@ -1,7 +1,13 @@
|
|
1
1
|
module Cadet
|
2
2
|
class Session
|
3
|
-
|
4
|
-
|
3
|
+
@@current_session = nil
|
4
|
+
|
5
|
+
def self.current_session
|
6
|
+
@@current_session
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(underlying)
|
10
|
+
@underlying = underlying
|
5
11
|
end
|
6
12
|
|
7
13
|
def self.open(location = nil, &block)
|
@@ -9,6 +15,7 @@ module Cadet
|
|
9
15
|
new(org.neo4j.graphdb.factory.GraphDatabaseFactory.new.newEmbeddedDatabase(location)) :
|
10
16
|
new(org.neo4j.test.TestGraphDatabaseFactory.new.newImpermanentDatabase))
|
11
17
|
.tap do |session|
|
18
|
+
@@current_session = session
|
12
19
|
if block_given?
|
13
20
|
session.instance_exec(session, &block)
|
14
21
|
session.close
|
@@ -17,23 +24,23 @@ module Cadet
|
|
17
24
|
end
|
18
25
|
|
19
26
|
def close
|
20
|
-
@
|
27
|
+
@underlying.shutdown
|
21
28
|
end
|
22
29
|
|
23
30
|
def create_node(label, properties, indexing_property = nil)
|
24
|
-
Node.new(@
|
31
|
+
Node.new(@underlying.createNode).tap do |n|
|
25
32
|
n.add_label label
|
26
33
|
properties.each { |prop, val| n[prop] = val }
|
27
34
|
end
|
28
35
|
end
|
29
36
|
|
30
37
|
def find_node(label, property, value)
|
31
|
-
( node = org.neo4j.helpers.collection.IteratorUtil.firstOrNull(@
|
38
|
+
( node = org.neo4j.helpers.collection.IteratorUtil.firstOrNull(@underlying.findNodesByLabelAndProperty(DynamicLabel.label(label), property, value)) ) ?
|
32
39
|
Node.new(node) : nil
|
33
40
|
end
|
34
41
|
|
35
42
|
def get_node(label, property, value)
|
36
|
-
find_node(label, property, value) || create_node(label, {property.to_sym => value}
|
43
|
+
find_node(label, property, value) || create_node(label, {property.to_sym => value})
|
37
44
|
end
|
38
45
|
|
39
46
|
def get_transaction
|
@@ -51,14 +58,14 @@ module Cadet
|
|
51
58
|
end
|
52
59
|
|
53
60
|
def constraint(label, property)
|
54
|
-
@
|
61
|
+
@underlying.schema
|
55
62
|
.constraintFor(DynamicLabel.label(label))
|
56
63
|
.assertPropertyIsUnique(property)
|
57
64
|
.create()
|
58
65
|
end
|
59
66
|
|
60
67
|
def begin_tx
|
61
|
-
@
|
68
|
+
@underlying.beginTx
|
62
69
|
end
|
63
70
|
|
64
71
|
def method_missing(name, *args, &block)
|
@@ -72,8 +79,8 @@ module Cadet
|
|
72
79
|
|
73
80
|
when /^create_([A-z_]*)$/
|
74
81
|
self.class.class_eval "
|
75
|
-
def #{name}(value
|
76
|
-
create_node :#{$1}, value
|
82
|
+
def #{name}(value)
|
83
|
+
create_node :#{$1}, value
|
77
84
|
end"
|
78
85
|
return self.send(name, *args, &block)
|
79
86
|
else
|
data/lib/cadet/version.rb
CHANGED
data/lib/cadet.rb
CHANGED
@@ -16,6 +16,7 @@ require 'cadet/transaction'
|
|
16
16
|
|
17
17
|
require 'cadet/batch_inserter/session'
|
18
18
|
require 'cadet/batch_inserter/node'
|
19
|
+
require 'cadet/batch_inserter/relationship'
|
19
20
|
require 'cadet/batch_inserter/transaction'
|
20
21
|
|
21
22
|
require 'cadet/batch_inserter/cadet_index/index_provider'
|
@@ -7,18 +7,21 @@ describe Cadet::BatchInserter do
|
|
7
7
|
Cadet::BatchInserter::Session.open(Dir.mktmpdir).class.should == Cadet::BatchInserter::Session
|
8
8
|
end
|
9
9
|
|
10
|
-
it "it should
|
10
|
+
it "it should also work in batch insert mode" do
|
11
11
|
at = Dir.mktmpdir
|
12
12
|
Cadet::BatchInserter::Session.open(at) do
|
13
13
|
self.class.should == Cadet::BatchInserter::Session
|
14
14
|
|
15
15
|
transaction do
|
16
|
-
|
17
|
-
|
18
|
-
houston = City_by_name("Houston")
|
16
|
+
Person_by_name("Javad").lives_in_to City_by_name("Chicago")
|
17
|
+
Person_by_name("Javad").lives_in_to City_by_name("Houston")
|
19
18
|
|
20
|
-
|
21
|
-
|
19
|
+
City_by_name("Chicago").city_of_to State_by_name("Illinois")
|
20
|
+
City_by_name("Houston").city_of_to State_by_name("Texas")
|
21
|
+
|
22
|
+
Person_by_name("Javad")[:birth_year] = 1988
|
23
|
+
City_by_name("Chicago")[:abbreviation] = "CHI"
|
24
|
+
City_by_name("Houston")[:abbreviation] = "HOU"
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
@@ -26,7 +29,20 @@ describe Cadet::BatchInserter do
|
|
26
29
|
transaction do
|
27
30
|
Person_by_name("Javad").outgoing(:lives_in).should =~ [City_by_name("Houston"), City_by_name("Chicago")]
|
28
31
|
Person_by_name("Javad").outgoing(:lives_in).should =~ [City_by_name("Chicago"), City_by_name("Houston")]
|
32
|
+
|
33
|
+
Person_by_name("Javad").outgoing(:lives_in).outgoing(:city_of).should == [State_by_name("Texas"), State_by_name("Illinois")]
|
34
|
+
Person_by_name("Javad").outgoing(:lives_in).outgoing(:city_of).to_a.should =~ [State_by_name("Illinois"), State_by_name("Texas")]
|
35
|
+
|
36
|
+
Person_by_name("Javad")[:birth_year].should == 1988
|
37
|
+
City_by_name("Chicago")[:abbreviation].should == "CHI"
|
38
|
+
City_by_name("Houston")[:abbreviation].should == "HOU"
|
29
39
|
end
|
30
40
|
end
|
31
41
|
end
|
42
|
+
|
43
|
+
it "should allow access to the cadet database session singleton object" do
|
44
|
+
Cadet::BatchInserter::Session.open(Dir.mktmpdir) do |session|
|
45
|
+
Cadet::BatchInserter::Session.current_session.should == session
|
46
|
+
end
|
47
|
+
end
|
32
48
|
end
|
data/spec/unit/cadet_spec.rb
CHANGED
@@ -220,5 +220,13 @@ describe Cadet do
|
|
220
220
|
end
|
221
221
|
end
|
222
222
|
|
223
|
+
it "should allow access to the cadet database session singleton object" do
|
224
|
+
Cadet::Session.open do |session|
|
225
|
+
transaction do
|
226
|
+
Cadet::Session.current_session.should == session
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
|
223
231
|
|
224
232
|
end
|
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.1.
|
4
|
+
version: 0.1.1
|
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-03-
|
11
|
+
date: 2014-03-26 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
|
+
- .ruby-version
|
48
49
|
- .travis.yml
|
49
50
|
- Gemfile
|
50
51
|
- README.md
|
@@ -55,6 +56,7 @@ files:
|
|
55
56
|
- lib/cadet/batch_inserter/cadet_index/index.rb
|
56
57
|
- lib/cadet/batch_inserter/cadet_index/index_provider.rb
|
57
58
|
- lib/cadet/batch_inserter/node.rb
|
59
|
+
- lib/cadet/batch_inserter/relationship.rb
|
58
60
|
- lib/cadet/batch_inserter/session.rb
|
59
61
|
- lib/cadet/batch_inserter/transaction.rb
|
60
62
|
- lib/cadet/direction.rb
|
@@ -100,7 +102,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
102
|
version: '0'
|
101
103
|
requirements: []
|
102
104
|
rubyforge_project:
|
103
|
-
rubygems_version: 2.
|
105
|
+
rubygems_version: 2.1.9
|
104
106
|
signing_key:
|
105
107
|
specification_version: 4
|
106
108
|
summary: ruby wrapper to Neo4j java API
|