neo4j-core 4.0.7 → 5.0.0.rc.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 72260cb19c0dbb17236bda1cfb681a6923c0f554
4
- data.tar.gz: 38f122d33a93040f6f438ebc973825f6defd7798
3
+ metadata.gz: 152633cd32f2477416b17fd287b5b9d849eb8f7b
4
+ data.tar.gz: 40739815340641373bb5be6879845e93c380868e
5
5
  SHA512:
6
- metadata.gz: ca2066f8fb868ddd5f521f2930535f5164e26d37f6dadb9ede54cb413ef965e9dc5eab2dd1003f25a8052f74af72a0d77420e823f6573f80578ca4a5116f041a
7
- data.tar.gz: ce86a44f21c7d5b3a1b971dfe4f472262f7ecee13c262cc86a3c607d6bac7f92beb175e165c6aacc27caec251cb3390f55c987058e4b06ef2db39bfe4d8014e5
6
+ metadata.gz: 2c8ae0e5f165c5c670a4ee932effe58e79cbb9fc5001b5819cd8ea1c6c38243b575252a02e29883cbfecee9fccb844763cf7fbd3736ff0251540175b074bb9bd
7
+ data.tar.gz: 2b22683eaf14a64bf2a6a37f5da9f4f067a4d43f00e3e94b667df7a5383a44ccf5ac41030e3d3448d62113891fbde15bc8b866d1daa80f3e6b33384906a5f97e
data/lib/ext/kernel.rb ADDED
@@ -0,0 +1,9 @@
1
+ module Kernel
2
+ def ergo(&b)
3
+ if block_given?
4
+ b.arity == 1 ? yield(self) : instance_eval(&b)
5
+ else
6
+ self
7
+ end
8
+ end
9
+ end
data/lib/neo4j/label.rb CHANGED
@@ -61,6 +61,8 @@ module Neo4j
61
61
 
62
62
  class << self
63
63
  include Neo4j::Core::CypherTranslator
64
+ INDEX_PATH = '/db/data/schema/index/'
65
+ CONSTRAINT_PATH = '/db/data/schema/constraint/'
64
66
 
65
67
  # Returns a label of given name that can be used to specifying constraints
66
68
  # @param [Symbol,String] name the name of the label
@@ -68,7 +70,6 @@ module Neo4j
68
70
  session.create_label(name)
69
71
  end
70
72
 
71
-
72
73
  # @return [Enumerable<Neo4j::Node>] all nodes having given label. Nodes can be wrapped in your own model ruby classes.
73
74
  def find_all_nodes(label_name, session = Neo4j::Session.current)
74
75
  session.find_all_nodes(label_name)
data/lib/neo4j/node.rb CHANGED
@@ -118,16 +118,11 @@ module Neo4j
118
118
  fail 'not implemented'
119
119
  end
120
120
 
121
- # @return true if the node exists in the database
121
+ # @return [Boolean] true if the node exists in the database
122
122
  def exist?
123
123
  fail 'not implemented'
124
124
  end
125
125
 
126
- # @return all the Neo4j labels for this node
127
- def labels
128
- fail 'not implemented'
129
- end
130
-
131
126
  # Returns the only node of a given type and direction that is attached to this node, or nil.
132
127
  # This is a convenience method that is used in the commonly occuring situation where a node has exactly zero or one relationships of a given type and direction to another node.
133
128
  # Typically this invariant is maintained by the rest of the code: if at any time more than one such relationships exist, it is a fatal error that should generate an exception.
@@ -160,11 +155,6 @@ module Neo4j
160
155
  fail 'not implemented'
161
156
  end
162
157
 
163
- # @return [Boolean] true if the node exists
164
- def exist?
165
- fail 'not implemented'
166
- end
167
-
168
158
  # Works like #rels method but instead returns the nodes.
169
159
  # It does try to load a Ruby wrapper around each node
170
160
  # @abstract
@@ -198,6 +188,13 @@ module Neo4j
198
188
  def find_nodes(label, value = nil, session = Neo4j::Session.current!)
