Module: Neoid

Defined in:
lib/neoid.rb,
lib/neoid/node.rb,
lib/neoid/batch.rb,
lib/neoid/config.rb,
lib/neoid/railtie.rb,
lib/neoid/version.rb,
lib/neoid/middleware.rb,
lib/neoid/model_config.rb,
lib/neoid/relationship.rb,
lib/neoid/search_session.rb,
lib/neoid/model_additions.rb,
lib/neoid/database_cleaner.rb

Defined Under Namespace

Modules: ModelAdditions, Node, Relationship Classes: Batch, BatchPromiseProxy, Config, Middleware, ModelConfig, NeoDatabaseCleaner, Railtie, SearchConfig, SearchSession, SingleResultPromiseProxy

Constant Summary collapse

DEFAULT_FULLTEXT_SEARCH_INDEX_NAME =
:neoid_default_search_index
NODE_AUTO_INDEX_NAME =
'node_auto_index'
RELATIONSHIP_AUTO_INDEX_NAME =
'relationship_auto_index'
UNIQUE_ID_KEY =
'neoid_unique_id'
VERSION =
'0.2.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configObject (readonly)

Returns the value of attribute config.



24
25
26
# File 'lib/neoid.rb', line 24

def config
  @config
end

.dbObject

Returns the value of attribute db.



20
21
22
# File 'lib/neoid.rb', line 20

def db
  @db
end

.env_loadedObject

Returns the value of attribute env_loaded.



23
24
25
# File 'lib/neoid.rb', line 23

def env_loaded
  @env_loaded
end

.loggerObject

Returns the value of attribute logger.



21
22
23
# File 'lib/neoid.rb', line 21

def logger
  @logger
end

.ref_nodeObject

Returns the value of attribute ref_node.



22
23
24
# File 'lib/neoid.rb', line 22

def ref_node
  @ref_node
end

Class Method Details

.batch(options = {}, &block) ⇒ Object



78
79
80
# File 'lib/neoid.rb', line 78

def batch(options={}, &block)
  Neoid::Batch.new(options, &block).run
end

.clean_db(confirm) ⇒ Object



94
95
96
97
# File 'lib/neoid.rb', line 94

def clean_db(confirm)
  puts 'must call with confirm: Neoid.clean_db(:yes_i_am_sure)' and return unless confirm == :yes_i_am_sure
  Neoid::NeoDatabaseCleaner.clean_db
end

.configure {|config| ... } ⇒ Object

Yields:



46
47
48
# File 'lib/neoid.rb', line 46

def configure
  yield config
end

.enabledObject Also known as: enabled?



103
104
105
106
107
# File 'lib/neoid.rb', line 103

def enabled
  flag = Thread.current[:neoid_enabled]
  # flag should be set by the middleware. in case it wasn't (non-rails app or console), default it to true
  flag.nil? ? true : flag
end

.enabled=(flag) ⇒ Object



99
100
101
# File 'lib/neoid.rb', line 99

def enabled=(flag)
  Thread.current[:neoid_enabled] = flag
end

.ensure_default_fulltext_search_indexObject

create a fulltext index if not exists



131
132
133
# File 'lib/neoid.rb', line 131

def ensure_default_fulltext_search_index
  Neoid.db.create_node_index(DEFAULT_FULLTEXT_SEARCH_INDEX_NAME, 'fulltext', 'lucene') unless (indexes = Neoid.db.list_node_indexes) && indexes[DEFAULT_FULLTEXT_SEARCH_INDEX_NAME]
end

.execute_script_or_add_to_batch(gremlin_query, script_vars) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/neoid.rb', line 117

def execute_script_or_add_to_batch(gremlin_query, script_vars)
  if Neoid::Batch.current_batch
    # returns a SingleResultPromiseProxy!
    Neoid::Batch.current_batch << [:execute_script, gremlin_query, script_vars]
  else
    value = Neoid.db.execute_script(gremlin_query, script_vars)

    value = yield(value) if block_given?

    Neoid::BatchPromiseProxy.new(value)
  end
end

.initialize_allObject



50
51
52
53
54
55
# File 'lib/neoid.rb', line 50

def initialize_all
  @env_loaded = true
  logger.info 'Neoid initialize_all'
  initialize_relationships
  initialize_server
end

.initialize_serverObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/neoid.rb', line 57

def initialize_server
  initialize_auto_index
  initialize_subrefs
  initialize_per_model_indexes
  # begin
  #   @initialized_server = true
  #   initialize_auto_index
  #   initialize_subrefs
  #   initialize_per_model_indexes
  # rescue Exception => e
  #   @initialized_server = false
  #   logger.error "Failed to initialize neoid: #{e.message}"
  # end
end

.node_modelsObject



26
27
28
# File 'lib/neoid.rb', line 26

def node_models
  @node_models ||= []
end

.relationship_modelsObject



30
31
32
# File 'lib/neoid.rb', line 30

def relationship_models
  @relationship_models ||= []
end

.reset_cached_variablesObject



90
91
92
# File 'lib/neoid.rb', line 90

def reset_cached_variables
  initialize_subrefs
end

.search(types, term, options = {}) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/neoid.rb', line 135

def search(types, term, options = {})
  options = options.reverse_merge(limit: 15, match_type: 'AND')

  types = [*types]

  query = []

  types.each do |type|
    query_for_type = []

    query_for_type << "ar_type:#{type.name}"

    case term
    when String
      search_in_fields = type.neoid_config.search_options.fulltext_fields.keys
      next if search_in_fields.empty?
      query_for_type << search_in_fields.map{ |field| generate_field_query(field, term, true, options[:match_type]) }.join(' OR ')
    when Hash
      term.each do |field, value|
        query_for_type << generate_field_query(field, value, false)
      end
    end

    query << "(#{query_for_type.join(') AND (')})"
  end

  query = "(#{query.join(') OR (')})"

  logger.info "Neoid query #{query}"

  gremlin_query = <<-GREMLIN
    #{options[:before_query]}

    idx = g.getRawGraph().index().forNodes('#{DEFAULT_FULLTEXT_SEARCH_INDEX_NAME}')
    hits = idx.query('#{sanitize_query_for_gremlin(query)}')

    hits = #{options[:limit] ? "hits.take(#{options[:limit]})" : 'hits'}

    #{options[:after_query]}
  GREMLIN

  logger.info "[NEOID] search:\n#{gremlin_query}"

  results = Neoid.db.execute_script(gremlin_query)

  SearchSession.new(results, *types)
end

.use(flag = true) ⇒ Object



110
111
112
113
114
115
# File 'lib/neoid.rb', line 110

def use(flag=true)
  old, self.enabled = enabled?, flag
  yield if block_given?
ensure
  self.enabled = old
end