Module: Neo4jBolt
- Defined in:
- lib/neo4j_bolt.rb,
lib/neo4j_bolt/version.rb
Defined Under Namespace
Modules: BoltMarker, ServerState
Classes: BoltBuffer, BoltSocket, ConstraintValidationFailedError, CypherError, Error, ExpectedOneResultError, IntegerOutOfRangeError, Node, Relationship, State, SyntaxError, UnexpectedServerResponse
Constant Summary
collapse
- CONSTRAINT_INDEX_PREFIX =
'neo4j_bolt_'
- SERVER_STATE_LABELS =
Hash[ServerState::constants.map { |value| [ServerState::const_get(value), value]
- BOLT_MARKER_LABELS =
- VERSION =
"0.3.0"
Class Attribute Summary collapse
Instance Method Summary
collapse
Class Attribute Details
.bolt_host ⇒ Object
Returns the value of attribute bolt_host.
9
10
11
|
# File 'lib/neo4j_bolt.rb', line 9
def bolt_host
@bolt_host
end
|
.bolt_port ⇒ Object
Returns the value of attribute bolt_port.
9
10
11
|
# File 'lib/neo4j_bolt.rb', line 9
def bolt_port
@bolt_port
end
|
.bolt_verbosity ⇒ Object
Returns the value of attribute bolt_verbosity.
9
10
11
|
# File 'lib/neo4j_bolt.rb', line 9
def bolt_verbosity
@bolt_verbosity
end
|
Instance Method Details
#cleanup_neo4j ⇒ Object
1074
1075
1076
1077
1078
1079
|
# File 'lib/neo4j_bolt.rb', line 1074
def cleanup_neo4j
if @bolt_socket
@bolt_socket.disconnect()
@bolt_socket = nil
end
end
|
#dump_database(io) ⇒ Object
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
|
# File 'lib/neo4j_bolt.rb', line 970
def dump_database(io)
tr_id = {}
id = 0
neo4j_query("MATCH (n) RETURN n ORDER BY ID(n);") do |row|
tr_id[row['n'].id] = id
node = {
:id => id,
:labels => row['n'].labels,
:properties => row['n']
}
io.puts "n #{node.to_json}"
id += 1
end
neo4j_query("MATCH ()-[r]->() RETURN r;") do |row|
rel = {
:from => tr_id[row['r'].start_node_id],
:to => tr_id[row['r'].end_node_id],
:type => row['r'].type,
:properties => row['r']
}
io.puts "r #{rel.to_json}"
end
end
|
#load_database_dump(io, force_append: false) ⇒ Object
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
|
# File 'lib/neo4j_bolt.rb', line 994
def load_database_dump(io, force_append: false)
unless force_append
transaction do
node_count = neo4j_query_expect_one('MATCH (n) RETURN COUNT(n) as count;')['count']
unless node_count == 0
raise "There are nodes in this database, exiting now."
end
end
end
n_count = 0
r_count = 0
node_tr = {}
node_batch_by_label = {}
relationship_batch_by_type = {}
io.each_line do |line|
line.strip!
next if line.empty?
if line[0] == 'n'
line = line[2, line.size - 2]
node = JSON.parse(line)
label_key = node['labels'].sort.join('/')
node_batch_by_label[label_key] ||= []
node_batch_by_label[label_key] << node
elsif line[0] == 'r'
line = line[2, line.size - 2]
relationship = JSON.parse(line)
relationship_batch_by_type[relationship['type']] ||= []
relationship_batch_by_type[relationship['type']] << relationship
else
STDERR.puts "Invalid entry: #{line}"
exit(1)
end
end
node_batch_by_label.each_pair do |label_key, batch|
while !batch.empty? do
slice = []
json_size = 0
while (!batch.empty?) && json_size < 0x20000 && slice.size < 256
x = batch.shift
slice << x
json_size += x.to_json.size
end
ids = neo4j_query(<<~END_OF_QUERY, {:properties => slice.map { |x| x['properties']}})
UNWIND $properties AS props
CREATE (n:#{slice.first['labels'].join(':')})
SET n = props
RETURN ID(n) AS id;
END_OF_QUERY
slice.each.with_index do |node, i|
node_tr[node['id']] = ids[i]['id']
end
n_count += slice.size
STDERR.print "\rLoaded #{n_count} nodes, #{r_count} relationships..."
end
end
relationship_batch_by_type.each_pair do |rel_type, batch|
batch.each_slice(256) do |slice|
slice.map! do |rel|
rel['from'] = node_tr[rel['from']]
rel['to'] = node_tr[rel['to']]
rel
end
count = neo4j_query_expect_one(<<~END_OF_QUERY, {:slice => slice})['count_r']
UNWIND $slice AS props
MATCH (from), (to) WHERE ID(from) = props.from AND ID(to) = props.to
CREATE (from)-[r:#{rel_type}]->(to)
SET r = props.properties
RETURN COUNT(r) AS count_r, COUNT(from) AS count_from, COUNT(to) AS count_to;
END_OF_QUERY
if count != slice.size
raise "Ooops... expected #{slice.size} relationships, got #{count}."
end
r_count += slice.size
STDERR.print "\rLoaded #{n_count} nodes, #{r_count} relationships..."
end
end
STDERR.puts
end
|
#neo4j_query(query, data = {}, &block) ⇒ Object
947
948
949
950
|
# File 'lib/neo4j_bolt.rb', line 947
def neo4j_query(query, data = {}, &block)
@bolt_socket ||= BoltSocket.new()
@bolt_socket.neo4j_query(query, data, &block)
end
|
#neo4j_query_expect_one(query, data = {}) ⇒ Object
952
953
954
955
|
# File 'lib/neo4j_bolt.rb', line 952
def neo4j_query_expect_one(query, data = {})
@bolt_socket ||= BoltSocket.new()
@bolt_socket.neo4j_query_expect_one(query, data)
end
|
#rollback ⇒ Object
943
944
945
|
# File 'lib/neo4j_bolt.rb', line 943
def rollback()
@bolt_socket.rollback
end
|
#setup_constraints_and_indexes(constraints, indexes) ⇒ Object
1081
1082
1083
1084
|
# File 'lib/neo4j_bolt.rb', line 1081
def setup_constraints_and_indexes(constraints, indexes)
@bolt_socket ||= BoltSocket.new()
@bolt_socket.setup_constraints_and_indexes(constraints, indexes)
end
|
#transaction(&block) ⇒ Object
938
939
940
941
|
# File 'lib/neo4j_bolt.rb', line 938
def transaction(&block)
@bolt_socket ||= BoltSocket.new()
@bolt_socket.transaction { yield }
end
|
#wait_for_neo4j ⇒ Object
957
958
959
960
961
962
963
964
965
966
967
968
|
# File 'lib/neo4j_bolt.rb', line 957
def wait_for_neo4j
delay = 1
10.times do
begin
neo4j_query("MATCH (n) RETURN n LIMIT 1;")
rescue
debug "Waiting #{delay} seconds for Neo4j to come up..."
sleep delay
delay += 1
end
end
end
|