module Neo4j # 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. # class Database attr_reader :graph, :lucene, :event_handler def initialize() @event_handler = EventHandler.new end def start #:nodoc: return if running? @running = true begin if self.class.locked? start_readonly_graph_db elsif Neo4j::Config['ha.db'] start_ha_graph_db Neo4j.migrate! else start_local_graph_db Neo4j.migrate! end rescue @running = false raise end at_exit { shutdown } end def start_readonly_graph_db #:nodoc: Neo4j.logger.info "Starting Neo4j in readonly mode since the #{Neo4j.config.storage_path} is locked" Neo4j.load_local_jars @graph = org.neo4j.kernel.EmbeddedReadOnlyGraphDatabase.new(Config[:storage_path], Config.to_java_map) end def start_local_graph_db #:nodoc: Neo4j.logger.info "Starting local Neo4j using db #{Neo4j.config.storage_path}" Neo4j.load_local_jars @graph = org.neo4j.kernel.EmbeddedGraphDatabase.new(Neo4j.config.storage_path, Config.to_java_map) @graph.register_transaction_event_handler(@event_handler) @lucene = @graph.index @event_handler.neo4j_started(self) end def start_ha_graph_db Neo4j.logger.info "starting Neo4j in HA mode, machine id: #{Neo4j.config['ha.machine_id']} at #{Neo4j.config['ha.server']} db #{Neo4j.config.storage_path}" Neo4j.load_ha_jars # those jars are only needed for the HighlyAvailableGraphDatabase @graph = org.neo4j.kernel.HighlyAvailableGraphDatabase.new(Neo4j.config.storage_path, Neo4j.config.to_java_map) @graph.register_transaction_event_handler(@event_handler) @lucene = @graph.index #org.neo4j.index.impl.lucene.LuceneIndexProvider.new @event_handler.neo4j_started(self) end def running? #:nodoc: @running end # 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). def read_only? @graph.isReadOnly end # check if the database is locked. A neo4j database is locked when there is running. 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 def shutdown #:nodoc: 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 end end def begin_tx #:nodoc: @graph.begin_tx end def each_node #:nodoc: iter = @graph.all_nodes.iterator while (iter.hasNext) yield iter.next.wrapper end end def _each_node #:nodoc: iter = @graph.all_nodes.iterator while (iter.hasNext) yield iter.next end end end end