neo4j-java-driver 0.1.1-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +8 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +68 -0
  5. data/Rakefile +16 -0
  6. data/jruby/neo4j/driver.rb +53 -0
  7. data/jruby/neo4j/driver/ext/exception_checkable.rb +21 -0
  8. data/jruby/neo4j/driver/ext/exception_mapper.rb +48 -0
  9. data/jruby/neo4j/driver/ext/graph_database.rb +56 -0
  10. data/jruby/neo4j/driver/ext/internal/summary/internal_result_summary.rb +17 -0
  11. data/jruby/neo4j/driver/ext/internal_driver.rb +18 -0
  12. data/jruby/neo4j/driver/ext/internal_record.rb +21 -0
  13. data/jruby/neo4j/driver/ext/internal_statement_result.rb +22 -0
  14. data/jruby/neo4j/driver/ext/map_accessor.rb +17 -0
  15. data/jruby/neo4j/driver/ext/ruby_converter.rb +55 -0
  16. data/jruby/neo4j/driver/ext/run_override.rb +73 -0
  17. data/jruby/neo4j/driver/ext/start_end_naming.rb +17 -0
  18. data/jruby/neo4j/driver/ext/statement.rb +13 -0
  19. data/lib/loader.rb +18 -0
  20. data/lib/neo4j/driver/auto_closable.rb +32 -0
  21. data/lib/neo4j/driver/exceptions/authentication_exception.rb +10 -0
  22. data/lib/neo4j/driver/exceptions/client_exception.rb +10 -0
  23. data/lib/neo4j/driver/exceptions/database_exception.rb +10 -0
  24. data/lib/neo4j/driver/exceptions/illegal_state_exception.rb +10 -0
  25. data/lib/neo4j/driver/exceptions/neo4j_exception.rb +18 -0
  26. data/lib/neo4j/driver/exceptions/no_such_record_exception.rb +33 -0
  27. data/lib/neo4j/driver/exceptions/protocol_exception.rb +10 -0
  28. data/lib/neo4j/driver/exceptions/security_exception.rb +10 -0
  29. data/lib/neo4j/driver/exceptions/service_unavailable_exception.rb +10 -0
  30. data/lib/neo4j/driver/exceptions/session_expired_exception.rb +10 -0
  31. data/lib/neo4j/driver/exceptions/transient_exception.rb +10 -0
  32. data/lib/neo4j/driver/exceptions/untrusted_server_exception.rb +10 -0
  33. data/lib/neo4j/driver/internal/duration_normalizer.rb +42 -0
  34. data/lib/neo4j/driver/internal/ruby_signature.rb +18 -0
  35. data/lib/neo4j/driver/types/byte_array.rb +17 -0
  36. data/lib/neo4j/driver/types/local_date_time.rb +20 -0
  37. data/lib/neo4j/driver/types/local_time.rb +19 -0
  38. data/lib/neo4j/driver/types/offset_time.rb +19 -0
  39. data/lib/neo4j/driver/types/point.rb +39 -0
  40. data/lib/neo4j/driver/types/time.rb +43 -0
  41. data/lib/neo4j/driver/version.rb +7 -0
  42. data/lib/neo4j_ruby_driver.rb +19 -0
  43. data/neo4j-ruby-driver.gemspec +63 -0
  44. metadata +216 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2420844cd278e077b7afec48f624b6af9d86844cf8276b7a04322ab7b6c7d257
