Class: Neo4j::Embedded::EmbeddedSession

Inherits:
Session
  • Object
show all
Extended by:
Forwardable, Core::TxMethods
Defined in:
lib/neo4j-embedded/embedded_session.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Core::TxMethods

tx_methods

Methods inherited from Session

_listeners, _notify_listeners, add_listener, #auto_commit?, clear_listeners, create_session, current, current!, inspect, named, on_next_session_available, open, query, register, register_db, #running, set_current, unregister, user_agent_string, validate_session_num!

Constructor Details

#initialize(db_location, config = {}) ⇒ EmbeddedSession

Returns a new instance of EmbeddedSession.



18
19
20
21
22
23
24
# File 'lib/neo4j-embedded/embedded_session.rb', line 18

def initialize(db_location, config = {})
  @config          = config
  @db_location     = db_location
  @auto_commit     = !!@config[:auto_commit]
  @properties_file = @config[:properties_file]
  Neo4j::Session.register(self)
end

Instance Attribute Details

#db_locationObject (readonly)

Returns the value of attribute db_location.



13
14
15
# File 'lib/neo4j-embedded/embedded_session.rb', line 13

def db_location
  @db_location
end

#graph_dbObject (readonly)

Returns the value of attribute graph_db.



13
14
15
# File 'lib/neo4j-embedded/embedded_session.rb', line 13

def graph_db
  @graph_db
end

#properties_fileObject (readonly)

Returns the value of attribute properties_file.



13
14
15
# File 'lib/neo4j-embedded/embedded_session.rb', line 13

def properties_file
  @properties_file
end

#properties_mapObject (readonly)

Returns the value of attribute properties_map.



13
14
15
# File 'lib/neo4j-embedded/embedded_session.rb', line 13

def properties_map
  @properties_map
end

Instance Method Details

#_load_node(neo_id) ⇒ Object

Same as load but does not return the node as a wrapped Ruby object.



108
109
110
111
112
113
# File 'lib/neo4j-embedded/embedded_session.rb', line 108

def _load_node(neo_id)
  return nil if neo_id.nil?
  @graph_db.get_node_by_id(neo_id.to_i)
rescue Java::OrgNeo4jGraphdb.NotFoundException
  nil
end

#_load_relationship(neo_id) ⇒ Object



120
121
122
123
124
125
# File 'lib/neo4j-embedded/embedded_session.rb', line 120

def _load_relationship(neo_id)
  return nil if neo_id.nil?
  @graph_db.get_relationship_by_id(neo_id.to_i)
rescue Java::OrgNeo4jGraphdb.NotFoundException
  nil
end

#_query(query, params = {}, options = {}) ⇒ Object

Performs a cypher query with given string. Remember that you should close the resource iterator.

Parameters:

  • q (String)

    the cypher query as a String



150
151
152
153
154
155
156
157
158
# File 'lib/neo4j-embedded/embedded_session.rb', line 150

def _query(query, params = {}, options = {})
  ActiveSupport::Notifications.instrument('neo4j.cypher_query', params: params, context: options[:context],
                                                                cypher: query, pretty_cypher: options[:pretty_cypher], params: params) do
    @engine ||= Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
    @engine.execute(query, indifferent_params(params))
  end
rescue StandardError => e
  raise Neo4j::Session::CypherError.new(e.message, e.class, 'cypher error')
end

#_query_or_fail(q) ⇒ Object



170
171
172
173
# File 'lib/neo4j-embedded/embedded_session.rb', line 170

def _query_or_fail(q)
  @engine ||= Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
  @engine.execute(q)
end

#begin_txObject



71
72
73
74
75
76
77
78
79
# File 'lib/neo4j-embedded/embedded_session.rb', line 71

def begin_tx
  if Neo4j::Transaction.current
    # Handle nested transaction "placebo transaction"
    Neo4j::Transaction.current.push_nested!
  else
    Neo4j::Embedded::EmbeddedTransaction.new(@graph_db.begin_tx)
  end
  Neo4j::Transaction.current
end

#closeObject



81
82
83
84
# File 'lib/neo4j-embedded/embedded_session.rb', line 81

def close
  super
  shutdown
end

#create_label(name) ⇒ Object



97
98
99
# File 'lib/neo4j-embedded/embedded_session.rb', line 97

def create_label(name)
  EmbeddedLabel.new(self, name)
end

#create_node(properties = nil, labels = []) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/neo4j-embedded/embedded_session.rb', line 179

