neospace 0.0.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (109) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE.txt +21 -0
  3. data/README.md +88 -0
  4. data/ffi/bolt/address.rb +11 -0
  5. data/ffi/bolt/address_resolver.rb +12 -0
  6. data/ffi/bolt/address_set.rb +9 -0
  7. data/ffi/bolt/auth.rb +10 -0
  8. data/ffi/bolt/auto_releasable.rb +22 -0
  9. data/ffi/bolt/boolean.rb +9 -0
  10. data/ffi/bolt/bytes.rb +10 -0
  11. data/ffi/bolt/config.rb +45 -0
  12. data/ffi/bolt/connection.rb +44 -0
  13. data/ffi/bolt/connector.rb +17 -0
  14. data/ffi/bolt/dictionary.rb +15 -0
  15. data/ffi/bolt/error.rb +74 -0
  16. data/ffi/bolt/float.rb +9 -0
  17. data/ffi/bolt/integer.rb +9 -0
  18. data/ffi/bolt/library.rb +12 -0
  19. data/ffi/bolt/lifecycle.rb +9 -0
  20. data/ffi/bolt/list.rb +10 -0
  21. data/ffi/bolt/log.rb +16 -0
  22. data/ffi/bolt/socket_options.rb +14 -0
  23. data/ffi/bolt/status.rb +25 -0
  24. data/ffi/bolt/string.rb +9 -0
  25. data/ffi/bolt/structure.rb +10 -0
  26. data/ffi/bolt/value.rb +35 -0
  27. data/ffi/neo4j/driver.rb +60 -0
  28. data/ffi/neo4j/driver/access_mode.rb +10 -0
  29. data/ffi/neo4j/driver/auth_tokens.rb +18 -0
  30. data/ffi/neo4j/driver/config.rb +40 -0
  31. data/ffi/neo4j/driver/graph_database.rb +52 -0
  32. data/ffi/neo4j/driver/internal/async/access_mode_connection.rb +19 -0
  33. data/ffi/neo4j/driver/internal/async/direct_connection.rb +106 -0
  34. data/ffi/neo4j/driver/internal/bolt_server_address.rb +18 -0
  35. data/ffi/neo4j/driver/internal/bookmarks_holder.rb +30 -0
  36. data/ffi/neo4j/driver/internal/direct_connection_provider.rb +28 -0
  37. data/ffi/neo4j/driver/internal/driver_factory.rb +125 -0
  38. data/ffi/neo4j/driver/internal/error_handling.rb +108 -0
  39. data/ffi/neo4j/driver/internal/explicit_transaction.rb +146 -0
  40. data/ffi/neo4j/driver/internal/handlers/pull_all_response_handler.rb +105 -0
  41. data/ffi/neo4j/driver/internal/handlers/response_handler.rb +49 -0
  42. data/ffi/neo4j/driver/internal/handlers/run_response_handler.rb +32 -0
  43. data/ffi/neo4j/driver/internal/handlers/session_pull_all_response_handler.rb +32 -0
  44. data/ffi/neo4j/driver/internal/handlers/transaction_pull_all_response_handler.rb +23 -0
  45. data/ffi/neo4j/driver/internal/internal_driver.rb +45 -0
  46. data/ffi/neo4j/driver/internal/internal_logger.rb +32 -0
  47. data/ffi/neo4j/driver/internal/internal_record.rb +26 -0
  48. data/ffi/neo4j/driver/internal/internal_resolver.rb +31 -0
  49. data/ffi/neo4j/driver/internal/internal_statement_result.rb +52 -0
  50. data/ffi/neo4j/driver/internal/messaging/bolt_protocol.rb +24 -0
  51. data/ffi/neo4j/driver/internal/messaging/v1/bolt_protocol_v1.rb +59 -0
  52. data/ffi/neo4j/driver/internal/messaging/v2/bolt_protocol_v2.rb +16 -0
  53. data/ffi/neo4j/driver/internal/messaging/v3/bolt_protocol_v3.rb +63 -0
  54. data/ffi/neo4j/driver/internal/network_session.rb +129 -0
  55. data/ffi/neo4j/driver/internal/retry/exponential_backoff_retry_logic.rb +80 -0
  56. data/ffi/neo4j/driver/internal/session_factory_impl.rb +28 -0
  57. data/ffi/neo4j/driver/internal/summary/internal_result_summary.rb +67 -0
  58. data/ffi/neo4j/driver/internal/summary/internal_server_info.rb +19 -0
  59. data/ffi/neo4j/driver/internal/summary/internal_summary_counters.rb +23 -0
  60. data/ffi/neo4j/driver/internal/util/metadata_extractor.rb +15 -0
  61. data/ffi/neo4j/driver/internal/value/base_time_value.rb +22 -0
  62. data/ffi/neo4j/driver/internal/value/date_value.rb +25 -0
  63. data/ffi/neo4j/driver/internal/value/duration_value.rb +27 -0
  64. data/ffi/neo4j/driver/internal/value/local_date_time_value.rb +24 -0
  65. data/ffi/neo4j/driver/internal/value/local_time_value.rb +19 -0
  66. data/ffi/neo4j/driver/internal/value/node_value.rb +18 -0
  67. data/ffi/neo4j/driver/internal/value/offset_time_value.rb +25 -0
  68. data/ffi/neo4j/driver/internal/value/path_value.rb +41 -0
  69. data/ffi/neo4j/driver/internal/value/point2_d_value.rb +24 -0
  70. data/ffi/neo4j/driver/internal/value/point3_d_value.rb +24 -0
  71. data/ffi/neo4j/driver/internal/value/relationship_value.rb +18 -0
  72. data/ffi/neo4j/driver/internal/value/structure_value.rb +42 -0
  73. data/ffi/neo4j/driver/internal/value/time_with_zone_id_value.rb +25 -0
  74. data/ffi/neo4j/driver/internal/value/time_with_zone_offset_value.rb +28 -0
  75. data/ffi/neo4j/driver/internal/value/unbound_relationship_value.rb +18 -0
  76. data/ffi/neo4j/driver/internal/value/value_adapter.rb +99 -0
  77. data/ffi/neo4j/driver/net/server_address.rb +13 -0
  78. data/ffi/neo4j/driver/statement.rb +15 -0
  79. data/ffi/neo4j/driver/summary/statement_type.rb +14 -0
  80. data/ffi/neo4j/driver/types/entity.rb +21 -0
  81. data/ffi/neo4j/driver/types/node.rb +16 -0
  82. data/ffi/neo4j/driver/types/path.rb +35 -0
  83. data/ffi/neo4j/driver/types/relationship.rb +19 -0
  84. data/lib/loader.rb +18 -0
  85. data/lib/neo4j/driver/auto_closable.rb +32 -0
  86. data/lib/neo4j/driver/exceptions/authentication_exception.rb +10 -0
  87. data/lib/neo4j/driver/exceptions/client_exception.rb +15 -0
  88. data/lib/neo4j/driver/exceptions/database_exception.rb +10 -0
  89. data/lib/neo4j/driver/exceptions/illegal_state_exception.rb +10 -0
  90. data/lib/neo4j/driver/exceptions/neo4j_exception.rb +23 -0
  91. data/lib/neo4j/driver/exceptions/no_such_record_exception.rb +33 -0
  92. data/lib/neo4j/driver/exceptions/protocol_exception.rb +10 -0
  93. data/lib/neo4j/driver/exceptions/security_exception.rb +10 -0
  94. data/lib/neo4j/driver/exceptions/service_unavailable_exception.rb +10 -0
  95. data/lib/neo4j/driver/exceptions/session_expired_exception.rb +10 -0
  96. data/lib/neo4j/driver/exceptions/transient_exception.rb +10 -0
  97. data/lib/neo4j/driver/exceptions/untrusted_server_exception.rb +10 -0
  98. data/lib/neo4j/driver/internal/duration_normalizer.rb +46 -0
  99. data/lib/neo4j/driver/internal/ruby_signature.rb +18 -0
  100. data/lib/neo4j/driver/internal/validator.rb +28 -0
  101. data/lib/neo4j/driver/types/bytes.rb +10 -0
  102. data/lib/neo4j/driver/types/local_date_time.rb +20 -0
  103. data/lib/neo4j/driver/types/local_time.rb +19 -0
  104. data/lib/neo4j/driver/types/offset_time.rb +19 -0
  105. data/lib/neo4j/driver/types/point.rb +39 -0
  106. data/lib/neo4j/driver/types/time.rb +43 -0
  107. data/lib/neo4j/driver/version.rb +7 -0
  108. data/lib/neo4j_ruby_driver.rb +20 -0
  109. metadata +314 -12
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Internal
6
+ class BoltServerAddress
7
+ include Net::ServerAddress
8
+
9
+ attr_reader :host, :port
10
+
11
+ def initialize(host, port)
12
+ @host = host
13
+ @port = port
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Internal
6
+ module BookmarksHolder
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ attr_reader :bookmarks
11
+ end
12
+
13
+ def initialize
14
+ @bookmarks = []
15
+ end
16
+
17
+ def bookmarks=(bookmarks)
18
+ bookmarks = Array(bookmarks).select(&:present?)
19
+ @bookmarks = bookmarks if bookmarks.present?
20
+ end
21
+
22
+ NO_OP = Class.new do
23
+ include BookmarksHolder
24
+
25
+ def bookmarks=; end
26
+ end.new
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Internal
6
+ class DirectConnectionProvider
7
+ include ErrorHandling
8
+
9
+ def initialize(connector, config)
10
+ @connector = connector
11
+ @config = config
12
+ end
13
+
14
+ def acquire_connection(mode)
15
+ Async::DirectConnection.new(@connector, mode, @config)
16
+ end
17
+
18
+ def verify_connectivity
19
+ acquire_connection(AccessMode::READ).release
20
+ end
21
+
22
+ def close
23
+ Bolt::Connector.destroy(@connector)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Internal
6
+ class DriverFactory
7
+ include ErrorHandling
8
+ BOLT_URI_SCHEME = 'bolt'
9
+ BOLT_ROUTING_URI_SCHEME = 'bolt+routing'
10
+ NEO4J_URI_SCHEME = 'neo4j'
11
+ DEFAULT_PORT = 7687
12
+
13
+ def new_instance(uri, auth_token, config)
14
+ uri = URI(uri)
15
+ routing_context = routing_context(uri)
16
+ connector, logger, resolver = create_connector(uri, auth_token, routing_context, config)
17
+ retry_logic = Retry::ExponentialBackoffRetryLogic.new(config[:max_transaction_retry_time], config[:logger])
18
+ create_driver(connector, logger, resolver, retry_logic, config).tap(&:verify_connectivity)
19
+ end
20
+
21
+ private
22
+
23
+ def create_connector(uri, auth_token, routing_context, config)
24
+ address = Bolt::Address.create(host(uri).gsub(/^\[(.*)\]$/, '\\1'), port(uri).to_s)
25
+ bolt_config = bolt_config(config)
26
+ logger = InternalLogger.register(bolt_config, config[:logger])
27
+ set_socket_options(bolt_config, config)
28
+ set_routing_context(bolt_config, routing_context)
29
+ set_scheme(bolt_config, uri, routing_context)
30
+ resolver = InternalResolver.register(bolt_config, config[:resolver])
31
+ [Bolt::Connector.create(address, auth_token, bolt_config), logger, resolver]
32
+ end
33
+
34
+ def bolt_config(config)
35
+ bolt_config = Bolt::Config.create
36
+ config.each do |key, value|
37
+ case key
38
+ when :max_connection_pool_size
39
+ check_error Bolt::Config.set_max_pool_size(bolt_config, value)
40
+ when :max_connection_life_time
41
+ check_error Bolt::Config.set_max_connection_life_time(bolt_config, DurationNormalizer.milliseconds(value))
42
+ when :connection_acquisition_timeout
43
+ check_error Bolt::Config.set_max_connection_acquisition_time(bolt_config,
44
+ DurationNormalizer.milliseconds(value))
45
+ when :encryption
46
+ check_error Bolt::Config.set_transport(
47
+ bolt_config,
48
+ value ? Bolt::Config::BOLT_TRANSPORT_ENCRYPTED : Bolt::Config::BOLT_TRANSPORT_PLAINTEXT
49
+ )
50
+ end
51
+ end
52
+ check_error Bolt::Config.set_user_agent(bolt_config, "neo4j-ruby-driver #{Neo4j::Driver::VERSION}")
53
+ bolt_config
54
+ end
55
+
56
+ def set_socket_options(bolt_config, config)
57
+ socket_options = Bolt::SocketOptions.create
58
+ config.each do |key, value|
59
+ case key
60
+ when :connection_timeout
61
+ check_error Bolt::SocketOptions.set_connect_timeout(socket_options,
62
+ DurationNormalizer.milliseconds(value))
63
+ when :keep_alive
64
+ check_error Bolt::SocketOptions.set_keep_alive(socket_options, value ? 1 : 0)
65
+ end
66
+ end
67
+ check_error Bolt::Config.set_socket_options(bolt_config, socket_options)
68
+ end
69
+
70
+ def routing_context(uri)
71
+ query = uri.query
72
+ return if query.blank?
73
+ URI.decode_www_form(query).to_h
74
+ end
75
+
76
+ def set_routing_context(bolt_config, routing_context)
77
+ value = Bolt::Value.create
78
+ check_error Bolt::Config.set_routing_context(bolt_config, Value::ValueAdapter.to_neo(value, routing_context))
79
+ end
80
+
81
+ def host(uri)
82
+ uri.host.tap { |host| raise ArgumentError, "Invalid address format `#{uri}`" unless host }
83
+ end
84
+
85
+ def port(uri)
86
+ uri.port&.tap { |port| raise ArgumentError, "Illegal port: #{port}" unless (0..65_535).cover?(port) } ||
87
+ DEFAULT_PORT
88
+ end
89
+
90
+ def set_scheme(bolt_config, uri, routing_context)
91
+ check_error Bolt::Config.set_scheme(bolt_config, scheme(uri, routing_context))
92
+ end
93
+
94
+ def scheme(uri, routing_context)
95
+ scheme = uri.scheme
96
+ case scheme
97
+ when BOLT_URI_SCHEME
98
+ assert_no_routing_context(uri, routing_context)
99
+ Bolt::Config::BOLT_SCHEME_DIRECT
100
+ when BOLT_ROUTING_URI_SCHEME, NEO4J_URI_SCHEME
101
+ Bolt::Config::BOLT_SCHEME_NEO4J
102
+ else
103
+ raise Exceptions::ClientException, "Unsupported URI scheme: #{scheme}"
104
+ end
105
+ end
106
+
107
+ def assert_no_routing_context(uri, routing_context)
108
+ if routing_context
109
+ raise ArgumentError, "Routing parameters are not supported with scheme 'bolt'. Given URI: '#{uri}'"
110
+ end
111
+ end
112
+
113
+ def create_driver(connector, logger, resolver, retry_logic, config)
114
+ connection_provider = DirectConnectionProvider.new(connector, config)
115
+ session_factory = create_session_factory(connection_provider, retry_logic, config)
116
+ InternalDriver.new(session_factory, logger, resolver)
117
+ end
118
+
119
+ def create_session_factory(connection_provider, retry_logic = nil, config = nil)
120
+ SessionFactoryImpl.new(connection_provider, retry_logic, config)
121
+ end
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,108 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Internal
6
+ module ErrorHandling
7
+ def check_error(error_code, status = nil)
8
+ case error_code
9
+ # Identifies a successful operation which is defined as 0
10
+ when Bolt::Error::BOLT_SUCCESS # 0
11
+ nil
12
+
13
+ # Permission denied
14
+ when Bolt::Error::BOLT_PERMISSION_DENIED # 7
15
+ throw Exceptions::AuthenticationException.new(error_code, 'Permission denied')
16
+
17
+ # Connection refused
18
+ when Bolt::Error::BOLT_CONNECTION_REFUSED # 11
19
+ throw Exceptions::ServiceUnavailableException.new(error_code, 'unable to acquire connection')
20
+
21
+ # Connection pool is full
22
+ when Bolt::Error::BOLT_POOL_FULL # 0x600
23
+ throw Exceptions::ClientException.new(
24
+ error_code,
25
+ 'Unable to acquire connection from the pool within configured maximum time of ' \
26
+ "#{DurationNormalizer.milliseconds(@config[:connection_acquisition_timeout])}ms"
27
+ )
28
+
29
+ # Routing table retrieval failed
30
+ when Bolt::Error::BOLT_ROUTING_UNABLE_TO_RETRIEVE_ROUTING_TABLE # 0x800
31
+ throw Exceptions::ServiceUnavailableException.new(
32
+ error_code,
33
+ 'Could not perform discovery. No routing servers available.'
34
+ )
35
+
36
+ # Error set in connection
37
+ when Bolt::Error::BOLT_CONNECTION_HAS_MORE_INFO, Bolt::Error::BOLT_STATUS_SET # 0xFFE, 0xFFF
38
+ status = Bolt::Connection.status(bolt_connection)
39
+ unqualified_error(error_code, status)
40
+ else
41
+ unqualified_error(error_code, status)
42
+ end
43
+ end
44
+
45
+ def on_failure(_error); end
46
+
47
+ def check_status(status)
48
+ check_error(Bolt::Status.get_error(status), status)
49
+ end
50
+
51
+ def with_status
52
+ status = Bolt::Status.create
53
+ yield status
54
+ ensure
55
+ check_status(status)
56
+ end
57
+
58
+ def new_neo4j_error(code:, message:)
59
+ case code.split('.')[1]
60
+ when 'ClientError'
61
+ if code.casecmp('Neo.ClientError.Security.Unauthorized').zero?
62
+ Exceptions::AuthenticationException
63
+ else
64
+ Exceptions::ClientException
65
+ end
66
+ when 'TransientError'
67
+ Exceptions::TransientException
68
+ else
69
+ Exceptions::DatabaseException
70
+ end.new(code, message)
71
+ end
72
+
73
+ private
74
+
75
+ def exception_class(state)
76
+ case state
77
+ when Bolt::Status::BOLT_CONNECTION_STATE_DEFUNCT
78
+ Exceptions::SessionExpiredException
79
+ else
80
+ Exceptions::Neo4jException
81
+ end
82
+ end
83
+
84
+ def throw(error)
85
+ on_failure(error)
86
+ raise error
87
+ end
88
+
89
+ def unqualified_error(error_code, status)
90
+ details = details(error_code, status)
91
+ throw exception_class(details[:state]).new(error_code,
92
+ details.map { |key, value| "#{key}: `#{value}`" }.join(', '))
93
+ end
94
+
95
+ def details(error_code, status)
96
+ details = {
97
+ code: error_code.to_s(16),
98
+ error: Bolt::Error.get_string(error_code)
99
+ }
100
+ return details unless status
101
+ details.merge(state: Bolt::Status.get_state(status),
102
+ error: Bolt::Status.get_error(status),
103
+ error_context: Bolt::Status.get_error_context(status))
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,146 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Neo4j
4
+ module Driver
5
+ module Internal
6
+ class ExplicitTransaction
7
+ include ErrorHandling
8
+
9
+ delegate :finalize, to: :@handler
10
+
11
+ def initialize(connection, session)
12
+ @connection = connection
13
+ @protocol = connection.protocol
14
+ @session = session
15
+ @state = :active
16
+ end
17
+
18
+ def begin(initial_bookmarks, config)
19
+ chain @protocol.begin_transaction(@connection, initial_bookmarks, config)
20
+ finalize if initial_bookmarks.present?
21
+ self
22
+ rescue StandardError => e
23
+ @connection.release
24
+ raise e
25
+ end
26
+
27
+ def run(query, parameters = {})
28
+ ensure_can_run_queries
29
+ @result&.failure
30
+ @result = @protocol.run_in_explicit_transaction(@connection, Statement.new(query, parameters), self)
31
+ end
32
+
33
+ def success
34
+ @state = :marked_success if @state == :active
35
+ end
36
+
37
+ def failure
38
+ @state = :marked_failed if %i[active marked_success].include? @state
39
+ end
40
+
41
+ def mark_terminated
42
+ @state = :terminated
43
+ end
44
+
45
+ def close
46
+ case @state
47
+ when :marked_success
48
+ commit
49
+ when :committed, :rolled_back
50
+ nil
51
+ else
52
+ rollback
53
+ end
54
+ end
55
+
56
+ def open?
57
+ !%i[committed rolled_back].include? @state
58
+ end
59
+
60
+ def chain(*handlers)
61
+ handlers.each do |handler|
62
+ handler.previous = @handler
63
+ @handler = handler
64
+ end
65
+ end
66
+
67
+ private
68
+
69
+ def commit
70
+ case @state
71
+ when :committed
72
+ nil
73
+ when :rolled_back
74
+ raise Exceptions::ClientExceptiom, "Can't commit, transaction has been rolled back"
75
+ else
76
+ begin
77
+ do_commit
78
+ # handleCommitOrRollback( error )
79
+ ensure
80
+ transaction_closed(:committed)
81
+ end
82
+ end
83
+ end
84
+
85
+ def do_commit
86
+ if @state == :terminated
87
+ raise Exceptions::ClientException, "Transaction can't be committed. " \
88
+ 'It has been rolled back either because of an error or explicit termination'
89
+ end
90
+
91
+ # @session.bookmarks = @protocol.commit_transaction(@connection)
92
+ chain @protocol.commit_transaction(@connection)
93
+ finalize
94
+ @session.bookmarks = @connection.last_bookmark
95
+ end
96
+
97
+ def rollback
98
+ case @state
99
+ when :committed
100
+ raise ClientException, "Can't rollback, transaction has been committed"
101
+ when :rolled_back
102
+ nil
103
+ else
104
+ begin
105
+ do_rollback
106
+ # resultCursors.retrieveNotConsumedError()
107
+ # handleCommitOrRollback( error )
108
+ ensure
109
+ transaction_closed(:rolled_back)
110
+ end
111
+ end
112
+ end
113
+
114
+ def do_rollback
115
+ return if @state == :terminated
116
+
117
+ chain @protocol.rollback_transaction(@connection)
118
+ finalize
119
+ end
120
+
121
+ def transaction_closed(new_state)
122
+ @state = new_state
123
+ @session.release_connection
124
+ end
125
+
126
+ def ensure_can_run_queries
127
+ reason =
128
+ case @state
129
+ when :committed
130
+ 'Cannot run more statements in this transaction, it has been committed'
131
+ when :rolled_back
132
+ 'Cannot run more statements in this transaction, it has been rolled back'
133
+ when :marked_failed
134
+ 'Cannot run more statements in this transaction, it has been marked for failure. ' \
135
+ 'Please either rollback or close this transaction'
136
+ when :terminated
137
+ 'Cannot run more statements in this transaction, ' \
138
+ 'it has either experienced an fatal error or was explicitly terminated'
139
+ end
140
+
141
+ raise Exceptions::ClientException, reason if reason
142
+ end
143
+ end
144
+ end
145
+ end
146
+ end