4
+ data.tar.gz: dbb48e01991356e19df8e4214e6c15bc7a7c6b0d530a339ce1b3d204256b19d4
5
+ SHA512:
6
+ metadata.gz: e2775629940356371ce687ec466cc8e6c2e74f8e302df8f4316d4dfd521e032d403b67e26db483559abf4a4fae5ab3ed6fddf2da1c458e111402c0dd65628beb
7
+ data.tar.gz: a89828e1144078ab4c01f7971ebdfb755171219ef84de6c366920ee8d3729c82fb3e2f721d366ba74fe17b69774c62b10bf5953949f9a0cc5b8a0ec72a3a13c7
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in neo4j-driver.gemspec
8
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Heinrich Klobuczek
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,68 @@
1
+ # Neo4j::Driver
2
+
3
+ Proposal for an API for neo4j ruby driver. This gem contains reference implementation in jruby with most of the features
4
+ completed.
5
+ The proposed API is heavilly inspired but the java and javascipt driver. Please add comments and suggestions if you feel there
6
+ is better idiomatic alternative in ruby.
7
+
8
+ The file `doc/dev_manual_examples_spec.rb` contains all the code examples included in the
9
+ [Chapter 4. Drivers][https://neo4j.com/docs/developer-manual/3.4/drivers/] of the Developer Manual and should be
10
+ reviewed side by side with that manual.
11
+
12
+ ## Installation
13
+
14
+ Add this line to your application's Gemfile:
15
+
16
+ ```ruby
17
+ gem 'neo4j-ruby-driver'
18
+ ```
19
+
20
+ And then execute:
21
+
22
+ $ bundle
23
+
24
+ Or install it yourself as:
25
+
26
+ $ gem install neo4j-ruby-driver
27
+
28
+ ## Usage
29
+
30
+ Refer to https://neo4j.com/docs/developer-manual/3.4/drivers/.
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies.
35
+ In order to run test by running `rake spec` you may have to set your own `NEO4J_BOLT_URL` URI or it will
36
+ fallback to `bolt://localhost:7687`.
37
+ You can also run `bin/console` for an interactive prompt that will allow you to experiment.
38
+
39
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
40
+
41
+ This gem includes 2 different implementations: java driver based and another one using seabolt via ffi
42
+
43
+ For java driver based:
44
+
45
+ $ rvm use jruby-9.2.5.0
46
+ $ bundle
47
+ $ rspec
48
+
49
+ FFI based:
50
+
51
+ $ rvm use 2.5.3 # or jruby-9.2.5.0
52
+ $ SEABOLT_LIB=~/seabolt/build/dist/lib/libseabolt17.dylib bundle
53
+ $ SEABOLT_LIB=~/seabolt/build/dist/lib/libseabolt17.dylib rspec
54
+
55
+ Please note that seabolt for now has to be installed separately: https://github.com/neo4j-drivers/seabolt
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/neo4j-driver.
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
+
65
+
66
+ [https://neo4j.com/docs/developer-manual/3.4/drivers/]: https://neo4j.com/docs/developer-manual/3.4/drivers/
67
+
68
+ [https://neo4j.com/docs/developer-manual/3.4/drivers/]: https://neo4j.com/docs/developer-manual/3.4/drivers/
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'neo4j/rake_tasks'
6
+
7
+ if RUBY_PLATFORM.match?(/java/) && ENV['SEABOLT_LIB']&.length&.positive?
8
+ require 'jars/installer'
9
+ task :install_jars do
10
+ Jars::Installer.vendor_jars!('jruby')
11
+ end
12
+ end
13
+
14
+ RSpec::Core::RakeTask.new(:spec)
15
+
16
+ task default: :spec
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash/keys'
4
+ require 'date'
5
+ require 'loader'
6
+ require 'neo4j-java-driver_jars'
7
+
8
+ module Neo4j
9
+ module Driver
10
+ include_package 'org.neo4j.driver.v1'
11
+
12
+ module Net
13
+ include_package 'org.neo4j.driver.v1.net'
14
+ end
15
+
16
+ module Summary
17
+ include_package 'org.neo4j.driver.v1.summary'
18
+ end
19
+
20
+ module Types
21
+ include_package 'org.neo4j.driver.v1.types'
22
+ end
23
+
24
+ # Workaround for missing zeitwerk support in jruby-9.2.8.0
25
+ if RUBY_PLATFORM.match?(/java/)
26
+ module Ext
27
+ module Internal
28
+ module Summary
29
+ end
30
+ end
31
+ end
32
+ end
33
+ # End workaround
34
+ end
35
+ end
36
+
37
+ Loader.load
38
+
39
+ Java::OrgNeo4jDriverInternal::InternalDriver.prepend Neo4j::Driver::Ext::InternalDriver
40
+ Java::OrgNeo4jDriverInternal::InternalNode.include Neo4j::Driver::Ext::MapAccessor
41
+ Java::OrgNeo4jDriverInternal::InternalPath.include Neo4j::Driver::Ext::MapAccessor
42
+ Java::OrgNeo4jDriverInternal::InternalPath.include Neo4j::Driver::Ext::StartEndNaming
43
+ Java::OrgNeo4jDriverInternal::InternalPath::SelfContainedSegment.include Neo4j::Driver::Ext::StartEndNaming
44
+ Java::OrgNeo4jDriverInternal::InternalRecord.prepend Neo4j::Driver::Ext::InternalRecord
45
+ Java::OrgNeo4jDriverInternal::InternalRelationship.include Neo4j::Driver::Ext::MapAccessor
46
+ Java::OrgNeo4jDriverInternal::InternalStatementResult.prepend Neo4j::Driver::Ext::InternalStatementResult
47
+ Java::OrgNeo4jDriverInternal::ExplicitTransaction.prepend Neo4j::Driver::Ext::RunOverride
48
+ Java::OrgNeo4jDriverInternal::NetworkSession.prepend Neo4j::Driver::Ext::RunOverride
49
+ Java::OrgNeo4jDriverInternalSummary::InternalResultSummary.prepend Neo4j::Driver::Ext::Internal::Summary::InternalResultSummary
50
+ Java::OrgNeo4jDriverInternalValue::ValueAdapter.include Neo4j::Driver::Ext::RubyConverter
51
+ Java::OrgNeo4jDriverV1::GraphDatabase.singleton_class.prepend Neo4j::Driver::Ext::GraphDatabase
52
+ Java::OrgNeo4jDriverV1::Statement.prepend Neo4j::Driver::Ext::Statement
53
+ Java::OrgNeo4jDriverV1Exceptions::Neo4jException.include Neo4j::Driver::Ext::ExceptionMapper
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module ExceptionCheckable
7
+ def check
8
+ yield
9
+ rescue Java::OrgNeo4jDriverV1Exceptions::Neo4jException => e
10
+ e.reraise
11
+ rescue Java::OrgNeo4jDriverV1Exceptions::NoSuchRecordException => e
12
+ raise Neo4j::Driver::Exceptions::NoSuchRecordException, e.message
13
+ rescue Java::OrgNeo4jDriverV1Exceptions::UntrustedServerException => e
14
+ raise Neo4j::Driver::Exceptions::UntrustedServerException, e.message
15
+ rescue Java::JavaLang::IllegalStateException => e
16
+ raise Neo4j::Driver::Exceptions::IllegalStateException, e.message
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ java_import org.neo4j.driver.v1.exceptions.AuthenticationException
4
+ java_import org.neo4j.driver.v1.exceptions.ClientException
5
+ java_import org.neo4j.driver.v1.exceptions.DatabaseException
6
+ java_import org.neo4j.driver.v1.exceptions.ProtocolException
7
+ java_import org.neo4j.driver.v1.exceptions.SecurityException
8
+ java_import org.neo4j.driver.v1.exceptions.ServiceUnavailableException
9
+ java_import org.neo4j.driver.v1.exceptions.SessionExpiredException
10
+ java_import org.neo4j.driver.v1.exceptions.TransientException
11
+ java_import org.neo4j.driver.v1.exceptions.UntrustedServerException
12
+
13
+ module Neo4j
14
+ module Driver
15
+ module Ext
16
+ module ExceptionMapper
17
+ def reraise
18
+ raise mapped_exception&.new(code, message, self) || self
19
+ end
20
+
21
+ private
22
+
23
+ def mapped_exception
24
+ case self
25
+ when AuthenticationException
26
+ Neo4j::Driver::Exceptions::AuthenticationException
27
+ when ClientException
28
+ Neo4j::Driver::Exceptions::ClientException
29
+ when DatabaseException
30
+ Neo4j::Driver::Exceptions::DatabaseException
31
+ when ProtocolException
32
+ Neo4j::Driver::Exceptions::ProtocolException
33
+ when SecurityException
34
+ Neo4j::Driver::Exceptions::SecurityException
35
+ when ServiceUnavailableException
36
+ Neo4j::Driver::Exceptions::ServiceUnavailableException
37
+ when SessionExpiredException
38
+ Neo4j::Driver::Exceptions::SessionExpiredException
39
+ when TransientException
40
+ Neo4j::Driver::Exceptions::TransientException
41
+ else
42
+ nil
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,56 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module GraphDatabase
7
+ extend AutoClosable
8
+ include ExceptionCheckable
9
+
10
+ auto_closable :driver
11
+
12
+ def driver(uri, auth_token = Neo4j::Driver::AuthTokens.none, config = {})
13
+ check do
14
+ java_method(:driver, [java.lang.String, org.neo4j.driver.v1.AuthToken, org.neo4j.driver.v1.Config])
15
+ .call(uri, auth_token, to_java_config(config))
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def to_java_config(hash)
22
+ hash.reduce(Neo4j::Driver::Config.build) { |object, key_value| object.send(*config_method(*key_value)) }
23
+ .to_config
24
+ end
25
+
26
+ def config_method(key, value)
27
+ method = :"with_#{key}"
28
+ unit = nil
29
+ case key.to_s
30
+ when 'encryption'
31
+ unless value
32
+ method = :without_encryption
33
+ value = nil
34
+ end
35
+ when 'load_balancing_strategy'
36
+ value = load_balancing_strategy(value)
37
+ when /Time(out)?$/i
38
+ unit = java.util.concurrent.TimeUnit::SECONDS
39
+ end
40
+ [method, value, unit].compact
41
+ end
42
+
43
+ def load_balancing_strategy(value)
44
+ case value
45
+ when :least_connected
46
+ Config::LoadBalancingStrategy::LEAST_CONNECTED
47
+ when :round_robin
48
+ Config::LoadBalancingStrategy::ROUND_ROBIN
49
+ else
50
+ raise ArgumentError
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module Internal
7
+ module Summary
8
+ module InternalResultSummary
9
+ %i[result_available_after result_consumed_after].each do |method|
10
+ define_method(method) { super(Java::JavaUtilConcurrent::TimeUnit::MILLISECONDS) }
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module InternalDriver
7
+ extend AutoClosable
8
+
9
+ auto_closable :session
10
+
11
+ def session(*args)
12
+ java_method(:session, [org.neo4j.driver.v1.AccessMode, java.lang.Iterable])
13
+ .call(*Neo4j::Driver::Internal::RubySignature.session(args))
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module InternalRecord
7
+ def values
8
+ java_send(:values).map(&:as_ruby_object)
9
+ end
10
+
11
+ define_method(:[]) do |key|
12
+ java_method(:get, [key.is_a?(Integer) ? Java::int : java.lang.String]).call(key).as_ruby_object
13
+ end
14
+
15
+ def first
16
+ java_method(:get, [Java::int]).call(0).as_ruby_object
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module InternalStatementResult
7
+ include Enumerable
8
+ include ExceptionCheckable
9
+
10
+ %i[has_next? next keys single consume].each do |method|
11
+ define_method(method) do |*args, &block|
12
+ check { super(*args, &block) }
13
+ end
14
+ end
15
+
16
+ def each(&block)
17
+ check { stream.for_each(&block) }
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module MapAccessor
7
+ def properties
8
+ as_map.to_hash.symbolize_keys
9
+ end
10
+
11
+ def [](key)
12
+ get(key.to_s).as_ruby_object
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Ext
6
+ module RubyConverter
7
+ def as_ruby_object
8
+ case type_constructor
9
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::LIST
10
+ java_method(:asList, [org.neo4j.driver.v1.util.Function]).call(&:as_ruby_object).to_a
11
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::MAP
12
+ as_map(->(x) { x.as_ruby_object }, nil).to_hash.symbolize_keys
13
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::DATE
14
+ date = as_local_date
15
+ Date.new(date.year, date.month_value, date.day_of_month)
16
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::DURATION
17
+ ActiveSupport::Duration.parse(as_iso_duration.to_s)
18
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::POINT
19
+ point = as_point
20
+ Types::Point.new(srid: point.srid, x: point.x, y: point.y, z: nullable(point.z))
21
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::BYTES
22
+ Types::ByteArray.new(String.from_java_bytes(as_byte_array))
23
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::TIME
24
+ Types::OffsetTime.parse(as_offset_time.to_string)
25
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::LOCAL_TIME
26
+ Types::LocalTime.parse(as_local_time.to_string)
27
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::LOCAL_DATE_TIME
28
+ Types::LocalDateTime.parse(as_local_date_time.to_string)
29
+ when Java::OrgNeo4jDriverInternalTypes::TypeConstructor::DATE_TIME
30
+ to_time
31
+ else
32
+ as_object
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def to_time
39
+ time = as_zoned_date_time
40
+ zone_id = time.zone.id
41
+ if /^Z|[+\-][0-9]{2}:[0-9]{2}$/.match?(zone_id)
42
+ Time.parse(time.to_string)
43
+ else
44
+ instant = time.to_instant
45
+ Time.at(instant.epoch_second, instant.nano, :nsec).in_time_zone(TZInfo::Timezone.get(zone_id))
46
+ end
47
+ end
48
+
49
+ def nullable(double)
50
+ double unless double == java.lang.Double::NaN
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end