199
189
  session.find_nodes(label, value)
200
190
  end
191
+
192
+ def validate_match!(match)
193
+ invalid_match_keys = match.keys - [:type, :dir, :between]
194
+ fail "Invalid match keys: #{invalid_match_keys.inspect}" if !invalid_match_keys.empty?
195
+
196
+ fail "Invalid dir: #{match[:dir]}" if ![nil, :incoming, :outgoing, :both].include?(match[:dir])
197
+ end
201
198
  end
202
199
 
203
200
  def initialize
@@ -7,16 +7,11 @@ module Neo4j
7
7
  get_property(key)
8
8
  end
9
9
 
10
-
11
10
  # Sets the neo4j property
12
11
  def []=(key, value)
13
- validate_property(value)
12
+ validate_property!(value)
14
13
 
15
- if value.nil?
16
- remove_property(key)
17
- else
18
- set_property(key, value)
19
- end
14
+ set_property(key, value)
20
15
  end
21
16
  end
22
17
  end
@@ -13,7 +13,7 @@ module Neo4j
13
13
  VALID_PROPERTY_VALUE_CLASSES.include?(value.class)
14
14
  end
15
15
 
16
- def validate_property(value)
16
+ def validate_property!(value)
17
17
  return if valid_property?(value)
18
18
 
19
19
  fail Neo4j::PropertyValidator::InvalidPropertyException, "Not valid Neo4j Property value #{value.class}, valid: #{Neo4j::Node::VALID_PROPERTY_VALUE_CLASSES.to_a.join(', ')}"
data/lib/neo4j/session.rb CHANGED
@@ -50,6 +50,7 @@ module Neo4j
50
50
  end
51
51
  end
52
52
 
53
+ class InitializationError < RuntimeError; end
53
54
 
54
55
  # Performs a cypher query. See {Neo4j::Core::Query} for more details, but basic usage looks like:
55
56
  #
@@ -94,21 +95,29 @@ module Neo4j
94
95
  #
95
96
  # @see also Neo4j::Server::CypherSession#open for :server_db params
96
97
  # @param db_type the type of database, e.g. :embedded_db, or :server_db
97
- def open(db_type = :server_db, *params)
98
- register(create_session(db_type, params))
98
+ # @param [String] endpoint_url The path to the server, either a URL or path to embedded DB
99
+ # @param [Hash] params Additional configuration options
100
+ def open(db_type = :server_db, endpoint_url = nil, params = {})
101
+ validate_session_num!(db_type)
102
+ name = params[:name]
103
+ default = params[:default]
104
+ [:name, :default].each { |k| params.delete(k) }
105
+ register(create_session(db_type, endpoint_url, params), name, default)
99
106
  end
100
107
 
101
- def open_named(db_type, name, default = nil, *params)
102
- fail 'Multiple sessions is currently only supported for Neo4j Server connections.' unless db_type == :server_db
103
- register(create_session(db_type, params), name, default)
108
+ # @private
109
+ def validate_session_num!(db_type)
110
+ return unless current && db_type == :embedded_db
111
+ fail InitializationError, 'Multiple sessions are not supported by Neo4j Embedded.'
104
112
  end
113
+ private :validate_session_num!
105
114
 
106
115
  # @private
107
- def create_session(db_type, params = {})
116
+ def create_session(db_type, endpoint_url, params = {})
108
117
  unless @@factories[db_type]
109
- fail "Can't connect to database '#{db_type}', available #{@@factories.keys.join(',')}"
118
+ fail InitializationError, "Can't connect to database '#{db_type}', available #{@@factories.keys.join(',')}"
110
119
  end
111
- @@factories[db_type].call(*params)
120
+ @@factories[db_type].call(endpoint_url, params)
112
121
  end
113
122
 
114
123
  # @return [Neo4j::Session] the current session
@@ -140,11 +149,11 @@ module Neo4j
140
149
 
141
150
  # Registers a callback which will be called immediately if session is already available,
