Class: Neo4j::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/session.rb

Defined Under Namespace

Classes: CypherError, InitializationError

Constant Summary collapse

@@all_sessions =
{}
@@factories =
{}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

._listenersObject



180
181
182
183
# File 'lib/neo4j/session.rb', line 180

def _listeners
  @@listeners ||= []
  @@listeners
end

._notify_listeners(event, data) ⇒ Object



186
187
188
# File 'lib/neo4j/session.rb', line 186

def _notify_listeners(event, data)
  _listeners.shift.call(event, data) until _listeners.empty?
end

.add_listener(&listener) ⇒ Object



175
176
177
# File 'lib/neo4j/session.rb', line 175

def add_listener(&listener)
  _listeners << listener
end

.clear_listenersObject



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

def clear_listeners
  @@listeners = []
end

.create_session(db_type, endpoint_url, params = {}) ⇒ Object



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

def create_session(db_type, endpoint_url, params = {})
  unless @@factories[db_type]
    fail InitializationError, "Can't connect to database '#{db_type}', available #{@@factories.keys.join(',')}"
  end
  @@factories[db_type].call(endpoint_url, params)
end

.currentNeo4j::Session

Returns the current session.

Returns:



123
124
125
# File 'lib/neo4j/session.rb', line 123

def current
  Thread.current[:neo4j_curr_session]
end

.current!Object

Returns the current session or raise an exception if no session is available



128
129
130
131
# File 'lib/neo4j/session.rb', line 128

def current!
  fail 'No session, please create a session first with Neo4j::Session.open(:server_db) or :embedded_db' unless current
  current
end

.inspectObject



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

def inspect
  "Neo4j::Session available: #{@@factories && @@factories.keys}"
end

.named(name) ⇒ Object

Returns a session with given name or else raise an exception



139
140
141
# File 'lib/neo4j/session.rb', line 139

def named(name)
  @@all_sessions[name] || fail("No session named #{name}.")
end

.on_next_session_availableObject

Registers a callback which will be called immediately if session is already available, or called when it later becomes available.



151
152
153
154
155
156
157
# File 'lib/neo4j/session.rb', line 151

def on_next_session_available
  return yield(Neo4j::Session.current) if Neo4j::Session.current

  add_listener do |event, data|
    yield data if event == :session_available
  end
end

.open(db_type = :server_db, endpoint_url = nil, params = {}) ⇒ Object

