Module: Lexster::Node::InstanceMethods

Defined in:
lib/lexster/node.rb

Instance Method Summary collapse

Instance Method Details

#_neo_saveObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/lexster/node.rb', line 88

def _neo_save
  return unless Lexster.enabled?

  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? }

  gremlin_query = <<-GREMLIN
    idx = g.idx('node_auto_index');
    q = null;
    if (idx) q = idx.get(unique_id_key, unique_id);

    node = null;
    if (q && q.hasNext()) {
      node = q.next();
      node_data.each {
        if (node.getProperty(it.key) != it.value) {
          node.setProperty(it.key, it.value);
        }
      }
    } else {
      node = g.addVertex(node_data);
      if (enable_subrefs) g.addEdge(g.v(subref_id), node, neo_subref_node_rel_type);

      if (enable_model_index) g.idx(neo_model_index_name).put('ar_id', node.ar_id, node);
    }

    node
  GREMLIN

   script_vars = {
    unique_id_key: Lexster::UNIQUE_ID_KEY,
    node_data: data,
    unique_id: self.neo_unique_id,
    enable_subrefs: Lexster.config.enable_subrefs,
    enable_model_index: Lexster.config.enable_per_model_indexes && self.class.lexster_config.enable_model_index
  }

  if Lexster.config.enable_subrefs
    script_vars.update(
      subref_id: self.class.neo_subref_node.neo_id,
      neo_subref_node_rel_type: self.class.neo_subref_node_rel_type
    )
  end

  if Lexster.config.enable_per_model_indexes && self.class.lexster_config.enable_model_index
    script_vars.update(
      neo_model_index_name: self.class.neo_model_index_name
    )
  end

  Lexster::logger.info "Node#neo_save #{self.class.name} #{self.id}"

  node = Lexster.execute_script_or_add_to_batch(gremlin_query, script_vars) do |value|
    @_neo_representation = Lexster::Node.from_hash(value)
  end.then do |result|
    neo_search_index
  end

  node
end

#neo_after_relationship_remove(relationship) ⇒ Object



186
187
188
# File 'lib/lexster/node.rb', line 186

def neo_after_relationship_remove(relationship)
  relationship.neo_destroy
end

#neo_after_relationship_through_remove(record) ⇒ Object



197
198
199
200
# File 'lib/lexster/node.rb', line 197

def neo_after_relationship_through_remove(record)
  @__neo_temp_rels.each { |record, relationship| relationship.neo_destroy }
  @__neo_temp_rels.delete(record)
end

#neo_before_relationship_through_remove(record) ⇒ Object



190
191
192
193
194
195
# File 'lib/lexster/node.rb', line 190

def neo_before_relationship_through_remove(record)
  rel_model, foreign_key_of_owner, foreign_key_of_record = Lexster::Relationship.[self.class.name.to_s][record.class.name.to_s]
  rel_model = rel_model.to_s.constantize
  @__neo_temp_rels ||= {}
  @__neo_temp_rels[record] = rel_model.where(foreign_key_of_owner => self.id, foreign_key_of_record => record.id).first
end

#neo_find_by_idObject



82
83
84
85
86
# File 'lib/lexster/node.rb', line 82

def neo_find_by_id
  # Lexster::logger.info "Node#neo_find_by_id #{self.class.neo_index_name} #{self.id}"
  node = Lexster.db.get_node_auto_index(Lexster::UNIQUE_ID_KEY, self.neo_unique_id)
  node.present? ? Lexster::Node.from_hash(node[0]) : nil
end

#neo_helper_get_field_value(field, options = {}) ⇒ Object



170
171
172
173
174
175
176
# File 'lib/lexster/node.rb', line 170

def neo_helper_get_field_value(field, options = {})
  if options[:block]
    options[:block].call
  else
    self.send(field) rescue (raise "No field #{field} for #{self.class.name}")
  end
end

#neo_load(hash) ⇒ Object



178
179
180
# File 'lib/lexster/node.rb', line 178

def neo_load(hash)
  Lexster::Node.from_hash(hash)
end

#neo_nodeObject



182
183
184
# File 'lib/lexster/node.rb', line 182

def neo_node
  _neo_representation
end

#neo_search_indexObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/lexster/node.rb', line 149

def neo_search_index
  return if self.class.lexster_config.search_options.blank? || (
    self.class.lexster_config.search_options.index_fields.blank? &&
    self.class.lexster_config.search_options.fulltext_fields.blank?
  )

  Lexster.ensure_default_fulltext_search_index

  Lexster.db.add_node_to_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, 'ar_type', self.class.name, neo_node.neo_id)

  self.class.lexster_config.search_options.fulltext_fields.each do |field, options|
    Lexster.db.add_node_to_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, "#{field}_fulltext", neo_helper_get_field_value(field, options), neo_node.neo_id)
  end

  self.class.lexster_config.search_options.index_fields.each do |field, options|
    Lexster.db.add_node_to_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, field, neo_helper_get_field_value(field, options), neo_node.neo_id)
  end

  neo_node
end