module Neo4j::Migrations::Helpers::IdProperty
Public Instance Methods
populate_id_property(label)
click to toggle source
# File lib/neo4j/migrations/helpers/id_property.rb 7 def populate_id_property(label) 8 model = label.to_s.constantize 9 max_per_batch = (ENV['MAX_PER_BATCH'] || default_max_per_batch).to_i 10 11 last_time_taken = nil 12 13 until (nodes_left = idless_count(label, model.primary_key)) == 0 14 print_status(last_time_taken, max_per_batch, nodes_left) 15 16 count = [nodes_left, max_per_batch].min 17 last_time_taken = Benchmark.realtime do 18 max_per_batch = id_batch_set(label, model.primary_key, Array.new(count) { new_id_for(model) }, count) 19 end 20 end 21 end
Protected Instance Methods
default_max_per_batch()
click to toggle source
# File lib/neo4j/migrations/helpers/id_property.rb 61 def default_max_per_batch 62 900 63 end
id_batch_set(label, id_property, new_ids, count)
click to toggle source
# File lib/neo4j/migrations/helpers/id_property.rb 29 def id_batch_set(label, id_property, new_ids, count) 30 tx = ActiveBase.new_transaction 31 32 execute("MATCH (n:`#{label}`) WHERE NOT EXISTS(n.#{id_property}) 33 with COLLECT(n) as nodes, #{new_ids} as ids 34 FOREACH(i in range(0,#{count - 1})| 35 FOREACH(node in [nodes[i]]| 36 SET node.#{id_property} = ids[i])) 37 RETURN distinct(true) 38 LIMIT #{count}") 39 40 count 41 rescue Neo4j::Server::CypherResponse::ResponseError, Faraday::TimeoutError 42 new_max_per_batch = (max_per_batch * 0.8).round 43 output "Error querying #{max_per_batch} nodes. Trying #{new_max_per_batch}" 44 new_max_per_batch 45 ensure 46 tx.close 47 end
idless_count(label, id_property)
click to toggle source
# File lib/neo4j/migrations/helpers/id_property.rb 25 def idless_count(label, id_property) 26 query.match(n: label).where("NOT EXISTS(n.#{id_property})").pluck('COUNT(n) AS ids').first 27 end
new_id_for(model)
click to toggle source
# File lib/neo4j/migrations/helpers/id_property.rb 65 def new_id_for(model) 66 if model.id_property_info[:type][:auto] 67 SecureRandom.uuid 68 else 69 model.new.send(model.id_property_info[:type][:on]) 70 end 71 end
print_status(last_time_taken, max_per_batch, nodes_left)
click to toggle source
# File lib/neo4j/migrations/helpers/id_property.rb 49 def print_status(last_time_taken, max_per_batch, nodes_left) 50 time_per_node = last_time_taken / max_per_batch if last_time_taken 51 message = if time_per_node 52 eta_seconds = (nodes_left * time_per_node).round 53 "#{nodes_left} nodes left. Last batch: #{(time_per_node * 1000.0).round(1)}ms / node (ETA: #{eta_seconds / 60} minutes)" 54 else 55 'Running first batch...' 56 end 57 58 output message 59 end