142
151
  # or called when it later becomes available.
143
- def on_session_available(&callback)
144
- callback.call(Neo4j::Session.current) if Neo4j::Session.current
152
+ def on_session_available
153
+ yield Neo4j::Session.current if Neo4j::Session.current
145
154
 
146
155
  add_listener do |event, data|
147
- callback.call(data) if event == :session_available
156
+ yield data if event == :session_available
148
157
  end
149
158
  end
150
159
 
@@ -159,6 +168,10 @@ module Neo4j
159
168
  "#{gem}-gem/#{version} (https://github.com/neo4jrb/#{gem})"
160
169
  end
161
170
 
171
+ def clear_listeners
172
+ @@listeners = []
173
+ end
174
+
162
175
  # @private
163
176
  def add_listener(&listener)
164
177
  _listeners << listener
@@ -13,7 +13,10 @@ module Neo4j
13
13
  # Toggles the status of Neo4j 2.2's basic auth
14
14
  def toggle_auth(status, source_text)
15
15
  status_string = status == :enable ? 'true' : 'false'
16
- set_property(source_text, 'dbms.security.authorization_enabled', status_string)
16
+ %w(dbms.security.authorization_enabled dbms.security.auth_enabled).each do |key|
17
+ source_text = set_property(source_text, key, status_string)
18
+ end
19
+ source_text
17
20
  end
18
21
 
19
22
  # POSTs to an endpoint with the form required to change a Neo4j password
@@ -7,48 +7,36 @@ require 'httparty'
7
7
  require File.expand_path('../config_server', __FILE__)
8
8
 
9
9
  namespace :neo4j do
10
- def download_neo4j(file)
11
- file_name, download_url = if OS::Underlying.windows?
12
- ['neo4j.zip', "http://dist.neo4j.org/neo4j-#{file}-windows.zip"]
13
- else
14
- ['neo4j-unix.tar.gz', "http://dist.neo4j.org/neo4j-#{file}-unix.tar.gz"]
15
- end
16
-
17
- unless File.exist?(file_name)
18
- # check if file is available
19
- status = HTTParty.head(download_url).code
20
- fail "#{file} is not available to download, try a different version" if status < 200 || status >= 300
21
- df = File.open(file_name, 'wb')
22
- success = false
23
- begin
24
- df << HTTParty.get(download_url)
25
- success = true
26
- ensure
27
- df.close
28
- File.delete(file_name) unless success
29
- end
30
- end
10
+ def file_name
11
+ OS::Underlying.windows? ? 'neo4j.zip' : 'neo4j-unix.tar.gz'
12
+ end
13
+
14
+ def download_url(edition)
15
+ "http://dist.neo4j.org/neo4j-#{edition}-#{OS::Underlying.windows? ? 'windows.zip' : 'unix.tar.gz'}"
16
+ end
17
+
18
+ def download_neo4j_unless_exists(edition)
19
+ download_neo4j(edition) unless File.exist?(file_name)
20
+ end
21
+
22
+ def download_neo4j(edition)
23
+ success = false
31
24
 
32
- # # http://download.neo4j.org/artifact?edition=community&version=2.1.2&distribution=tarball&dlid=3462770&_ga=1.110610309.1220184053.1399636580
33
- #
34
- # parsed_url = URI.parse(download_url)
35
- #
36
- # puts "parsed_url.host #{parsed_url.host} port #{parsed_url.port} uri: #{parsed_url.request_uri}"
37
- # Net::HTTP.start(parsed_url.host, parsed_url.port) do |http|
38
- # request = Net::HTTP::Get.new parsed_url.request_uri
39
- # http.request request do |response|
40
- # File.open 'large_file.tar.gz', 'wb' do |io|
41
- # response.read_body do |chunk|
42
- # io.write chunk
43
- # end
44
- # end
45
- # end
46
- # end
47
- #
48
- # puts "DOWN LOAD URL #{download_url}, exist #{file_name} : #{File.exist?(file_name)}"
49
- #
25
+ File.open(file_name, 'wb') do |file|
26
+ file << request_url(download_url(edition))
27
+ success = true
28
+ end
50
29
 
