neo4j-core 0.0.1-java
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +27 -0
- data/README.rdoc +27 -0
- data/config/neo4j/config.yml +102 -0
- data/lib/db/active_tx_log +1 -0
- data/lib/db/index/lucene-store.db +0 -0
- data/lib/db/index/lucene.log.1 +0 -0
- data/lib/db/index/lucene.log.active +0 -0
- data/lib/db/lock +0 -0
- data/lib/db/messages.log +530 -0
- data/lib/db/neostore +0 -0
- data/lib/db/neostore.id +0 -0
- data/lib/db/neostore.nodestore.db +0 -0
- data/lib/db/neostore.nodestore.db.id +0 -0
- data/lib/db/neostore.propertystore.db +0 -0
- data/lib/db/neostore.propertystore.db.arrays +0 -0
- data/lib/db/neostore.propertystore.db.arrays.id +0 -0
- data/lib/db/neostore.propertystore.db.id +0 -0
- data/lib/db/neostore.propertystore.db.index +0 -0
- data/lib/db/neostore.propertystore.db.index.id +0 -0
- data/lib/db/neostore.propertystore.db.index.keys +0 -0
- data/lib/db/neostore.propertystore.db.index.keys.id +0 -0
- data/lib/db/neostore.propertystore.db.strings +0 -0
- data/lib/db/neostore.propertystore.db.strings.id +0 -0
- data/lib/db/neostore.relationshipstore.db +0 -0
- data/lib/db/neostore.relationshipstore.db.id +0 -0
- data/lib/db/neostore.relationshiptypestore.db +0 -0
- data/lib/db/neostore.relationshiptypestore.db.id +0 -0
- data/lib/db/neostore.relationshiptypestore.db.names +0 -0
- data/lib/db/neostore.relationshiptypestore.db.names.id +0 -0
- data/lib/db/nioneo_logical.log.2 +0 -0
- data/lib/db/nioneo_logical.log.active +0 -0
- data/lib/db/tm_tx_log.1 +0 -0
- data/lib/neo4j/config.rb +139 -0
- data/lib/neo4j/cypher.rb +156 -0
- data/lib/neo4j/neo4j.rb +244 -0
- data/lib/neo4j/neo4j.rb~ +214 -0
- data/lib/neo4j/node.rb +39 -0
- data/lib/neo4j/relationship.rb +61 -0
- data/lib/neo4j/transaction.rb +86 -0
- data/lib/neo4j/type_converters/type_converters.rb +287 -0
- data/lib/neo4j-core/cypher/cypher.rb +867 -0
- data/lib/neo4j-core/cypher/result_wrapper.rb +39 -0
- data/lib/neo4j-core/database.rb +191 -0
- data/lib/neo4j-core/equal/equal.rb +23 -0
- data/lib/neo4j-core/event_handler.rb +265 -0
- data/lib/neo4j-core/index/class_methods.rb +117 -0
- data/lib/neo4j-core/index/index.rb +36 -0
- data/lib/neo4j-core/index/index_config.rb +112 -0
- data/lib/neo4j-core/index/indexer.rb +243 -0
- data/lib/neo4j-core/index/indexer_registry.rb +55 -0
- data/lib/neo4j-core/index/lucene_query.rb +264 -0
- data/lib/neo4j-core/lazy_map.rb +21 -0
- data/lib/neo4j-core/node/class_methods.rb +77 -0
- data/lib/neo4j-core/node/node.rb +47 -0
- data/lib/neo4j-core/property/property.rb +94 -0
- data/lib/neo4j-core/relationship/class_methods.rb +80 -0
- data/lib/neo4j-core/relationship/relationship.rb +97 -0
- data/lib/neo4j-core/relationship_set.rb +61 -0
- data/lib/neo4j-core/rels/rels.rb +147 -0
- data/lib/neo4j-core/rels/traverser.rb +99 -0
- data/lib/neo4j-core/to_java.rb +51 -0
- data/lib/neo4j-core/traversal/evaluator.rb +36 -0
- data/lib/neo4j-core/traversal/filter_predicate.rb +30 -0
- data/lib/neo4j-core/traversal/prune_evaluator.rb +20 -0
- data/lib/neo4j-core/traversal/rel_expander.rb +35 -0
- data/lib/neo4j-core/traversal/traversal.rb +130 -0
- data/lib/neo4j-core/traversal/traverser.rb +295 -0
- data/lib/neo4j-core/version.rb +5 -0
- data/lib/neo4j-core.rb +64 -0
- data/neo4j-core.gemspec +31 -0
- metadata +145 -0
data/lib/neo4j/config.rb
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
module Neo4j
|
2
|
+
|
3
|
+
|
4
|
+
# == Keeps configuration for neo4j
|
5
|
+
#
|
6
|
+
# The most important configuration is storage_path which is used to
|
7
|
+
# locate where the neo4j database is stored on the filesystem.
|
8
|
+
# If this directory is empty then a new database will be created, otherwise it will use the
|
9
|
+
# database from that directory.
|
10
|
+
#
|
11
|
+
# == Configurations keys
|
12
|
+
#
|
13
|
+
# storage_path where the database is stored
|
14
|
+
# timestamps if timestamps should be used when saving the model (Neo4j::Rails::Model)
|
15
|
+
# lucene lucene configuration for fulltext and exact indices
|
16
|
+
# enable_rules if false the _all relationship to all instances will not be created and custom rules will not be available. (Neo4j::NodeMixin and Neo4j::Rails::Model)
|
17
|
+
# identity_map default false, See Neo4j::IdentityMap (Neo4j::NodeMixin and Neo4j::Rails::Model)
|
18
|
+
#
|
19
|
+
class Config
|
20
|
+
|
21
|
+
class << self
|
22
|
+
|
23
|
+
# @return [Fixnum] The location of the default configuration file.
|
24
|
+
def default_file
|
25
|
+
@default_file ||= File.expand_path(File.join(File.dirname(__FILE__), "..", "..", "config", "neo4j", "config.yml"))
|
26
|
+
end
|
27
|
+
|
28
|
+
# Sets the location of the configuration YAML file and old deletes configurations.
|
29
|
+
#
|
30
|
+
# @param [String] file_path represent the path to the file.
|
31
|
+
def default_file=(file_path)
|
32
|
+
@configuration = nil
|
33
|
+
@defaults = nil
|
34
|
+
@default_file = File.expand_path(file_path)
|
35
|
+
end
|
36
|
+
|
37
|
+
# @return [Hash] the default file loaded by yaml
|
38
|
+
def defaults
|
39
|
+
require 'yaml'
|
40
|
+
@defaults ||= YAML.load_file(default_file)
|
41
|
+
end
|
42
|
+
|
43
|
+
# @return [String] the expanded path of the Config[:storage_path] property
|
44
|
+
def storage_path
|
45
|
+
File.expand_path(self[:storage_path])
|
46
|
+
end
|
47
|
+
|
48
|
+
# Yields the configuration
|
49
|
+
#
|
50
|
+
# @example
|
51
|
+
# Neo4j::Config.use do |config|
|
52
|
+
# config[:storage_path] = '/var/neo4j'
|
53
|
+
# end
|
54
|
+
#
|
55
|
+
# @return nil
|
56
|
+
# @yield config
|
57
|
+
# @yieldparam [Neo4j::Config] config - this configuration class
|
58
|
+
def use
|
59
|
+
@configuration ||= {}
|
60
|
+
yield @configuration
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
# Sets the value of a config entry.
|
66
|
+
#
|
67
|
+
# @param [Symbol] key the key to set the parameter for
|
68
|
+
# @param val the value of the parameter.
|
69
|
+
def []=(key, val)
|
70
|
+
(@configuration ||= setup)[key.to_s] = val
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
# @param [Symbol] key The key of the config entry value we want
|
75
|
+
# @return the the value of a config entry
|
76
|
+
def [](key)
|
77
|
+
(@configuration ||= setup)[key.to_s]
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
# Remove the value of a config entry.
|
82
|
+
#
|
83
|
+
# @param [Symbol] key the key of the configuration entry to delete
|
84
|
+
# @return The value of the removed entry.
|
85
|
+
def delete(key)
|
86
|
+
@configuration.delete(key)
|
87
|
+
end
|
88
|
+
|
89
|
+
|
90
|
+
# Remove all configuration. This can be useful for testing purpose.
|
91
|
+
#
|
92
|
+
# @return nil
|
93
|
+
def delete_all
|
94
|
+
@configuration = nil
|
95
|
+
end
|
96
|
+
|
97
|
+
|
98
|
+
# @return [Hash] The config as a hash.
|
99
|
+
def to_hash
|
100
|
+
@configuration ||= setup
|
101
|
+
end
|
102
|
+
|
103
|
+
# @return [String] The config as a YAML
|
104
|
+
def to_yaml
|
105
|
+
@configuration.to_yaml
|
106
|
+
end
|
107
|
+
|
108
|
+
# Converts the defaults hash to a Java HashMap used by the Neo4j API.
|
109
|
+
# @return a Java HashMap used by the Java Neo4j API as configuration for the GraphDatabase
|
110
|
+
def to_java_map
|
111
|
+
map = java.util.HashMap.new
|
112
|
+
to_hash.each_pair do |k, v|
|
113
|
+
case v
|
114
|
+
when TrueClass
|
115
|
+
map[k.to_s] = "YES"
|
116
|
+
when FalseClass
|
117
|
+
map[k.to_s] = "NO"
|
118
|
+
when String, Fixnum, Float
|
119
|
+
map[k.to_s] = v.to_s
|
120
|
+
# skip list and hash values - not accepted by the Java Neo4j API
|
121
|
+
end
|
122
|
+
end
|
123
|
+
map
|
124
|
+
end
|
125
|
+
|
126
|
+
private
|
127
|
+
|
128
|
+
# @return The a new configuration using default values as a hash.
|
129
|
+
def setup()
|
130
|
+
@configuration = {}
|
131
|
+
@configuration.merge!(defaults)
|
132
|
+
@configuration
|
133
|
+
end
|
134
|
+
|
135
|
+
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
end
|
data/lib/neo4j/cypher.rb
ADDED
@@ -0,0 +1,156 @@
|
|
1
|
+
module Neo4j
|
2
|
+
class Cypher
|
3
|
+
attr_reader :expressions
|
4
|
+
|
5
|
+
include Neo4j::Core::Cypher
|
6
|
+
include Neo4j::Core::Cypher::MathFunctions
|
7
|
+
|
8
|
+
# Creates a Cypher DSL query.
|
9
|
+
# To create a new cypher query you must initialize it either an String or a Block.
|
10
|
+
#
|
11
|
+
# @example <tt>START n0=node(3) MATCH (n0)--(x) RETURN x</tt> same as
|
12
|
+
# Cypher.new { start n = node(3); match n <=> :x; ret :x }.to_s
|
13
|
+
#
|
14
|
+
# @example <tt>START n0=node(3) MATCH (n0)-[r]->(x) RETURN r</tt> same as
|
15
|
+
# node(3) > :r > :x; :r
|
16
|
+
#
|
17
|
+
# @example <tt>START n0=node(3) MATCH (n0)-->(x) RETURN x</tt> same as
|
18
|
+
# node(3) >> :x; :x
|
19
|
+
#
|
20
|
+
# @param args the argument for the dsl_block
|
21
|
+
# @yield the block which will be evaluated in the context of this object in order to create an Cypher Query string
|
22
|
+
# @yieldreturn [Return, Object] If the return is not an instance of Return it will be converted it to a Return object (if possible).
|
23
|
+
def initialize(*args, &dsl_block)
|
24
|
+
@expressions = []
|
25
|
+
@variables = []
|
26
|
+
res = self.instance_exec(*args, &dsl_block)
|
27
|
+
unless res.kind_of?(Return)
|
28
|
+
res.respond_to?(:to_a) ? ret(*res) : ret(res)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Does nothing, just for making the DSL easier to read (maybe).
|
33
|
+
# @return self
|
34
|
+
def match(*)
|
35
|
+
self
|
36
|
+
end
|
37
|
+
|
38
|
+
# Does nothing, just for making the DSL easier to read (maybe)
|
39
|
+
# @return self
|
40
|
+
def start(*)
|
41
|
+
self
|
42
|
+
end
|
43
|
+
|
44
|
+
def where(w=nil)
|
45
|
+
Where.new(@expressions, w) if w.is_a?(String)
|
46
|
+
self
|
47
|
+
end
|
48
|
+
|
49
|
+
# Specifies a start node by performing a lucene query.
|
50
|
+
# @param [Class] index_class a class responsible for an index
|
51
|
+
# @param [String] q the lucene query
|
52
|
+
# @param [Symbol] index_type the type of index
|
53
|
+
# @return [NodeQuery]
|
54
|
+
def query(index_class, q, index_type = :exact)
|
55
|
+
NodeQuery.new(index_class, q, index_type, @expressions)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Specifies a start node by performing a lucene query.
|
59
|
+
# @param [Class] index_class a class responsible for an index
|
60
|
+
# @param [String, Symbol] key the key we ask for
|
61
|
+
# @param [String, Symbol] value the value of the key we ask for
|
62
|
+
# @return [NodeLookup]
|
63
|
+
def lookup(index_class, key, value)
|
64
|
+
NodeLookup.new(index_class, key, value, @expressions)
|
65
|
+
end
|
66
|
+
|
67
|
+
# Creates a node variable.
|
68
|
+
# It will create different variables depending on the type of the first element in the nodes argument.
|
69
|
+
# * Fixnum - it will be be used as neo_id for start node(s) (StartNode)
|
70
|
+
# * Symbol - it will create an unbound node variable with the same name as the symbol (NodeVar#as)
|
71
|
+
# * empty array - it will create an unbound node variable (NodeVar)
|
72
|
+
#
|
73
|
+
# @param [Fixnum,Symbol,String] nodes the id of the nodes we want to start from
|
74
|
+
# @return [StartNode, NodeVar]
|
75
|
+
def node(*nodes)
|
76
|
+
if nodes.first.is_a?(Symbol)
|
77
|
+
NodeVar.new(@expressions, @variables).as(nodes.first)
|
78
|
+
elsif !nodes.empty?
|
79
|
+
StartNode.new(nodes, @expressions)
|
80
|
+
else
|
81
|
+
NodeVar.new(@expressions, @variables)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Similar to #node
|
86
|
+
# @return [StartRel, RelVar]
|
87
|
+
def rel(*rels)
|
88
|
+
if rels.first.is_a?(Fixnum) || rels.first.respond_to?(:neo_id)
|
89
|
+
StartRel.new(rels, @expressions)
|
90
|
+
elsif rels.first.is_a?(Symbol)
|
91
|
+
RelVar.new(@expressions, @variables, "").as(rels.first)
|
92
|
+
elsif rels.first.is_a?(String)
|
93
|
+
RelVar.new(@expressions, @variables, rels.first)
|
94
|
+
else
|
95
|
+
raise "Unknown arg #{rels.inspect}"
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
# Specifies a return statement.
|
100
|
+
# Notice that this is not needed, since the last value of the DSL block will be converted into one or more
|
101
|
+
# return statements.
|
102
|
+
# @param [Symbol, #var_name] returns a list of variables we want to return
|
103
|
+
# @return [Return]
|
104
|
+
def ret(*returns)
|
105
|
+
options = returns.last.is_a?(Hash) ? returns.pop : {}
|
106
|
+
@expressions -= @expressions.find_all { |r| r.clause == :return && returns.include?(r) }
|
107
|
+
returns.each { |ret| Return.new(ret, @expressions, options) unless ret.respond_to?(:clause) && [:order_by, :skip, :limit].include?(ret.clause)}
|
108
|
+
@expressions.last
|
109
|
+
end
|
110
|
+
|
111
|
+
def shortest_path(&block)
|
112
|
+
match = instance_eval(&block)
|
113
|
+
match.algorithm = 'shortestPath'
|
114
|
+
match.find_match_start
|
115
|
+
end
|
116
|
+
|
117
|
+
def shortest_paths(&block)
|
118
|
+
match = instance_eval(&block)
|
119
|
+
match.algorithm = 'shortestPaths'
|
120
|
+
match.find_match_start
|
121
|
+
end
|
122
|
+
|
123
|
+
# @param [Symbol,nil] variable the entity we want to count or wildcard (*)
|
124
|
+
# @return [Return] a counter return clause
|
125
|
+
def count(variable='*')
|
126
|
+
Return.new("count(#{variable.to_s})", @expressions)
|
127
|
+
end
|
128
|
+
|
129
|
+
def coalesce(*args)
|
130
|
+
s = args.map { |x| x.var_name }.join(", ")
|
131
|
+
Return.new("coalesce(#{s})", @expressions)
|
132
|
+
end
|
133
|
+
|
134
|
+
def nodes(*args)
|
135
|
+
s = args.map { |x| x.referenced!; x.var_name }.join(", ")
|
136
|
+
Return.new("nodes(#{s})", @expressions)
|
137
|
+
end
|
138
|
+
|
139
|
+
def rels(*args)
|
140
|
+
s = args.map { |x| x.referenced!; x.var_name }.join(", ")
|
141
|
+
Return.new("relationships(#{s})", @expressions)
|
142
|
+
end
|
143
|
+
|
144
|
+
# Converts the DSL query to a cypher String which can be executed by cypher query engine.
|
145
|
+
def to_s
|
146
|
+
clause = nil
|
147
|
+
@expressions.map do |expr|
|
148
|
+
next unless expr.valid?
|
149
|
+
expr_to_s = expr.clause != clause ? "#{expr.prefix} #{expr.to_s}" : "#{expr.separator}#{expr.to_s}"
|
150
|
+
clause = expr.clause
|
151
|
+
expr_to_s
|
152
|
+
end.join
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
end
|
data/lib/neo4j/neo4j.rb
ADDED
@@ -0,0 +1,244 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
# = Neo4j
|
4
|
+
#
|
5
|
+
# The Neo4j modules is used to interact with an Neo4j Database instance.
|
6
|
+
# You can for example start and stop an instance and list all the nodes that exist in the database.
|
7
|
+
#
|
8
|
+
# === Starting and Stopping Neo4j
|
9
|
+
# You don't normally need to start the Neo4j database since it will be automatically started when needed.
|
10
|
+
# Before the database is started you should configure where the database is stored, see {Neo4j::Config]}.
|
11
|
+
#
|
12
|
+
module Neo4j
|
13
|
+
|
14
|
+
# @return [String] The version of the Neo4j jar files
|
15
|
+
NEO_VERSION = Neo4j::Community::VERSION
|
16
|
+
|
17
|
+
class << self
|
18
|
+
|
19
|
+
# Start Neo4j using the default database.
|
20
|
+
# This is usually not required since the database will be started automatically when it is used.
|
21
|
+
# If the global variable $NEO4J_SERVER is defined then it will use that as the Java Graph DB. This can
|
22
|
+
# be used if you want to embed neo4j.rb and already got an instance of the Java Neo4j Database service.
|
23
|
+
#
|
24
|
+
# @param [String] config_file (optionally) if this is nil or not given use the Neo4j::Config, otherwise setup the Neo4j::Config file using the provided YAML configuration file.
|
25
|
+
# @param [Java::OrgNeo4jKernel::EmbeddedGraphDatabase] external_db (optionally) use this Java Neo4j instead of creating a new neo4j database service.
|
26
|
+
def start(config_file=nil, external_db = $NEO4J_SERVER)
|
27
|
+
return if @db && @db.running?
|
28
|
+
|
29
|
+
Neo4j.config.default_file = config_file if config_file
|
30
|
+
if external_db
|
31
|
+
@db ||= Neo4j::Core::Database.new
|
32
|
+
self.db.start_external_db(external_db)
|
33
|
+
else
|
34
|
+
db.start
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
|
39
|
+
# Sets which Neo4j::Database instance to use. It wraps both the Neo4j Database and Lucene Database.
|
40
|
+
# @param [Neo4j::Database] my_db - the database instance to use
|
41
|
+
def db=(my_db)
|
42
|
+
@db = my_db
|
43
|
+
end
|
44
|
+
|
45
|
+
# Returns the database holding references to both the Neo4j Graph Database and the Lucene Database.
|
46
|
+
# Creates a new one if it does not exist, but does not start it.
|
47
|
+
#
|
48
|
+
# @return [Neo4j::Database] the created or existing database
|
49
|
+
def db
|
50
|
+
@db ||= Neo4j::Core::Database.new
|
51
|
+
end
|
52
|
+
|
53
|
+
# Checks read only mode of the database. Only one process can have write access to the database.
|
54
|
+
# @return [true, false] if the database has started up in read only mode
|
55
|
+
def read_only?
|
56
|
+
(@db && @db.graph && @db.read_only?)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Returns a started db instance. Starts it's not running.
|
60
|
+
# if $NEO4J_SERVER is defined then use that Java Neo4j Database service instead of creating a new one.
|
61
|
+
# @return [Neo4j::Database] a started database
|
62
|
+
def started_db
|
63
|
+
start unless db.running?
|
64
|
+
db
|
65
|
+
end
|
66
|
+
|
67
|
+
# Runs all user defined migrations.
|
68
|
+
def migrate!
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
# @return [String, nil] the current storage path of a running neo4j database. If the database is not running it returns nil.
|
73
|
+
def storage_path
|
74
|
+
return nil unless db.running?
|
75
|
+
db.storage_path
|
76
|
+
end
|
77
|
+
|
78
|
+
# Same as typing; Neo4j::Config
|
79
|
+
# @return [Neo4j::Config] the Neo4j::Config class
|
80
|
+
def config
|
81
|
+
Neo4j::Config
|
82
|
+
end
|
83
|
+
|
84
|
+
# Executes a Cypher Query
|
85
|
+
# Check the neo4j
|
86
|
+
# Returns an enumerable of hash values.
|
87
|
+
#
|
88
|
+
# @example Using the Cypher DSL
|
89
|
+
# q = Neo4j.query{ match node(3) <=> node(:x); ret :x}
|
90
|
+
# q.first[:n] #=> the @node
|
91
|
+
# q.columns.first => :n
|
92
|
+
#
|
93
|
+
# @example Using the Cypher DSL and one parameter (n=Neo4j.ref_node)
|
94
|
+
# q = Neo4j.query(Neo4j.ref_node){|n| n <=> node(:x); :x}
|
95
|
+
# q.first[:n] #=> the @node
|
96
|
+
# q.columns.first => :n
|
97
|
+
#
|
98
|
+
# @example With an string
|
99
|
+
# q = Neo4j._query("START n=node(42) RETURN n")
|
100
|
+
# q.first(:n) #=> the @node
|
101
|
+
# q.columns.first => :n
|
102
|
+
#
|
103
|
+
# @see Cypher
|
104
|
+
# @see {http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html The Cypher Query Language Documentation}
|
105
|
+
# @note Returns a read-once only forward iterable.
|
106
|
+
# @param params parameter for the query_dsl block
|
107
|
+
# @return [Neo4j::Core::Cypher::ResultWrapper] a forward read once only Enumerable, containing hash values.
|
108
|
+
def query(*params, &query_dsl)
|
109
|
+
q = Cypher.new(*params, &query_dsl).to_s
|
110
|
+
_query(q)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Performs a cypher query with given string
|
114
|
+
# @param [String] q the cypher query as a String
|
115
|
+
# @return (see #query)
|
116
|
+
def _query(q)
|
117
|
+
engine = Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(db)
|
118
|
+
result = engine.execute(q)
|
119
|
+
Neo4j::Core::Cypher::ResultWrapper.new(result)
|
120
|
+
end
|
121
|
+
|
122
|
+
|
123
|
+
# Returns the logger used by neo4j.
|
124
|
+
# If not specified (with Neo4j.logger=) it will use the standard Ruby logger.
|
125
|
+
# You can change standard logger threshold by configuration :logger_level.
|
126
|
+
#
|
127
|
+
# You can also specify which logger class should take care of logging with the
|
128
|
+
# :logger configuration.
|
129
|
+
#
|
130
|
+
# @example
|
131
|
+
#
|
132
|
+
# Neo4j::Config[:logger] = Logger.new(STDOUT)
|
133
|
+
# Neo4j::Config[:logger_level] = Logger::ERROR
|
134
|
+
#
|
135
|
+
# @see #logger=
|
136
|
+
def logger
|
137
|
+
@logger ||= Neo4j::Config[:logger] || default_logger
|
138
|
+
end
|
139
|
+
|
140
|
+
# Sets which logger should be used.
|
141
|
+
# If this this is not called then the standard Ruby logger will be used.
|
142
|
+
# @see #logger
|
143
|
+
def logger=(logger)
|
144
|
+
@logger = logger
|
145
|
+
end
|
146
|
+
|
147
|
+
# @return the default Ruby logger
|
148
|
+
def default_logger #:nodoc:
|
149
|
+
require 'logger'
|
150
|
+
logger = Logger.new(STDOUT)
|
151
|
+
logger.sev_threshold = Neo4j::Config[:logger_level] || Logger::INFO
|
152
|
+
logger
|
153
|
+
end
|
154
|
+
|
155
|
+
|
156
|
+
# Returns an unstarted db instance
|
157
|
+
#
|
158
|
+
# This is typically used for configuring the database, which must sometimes
|
159
|
+
# be done before the database is started
|
160
|
+
# if the database was already started an exception will be raised
|
161
|
+
# @return [Neo4j::Database] an not started database
|
162
|
+
def unstarted_db
|
163
|
+
@db ||= Neo4j::Core::Database.new
|
164
|
+
raise "database was already started" if @db.running?
|
165
|
+
@db
|
166
|
+
end
|
167
|
+
|
168
|
+
# @return [true,false] if the database is running
|
169
|
+
def running?
|
170
|
+
!!(@db && @db.running?)
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
# Stops this database
|
175
|
+
# There are Ruby hooks that will do this automatically for you.
|
176
|
+
def shutdown(this_db = @db)
|
177
|
+
this_db.shutdown if this_db
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
# @return the default reference node, which is a "starting point" in the node space.
|
182
|
+
def default_ref_node(this_db = self.started_db)
|
183
|
+
this_db.graph.reference_node
|
184
|
+
end
|
185
|
+
|
186
|
+
# Usually, a client attaches relationships to this node that leads into various parts of the node space.
|
187
|
+
# ®return the reference node, which is a "starting point" in the node space.
|
188
|
+
# @note In case the ref_node has been assigned via the threadlocal_ref_node method,
|
189
|
+
# then that node will be returned instead.
|
190
|
+
# @see the design guide at http://wiki.neo4j.org/content/Design_Guide
|
191
|
+
def ref_node(this_db = self.started_db)
|
192
|
+
return Thread.current[:local_ref_node] if Thread.current[:local_ref_node]
|
193
|
+
default_ref_node(this_db)
|
194
|
+
end
|
195
|
+
|
196
|
+
# Changes the reference node on a threadlocal basis.
|
197
|
+
# This can be used to achieve multitenancy. All new entities will be attached to the new ref_node,
|
198
|
+
# which effectively partitions the graph, and hence scopes traversals.
|
199
|
+
def threadlocal_ref_node=(reference_node)
|
200
|
+
Thread.current[:local_ref_node] = reference_node.nil? ? nil : reference_node._java_node
|
201
|
+
end
|
202
|
+
|
203
|
+
# Returns a Management JMX Bean.
|
204
|
+
#
|
205
|
+
# Notice that this information is also provided by the jconsole Java tool, check http://wiki.neo4j.org/content/Monitoring_and_Deployment
|
206
|
+
# and http://docs.neo4j.org/chunked/milestone/operations-monitoring.html
|
207
|
+
#
|
208
|
+
# By default it returns the Primitivies JMX Bean that can be used to find number of nodes in use.
|
209
|
+
#
|
210
|
+
# @example Example Neo4j Primititives
|
211
|
+
#
|
212
|
+
# Neo4j.management.get_number_of_node_ids_in_use
|
213
|
+
# Neo4j.management.getNumberOfPropertyIdsInUse
|
214
|
+
# Neo4j.management.getNumberOfRelationshipIdsInUse
|
215
|
+
# Neo4j.management.get_number_of_relationship_type_ids_in_use
|
216
|
+
#
|
217
|
+
# @example Example Neo4j HA Cluster Info
|
218
|
+
#
|
219
|
+
# Neo4j.management(org.neo4j.management.HighAvailability).isMaster
|
220
|
+
#
|
221
|
+
# @param jmx_clazz the JMX class http://api.neo4j.org/current/org/neo4j/management/package-summary.html
|
222
|
+
# @param this_db default currently runnig instance or a newly started neo4j db instance
|
223
|
+
# @see for the jmx_clazz p
|
224
|
+
def management(jmx_clazz = org.neo4j.jmx.Primitives, this_db = self.started_db)
|
225
|
+
this_db.management(jmx_clazz)
|
226
|
+
end
|
227
|
+
|
228
|
+
# @return [Enumerable] all nodes in the database
|
229
|
+
def all_nodes(this_db = self.started_db)
|
230
|
+
Enumerable::Enumerator.new(this_db, :each_node)
|
231
|
+
end
|
232
|
+
|
233
|
+
# @return [Enumerable] all nodes in the database but not wrapped in ruby classes.
|
234
|
+
def _all_nodes(this_db = self.started_db)
|
235
|
+
Enumerator.new(this_db, :_each_node)
|
236
|
+
end
|
237
|
+
|
238
|
+
# @return [Neo4j::Core::EventHandler] the event handler
|
239
|
+
def event_handler(this_db = db)
|
240
|
+
this_db.event_handler
|
241
|
+
end
|
242
|
+
|
243
|
+
end
|
244
|
+
end
|