neo4j-core 8.1.4 → 9.0.0.alpha.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +71 -8
  3. data/lib/neo4j-core.rb +3 -49
  4. data/lib/neo4j/core.rb +4 -0
  5. data/lib/neo4j/core/config.rb +13 -0
  6. data/lib/neo4j/core/cypher_session/adaptors.rb +15 -15
  7. data/lib/neo4j/core/cypher_session/adaptors/bolt.rb +39 -48
  8. data/lib/neo4j/core/cypher_session/adaptors/bolt/chunk_writer_io.rb +0 -4
  9. data/lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb +7 -3
  10. data/lib/neo4j/core/cypher_session/adaptors/embedded.rb +1 -2
  11. data/lib/neo4j/core/cypher_session/adaptors/has_uri.rb +4 -0
  12. data/lib/neo4j/core/cypher_session/adaptors/http.rb +1 -3
  13. data/lib/neo4j/core/cypher_session/responses.rb +1 -1
  14. data/lib/neo4j/core/cypher_session/responses/bolt.rb +0 -17
  15. data/lib/neo4j/core/cypher_session/responses/embedded.rb +9 -7
  16. data/lib/neo4j/core/cypher_session/responses/http.rb +3 -4
  17. data/lib/neo4j/core/cypher_session/transactions.rb +2 -0
  18. data/lib/{neo4j-core → neo4j/core}/helpers.rb +1 -14
  19. data/lib/neo4j/core/logging.rb +44 -0
  20. data/lib/{neo4j-core → neo4j/core}/query.rb +7 -6
  21. data/lib/{neo4j-core → neo4j/core}/query_clauses.rb +9 -16
  22. data/lib/{neo4j-core → neo4j/core}/query_find_in_batches.rb +3 -5
  23. data/lib/{neo4j-core → neo4j/core}/version.rb +1 -1
  24. data/lib/neo4j/transaction.rb +6 -8
  25. data/neo4j-core.gemspec +13 -11
  26. metadata +46 -50
  27. data/lib/ext/kernel.rb +0 -9
  28. data/lib/neo4j-core/active_entity.rb +0 -11
  29. data/lib/neo4j-core/label.rb +0 -9
  30. data/lib/neo4j-embedded.rb +0 -16
  31. data/lib/neo4j-embedded/cypher_response.rb +0 -71
  32. data/lib/neo4j-embedded/embedded_database.rb +0 -26
  33. data/lib/neo4j-embedded/embedded_ha_session.rb +0 -30
  34. data/lib/neo4j-embedded/embedded_impermanent_session.rb +0 -17
  35. data/lib/neo4j-embedded/embedded_label.rb +0 -88
  36. data/lib/neo4j-embedded/embedded_node.rb +0 -206
  37. data/lib/neo4j-embedded/embedded_relationship.rb +0 -77
  38. data/lib/neo4j-embedded/embedded_session.rb +0 -203
  39. data/lib/neo4j-embedded/embedded_transaction.rb +0 -30
  40. data/lib/neo4j-embedded/label.rb +0 -66
  41. data/lib/neo4j-embedded/property.rb +0 -106
  42. data/lib/neo4j-embedded/to_java.rb +0 -44
  43. data/lib/neo4j-server.rb +0 -12
  44. data/lib/neo4j-server/cypher_label.rb +0 -35
  45. data/lib/neo4j-server/cypher_node.rb +0 -221
  46. data/lib/neo4j-server/cypher_relationship.rb +0 -142
  47. data/lib/neo4j-server/cypher_response.rb +0 -248
  48. data/lib/neo4j-server/cypher_session.rb +0 -263
  49. data/lib/neo4j-server/cypher_transaction.rb +0 -100
  50. data/lib/neo4j-server/label.rb +0 -40
  51. data/lib/neo4j-server/resource.rb +0 -57
  52. data/lib/neo4j/entity_equality.rb +0 -8
  53. data/lib/neo4j/entity_marshal.rb +0 -20
  54. data/lib/neo4j/label.rb +0 -90
  55. data/lib/neo4j/node.rb +0 -216
  56. data/lib/neo4j/property_container.rb +0 -17
  57. data/lib/neo4j/property_validator.rb +0 -22
  58. data/lib/neo4j/relationship.rb +0 -161
  59. data/lib/neo4j/session.rb +0 -222
