35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
94
|
# File 'lib/lexster/relationship.rb', line 35
def _neo_save
return unless Lexster.enabled?
options = self.class.lexster_config.relationship_options
start_item = self.send(options[:start_node])
end_item = self.send(options[:end_node])
return unless start_item && end_item
start_item.neo_node
end_item.neo_node
data = self.to_neo.merge(ar_type: self.class.name, ar_id: self.id, Lexster::UNIQUE_ID_KEY => self.neo_unique_id)
data.reject! { |k, v| v.nil? }
rel_type = options[:type].is_a?(Proc) ? options[:type].call(self) : options[:type]
gremlin_query = <<-GREMLIN
idx = g.idx('relationship_auto_index');
q = null;
if (idx) q = idx.get(unique_id_key, unique_id);
relationship = null;
if (q && q.hasNext()) {
relationship = q.next();
relationship_data.each {
if (relationship.getProperty(it.key) != it.value) {
relationship.setProperty(it.key, it.value);
}
}
} else {
node_index = g.idx('node_auto_index');
start_node = node_index.get(unique_id_key, start_node_unique_id).next();
end_node = node_index.get(unique_id_key, end_node_unique_id).next();
relationship = g.addEdge(start_node, end_node, rel_type, relationship_data);
}
relationship
GREMLIN
script_vars = {
unique_id_key: Lexster::UNIQUE_ID_KEY,
relationship_data: data,
unique_id: self.neo_unique_id,
start_node_unique_id: start_item.neo_unique_id,
end_node_unique_id: end_item.neo_unique_id,
rel_type: rel_type
}
Lexster::logger.info "Relationship#neo_save #{self.class.name} #{self.id}"
relationship = Lexster.execute_script_or_add_to_batch gremlin_query, script_vars do |value|
Lexster::Relationship.from_hash(value)
end
relationship
end
|