51
30
  file_name
31
+ ensure
32
+ File.delete(file_name) unless success
33
+ end
34
+
35
+ def request_url(url)
36
+ status = HTTParty.head(url).code
37
+ fail "#{edition} is not available to download, try a different version" if status < 200 || status >= 300
38
+
39
+ HTTParty.get(url)
52
40
  end
53
41
 
54
42
  def get_environment(args)
@@ -67,24 +55,36 @@ namespace :neo4j do
67
55
  def start_server(command, args)
68
56
  puts "Starting Neo4j #{get_environment(args)}..."
69
57
  if OS::Underlying.windows?
70
- if `reg query "HKU\\S-1-5-19"`.size > 0
71
- `#{install_location(args)}/bin/Neo4j.bat #{command}` # start service
72
- else
73
- puts 'Starting Neo4j directly, not as a service.'
74
- `#{install_location(args)}/bin/Neo4j.bat`
75
- end
58
+ start_windows_server(command, args)
59
+ else
60
+ start_starnix_server(command, args)
61
+ end
62
+ end
63
+
64
+ def system_or_fail(command)
65
+ system(command) or fail "Unable to run: #{command}" # rubocop:disable Style/AndOr
66
+ end
67
+
68
+ def start_windows_server(command, args)
69
+ if system_or_fail!('reg query "HKU\\S-1-5-19"').size > 0
70
+ system_or_fail("#{install_location(args)}/bin/Neo4j.bat #{command}") # start service
76
71
  else
77
- `#{install_location(args)}/bin/neo4j #{command}`
72
+ puts 'Starting Neo4j directly, not as a service.'
73
+ system_or_fail("#{install_location(args)}/bin/Neo4j.bat")
78
74
  end
79
75
  end
80
76
 
77
+ def start_starnix_server(command, args)
78
+ system_or_fail("#{install_location(args)}/bin/neo4j #{command}")
79
+ end
80
+
81
81
  desc 'Install Neo4j with auth disabled in v2.2+, example neo4j:install[community-2.1.3,development]'
82
82
  task :install, :edition, :environment do |_, args|
83
- file = args[:edition]
83
+ edition = args[:edition]
84
84
  environment = get_environment(args)
85
- puts "Installing Neo4j-#{file} environment: #{environment}"
85
+ puts "Installing Neo4j-#{edition} environment: #{environment}"
86
86
 
87
- downloaded_file = download_neo4j file
87
+ downloaded_file = download_neo4j_unless_exists edition
88
88
 
89
89
  if OS::Underlying.windows?
90
90
  # Extract and move to neo4j directory
@@ -100,20 +100,20 @@ namespace :neo4j do
100
100
  end
101
101
  end
102
102
  end
103
- FileUtils.mv "neo4j-#{file}", install_location(args)
103
+ FileUtils.mv "neo4j-#{edition}", install_location(args)
104
104
  FileUtils.rm downloaded_file
105
105
  end
106
106
 
107
107
  # Install if running with Admin Privileges
108
- if `reg query "HKU\\S-1-5-19"`.size > 0
109
- `"#{install_location(args)}/bin/neo4j install"`
108
+ if system_or_fail('reg query "HKU\\S-1-5-19"').size > 0
109
+ system_or_fail("\"#{install_location(args)}/bin/neo4j install\"")
110
110
  puts 'Neo4j Installed as a service.'
111
111
  end
112
112
 
113
113
  else
114
- `tar -xvf #{downloaded_file}`
115
- `mv neo4j-#{file} #{install_location(args)}`
116
- `rm #{downloaded_file}`
114
+ system_or_fail("tar -xvf #{downloaded_file}")
115
+ system_or_fail("mv neo4j-#{edition} #{install_location(args)}")
116
+ system_or_fail("rm #{downloaded_file}")
117
117
  puts 'Neo4j Installed in to neo4j directory.'