@@ -1,206 +0,0 @@
1
- module Neo4j
2
- module Embedded
3
- class RelsIterator
4
- include Enumerable
5
- extend Neo4j::Core::TxMethods
6
-
7
- MARSHAL_INSTANCE_VARIABLES = %i[@node @match]
8
-
9
- def initialize(node, match)
10
- @node = node
11
- ::Neo4j::Node.validate_match!(match)
12
- @match = match
13
- end
14
-
15
- def inspect
16
- 'Enumerable<Neo4j::Relationship>'
17
- end
18
-
19
- def each
20
- @node._rels(@match).each { |r| yield(r.wrapper) }
21
- end
22
- tx_methods :each
23
-
24
- def empty?
25
- first.nil?
26
- end
27
- end
28
-
29
- class NodesIterator
30
- include Enumerable
31
- extend Neo4j::Core::TxMethods
32
-
33
- def initialize(node, match)
34
- @node = node
35
- @match = match
36
- end
37
-
38
- def inspect
39
- 'Enumerable<Neo4j::Node>'
40
- end
41
-
42
- def each
43
- @node._rels(@match).each { |r| yield(r.other_node(@node)) }
44
- end
45
- tx_methods :each
46
-
47
- def empty?
48
- first.nil?
49
- end
50
- end
51
-
52
- class EmbeddedNode < Neo4j::Node
53
- class << self
54
- if !Neo4j::Core::Config.using_new_session?
55
- # This method is used to extend a Java Neo4j class so that it includes the same mixins as this class.
56
- Java::OrgNeo4jKernelImplCore::NodeProxy.class_eval do
57
- include Neo4j::Embedded::Property
58
- include Neo4j::EntityEquality
59
- include Neo4j::Core::ActiveEntity
60
- extend Neo4j::Core::TxMethods
61
-
62
- def inspect
63
- "EmbeddedNode neo_id: #{neo_id}"
64
- end
65
-
66
- def exist?
67
- !!graph_database.get_node_by_id(neo_id)
68
- rescue Java::OrgNeo4jGraphdb.NotFoundException
69
- false
70
- end
71
- tx_methods :exist?
72
-
73
- def labels
74
- _labels.iterator.map { |x| x.name.to_sym }
75
- end
76
- tx_methods :labels
77
-
78
- alias_method :_labels, :getLabels
79
-
80
- def _java_label(label_name)
81
- Java::OrgNeo4jGraphdb.DynamicLabel.label(label_name)
82
- end
83
-
84
- def _add_label(*label_name)
85
- label_name.each do |name|
86
- addLabel(_java_label(name))
87
- end
88
- end
89
-
90
- alias_method :add_label, :_add_label
91
- tx_methods :add_label
92
-
93
- def _remove_label(*label_name)
94
- label_name.each do |name|
95
- removeLabel(_java_label(name))
96
- end
97
- end
98
-
99
- alias_method :remove_label, :_remove_label
100
- tx_methods :remove_label
101
-
102
- def set_label(*label_names)
103
- label_as_symbols = label_names.map(&:to_sym)
104
- to_keep = labels & label_as_symbols
105
- to_remove = labels - to_keep
106
- _remove_label(*to_remove)
107
- to_add = label_as_symbols - to_keep
108
- _add_label(*to_add)
109
- end
110
- tx_methods :set_label
111
-
112
- def del
113
- _rels.each(&:del)
114
- delete
115
- nil
116
- end
117
- tx_methods :del
118
- tx_methods :delete
119
-
120
- alias_method :destroy, :del
121
- tx_methods :destroy
122
-
123
- def create_rel(type, other_node, props = nil)
124
- rel = create_relationship_to(other_node.neo4j_obj, ToJava.type_to_java(type))
125
- props.each_pair { |k, v| rel[k] = v } if props
126
- rel
127
- end
128
- tx_methods :create_rel
129
-
130
-
131
- def rels(match = {})
132
- RelsIterator.new(self, match)
133
- end
134
-
135
- def nodes(match = {})
136
- NodesIterator.new(self, match)
137
- end
138
-
139
- def node(match = {})
140
- rel = _rel(match)
141
- rel && rel.other_node(self).wrapper
142
- end
143
- tx_methods :node
144
-
145
- def rel?(match = {})
146
- _rels(match).has_next
147
- end
148
- tx_methods :rel?
149
-
150
- def rel(match = {})
151
- _rel(match)
152
- end
153
- tx_methods :rel
154
-
155
- def _rel(match = {})
156
- dir, rel_type, between_id = _parse_match(match)
157
-
158
- rel = if rel_type
159
- get_single_relationship(rel_type, dir)
160
- else
161
- _get_single_relationship_by_dir(dir)
162
- end
163
-
164
- rel if !(rel && between_id) || rel.other_node(self).neo_id == between_id
165
- end
166
-
167
- def _get_single_relationship_by_dir(dir)
168
- iter = get_relationships(dir).iterator
169
-
170
- return nil if not iter.has_next
171
-
172
- first = iter.next
173
- fail ArgumentError, "Expected to only find one relationship from node #{neo_id} matching" if iter.has_next
174
- first
175
- end
176
-
177
- def _rels(match = {})
178
- dir, rel_type, between_id = _parse_match(match)
179
-
180
- args = rel_type ? [rel_type, dir] : [dir]
181
- rels = get_relationships(*args).iterator
182
-
183
- if between_id
184
- rels.find_all { |r| r.end_node.neo_id == between_id || r.start_node.neo_id == between_id }
185
- else
186
- rels
187
- end
188
- end
189
-
190
- def _parse_match(match)
191
- ::Neo4j::Node.validate_match!(match)
192
-
193
- [
194
- ToJava.dir_to_java(match[:dir] || :both),
195
- ToJava.type_to_java(match[:type]),
196
- match[:between] && match[:between].neo_id
197
- ]
198
- end
199
-
200
- include Neo4j::Node::Wrapper
201
- end
202
- end
203
- end
204
- end
205
- end
206
- end
@@ -1,77 +0,0 @@
1
- module Neo4j
2
- module Embedded
3
- class EmbeddedRelationship
4
- MARSHAL_INSTANCE_VARIABLES = []
5
-
6
- class << self
7
- if !Neo4j::Core::Config.using_new_session?
8
- Java::OrgNeo4jKernelImplCore::RelationshipProxy.class_eval do
9
- include Neo4j::Embedded::Property
10
- include Neo4j::EntityEquality
11
- include Neo4j::Relationship::Wrapper
12
- include Neo4j::Core::ActiveEntity
13
- extend Neo4j::Core::TxMethods
14
-
15
- alias_method :_other_node, :getOtherNode
16
-
17
- def exist?
18
- !!graph_database.get_relationship_by_id(neo_id)
19
- rescue Java::OrgNeo4jGraphdb.NotFoundException
20
- false
21
- end
22
- tx_methods :exist?
23
-
24
- def inspect
25
- "EmbeddedRelationship neo_id: #{neo_id}"
26
- end
27
-
28
- def start_node
29
- _start_node.wrapper
30
- end
31
- tx_methods :start_node
32
- alias_method :_start_node_id, :start_node
33
- tx_methods :_start_node_id
34
-
35
- def _start_node
36
- getStartNode
37
- end
38
-
39
- def rel_type
40
- @_rel_type ||= _rel_type
41
- end
42
-
43
- def _rel_type
44
- getType.name.to_sym
45
- end
46
- tx_methods :rel_type
47
-
48
- def del
49
- delete
50
- end
51
- tx_methods :del
52
- tx_methods :delete
53
-
54
- alias_method :destroy, :del
55
- tx_methods :destroy
56
-
57
- def other_node(n)
58
- _other_node(n.neo4j_obj).wrapper
59
- end
60
- tx_methods :other_node
61
-
62
- def end_node
63
- _end_node.wrapper
64
- end
65
- tx_methods :end_node
66
- alias_method :_end_node_id, :end_node
67
- tx_methods :_end_node_id
68
-
69
- def _end_node
70
- getEndNode
71
- end
72
- end
73
- end
74
- end
75
- end
76
- end
77
- end
@@ -1,203 +0,0 @@
1
- # Plugin
2
-
3
- Neo4j::Session.register_db(:embedded_db) do |*args|
4
- Neo4j::Embedded::EmbeddedSession.new(*args)
5
- end
6
-
7
- module Neo4j
8
- module Embedded
9
- class EmbeddedSession < Neo4j::Session
10
- class Error < StandardError
11
- end
12
-
13
- attr_reader :graph_db, :db_location, :properties_file, :properties_map
14
- extend Forwardable
15
- extend Neo4j::Core::TxMethods
16
- def_delegator :@graph_db, :begin_tx
17
-
18
- def initialize(db_location, config = {})
19
- @config = config
20
- @db_location = db_location
21
- @auto_commit = !!@config[:auto_commit]
22
- @properties_file = @config[:properties_file]
23
- Neo4j::Session.register(self)
24
- end
25
-
26
- def properties_map
27
- return @properties_map if @properties_map
28
-
29
- props = if @config[:properties_map].is_a?(Hash)
30
- @config[:properties_map].each_with_object({}) do |(k, v), m|
31
- m[k.to_s.to_java] = v.to_s.to_java
32
- end
33
- else
34
- {}
35
- end
36
- @properties_map = java.util.HashMap.new(props)
37
- end
38
-
39
- def db_type
40
- :embedded_db
41
- end
42
-
43
- def inspect
44
- "#{self.class} db_location: '#{@db_location}', running: #{running?}"
45
- end
46
-
47
- def version
48
- # Wow
49
- # Yeah, agreed...
50
- version_string = @graph_db.to_java(Java::OrgNeo4jKernel::GraphDatabaseAPI).getDependencyResolver.resolveDependency(Java::OrgNeo4jKernel::KernelData.java_class).version.to_s
51
- version_string.split(' ')[-1]
52
- end
53
-
54
- def start
55
- fail Error, 'Embedded Neo4j db is already running' if running?
56
- puts "Start embedded Neo4j db at #{db_location}"
57
- factory = Java::OrgNeo4jGraphdbFactory::GraphDatabaseFactory.new
58
- db_service = factory.newEmbeddedDatabaseBuilder(db_location)
59
- db_service.loadPropertiesFromFile(properties_file) if properties_file
60
- db_service.setConfig(properties_map) if properties_map
61
-
62
- @graph_db = db_service.newGraphDatabase
63
- Neo4j::Session._notify_listeners(:session_available, self)
64
- @engine = Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
65
- end
66
-
67
- def factory_class
68
- Java::OrgNeo4jTest::ImpermanentGraphDatabase
69
- end
70
-
71
- def close
72
- super
73
- shutdown
74
- end
75
-
76
- def self.transaction_class
77
- Neo4j::Embedded::EmbeddedTransaction
78
- end
79
-
80
- # Duplicate of CypherSession::Adaptor::Base#transaction
81
- def transaction
82
- return self.class.transaction_class.new(self) if !block_given?
83
-
84
- begin
85
- tx = transaction
86
-
87
- yield tx
88
- rescue Exception => e # rubocop:disable Lint/RescueException
89
- tx.mark_failed
90
-
91
- raise e
92
- ensure
93
- tx.close
94
- end
95
- end
96
-
97
- def shutdown
98
- @graph_db && @graph_db.shutdown
99
-
100
- Neo4j::Session.clear_listeners
101
- @graph_db = nil
102
- end
103
-
104
- def running?
105
- !!@graph_db
106
- end
107
-
108
- def create_label(name)
109
- EmbeddedLabel.new(self, name)
110
- end
111
-
112
- def load_node(neo_id)
113
- _load_node(neo_id)
114
- end
115
- tx_methods :load_node
116
-
117
- # Same as load but does not return the node as a wrapped Ruby object.
118
- #
119
- def _load_node(neo_id)
120
- return nil if neo_id.nil?
121
- @graph_db.get_node_by_id(neo_id.to_i)
122
- rescue Java::OrgNeo4jGraphdb.NotFoundException
123
- nil
124
- end
125
-
126
- def load_relationship(neo_id)
127
- _load_relationship(neo_id)
128
- end
129
- tx_methods :load_relationship
130
-
131
- def _load_relationship(neo_id)
132
- return nil if neo_id.nil?
133
- @graph_db.get_relationship_by_id(neo_id.to_i)
134
- rescue Java::OrgNeo4jGraphdb.NotFoundException
135
- nil
136
- end
137
-
138
- def query(*args)
139
- if [[String], [String, Hash]].include?(args.map(&:class))
140
- query, params = args[0, 2]
141
- Neo4j::Embedded::ResultWrapper.new(_query(query, params), query)
142
- else
143
- options = args[0] || {}
144
- Neo4j::Core::Query.new(options.merge(session: self))
145
- end
146
- end
147
-
148
-
149
- def find_all_nodes(label)
150
- EmbeddedLabel.new(self, label).find_nodes
151
- end
152
-
153
- def find_nodes(label, key, value)
154
- EmbeddedLabel.new(self, label).find_nodes(key, value)
155
- end
156
-
157
- # Performs a cypher query with given string.
158
- # Remember that you should close the resource iterator.
159
- # @param [String] q the cypher query as a String
160
- # @return (see #query)
161
- def _query(query, params = {}, options = {})
162
- ActiveSupport::Notifications.instrument('neo4j.cypher_query', params: params, context: options[:context],
163
- cypher: query, pretty_cypher: options[:pretty_cypher], params: params) do
164
- @engine ||= Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
165
- @engine.execute(query, indifferent_params(params))
166
- end
167
- rescue StandardError => e
168
- raise Neo4j::Session::CypherError.new(e.message, e.class, 'cypher error')
169
- end
170
-
171
- def indifferent_params(params)
172
- return {} unless params
173
- params.each { |k, v| params[k] = HashWithIndifferentAccess.new(params[k]) if v.is_a?(Hash) && !v.respond_to?(:nested_under_indifferent_access) }
174
- HashWithIndifferentAccess.new(params)
175
- end
176
-
177
- def query_default_return(as)
178
- " RETURN #{as}"
179
- end
180
-
181
- def _query_or_fail(q)
182
- @engine ||= Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
183
- @engine.execute(q)
184
- end
185
-
186
- def search_result_to_enumerable(result)
187
- result.map { |column| column['n'].wrapper }
188
- end
189
-
190
- def create_node(properties = nil, labels = [])
191
- if labels.empty?
192
- @graph_db.create_node
193
- else
194
- labels = EmbeddedLabel.as_java(labels)
195
- @graph_db.create_node(labels)
196
- end.tap do |java_node|
197
- properties.each_pair { |k, v| java_node[k] = v } if properties
198
- end
199
- end
200
- tx_methods :create_node
201
- end
202
- end
203
- end