neo4j_bolt 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +12 -1
- data/lib/neo4j_bolt/version.rb +1 -1
- data/lib/neo4j_bolt.rb +94 -0
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 388dd7f6a3d0b491052bc844c0cd45bbaeb71eef90c1cdb3d93717167afe668a
|
|
4
|
+
data.tar.gz: 40ba4fbcd3dd14ec31cdc5672f6854681de6fe4d1e3de0643acd11effed3366f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 217aa1229a23d9bbcfabe4fd6977bd24e4988bcabbb34093627cb8fc8707457b1886694a9082edc9d65987108ef77fc2b70d4ba53f7d58810fd85f2e269d4da6
|
|
7
|
+
data.tar.gz: 1c74dd4f9d10e8ebb9dffa78cbef3375ba23d7e9223bec7c72d7f11baa6b416f53988e824ca0a798ee37b5461122476f2e3b6e4b586417a2aa29790b48b4fa7a
|
data/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Neo4jBolt 0.4 is a small compatibility and convenience layer for Ruby applicatio
|
|
|
4
4
|
|
|
5
5
|
Neo4jBolt no longer implements the Bolt wire protocol itself.
|
|
6
6
|
|
|
7
|
-
`0.5.
|
|
7
|
+
`0.5.2` requires Ruby 3.4 or newer and pins `neo4j-ruby-driver` to `6.2.1.beta.4`. Applications on older Rubies can remain on the Neo4jBolt 0.3.x line; this prerelease is intentionally not an automatic upgrade for them.
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
|
@@ -89,6 +89,12 @@ row = neo4j_query_expect_one(
|
|
|
89
89
|
|
|
90
90
|
`neo4j_query_expect_one` raises `Neo4jBolt::ExpectedOneResultError` unless the query produces exactly one row.
|
|
91
91
|
|
|
92
|
+
String parameters always go to Neo4j as strings, whatever their Ruby encoding, as in 0.3. That matters for values taken from a Rack request: paths, query strings and headers arrive as binary (ASCII-8BIT) strings, which the upstream driver alone would send as byte arrays. Binary strings that are valid UTF-8 are sent as UTF-8 strings, strings in other encodings are converted, and a binary string that is not valid UTF-8 raises `Neo4jBolt::InvalidStringParameterError`. To store a byte array, wrap the data explicitly:
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
neo4j_query("CREATE (:Blob {data: $data})", data: Neo4jBolt::Bytes.new(binary_string))
|
|
96
|
+
```
|
|
97
|
+
|
|
92
98
|
### Result compatibility
|
|
93
99
|
|
|
94
100
|
Result-row keys are strings:
|
|
@@ -299,6 +305,11 @@ To point RSpec itself at an already disposable database, set `NEO4J_BOLT_TEST_HO
|
|
|
299
305
|
| CLI commands | Preserved |
|
|
300
306
|
| `BoltSocket`, `BoltBuffer`, protocol markers/state/parser/packer | Intentionally removed private implementation details |
|
|
301
307
|
|
|
308
|
+
## 0.5.2
|
|
309
|
+
|
|
310
|
+
- String parameters are sent as strings again, whatever their Ruby encoding (0.3 behavior). In 0.5.0/0.5.1 a binary (ASCII-8BIT) String – which is how Rack passes request paths, query strings and headers – went to Neo4j as a byte array: a lookup by a value from the URL matched nothing, and values from headers were stored as byte arrays. Byte arrays now need `Neo4jBolt::Bytes`; a binary string that is not valid UTF-8 raises `Neo4jBolt::InvalidStringParameterError`.
|
|
311
|
+
- Fork safety: a process forked after Neo4j was used (puma with workers and preloading, unicorn, …) inherited the parent's driver and hung on its first query. The child now drops the inherited driver (without closing the parent's connections) and opens its own, via `Process._fork`.
|
|
312
|
+
|
|
302
313
|
## 0.5.1
|
|
303
314
|
|
|
304
315
|
- Dumps keep dates, datetimes, local datetimes, local times, times, durations and points; before, they were written as text and came back as strings. Older dumps load as before.
|
data/lib/neo4j_bolt/version.rb
CHANGED
data/lib/neo4j_bolt.rb
CHANGED
|
@@ -243,6 +243,7 @@ module Neo4jBolt
|
|
|
243
243
|
class SyntaxError < Error; end
|
|
244
244
|
class ConstraintValidationFailedError < Error; end
|
|
245
245
|
class ExpectedOneResultError < Error; end
|
|
246
|
+
class InvalidStringParameterError < Error; end
|
|
246
247
|
|
|
247
248
|
class Node < Hash
|
|
248
249
|
attr_reader :id, :labels, :element_id
|
|
@@ -274,6 +275,26 @@ module Neo4jBolt
|
|
|
274
275
|
|
|
275
276
|
TransactionContext = Struct.new(:session, :transaction, :depth, :rollback_only, keyword_init: true)
|
|
276
277
|
|
|
278
|
+
# A byte array query parameter. Plain Ruby strings are always sent as
|
|
279
|
+
# strings, whatever their encoding (as in 0.3); wrap binary data in Bytes
|
|
280
|
+
# to store a Neo4j byte array.
|
|
281
|
+
class Bytes
|
|
282
|
+
attr_reader :data
|
|
283
|
+
|
|
284
|
+
def initialize(data)
|
|
285
|
+
@data = String(data).b.freeze
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def ==(other)
|
|
289
|
+
other.is_a?(Bytes) && other.data == data
|
|
290
|
+
end
|
|
291
|
+
alias eql? ==
|
|
292
|
+
|
|
293
|
+
def hash
|
|
294
|
+
[Bytes, data].hash
|
|
295
|
+
end
|
|
296
|
+
end
|
|
297
|
+
|
|
277
298
|
# Owns one process-wide upstream driver and its connection pool. Leases let
|
|
278
299
|
# cleanup wait for in-flight sessions without serializing independent queries.
|
|
279
300
|
module DriverRegistry
|
|
@@ -287,6 +308,25 @@ module Neo4jBolt
|
|
|
287
308
|
release if driver
|
|
288
309
|
end
|
|
289
310
|
|
|
311
|
+
# Called in a child process right after fork (see ForkHook). The child
|
|
312
|
+
# inherits the parent's driver object, but not the threads that serve
|
|
313
|
+
# its connections, and the sockets belong to the parent: using it hangs,
|
|
314
|
+
# closing it would disturb the parent. So the child forgets it (and the
|
|
315
|
+
# synchronization state, which another parent thread may have held) and
|
|
316
|
+
# opens its own driver on first use.
|
|
317
|
+
def after_fork
|
|
318
|
+
@mutex = Mutex.new
|
|
319
|
+
@condition = ConditionVariable.new
|
|
320
|
+
@driver = nil
|
|
321
|
+
@leases = 0
|
|
322
|
+
@closing = false
|
|
323
|
+
@pid = Process.pid
|
|
324
|
+
end
|
|
325
|
+
|
|
326
|
+
def forked?
|
|
327
|
+
!@pid.nil? && @pid != Process.pid
|
|
328
|
+
end
|
|
329
|
+
|
|
290
330
|
def cleanup
|
|
291
331
|
if Thread.current.thread_variable_get(LEASE_KEY).to_i.positive?
|
|
292
332
|
raise Error, "cleanup_neo4j cannot run while this thread is using Neo4j"
|
|
@@ -316,8 +356,12 @@ module Neo4jBolt
|
|
|
316
356
|
private
|
|
317
357
|
|
|
318
358
|
def acquire
|
|
359
|
+
# second line of defence if the fork hook didn't run (e.g. a fork
|
|
360
|
+
# through a path that bypasses Process._fork)
|
|
361
|
+
after_fork if forked?
|
|
319
362
|
driver = mutex.synchronize do
|
|
320
363
|
condition.wait(mutex) while @closing
|
|
364
|
+
@pid ||= Process.pid
|
|
321
365
|
@driver ||= build_driver
|
|
322
366
|
@leases = leases + 1
|
|
323
367
|
@driver
|
|
@@ -373,6 +417,18 @@ module Neo4jBolt
|
|
|
373
417
|
end
|
|
374
418
|
end
|
|
375
419
|
|
|
420
|
+
# Preloading servers (puma with workers, unicorn, ...) load the app, which
|
|
421
|
+
# may already have queried Neo4j, and then fork. Process._fork (Ruby >= 3.1)
|
|
422
|
+
# runs for every fork; in the child, the inherited driver is dropped.
|
|
423
|
+
module ForkHook
|
|
424
|
+
def _fork
|
|
425
|
+
pid = super
|
|
426
|
+
DriverRegistry.after_fork if pid.zero?
|
|
427
|
+
pid
|
|
428
|
+
end
|
|
429
|
+
end
|
|
430
|
+
Process.singleton_class.prepend(ForkHook)
|
|
431
|
+
|
|
376
432
|
class << self
|
|
377
433
|
attr_reader :bolt_host, :bolt_port, :bolt_username, :bolt_password
|
|
378
434
|
attr_accessor :bolt_verbosity, :bolt_database
|
|
@@ -447,6 +503,7 @@ module Neo4jBolt
|
|
|
447
503
|
|
|
448
504
|
def neo4j_query(query, data = {}, &block)
|
|
449
505
|
validate_parameters!(data)
|
|
506
|
+
data = normalize_parameters(data)
|
|
450
507
|
log_query(query, data)
|
|
451
508
|
|
|
452
509
|
context = transaction_context
|
|
@@ -905,6 +962,43 @@ module Neo4jBolt
|
|
|
905
962
|
end
|
|
906
963
|
end
|
|
907
964
|
|
|
965
|
+
# Strings go to Neo4j as strings, as in 0.3. The upstream driver would send
|
|
966
|
+
# a binary (ASCII-8BIT) String as a byte array instead – and Rack hands
|
|
967
|
+
# request paths, query strings and headers to the app as binary strings, so
|
|
968
|
+
# a query with such a value silently matched nothing ("tag" from the URL)
|
|
969
|
+
# or stored a byte array where a string belongs. Byte arrays now need an
|
|
970
|
+
# explicit Neo4jBolt::Bytes.
|
|
971
|
+
def normalize_parameters(value)
|
|
972
|
+
case value
|
|
973
|
+
when Bytes then value.data
|
|
974
|
+
when String then normalize_string_parameter(value)
|
|
975
|
+
when Hash
|
|
976
|
+
value.to_h do |key, nested_value|
|
|
977
|
+
[key.is_a?(String) ? normalize_string_parameter(key) : key, normalize_parameters(nested_value)]
|
|
978
|
+
end
|
|
979
|
+
when Array then value.map { |nested_value| normalize_parameters(nested_value) }
|
|
980
|
+
when Set then Set.new(value.map { |nested_value| normalize_parameters(nested_value) })
|
|
981
|
+
else value
|
|
982
|
+
end
|
|
983
|
+
end
|
|
984
|
+
|
|
985
|
+
def normalize_string_parameter(string)
|
|
986
|
+
return string if string.encoding == Encoding::UTF_8 && string.valid_encoding?
|
|
987
|
+
|
|
988
|
+
if [Encoding::BINARY, Encoding::US_ASCII, Encoding::UTF_8].include?(string.encoding)
|
|
989
|
+
utf8 = string.dup.force_encoding(Encoding::UTF_8)
|
|
990
|
+
return utf8 if utf8.valid_encoding?
|
|
991
|
+
|
|
992
|
+
raise InvalidStringParameterError,
|
|
993
|
+
"String parameter is not valid UTF-8 (#{string.bytesize} bytes); " \
|
|
994
|
+
"wrap binary data in Neo4jBolt::Bytes to store a byte array"
|
|
995
|
+
end
|
|
996
|
+
|
|
997
|
+
string.encode(Encoding::UTF_8)
|
|
998
|
+
rescue EncodingError => error
|
|
999
|
+
raise InvalidStringParameterError, "String parameter can't be converted to UTF-8: #{error.message}"
|
|
1000
|
+
end
|
|
1001
|
+
|
|
908
1002
|
def raise_mapped_error(error)
|
|
909
1003
|
mapped = case error.code
|
|
910
1004
|
when "Neo.ClientError.Statement.SyntaxError"
|