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?, create_session, current, current!, inspect, named, on_session_available, open, open_named, query, register, register_db, #running, set_current, unregister, user_agent_string

Constructor Details

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

Returns a new instance of EmbeddedSession.



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

def initialize(db_location, 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.



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

def db_location
  @db_location
end

#graph_dbObject (readonly)

Returns the value of attribute graph_db.



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

def graph_db
  @graph_db
end

#properties_fileObject (readonly)

Returns the value of attribute properties_file.



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

def properties_file
  @properties_file
end

Instance Method Details

#_load_node(neo_id) ⇒ Object

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



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

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



103
104
105
106
107
108
# File 'lib/neo4j-embedded/embedded_session.rb', line 103

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(q, params = {}) ⇒ 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



133
134
135
136
137
138
# File 'lib/neo4j-embedded/embedded_session.rb', line 133

def _query(q, params = {})
  @engine ||= Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
  @engine.execute(q, Neo4j::Core::HashWithIndifferentAccess.new(params))
rescue StandardError => e
  raise Neo4j::Session::CypherError.new(e.message, e.class, 'cypher error')
end

#_query_or_fail(q) ⇒ Object



144
145
146
147
# File 'lib/neo4j-embedded/embedded_session.rb', line 144

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

#begin_txObject



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

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



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

def close
  super
  shutdown
end

#create_label(name) ⇒ Object



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

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

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/neo4j-embedded/embedded_session.rb', line 153

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



27
28
29
# File 'lib/neo4j-embedded/embedded_session.rb', line 27

def db_type
  :embedded_db
end

#factory_classObject



52
53
54
# File 'lib/neo4j-embedded/embedded_session.rb', line 52

def factory_class
  Java::OrgNeo4jTest::ImpermanentGraphDatabase
end

#find_all_nodes(label) ⇒ Object



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

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

#find_nodes(label, key, value) ⇒ Object



125
126
127
# File 'lib/neo4j-embedded/embedded_session.rb', line 125

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

#inspectObject



31
32
33
# File 'lib/neo4j-embedded/embedded_session.rb', line 31

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

#load_node(neo_id) ⇒ Object



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

def load_node(neo_id)
  _load_node(neo_id)
end

#load_relationship(neo_id) ⇒ Object



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

def load_relationship(neo_id)
  _load_relationship(neo_id)
end

#query(*args) ⇒ Object



110
111
112
113
114
115
116
117
118
# File 'lib/neo4j-embedded/embedded_session.rb', line 110

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



140
141
142
# File 'lib/neo4j-embedded/embedded_session.rb', line 140

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

#running?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/neo4j-embedded/embedded_session.rb', line 76

def running?
  !!graph_db
end

#search_result_to_enumerable(result) ⇒ Object



149
150
151
# File 'lib/neo4j-embedded/embedded_session.rb', line 149

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

#shutdownObject



71
72
73
74
# File 'lib/neo4j-embedded/embedded_session.rb', line 71

def shutdown
  graph_db && graph_db.shutdown
  @graph_db = nil
end

#startObject



41
42
43
44
45
46
47
48
49
50
# File 'lib/neo4j-embedded/embedded_session.rb', line 41

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
  @graph_db = db_service.newGraphDatabase
  Neo4j::Session._notify_listeners(:session_available, self)
  @engine = Java::OrgNeo4jCypherJavacompat::ExecutionEngine.new(@graph_db)
end

#versionObject



35
36
37
38
39
# File 'lib/neo4j-embedded/embedded_session.rb', line 35

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