118
118
  end
119
119
  rake_auth_toggle(args, :disable) unless /-2\.0|1\.[0-9]/.match(args[:edition])
@@ -141,81 +141,58 @@ namespace :neo4j do
141
141
  File.open(location, 'w') { |file| file.puts replace }
142
142
  end
143
143
 
144
+ def validate_is_system_admin!
145
+ return unless OS::Underlying.windows?
146
+ return if system_or_fail("reg query \"HKU\\S-1-5-19\"").size > 0
147
+
148
+ fail 'You do not have administrative rights to stop the Neo4j Service'
149
+ end
150
+
151
+ def run_neo4j_command_or_fail!(args, command)
152
+ binary = OS::Underlying.windows? ? 'Neo4j.bat' : 'neo4j'
153
+
154
+ system_or_fail("#{install_location(args)}/bin/#{binary} #{command}")
155
+ end
156
+
144
157
  desc 'Stop the Neo4j Server'
145
158
  task :stop, :environment do |_, args|
146
159
  puts "Stopping Neo4j #{get_environment(args)}..."
147
- if OS::Underlying.windows?
148
- if `reg query "HKU\\S-1-5-19"`.size > 0
149
- `#{install_location(args)}/bin/Neo4j.bat stop` # stop service
150
- else
151
- puts 'You do not have administrative rights to stop the Neo4j Service'
152
- end
153
- else
154
- `#{install_location(args)}/bin/neo4j stop`
155
- end
160
+ validate_is_system_admin!
161
+
162
+ run_neo4j_command_or_fail!(args, :stop)
156
163
  end
157
164
 
158
165
  desc 'Get info the Neo4j Server'
159
166
  task :info, :environment do |_, args|
160
167
  puts "Info from Neo4j #{get_environment(args)}..."
161
- if OS::Underlying.windows?
162
- if `reg query "HKU\\S-1-5-19"`.size > 0
163
- `#{install_location(args)}/bin/Neo4j.bat info` # stop service
164
- else
165
- puts 'You do not have administrative rights to get info from the Neo4j Service'
166
- end
167
- else
168
- puts `#{install_location(args)}/bin/neo4j info`
169
- end
168
+ validate_is_system_admin!
169
+
170
+ run_neo4j_command_or_fail!(args, :info)
170
171
  end
171
172
 
172
173
  desc 'Restart the Neo4j Server'
173
174
  task :restart, :environment do |_, args|
174
175
  puts "Restarting Neo4j #{get_environment(args)}..."
175
- if OS::Underlying.windows?
176
- if `reg query "HKU\\S-1-5-19"`.size > 0
177
- `#{install_location(args)}/bin/Neo4j.bat restart`
178
- else
179
- puts 'You do not have administrative rights to restart the Neo4j Service'
180
- end
181
- else
182
- `#{install_location(args)}/bin/neo4j restart`
183
- end
176
+ validate_is_system_admin!
177
+
178
+ run_neo4j_command_or_fail!(args, :restart)
184
179
  end
185
180
 
186
181
  desc 'Reset the Neo4j Server'
187
182
  task :reset_yes_i_am_sure, :environment do |_, args|
188
- # Stop the server
189
- if OS::Underlying.windows?
190
- if `reg query "HKU\\S-1-5-19"`.size > 0
191
- `#{install_location(args)}/bin/Neo4j.bat stop`
192
-
193
- # Reset the database
194
- FileUtils.rm_rf("#{install_location(args)}/data/graph.db")
195
- FileUtils.mkdir("#{install_location(args)}/data/graph.db")
196
-
197
- # Remove log files
198
- FileUtils.rm_rf("#{install_location(args)}/data/log")
199
- FileUtils.mkdir("#{install_location(args)}/data/log")
183
+ validate_is_system_admin!
200
184
 
