Class: ActiveCypher::Fixtures::RelBuilder
- Inherits:
-
Object
- Object
- ActiveCypher::Fixtures::RelBuilder
- Defined in:
- lib/active_cypher/fixtures/rel_builder.rb
Class Method Summary collapse
-
.bulk_build(rels, batch_size: 200) ⇒ Object
Bulk create relationships for performance using UNWIND batching.
Instance Method Summary collapse
-
#build(_ref, from_ref, type, to_ref, props = {}) ⇒ void
Builds a relationship between two nodes, enforcing cross-DB safety.
Class Method Details
.bulk_build(rels, batch_size: 200) ⇒ Object
Bulk create relationships for performance using UNWIND batching
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_cypher/fixtures/rel_builder.rb', line 49 def self.bulk_build(rels, batch_size: 200) # Check all relationships for cross-DB violations first rels.each do |rel| from = Registry.get(rel[:from_ref]) to = Registry.get(rel[:to_ref]) raise FixtureError, "Both endpoints must exist: #{rel[:from_ref]}, #{rel[:to_ref]}" unless from && to from_conn = from.class.connection to_conn = to.class.connection raise FixtureError, 'Cross-database relationship? Sorry, your data has commitment issues.' if from_conn != to_conn end # Group by connection grouped = rels.group_by do |rel| from = Registry.get(rel[:from_ref]) from.class.connection end grouped.each do |conn, group| group.each_slice(batch_size) do |batch| unwind_batch = batch.map do |rel| from = Registry.get(rel[:from_ref]) to = Registry.get(rel[:to_ref]) { from_name: from.name, from_label: from.class.labels.first, to_name: to.name, to_label: to.class.labels.first, props: rel[:props] || {}, type: rel[:type].to_s } end cypher = " UNWIND $rows AS row\n MATCH (a:\#{unwind_batch.first[:from_label]} {name: row.from_name})\n MATCH (b:\#{unwind_batch.first[:to_label]} {name: row.to_name})\n CREATE (a)-[r:\#{unwind_batch.first[:type]} {props: row.props}]->(b)\n CYPHER\n\n conn.execute_cypher(cypher, rows: unwind_batch)\n end\n end\nend\n" |
Instance Method Details
#build(_ref, from_ref, type, to_ref, props = {}) ⇒ void
This method returns an undefined value.
Builds a relationship between two nodes, enforcing cross-DB safety.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_cypher/fixtures/rel_builder.rb', line 15 def build(_ref, from_ref, type, to_ref, props = {}) from = Registry.get(from_ref) to = Registry.get(to_ref) raise FixtureError, "Missing from node: #{from_ref}" unless from raise FixtureError, "Missing to node: #{to_ref}" unless to from_conn = from.class.connection to_conn = to.class.connection raise FixtureError, 'Cross-database relationship? Sorry, your data has commitment issues.' if from_conn != to_conn from_label = from.class.labels.first to_label = to.class.labels.first cypher = " MATCH (a:\#{from_label} {name: $from_name}), (b:\#{to_label} {name: $to_name})\n CREATE (a)-[r:\#{type} $props]->(b)\n RETURN r\n CYPHER\n\n from_conn.execute_cypher(\n cypher,\n from_name: from.name,\n to_name: to.name,\n props: props\n )\n\n nil\nend\n" |