Module: Neo4j

Defined in:
lib/neo4j/neo4j.rb,
lib/neo4j-core.rb,
lib/neo4j/algo.rb,
lib/neo4j/node.rb,
lib/neo4j/config.rb,
lib/neo4j/cypher.rb,
lib/neo4j/transaction.rb,
lib/neo4j-core/to_java.rb,
lib/neo4j-core/version.rb,
lib/neo4j/relationship.rb,
lib/neo4j-core/database.rb,
lib/neo4j-core/lazy_map.rb,
lib/neo4j-core/node/node.rb,
lib/neo4j-core/rels/rels.rb,
lib/neo4j-core/equal/equal.rb,
lib/neo4j-core/index/index.rb,
lib/neo4j-core/cypher/cypher.rb,
lib/neo4j-core/event_handler.rb,
lib/neo4j-core/index/indexer.rb,
lib/neo4j-core/property/java.rb,
lib/neo4j-core/rels/traverser.rb,
lib/neo4j-core/wrapper/wrapper.rb,
lib/neo4j-core/relationship_set.rb,
lib/neo4j-core/property/property.rb,
lib/neo4j-core/index/index_config.rb,
lib/neo4j-core/index/lucene_query.rb,
lib/neo4j-core/node/class_methods.rb,
lib/neo4j-core/index/class_methods.rb,
lib/neo4j-core/traversal/evaluator.rb,
lib/neo4j-core/traversal/traversal.rb,
lib/neo4j-core/traversal/traverser.rb,
lib/neo4j-core/index/unique_factory.rb,
lib/neo4j-core/cypher/result_wrapper.rb,
lib/neo4j-core/wrapper/class_methods.rb,
lib/neo4j-core/index/indexer_registry.rb,
lib/neo4j-core/traversal/rel_expander.rb,
lib/neo4j-core/relationship/relationship.rb,
lib/neo4j-core/traversal/prune_evaluator.rb,
lib/neo4j-core/relationship/class_methods.rb,
lib/neo4j-core/traversal/filter_predicate.rb,
lib/neo4j-core/hash_with_indifferent_access.rb

Overview

Neo4j

The Neo4j modules is used to interact with an Neo4j Database instance. You can for example start and stop an instance and list all the nodes that exist in the database.

Notice - Starting and Stopping Neo4j

You don’t normally need to start the Neo4j database since it will be automatically started when needed. Before the database is started you should configure where the database is stored, see Config].

Examples:

require 'neo4j'
Neo4j::Transaction.run { Neo4j.ref_node.outgoing(:friends) << Neo4j::Node.new(:name => 'andreas')}
Neo4j.ref_node.node(:outgoing, :friends)[:name] #=> 'andreas'
Neo4j.query(Neo4j.ref_node){|ref| ref > ':friends' > :x; :x}

Defined Under Namespace

Modules: Core Classes: Algo, Config, Cypher, Node, Relationship, Transaction

Constant Summary collapse

Enumerator =

Enumerator has been moved to top level in Ruby 1.9.2, make it compatible with Ruby 1.8.7

Enumerable::Enumerator
NEO_VERSION =

Returns The version of the Neo4j jar files.

Returns:

  • (String)

    The version of the Neo4j jar files

Neo4j::Community::VERSION

Class Method Summary collapse

Class Method Details

._all_nodes(this_db = self.started_db) ⇒ Enumerable

Returns all nodes in the database but not wrapped in ruby classes.

Returns:

  • (Enumerable)

    all nodes in the database but not wrapped in ruby classes.



251
252
253
# File 'lib/neo4j/neo4j.rb', line 251

def _all_nodes(this_db = self.started_db)
  Enumerator.new(this_db, :_each_node)
end

._query(q) ⇒ Object

Performs a cypher query with given string

Parameters:

  • q (String)

    the cypher query as a String



124
125
126
127
128
# File 'lib/neo4j/neo4j.rb', line 124

def _query(q)
  engine = Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(db)
  result = engine.execute(q)
  Neo4j::Core::Cypher::ResultWrapper.new(result)
end

.all_nodes(this_db = self.started_db) ⇒ Enumerable

Returns all nodes in the database.

Returns:

  • (Enumerable)

    all nodes in the database



246
247
248
# File 'lib/neo4j/neo4j.rb', line 246

def all_nodes(this_db = self.started_db)
  Enumerator.new(this_db, :each_node)
end

.configNeo4j::Config

Same as typing; Neo4j::Config

Returns:



84
85
86
# File 'lib/neo4j/neo4j.rb', line 84

def config
  Neo4j::Config
end

.dbNeo4j::Database

