Class: Neo4j::Core::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j-core/database.rb

Overview

Wraps both Java Neo4j GraphDatabaseService and Lucene. You can access the raw java neo4j and lucene db’s with the graph and lucene properties.

This class is also responsible for checking if there is already a running neo4j database. If one tries to start an already started database then a read only instance to neo4j will be used. Many of the methods here are delegated from the Neo4j module

Constant Summary collapse

SEMAPHORE =
Mutex.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDatabase

Returns a new instance of Database.



33
34
35
# File 'lib/neo4j-core/database.rb', line 33

def initialize()
  @event_handler = EventHandler.new
end

Instance Attribute Details

#event_handlerNeo4j::EventHandler (readonly)

Returns the event handler listining to commit events.

Returns:

  • (Neo4j::EventHandler)

    the event handler listining to commit events



26
27
28
# File 'lib/neo4j-core/database.rb', line 26

def event_handler
  @event_handler
end

#graphJava::OrgNeo4jGraphdb::GraphDatabaseService (readonly)

The Java graph database

Returns:

  • (Java::OrgNeo4jGraphdb::GraphDatabaseService)

See Also:



18
19
20
# File 'lib/neo4j-core/database.rb', line 18

def graph
  @graph
end

#luceneJava::OrgNeo4jGraphdbIndex::IndexManager (readonly) Also known as: index

The lucene index manager

Returns:

  • (Java::OrgNeo4jGraphdbIndex::IndexManager)

See Also:



23
24
25
# File 'lib/neo4j-core/database.rb', line 23

def lucene
  @lucene
end

#storage_pathString (readonly)

Returns The location of the database.

Returns:

  • (String)

    The location of the database



29
30
31
# File 'lib/neo4j-core/database.rb', line 29

def storage_path
  @storage_path
end

Class Method Details

.default_embedded_dbObject



37
38
39
# File 'lib/neo4j-core/database.rb', line 37

def self.default_embedded_db
  @default_embedded_db || Java::OrgNeo4jKernel::EmbeddedGraphDatabase
end

.default_embedded_db=(db) ⇒ Object



41
42
43
# File 'lib/neo4j-core/database.rb', line 41

def self.default_embedded_db=(db)
  @default_embedded_db = db
end

.ha_enabled?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/neo4j-core/database.rb', line 45

def self.ha_enabled?
  !!Neo4j::Config[:enable_ha]
end

.locked?Boolean

check if the database is locked. A neo4j database is locked when the database is running.

Returns:

  • (Boolean)


91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/neo4j-core/database.rb', line 91

def self.locked?
  lock_file = File.join(Neo4j.config.storage_path, 'neostore')
  return false unless File.exist?(lock_file)
  rfile = java.io.RandomAccessFile.new(lock_file, 'rw')
  begin
    lock = rfile.getChannel.tryLock
    lock.release if lock
    return lock == nil # we got the lock, so that means it is not locked.
  rescue Exception => e
    return false
  end
end

Instance Method Details

#_each_nodeObject

See Also:



143
144
145
146
147
148
# File 'lib/neo4j-core/database.rb', line 143

def _each_node
  iter = @graph.all_nodes.iterator
  while (iter.hasNext)
    yield iter.next
  end
end

#begin_txObject

private method, used from Neo4j::Transaction.new



130
131
132
# File 'lib/neo4j-core/database.rb', line 130

def begin_tx
  @graph.begin_tx
end

#each_nodeObject

See Also:



135
136
137
138
139
140
# File 'lib/neo4j-core/database.rb', line 135

def each_node
  iter = @graph.all_nodes.iterator
  while (iter.hasNext)
    yield iter.next.wrapper
  end
end

#management(jmx_clazz) ⇒ Object

See Also:



124
125
126
127
# File 'lib/neo4j-core/database.rb', line 124

def management(jmx_clazz)
  @neo4j_manager ||= Java::OrgNeo4jManagement::Neo4jManager.new(@graph.get_management_bean(org.neo4j.jmx.Kernel.java_class))
  @neo4j_manager.getBean(jmx_clazz.java_class)
end

#read_only?Boolean

Returns true if the neo4j db was started in read only mode. This can occur if the database was locked (it was already one instance running).

Returns:

  • (Boolean)

See Also:

  • Neo4j#read_only?


86
87
88
# File 'lib/neo4j-core/database.rb', line 86

def read_only?
  @graph.java_class == Java::OrgNeo4jKernel::EmbeddedReadOnlyGraphDatabase
end

#running?Boolean

true if the database has started

Returns:

  • (Boolean)


79
80
81
# File 'lib/neo4j-core/database.rb', line 79

def running?
  @running
end

#shutdownObject

Internal method, see Neo4j#shutdown



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/neo4j-core/database.rb', line 105

def shutdown
  if @running
    @graph.unregister_transaction_event_handler(@event_handler) unless read_only?
    @event_handler.neo4j_shutdown(self)
    @graph.shutdown
    @graph = nil
    @lucene = nil
    @running = false
    @neo4j_manager = nil
    if self.class.ha_enabled?
      Neo4j.logger.info "Neo4j (HA mode) has been shutdown, machine id: #{Neo4j.config['ha.server_id']} at #{Neo4j.config['ha.server']} db #{@storage_path}"
    else
      Neo4j.logger.info "Neo4j has been shutdown using storage_path: #{@storage_path}"
    end
  end
end

#startObject

Private start method, use Neo4j.start instead

See Also:

  • Neo4j#start


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/neo4j-core/database.rb', line 52

def start
  SEMAPHORE.synchronize do
    return if running?
    @storage_path = Config.storage_path
    Java::JavaLang::System.setProperty("neo4j.ext.udc.source", "neo4rb")

    begin
      if self.class.locked?
        start_readonly_graph_db
      elsif self.class.ha_enabled?
        start_ha_graph_db
        Neo4j.migrate! if Neo4j.respond_to?(:migrate!)
      else
        start_local_graph_db
        Neo4j.migrate! if Neo4j.respond_to?(:migrate!)
      end
    rescue
      @running = false
      raise
    end

    at_exit { shutdown }
  end
end

#start_external_db(external_graph_db) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/neo4j-core/database.rb', line 151

def start_external_db(external_graph_db)
  begin
    @graph = external_graph_db
    @graph.register_transaction_event_handler(@event_handler)
    @lucene = @graph.index
    @running = true
    @event_handler.neo4j_started(self)
    Neo4j.logger.info("Started with external db")
  rescue
    @running = false
    raise
  end
end