def create_node(properties = nil, labels = [])
  if labels.empty?
    @graph_db.create_node
  else
    labels = EmbeddedLabel.as_java(labels)
    @graph_db.create_node(labels)
  end.tap do |java_node|
    properties.each_pair { |k, v| java_node[k] = v } if properties
  end
end

#db_typeObject



39
40
41
# File 'lib/neo4j-embedded/embedded_session.rb', line 39

def db_type
  :embedded_db
end

#factory_classObject



67
68
69
# File 'lib/neo4j-embedded/embedded_session.rb', line 67

def factory_class
  Java::OrgNeo4jTest::ImpermanentGraphDatabase
end

#find_all_nodes(label) ⇒ Object



138
139
140
# File 'lib/neo4j-embedded/embedded_session.rb', line 138

def find_all_nodes(label)
  EmbeddedLabel.new(self, label).find_nodes
end

#find_nodes(label, key, value) ⇒ Object



142
143
144
# File 'lib/neo4j-embedded/embedded_session.rb', line 142

def find_nodes(label, key, value)
  EmbeddedLabel.new(self, label).find_nodes(key, value)
end

#indifferent_params(params) ⇒ Object



160
161
162
163
164
# File 'lib/neo4j-embedded/embedded_session.rb', line 160

def indifferent_params(params)
  return {} unless params
  params.each { |k, v| params[k] = HashWithIndifferentAccess.new(params[k]) if v.is_a?(Hash) && !v.respond_to?(:nested_under_indifferent_access) }
  HashWithIndifferentAccess.new(params)
end

#inspectObject



43
44
45
# File 'lib/neo4j-embedded/embedded_session.rb', line 43

def inspect
  "#{self.class} db_location: '#{@db_location}', running: #{running?}"
end

#load_node(neo_id) ⇒ Object



101
102
103
# File 'lib/neo4j-embedded/embedded_session.rb', line 101

def load_node(neo_id)
  _load_node(neo_id)
end

#load_relationship(neo_id) ⇒ Object



115
116
117
# File 'lib/neo4j-embedded/embedded_session.rb', line 115

def load_relationship(neo_id)
  _load_relationship(neo_id)
end

#query(*args) ⇒ Object



127
128
129
130
131
132
133
134
135
# File 'lib/neo4j-embedded/embedded_session.rb', line 127

def query(*args)
  if [[String], [String, Hash]].include?(args.map(&:class))
    query, params = args[0, 2]
    Neo4j::Embedded::ResultWrapper.new(_query(query, params), query)
  else
    options = args[0] || {}
    Neo4j::Core::Query.new(options.merge(session: self))
  end
end

#query_default_return(as) ⇒ Object



166
167
168
# File 'lib/neo4j-embedded/embedded_session.rb', line 166

def query_default_return(as)
  " RETURN #{as}"
end

#running?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/neo4j-embedded/embedded_session.rb', line 93

def running?
  !!@graph_db
end

#search_result_to_enumerable(result) ⇒ Object



175
176
177
# File 'lib/neo4j-embedded/embedded_session.rb', line 175

def search_result_to_enumerable(result)
  result.map { |column| column['n'].wrapper }
end

#shutdownObject



86
87
88
89
90
91
# File 'lib/neo4j-embedded/embedded_session.rb', line 86

def shutdown
  @graph_db && @graph_db.shutdown

  Neo4j::Session.clear_listeners
  @graph_db = nil
end

#startObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/neo4j-embedded/embedded_session.rb', line 54

def start
  fail Error, 'Embedded Neo4j db is already running' if running?
  puts "Start embedded Neo4j db at #{db_location}"
  factory    = Java::OrgNeo4jGraphdbFactory::GraphDatabaseFactory.new
  db_service = factory.newEmbeddedDatabaseBuilder(db_location)
  db_service.loadPropertiesFromFile(properties_file) if properties_file
  db_service.setConfig(properties_map)               if properties_map

  @graph_db = db_service.newGraphDatabase
  Neo4j::Session._notify_listeners(:session_available, self)
  @engine = Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
end

#versionObject



47
48
49
50
51
52
# File 'lib/neo4j-embedded/embedded_session.rb', line 47

def version
  # Wow
  # Yeah, agreed...
  version_string = @graph_db.to_java(Java::OrgNeo4jKernel::GraphDatabaseAPI).getDependencyResolver.resolveDependency(Java::OrgNeo4jKernel::KernelData.java_class).version.to_s
  version_string.split(' ')[-1]
end