Class: Neo4j::Core::CypherSession::Adaptors::Embedded

Inherits:
Base
  • Object
show all
Defined in:
lib/neo4j/core/cypher_session/adaptors/embedded.rb

Constant Summary collapse

CONSTRAINT_TYPES =
{
  'UNIQUENESS' => :uniqueness
}

Constants inherited from Base

Base::EMPTY, Base::NEWLINE_W_SPACES, Base::USER_AGENT_STRING

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #wrap_level

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#close, #logger, #queries, #query, #setup_queries!, #supports_metadata?, #transaction

Constructor Details

#initialize(path, options = {}) ⇒ Embedded

Returns a new instance of Embedded



12
13
14
15
16
17
18
19
20
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 12

def initialize(path, options = {})
  fail 'JRuby is required for embedded mode' if RUBY_PLATFORM != 'java'
  # TODO: Will this cause an error if a new path is specified?
  fail ArgumentError, "Invalid path: #{path}" if File.file?(path)
  FileUtils.mkdir_p(path)

  @path = path
  @options = options
end

Instance Attribute Details

#graph_dbObject (readonly)

Returns the value of attribute graph_db



10
11
12
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 10

def graph_db
  @graph_db
end

#pathObject (readonly)

Returns the value of attribute path



10
11
12
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 10

def path
  @path
end

Class Method Details

.transaction_classObject



79
80
81
82
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 79

def self.transaction_class
  require 'neo4j/core/cypher_session/transactions/embedded'
  Neo4j::Core::CypherSession::Transactions::Embedded
end

Instance Method Details

#connectObject



22
23
24
25
26
27
28
29
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 22

def connect
  factory    = Java::OrgNeo4jGraphdbFactory::GraphDatabaseFactory.new
  db_service = factory.newEmbeddedDatabaseBuilder(@path)
  db_service.loadPropertiesFromFile(@options[:properties_file]) if @options[:properties_file]
  db_service.setConfig(@options[:properties_map])               if @options[:properties_map]

  @graph_db = db_service.newGraphDatabase
end

#connected?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 84

def connected?
  !!@graph_db
end

#constraints(session) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 67

def constraints(session)
  Transaction.run(session) do
    all_labels(session).flat_map do |label|
      graph_db.schema.get_constraints(label).map do |definition|
        {label: label.to_s.to_sym,
         properties: definition.property_keys.map(&:to_sym),
         type: CONSTRAINT_TYPES[definition.get_constraint_type.to_s]}
      end
    end
  end
end

#default_subscribeObject



94
95
96
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 94

def default_subscribe
  subscribe_to_transaction
end

#indexes(session, _label = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 53

def indexes(session, _label = nil)
  Transaction.run(session) do
    graph_db = session.adaptor.graph_db

    graph_db.schema.get_indexes.map do |definition|
      {properties: definition.property_keys.map(&:to_sym),
       label: definition.label.to_s.to_sym}
    end
  end
end

#query_set(transaction, queries, options = {}) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 31

def query_set(transaction, queries, options = {})
  # I think that this is the best way to do a batch in embedded...
  # Should probably do within a transaction in case of errors...
  setup_queries!(queries, transaction, options)

  self.class.instrument_transaction do
    Responses::Embedded.new(execution_results(queries), wrap_level: options[:wrap_level] || @options[:wrap_level]).results
  end
rescue Java::OrgNeo4jCypher::CypherExecutionException, Java::OrgNeo4jCypher::SyntaxException => e
  raise CypherError.new_from(e.status.to_s, e.message) # , e.stack_track.to_a
end

#version(_session) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/neo4j/core/cypher_session/adaptors/embedded.rb', line 43

def version(_session)
  if defined?(::Neo4j::Community)
    ::Neo4j::Community::NEO_VERSION
  elsif defined?(::Neo4j::Enterprise)
    ::Neo4j::Enterprise::NEO_VERSION
  else
    fail 'Could not determine embedded version!'
  end
end