Returns the database holding references to both the Neo4j Graph Database and the Lucene Database. Creates a new one if it does not exist, but does not start it.

Returns:

  • (Neo4j::Database)

    the created or existing database



56
57
58
# File 'lib/neo4j/neo4j.rb', line 56

def db
  @db ||= Neo4j::Core::Database.new
end

.db=(my_db) ⇒ Object

Sets which Neo4j::Database instance to use. It wraps both the Neo4j Database and Lucene Database.

Parameters:

  • my_db (Neo4j::Database)
    • the database instance to use



48
49
50
# File 'lib/neo4j/neo4j.rb', line 48

def db=(my_db)
  @db = my_db
end

.default_loggerObject

Returns the default Ruby logger.

Returns:

  • the default Ruby logger



156
157
158
159
160
161
# File 'lib/neo4j/neo4j.rb', line 156

def default_logger #:nodoc:
  require 'logger'
  logger = Logger.new(STDOUT)
  logger.sev_threshold = Neo4j::Config[:logger_level] || Logger::INFO
  logger
end

.default_ref_node(this_db = self.started_db) ⇒ Object

Returns the default reference node, which is a “starting point” in the node space.

Returns:

  • the default reference node, which is a “starting point” in the node space.



190
191
192
# File 'lib/neo4j/neo4j.rb', line 190

def default_ref_node(this_db = self.started_db)
  this_db.graph.reference_node
end

.event_handler(this_db = db) ⇒ Neo4j::Core::EventHandler

Returns the event handler.

Returns:



256
257
258
# File 'lib/neo4j/neo4j.rb', line 256

def event_handler(this_db = db)
  this_db.event_handler
end

.ha_master?Boolean

Only available using the neo4j-enterprise gem.

Returns:

  • (Boolean)

    true if the Neo4j database is the HA master



241
242
243
# File 'lib/neo4j/neo4j.rb', line 241

def ha_master?
  Neo4j.management(Java::OrgNeo4jManagement::HighAvailability).isMaster
end

.loggerObject

Returns the logger used by neo4j. If not specified (with Neo4j.logger=) it will use the standard Ruby logger. You can change standard logger threshold by configuration :logger_level.

You can also specify which logger class should take care of logging with the :logger configuration.

Examples:


Neo4j::Config[:logger] = Logger.new(STDOUT)
Neo4j::Config[:logger_level] = Logger::ERROR

See Also:

  • #logger=


144
145
146
# File 'lib/neo4j/neo4j.rb', line 144

def logger
  @logger ||= Neo4j::Config[:logger] || default_logger
end

.logger=(logger) ⇒ Object

Sets which logger should be used. If this this is not called then the standard Ruby logger will be used.

See Also:

  • #logger


151
152
153
# File 'lib/neo4j/neo4j.rb', line 151

def logger=(logger)
  @logger = logger
end

.management(jmx_clazz = Java::OrgNeo4jJmx::Primitives, this_db = self.started_db) ⇒ Object

Returns a Management JMX Bean.

Notice that this information is also provided by the jconsole Java tool, check wiki.neo4j.org/content/Monitoring_and_Deployment and docs.neo4j.org/chunked/milestone/operations-monitoring.html. Some of these management features are only available when using the neo4j-advanced and neo4j-enterprise gems.

By default it returns the Primitivies JMX Bean that can be used to find number of nodes in use.

Notice you must require the neo4j-advanced gem for these stats.

Examples:

Example Neo4j Primititives


Neo4j.management.get_number_of_node_ids_in_use
Neo4j.management.getNumberOfPropertyIdsInUse
Neo4j.management.getNumberOfRelationshipIdsInUse
Neo4j.management.get_number_of_relationship_type_ids_in_use

Example Neo4j HA Cluster Info


Neo4j.management(org.neo4j.management.HighAvailability).isMaster

Parameters:

See Also:

  • the jmx_clazz p


235
236
237
# File 'lib/neo4j/neo4j.rb', line 235

def management(jmx_clazz = Java::OrgNeo4jJmx::Primitives, this_db = self.started_db)
  this_db.management(jmx_clazz)
end

.query(*params, &query_dsl) ⇒ Neo4j::Core::Cypher::ResultWrapper

Note:

Returns a read-once only forward iterable.

Executes a Cypher Query Check the neo4j Returns an enumerable of hash values.

Examples:

Using the Cypher DSL

q = Neo4j.query{ match node(3) <=> node(:x); ret :x}
q.first[:n] #=> the @node
q.columns.first => :n

Using the Cypher DSL and one parameter (n=Neo4j.ref_node)

q = Neo4j.query(Neo4j.ref_node){|n| n <=> node(:x); :x}
q.first[:n] #=> the @node
q.columns.first => :n