201
- `#{install_location(args)}/bin/Neo4j.bat start`
202
- else
203
- puts 'You do not have administrative rights to reset the Neo4j Service'
204
- end
205
- else
206
- `#{install_location(args)}/bin/neo4j stop`
185
+ run_neo4j_command_or_fail!(args, :stop)
207
186
 
208
- # Reset the database
209
- FileUtils.rm_rf("#{install_location(args)}/data/graph.db")
210
- FileUtils.mkdir("#{install_location(args)}/data/graph.db")
187
+ # Reset the database
188
+ FileUtils.rm_rf("#{install_location(args)}/data/graph.db")
189
+ FileUtils.mkdir("#{install_location(args)}/data/graph.db")
211
190
 
212
- # Remove log files
213
- FileUtils.rm_rf("#{install_location(args)}/data/log")
214
- FileUtils.mkdir("#{install_location(args)}/data/log")
191
+ # Remove log files
192
+ FileUtils.rm_rf("#{install_location(args)}/data/log")
193
+ FileUtils.mkdir("#{install_location(args)}/data/log")
215
194
 
216
- # Start the server
217
- `#{install_location(args)}/bin/neo4j start`
218
- end
195
+ run_neo4j_command_or_fail!(args, :start)
219
196
  end
220
197
 
221
198
  desc 'Neo4j 2.2: Change connection password'
@@ -24,7 +24,6 @@ module Neo4j
24
24
  def mark_expired
25
25
  @expired = true
26
26
  end
27
- alias_method :expired, :mark_expired
28
27
 
29
28
  def expired?
30
29
  !!@expired
@@ -62,7 +61,7 @@ module Neo4j
62
61
  return if @pushed_nested >= 0
63
62
  fail "Can't commit transaction, already committed" if @pushed_nested < -1
64
63
  Neo4j::Transaction.unregister(self)
65
- failed? ? _delete_tx : _commit_tx
64
+ failed? ? delete : commit
66
65
  end
67
66
  end
68
67
 
@@ -79,20 +78,14 @@ module Neo4j
79
78
 
80
79
  return yield(nil) unless run_in_tx
81
80
 
82
- begin
83
- tx = Neo4j::Transaction.new
84
- ret = yield tx
85
- rescue Exception => e # rubocop:disable Lint/RescueException
86
- if e.respond_to?(:cause) && e.cause.respond_to?(:print_stack_trace)
87
- puts "Java Exception in a transaction, cause: #{e.cause}"
88
- e.cause.print_stack_trace
89
- end
90
- tx.mark_failed unless tx.nil?
91
- raise
92
- ensure
93
- tx.close unless tx.nil?
94
- end
95
- ret
81
+ tx = Neo4j::Transaction.new
82
+ yield tx
83
+ rescue Exception => e # rubocop:disable Lint/RescueException
84
+ print_exception_cause(e)
85
+ tx.mark_failed unless tx.nil?
86
+ raise
87
+ ensure
88
+ tx.close unless tx.nil?
96
89
  end
97
90
 
98
91
  # @return [Neo4j::Transaction]
@@ -100,6 +93,14 @@ module Neo4j
100
93
  Thread.current[:neo4j_curr_tx]
101
94
  end
102
95
 
96
+ # @private
97
+ def print_exception_cause(exception)
98
+ return if !exception.respond_to?(:cause) || !exception.cause.respond_to?(:print_stack_trace)
99
+
100
+ puts "Java Exception in a transaction, cause: #{exception.cause}"
101
+ exception.cause.print_stack_trace
102
+ end
103
+
103
104
  # @private
104
105
  def unregister(tx)
105
106
  Thread.current[:neo4j_curr_tx] = nil if tx == Thread.current[:neo4j_curr_tx]
@@ -44,7 +44,7 @@ module Neo4j
44
44
  end
45
45
 
46
46
  # Cypher Helper
47
- def cypher_prop_list(props)
47
+ def cypher_prop_list!(props)
48
48
  return nil unless props
49
49
  props.reject! { |_, v| v.nil? }
50
50
  {props: props.each { |k, v| props[k] = create_escape_value(v) }}