neo4j-ruby-driver 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/Rakefile +16 -0
- data/ffi/bolt/address.rb +11 -0
- data/ffi/bolt/auth.rb +10 -0
- data/ffi/bolt/auto_releasable.rb +22 -0
- data/ffi/bolt/boolean.rb +9 -0
- data/ffi/bolt/bytes.rb +10 -0
- data/ffi/bolt/config.rb +10 -0
- data/ffi/bolt/connection.rb +44 -0
- data/ffi/bolt/connector.rb +17 -0
- data/ffi/bolt/dictionary.rb +15 -0
- data/ffi/bolt/error.rb +74 -0
- data/ffi/bolt/float.rb +9 -0
- data/ffi/bolt/integer.rb +9 -0
- data/ffi/bolt/library.rb +12 -0
- data/ffi/bolt/lifecycle.rb +9 -0
- data/ffi/bolt/list.rb +10 -0
- data/ffi/bolt/status.rb +15 -0
- data/ffi/bolt/string.rb +9 -0
- data/ffi/bolt/structure.rb +10 -0
- data/ffi/bolt/value.rb +35 -0
- data/ffi/bolt/values_private.rb +36 -0
- data/ffi/neo4j/driver.rb +45 -0
- data/ffi/neo4j/driver/access_mode.rb +10 -0
- data/ffi/neo4j/driver/auth_tokens.rb +17 -0
- data/ffi/neo4j/driver/config.rb +11 -0
- data/ffi/neo4j/driver/graph_database.rb +29 -0
- data/ffi/neo4j/driver/internal/async/access_mode_connection.rb +19 -0
- data/ffi/neo4j/driver/internal/async/direct_connection.rb +88 -0
- data/ffi/neo4j/driver/internal/bookmarks_holder.rb +30 -0
- data/ffi/neo4j/driver/internal/direct_connection_provider.rb +27 -0
- data/ffi/neo4j/driver/internal/driver_factory.rb +38 -0
- data/ffi/neo4j/driver/internal/error_handling.rb +50 -0
- data/ffi/neo4j/driver/internal/explicit_transaction.rb +144 -0
- data/ffi/neo4j/driver/internal/handlers/pull_all_response_handler.rb +101 -0
- data/ffi/neo4j/driver/internal/handlers/response_handler.rb +46 -0
- data/ffi/neo4j/driver/internal/handlers/run_response_handler.rb +32 -0
- data/ffi/neo4j/driver/internal/handlers/session_pull_all_response_handler.rb +32 -0
- data/ffi/neo4j/driver/internal/handlers/transaction_pull_all_response_handler.rb +23 -0
- data/ffi/neo4j/driver/internal/internal_driver.rb +41 -0
- data/ffi/neo4j/driver/internal/internal_record.rb +22 -0
- data/ffi/neo4j/driver/internal/internal_statement_result.rb +52 -0
- data/ffi/neo4j/driver/internal/messaging/bolt_protocol.rb +24 -0
- data/ffi/neo4j/driver/internal/messaging/v1/bolt_protocol_v1.rb +59 -0
- data/ffi/neo4j/driver/internal/messaging/v2/bolt_protocol_v2.rb +16 -0
- data/ffi/neo4j/driver/internal/messaging/v3/bolt_protocol_v3.rb +63 -0
- data/ffi/neo4j/driver/internal/network_session.rb +124 -0
- data/ffi/neo4j/driver/internal/session_factory_impl.rb +28 -0
- data/ffi/neo4j/driver/internal/summary/internal_result_summary.rb +67 -0
- data/ffi/neo4j/driver/internal/summary/internal_server_info.rb +19 -0
- data/ffi/neo4j/driver/internal/summary/internal_summary_counters.rb +23 -0
- data/ffi/neo4j/driver/internal/util/metadata_extractor.rb +15 -0
- data/ffi/neo4j/driver/internal/value/base_time_value.rb +22 -0
- data/ffi/neo4j/driver/internal/value/date_value.rb +25 -0
- data/ffi/neo4j/driver/internal/value/duration_value.rb +27 -0
- data/ffi/neo4j/driver/internal/value/local_date_time_value.rb +24 -0
- data/ffi/neo4j/driver/internal/value/local_time_value.rb +19 -0
- data/ffi/neo4j/driver/internal/value/node_value.rb +18 -0
- data/ffi/neo4j/driver/internal/value/offset_time_value.rb +25 -0
- data/ffi/neo4j/driver/internal/value/path_value.rb +41 -0
- data/ffi/neo4j/driver/internal/value/point2_d_value.rb +24 -0
- data/ffi/neo4j/driver/internal/value/point3_d_value.rb +24 -0
- data/ffi/neo4j/driver/internal/value/relationship_value.rb +18 -0
- data/ffi/neo4j/driver/internal/value/structure_value.rb +42 -0
- data/ffi/neo4j/driver/internal/value/time_with_zone_id_value.rb +25 -0
- data/ffi/neo4j/driver/internal/value/time_with_zone_offset_value.rb +28 -0
- data/ffi/neo4j/driver/internal/value/unbound_relationship_value.rb +18 -0
- data/ffi/neo4j/driver/internal/value/value_adapter.rb +95 -0
- data/ffi/neo4j/driver/statement.rb +14 -0
- data/ffi/neo4j/driver/summary/statement_type.rb +14 -0
- data/ffi/neo4j/driver/types/entity.rb +17 -0
- data/ffi/neo4j/driver/types/node.rb +16 -0
- data/ffi/neo4j/driver/types/path.rb +35 -0
- data/ffi/neo4j/driver/types/relationship.rb +19 -0
- data/lib/loader.rb +18 -0
- data/lib/neo4j/driver/auto_closable.rb +32 -0
- data/lib/neo4j/driver/exceptions/authentication_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/client_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/database_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/illegal_state_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/neo4j_exception.rb +18 -0
- data/lib/neo4j/driver/exceptions/no_such_record_exception.rb +33 -0
- data/lib/neo4j/driver/exceptions/protocol_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/security_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/service_unavailable_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/session_expired_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/transient_exception.rb +10 -0
- data/lib/neo4j/driver/exceptions/untrusted_server_exception.rb +10 -0
- data/lib/neo4j/driver/internal/duration_normalizer.rb +42 -0
- data/lib/neo4j/driver/internal/ruby_signature.rb +18 -0
- data/lib/neo4j/driver/types/byte_array.rb +17 -0
- data/lib/neo4j/driver/types/local_date_time.rb +20 -0
- data/lib/neo4j/driver/types/local_time.rb +19 -0
- data/lib/neo4j/driver/types/offset_time.rb +19 -0
- data/lib/neo4j/driver/types/point.rb +39 -0
- data/lib/neo4j/driver/types/time.rb +43 -0
- data/lib/neo4j/driver/version.rb +7 -0
- data/lib/neo4j_ruby_driver.rb +19 -0
- data/neo4j-ruby-driver.gemspec +63 -0
- metadata +274 -0
@@ -0,0 +1,144 @@
|
|
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
|
+
self
|
21
|
+
rescue StandardError => e
|
22
|
+
@connection.release
|
23
|
+
raise e
|
24
|
+
end
|
25
|
+
|
26
|
+
def run(query, parameters = {})
|
27
|
+
ensure_can_run_queries
|
28
|
+
@protocol.run_in_explicit_transaction(@connection, Statement.new(query, parameters), self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def success
|
32
|
+
@state = :marked_success if @state == :active
|
33
|
+
end
|
34
|
+
|
35
|
+
def failure
|
36
|
+
@state = :marked_failed if %i[active marked_success].include? @state
|
37
|
+
end
|
38
|
+
|
39
|
+
def mark_terminated
|
40
|
+
@state = :terminated
|
41
|
+
end
|
42
|
+
|
43
|
+
def close
|
44
|
+
case @state
|
45
|
+
when :marked_success
|
46
|
+
commit
|
47
|
+
when :committed, :rolled_back
|
48
|
+
nil
|
49
|
+
else
|
50
|
+
rollback
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def open?
|
55
|
+
!%i[committed rolled_back].include? @state
|
56
|
+
end
|
57
|
+
|
58
|
+
def chain(*handlers)
|
59
|
+
handlers.each do |handler|
|
60
|
+
handler.previous = @handler
|
61
|
+
@handler = handler
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def commit
|
68
|
+
case @state
|
69
|
+
when :committed
|
70
|
+
nil
|
71
|
+
when :rolled_back
|
72
|
+
raise Exceptions::ClientExceptiom, "Can't commit, transaction has been rolled back"
|
73
|
+
else
|
74
|
+
begin
|
75
|
+
do_commit
|
76
|
+
# handleCommitOrRollback( error )
|
77
|
+
ensure
|
78
|
+
transaction_closed(:committed)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def do_commit
|
84
|
+
if @state == :terminated
|
85
|
+
raise Exceptions::ClientException, "Transaction can't be committed. " \
|
86
|
+
'It has been rolled back either because of an error or explicit termination'
|
87
|
+
end
|
88
|
+
|
89
|
+
# @session.bookmarks = @protocol.commit_transaction(@connection)
|
90
|
+
chain @protocol.commit_transaction(@connection)
|
91
|
+
finalize
|
92
|
+
@session.bookmarks = @connection.last_bookmark
|
93
|
+
end
|
94
|
+
|
95
|
+
def rollback
|
96
|
+
case @state
|
97
|
+
when :committed
|
98
|
+
raise ClientException, "Can't rollback, transaction has been committed"
|
99
|
+
when :rolled_back
|
100
|
+
nil
|
101
|
+
else
|
102
|
+
begin
|
103
|
+
do_rollback
|
104
|
+
# resultCursors.retrieveNotConsumedError()
|
105
|
+
# handleCommitOrRollback( error )
|
106
|
+
ensure
|
107
|
+
transaction_closed(:rolled_back)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def do_rollback
|
113
|
+
return if @state == :terminated
|
114
|
+
|
115
|
+
chain @protocol.rollback_transaction(@connection)
|
116
|
+
finalize
|
117
|
+
end
|
118
|
+
|
119
|
+
def transaction_closed(new_state)
|
120
|
+
@state = new_state
|
121
|
+
@session.release_connection
|
122
|
+
end
|
123
|
+
|
124
|
+
def ensure_can_run_queries
|
125
|
+
reason =
|
126
|
+
case @state
|
127
|
+
when :committed
|
128
|
+
'Cannot run more statements in this transaction, it has been committed'
|
129
|
+
when :rolled_back
|
130
|
+
'Cannot run more statements in this transaction, it has been rolled back'
|
131
|
+
when :marked_failed
|
132
|
+
'Cannot run more statements in this transaction, it has been marked for failure. ' \
|
133
|
+
'Please either rollback or close this transaction'
|
134
|
+
when :terminated
|
135
|
+
'Cannot run more statements in this transaction, ' \
|
136
|
+
'it has either experienced an fatal error or was explicitly terminated'
|
137
|
+
end
|
138
|
+
|
139
|
+
raise Exceptions::ClientException, reason if reason
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
module Handlers
|
7
|
+
class PullAllResponseHandler < ResponseHandler
|
8
|
+
delegate :bolt_connection, to: :connection
|
9
|
+
delegate :statement_keys, to: :run_handler
|
10
|
+
attr_reader :connection
|
11
|
+
|
12
|
+
def initialize(statement, run_handler, connection, metadata_extractor)
|
13
|
+
super(connection)
|
14
|
+
@statement = statement
|
15
|
+
@previous = run_handler
|
16
|
+
@metadate_extractor = metadata_extractor
|
17
|
+
@records = []
|
18
|
+
end
|
19
|
+
|
20
|
+
def consume
|
21
|
+
# @ignore_records = true
|
22
|
+
@records.clear
|
23
|
+
finalize
|
24
|
+
@summary
|
25
|
+
rescue StandardError => e
|
26
|
+
on_failure(e)
|
27
|
+
raise e
|
28
|
+
end
|
29
|
+
|
30
|
+
def summary
|
31
|
+
unless @finished
|
32
|
+
while (record = fetch)
|
33
|
+
@records << record
|
34
|
+
end
|
35
|
+
end
|
36
|
+
@summary ||= Summary::InternalResultSummary.new(@statement, run_handler.result_available_after,
|
37
|
+
bolt_connection)
|
38
|
+
end
|
39
|
+
|
40
|
+
def peek
|
41
|
+
@records.first || ((@records << fetch).first unless @finished)
|
42
|
+
end
|
43
|
+
|
44
|
+
def next
|
45
|
+
peek
|
46
|
+
@records.shift
|
47
|
+
end
|
48
|
+
|
49
|
+
def on_success
|
50
|
+
@finished = true
|
51
|
+
summary
|
52
|
+
|
53
|
+
after_success(nil)
|
54
|
+
|
55
|
+
# @records.clear
|
56
|
+
@failure = nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def failure
|
60
|
+
# bolt_summary if connection.open?
|
61
|
+
end
|
62
|
+
|
63
|
+
def on_failure(error)
|
64
|
+
@finished = true
|
65
|
+
summary
|
66
|
+
|
67
|
+
after_failure(error)
|
68
|
+
|
69
|
+
@records.clear
|
70
|
+
@failure = error
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def run_handler
|
76
|
+
@previous
|
77
|
+
end
|
78
|
+
|
79
|
+
def fetch
|
80
|
+
run_handler.finalize
|
81
|
+
bolt_connection_fetch = Bolt::Connection.fetch(bolt_connection, request)
|
82
|
+
case bolt_connection_fetch
|
83
|
+
when -1
|
84
|
+
check_status(Bolt::Connection.status(bolt_connection))
|
85
|
+
when 1
|
86
|
+
InternalRecord.new(run_handler.statement_keys,
|
87
|
+
Value::ValueAdapter.to_ruby(Bolt::Connection.field_values(bolt_connection)))
|
88
|
+
else
|
89
|
+
@finished = true
|
90
|
+
check_summary_failure
|
91
|
+
nil
|
92
|
+
end
|
93
|
+
rescue StandardError => e
|
94
|
+
@finished = true
|
95
|
+
raise e
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
module Handlers
|
7
|
+
class ResponseHandler
|
8
|
+
delegate :bolt_connection, to: :connection
|
9
|
+
attr_reader :connection
|
10
|
+
attr_accessor :request, :previous
|
11
|
+
|
12
|
+
def initialize(connection)
|
13
|
+
@connection = connection
|
14
|
+
end
|
15
|
+
|
16
|
+
def finalize
|
17
|
+
return if @finished
|
18
|
+
@finished = true
|
19
|
+
begin
|
20
|
+
previous&.finalize
|
21
|
+
ensure
|
22
|
+
Bolt::Connection.fetch_summary(bolt_connection, request)
|
23
|
+
check_summary_failure
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
private
|
28
|
+
|
29
|
+
def check_summary_failure
|
30
|
+
summary
|
31
|
+
if Bolt::Connection.summary_success(bolt_connection) == 1
|
32
|
+
after_success(nil)
|
33
|
+
else
|
34
|
+
failure = Value::ValueAdapter.to_ruby(Bolt::Connection.failure(bolt_connection))
|
35
|
+
raise Exceptions::ClientException.new(failure[:code], failure[:message])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def summary; end
|
40
|
+
|
41
|
+
def after_success(metadata); end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
module Handlers
|
7
|
+
class RunResponseHandler < ResponseHandler
|
8
|
+
attr_reader :result_available_after
|
9
|
+
|
10
|
+
def initialize(connection, metadata_extractor)
|
11
|
+
super(connection)
|
12
|
+
@statement_keys = []
|
13
|
+
@metadata_extractor = metadata_extractor
|
14
|
+
end
|
15
|
+
|
16
|
+
def statement_keys
|
17
|
+
finalize
|
18
|
+
@statement_keys
|
19
|
+
end
|
20
|
+
|
21
|
+
def finalize
|
22
|
+
return if @finished
|
23
|
+
super
|
24
|
+
@statement_keys = Value::ValueAdapter.to_ruby(Bolt::Connection.field_names(bolt_connection))
|
25
|
+
metadata = Value::ValueAdapter.to_ruby(Bolt::Connection.metadata(bolt_connection))
|
26
|
+
@result_available_after = metadata[:result_available_after] || metadata[:t_first]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
module Handlers
|
7
|
+
class SessionPullAllResponseHandler < PullAllResponseHandler
|
8
|
+
def initialize(statement, run_handler, connection, bookmarks_holder, metadata_extractor)
|
9
|
+
super(statement, run_handler, connection, metadata_extractor)
|
10
|
+
@bookmarks_holder = bookmarks_holder
|
11
|
+
end
|
12
|
+
|
13
|
+
def after_success(metadata)
|
14
|
+
@bookmarks_holder.bookmarks = connection.last_bookmark
|
15
|
+
release_connection
|
16
|
+
# @bookmarks_holder.bookmarks = @metadata_extractor.extract_bookmarks(metadata)
|
17
|
+
end
|
18
|
+
|
19
|
+
def after_failure(_error)
|
20
|
+
release_connection
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def release_connection
|
26
|
+
connection.release
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
module Handlers
|
7
|
+
class TransactionPullAllResponseHandler < PullAllResponseHandler
|
8
|
+
def initialize(statement, run_handler, connection, tx, metadata_extractor)
|
9
|
+
super(statement, run_handler, connection, metadata_extractor)
|
10
|
+
@tx = tx
|
11
|
+
@tx.chain run_handler, self
|
12
|
+
end
|
13
|
+
|
14
|
+
def after_success(_metadata); end
|
15
|
+
|
16
|
+
def after_failure(_error)
|
17
|
+
@tx.mark_terminated
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
class InternalDriver
|
7
|
+
extend AutoClosable
|
8
|
+
include ErrorHandling
|
9
|
+
|
10
|
+
attr_reader :session_factory
|
11
|
+
delegate :verify_connectivity, to: :session_factory
|
12
|
+
auto_closable :session
|
13
|
+
|
14
|
+
def initialize(session_factory)
|
15
|
+
@session_factory = session_factory
|
16
|
+
@closed = Concurrent::AtomicBoolean.new(false)
|
17
|
+
end
|
18
|
+
|
19
|
+
def session(*args)
|
20
|
+
new_session(*Neo4j::Driver::Internal::RubySignature.session(args))
|
21
|
+
end
|
22
|
+
|
23
|
+
def close
|
24
|
+
session_factory.close if @closed.make_true
|
25
|
+
# Bolt::Connector.destroy(@connector)
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def new_session(mode, bookmarks)
|
31
|
+
assert_open
|
32
|
+
session_factory.new_instance(mode, bookmarks)
|
33
|
+
end
|
34
|
+
|
35
|
+
def assert_open
|
36
|
+
raise Exceptions::IllegalStateException, 'This driver instance has already been closed' if @closed.true?
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Neo4j
|
4
|
+
module Driver
|
5
|
+
module Internal
|
6
|
+
class InternalRecord
|
7
|
+
attr_reader :values
|
8
|
+
delegate :first, to: :values
|
9
|
+
|
10
|
+
def initialize(keys, values)
|
11
|
+
@keys = keys
|
12
|
+
@values = values
|
13
|
+
end
|
14
|
+
|
15
|
+
def [](key)
|
16
|
+
field_index = key.is_a?(Integer) ? key : @keys.index(key.to_s)
|
17
|
+
@values[field_index] if field_index
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|