Class: Neo4j::Embedded::EmbeddedSession

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

Direct Known Subclasses

EmbeddedHaSession, EmbeddedImpermanentSession

Defined Under Namespace

Classes: Error

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Core::TxMethods

tx_methods

Methods inherited from Session

#auto_commit?, current, current!, inspect, named, on_session_available, open, open_named, query, #running, set_current, user_agent_string

Constructor Details

- (EmbeddedSession) initialize(db_location, config = {})

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

- (Object) db_location (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

- (Object) graph_db (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

- (Object) properties_file (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

- (Object) _load_node(neo_id)

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

- (Object) _load_relationship(neo_id)



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

- (Object) _query(q, params = {})

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

- (Object) _query_or_fail(q)



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

- (Object) begin_tx



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

- (Object) close



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

def close
  super
  shutdown
end

- (Object) create_label(name)



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

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

- (Object) create_node(properties = nil, labels = [])



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

- (Object) db_type



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

def db_type
  :embedded_db
end

- (Object) factory_class



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

def factory_class
  Java::OrgNeo4jTest::ImpermanentGraphDatabase
end

- (Object) find_all_nodes(label)



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

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

- (Object) find_nodes(label, key, value)



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

- (Object) inspect



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

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

- (Object) load_node(neo_id)



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

def load_node(neo_id)
  _load_node(neo_id)
end

- (Object) load_relationship(neo_id)



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

def load_relationship(neo_id)
  _load_relationship(neo_id)
end

- (Object) query(*args)



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

def query(*args)
  if [[String], [String, String]].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

- (Object) query_default_return(as)



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

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

- (Boolean) running?

Returns:

  • (Boolean)


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

def running?
  !!graph_db
end

- (Object) search_result_to_enumerable(result)



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

- (Object) shutdown



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

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

- (Object) start



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

- (Object) version



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