Creates a new session to Neo4j. This will be the default session to be used unless there is already a session created (see #current and #set_current)

Examples:

A Neo4j Server session

Neo4j::Session.open(:server_db, 'http://localhost:7474', {basic_auth: {username: 'foo', password: 'bar'}})

Using a user defined Faraday HTTP connection

connection = Faraday.new do |b|
  # faraday config
end
Neo4j::Session.open(:server_db, 'http://localhost:7474', connection: connection)

A embedded Neo4j session

Neo4j::Session.open(:embedded_db, 'path/to/db')

Parameters:

  • db_type (defaults to: :server_db)

    the type of database, e.g. :embedded_db, or :server_db

  • endpoint_url (String) (defaults to: nil)

    The path to the server, either a URL or path to embedded DB

  • params (Hash) (defaults to: {})

    Additional configuration options

See Also:

  • Neo4j::Server::CypherSession#open for :server_db params


99
100
101
102
103
104
105
# File 'lib/neo4j/session.rb', line 99

def open(db_type = :server_db, endpoint_url = nil, params = {})
  validate_session_num!(db_type)
  name = params[:name]
  default = params[:default]
  [:name, :default].each { |k| params.delete(k) }
  register(create_session(db_type, endpoint_url, params), name, default)
end

.query(*args) ⇒ Object

See Also:



134
135
136
# File 'lib/neo4j/session.rb', line 134

def query(*args)
  current!.query(*args)
end

.register(session, name = nil, default = nil) ⇒ Object



191
192
193
194
195
196
197
198
199
# File 'lib/neo4j/session.rb', line 191

def register(session, name = nil, default = nil)
  if default == true
    set_current(session)
  elsif default.nil?
    set_current(session) unless current
  end
  @@all_sessions[name] = session if name
  session
end

.register_db(db, &session_factory) ⇒ Object



211
212
213
214
# File 'lib/neo4j/session.rb', line 211

def register_db(db, &session_factory)
  puts "replace factory for #{db}" if @@factories[db]
  @@factories[db] = session_factory
end

.set_current(session) ⇒ Object

Sets the session to be used as default

Parameters:



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

def set_current(session)
  Thread.current[:neo4j_curr_session] = session
end

.unregister(session) ⇒ Object



202
203
204
# File 'lib/neo4j/session.rb', line 202

def unregister(session)
  set_current(nil) if current == session
end

.user_agent_stringObject



159
160
161
162
163
164
165
166
167
168
# File 'lib/neo4j/session.rb', line 159

def user_agent_string
  gem, version = if defined?(::Neo4j::ActiveNode)
                   ['neo4j', ::Neo4j::VERSION]
                 else
                   ['neo4j-core', ::Neo4j::Core::VERSION]
                 end


  "#{gem}-gem/#{version} (https://github.com/neo4jrb/#{gem})"
end

.validate_session_num!(db_type) ⇒ Object



108
109
110
111
# File 'lib/neo4j/session.rb', line 108

def validate_session_num!(db_type)
  return unless current && db_type == :embedded_db
  fail InitializationError, 'Multiple sessions are not supported by Neo4j Embedded.'
end

Instance Method Details

#_query(*params) ⇒ Object

This method is abstract.

Same as #query but does not accept an DSL and returns the raw result from the database. Notice, it might return different values depending on which database is used, embedded or server.



75
76
77
# File 'lib/neo4j/session.rb', line 75

def _query(*params)
  fail 'not implemented'
end

#auto_commit?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/neo4j/session.rb', line 34

def auto_commit?
  true # TODO
end

#begin_txObject

This method is abstract.


39
40
41
# File 'lib/neo4j/session.rb', line 39

def begin_tx
  fail 'not impl.'
end

#closeObject

This method is abstract.


7
8
9
# File 'lib/neo4j/session.rb', line 7

def close
  self.class.unregister(self)
end

#db_type:embedded_db | :server_db

Returns:

  • (:embedded_db | :server_db)


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

def db_type
  fail 'not impl.'
end

#query(options = {}) ⇒ Neo4j::Core::Query, Enumerable

Performs a cypher query. See Core::Query for more details, but basic usage looks like:

Examples:

Using cypher DSL

Neo4j::Session.query.match("(c:person)-[:friends]->(p:person)").where(c: {name: 'andreas'}).pluck(:p).first[:name]

Show the generated Cypher

Neo4j::Session.query..match("(c:person)-[:friends]->(p:person)").where(c: {name: 'andreas'}).return(:p).to_cypher

Use Cypher string instead of the cypher DSL

Neo4j::Session.query("MATCH (c:person)-[:friends]->(p:person) WHERE c.name = \"andreas\" RETURN p").first[:p][:name]

Returns:

  • (Neo4j::Core::Query, Enumerable)

    return a Query object for DSL or a Enumerable if using raw cypher strings

See Also:



68
69
70
# File 'lib/neo4j/session.rb', line 68

def query(options = {})
  fail 'not implemented, abstract'
end

#runningObject

This method is abstract.

Only for embedded database



25
26
27
# File 'lib/neo4j/session.rb', line 25

def running
  fail 'not impl.'
end

#shutdownObject

This method is abstract.

Only for embedded database



19
20
21
# File 'lib/neo4j/session.rb', line 19

def shutdown
  fail 'not impl.'
end

#startObject

This method is abstract.

Only for embedded database



13
14
15
# File 'lib/neo4j/session.rb', line 13

def start
  fail 'not impl.'
end