Using an array of nodes

# same as - two_nodes=node(Neo4j.ref_node.neo_id, node_b.neo_id), b = node(b.neo_id)
q = Neo4j.query([Neo4j.ref_node, node_b], node_c){|two_nodes, b| two_nodes <=> b; b}

With an string

q = Neo4j._query("START n=node(42) RETURN n")
q.first(:n) #=> the @node
q.columns.first => :n

Parameters:

  • params

    parameter for the query_dsl block

Returns:

See Also:



116
117
118
119
# File 'lib/neo4j/neo4j.rb', line 116

def query(*params, &query_dsl)
  q = Cypher.new(*params, &query_dsl).to_s
  _query(q)
end

.read_only?true, false

Checks read only mode of the database. Only one process can have write access to the database. It will always return false if the database is not started yet.

Returns:

  • (true, false)

    if the database has started up in read only mode



63
64
65
# File 'lib/neo4j/neo4j.rb', line 63

def read_only?
  !!(@db && @db.graph && @db.read_only?)
end

.ref_node(this_db = self.started_db) ⇒ Object

Note:

In case the ref_node has been assigned via the threadlocal_ref_node method, then that node will be returned instead.

Usually, a client attaches relationships to this node that leads into various parts of the node space. ®return the reference node, which is a “starting point” in the node space.



198
199
200
201
# File 'lib/neo4j/neo4j.rb', line 198

def ref_node(this_db = self.started_db)
  return Thread.current[:local_ref_node] if Thread.current[:local_ref_node]
  default_ref_node(this_db)
end

.running?true, false

Returns if the database is running.

Returns:

  • (true, false)

    if the database is running



177
178
179
# File 'lib/neo4j/neo4j.rb', line 177

def running?
  !!(@db && @db.running?)
end

.shutdown(this_db = @db) ⇒ Object

Stops this database There are Ruby hooks that will do this automatically for you.



184
185
186
# File 'lib/neo4j/neo4j.rb', line 184

def shutdown(this_db = @db)
  this_db.shutdown if this_db
end

.start(config_file = nil, external_db = $NEO4J_SERVER) ⇒ Object

Start Neo4j using the default database. This is usually not required since the database will be started automatically when it is used. If the global variable $NEO4J_SERVER is defined then it will use that as the Java Graph DB. This can be used if you want to embed neo4j.rb and already got an instance of the Java Neo4j Database service.

Parameters:

  • config_file (String) (defaults to: nil)

    (optionally) if this is nil or not given use the Neo4j::Config, otherwise setup the Neo4j::Config file using the provided YAML configuration file.

  • external_db (Java::OrgNeo4jKernel::EmbeddedGraphDatabase) (defaults to: $NEO4J_SERVER)

    (optionally) use this Java Neo4j instead of creating a new neo4j database service.



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/neo4j/neo4j.rb', line 33

def start(config_file=nil, external_db = $NEO4J_SERVER)
  return if @db && @db.running?

  Neo4j.config.default_file = config_file if config_file
  if external_db
    @db ||= Neo4j::Core::Database.new
    self.db.start_external_db(external_db)
  else
    db.start
  end
end

.started_dbNeo4j::Database

Returns a started db instance. Starts it’s not running. if $NEO4J_SERVER is defined then use that Java Neo4j Database service instead of creating a new one.

Returns:

  • (Neo4j::Database)

    a started database



70
71
72
73
# File 'lib/neo4j/neo4j.rb', line 70

def started_db
  start unless db.running?
  db
end

.storage_pathString

If the database is not running it returns Neo4j::Config.storage_path otherwise it asks the database

Returns:

  • (String)

    the current storage path of the database



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

def storage_path
  return Neo4j::Config.storage_path unless db.running?
  db.storage_path
end

.threadlocal_ref_node=(reference_node) ⇒ Object

Changes the reference node on a threadlocal basis. This can be used to achieve multitenancy. All new entities will be attached to the new ref_node, which effectively partitions the graph, and hence scopes traversals.



206
207
208
# File 'lib/neo4j/neo4j.rb', line 206

def threadlocal_ref_node=(reference_node)
  Thread.current[:local_ref_node] = reference_node.nil? ? nil : reference_node._java_node
end

.unstarted_dbNeo4j::Database

Returns an unstarted db instance

This is typically used for configuring the database, which must sometimes be done before the database is started if the database was already started an exception will be raised

Returns:

  • (Neo4j::Database)

    an not started database



170
171
172
173
174
# File 'lib/neo4j/neo4j.rb', line 170

def unstarted_db
  @db ||= Neo4j::Core::Database.new
  raise "database was already started" if @db.running?
  @db
end