View Javadoc

    1   /**
    2    * Copyright (c) 2002-2011 "Neo Technology,"
    3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
    4    *
    5    * This file is part of Neo4j.
    6    *
    7    * Neo4j is free software: you can redistribute it and/or modify
    8    * it under the terms of the GNU General Public License as published by
    9    * the Free Software Foundation, either version 3 of the License, or
    10   * (at your option) any later version.
    11   *
    12   * This program is distributed in the hope that it will be useful,
    13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15   * GNU General Public License for more details.
    16   *
    17   * You should have received a copy of the GNU General Public License
    18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    19   */
    20  package org.neo4j.server.database;
    21
    22  import java.util.HashMap;
    23  import java.util.Map;
    24
    25  import org.neo4j.ext.udc.UdcProperties;
    26  import org.neo4j.graphdb.Node;
    27  import org.neo4j.graphdb.Relationship;
    28  import org.neo4j.graphdb.index.IndexManager;
    29  import org.neo4j.graphdb.index.RelationshipIndex;
    30  import org.neo4j.kernel.AbstractGraphDatabase;
    31  import org.neo4j.server.logging.Logger;
    32  import org.rrd4j.core.RrdDb;
    33
    34  public class Database
    35  {
    36      public static Logger log = Logger.getLogger( Database.class );
    37
    38      public AbstractGraphDatabase graph;
    39
    40      private final String databaseStoreDirectory;
    41      private RrdDb rrdDb;
    42
    43      public Database( AbstractGraphDatabase db )
    44      {
    45          this.databaseStoreDirectory = db.getStoreDir();
    46          graph = db;
    47      }
    48
    49      public Database( GraphDatabaseFactory factory, String databaseStoreDirectory )
    50      {
    51          this( createDatabase( factory, databaseStoreDirectory, null ) );
    52          log.warn( "No database tuning properties set in the property file, using defaults. Please specify the performance properties file with org.neo4j.server.db.tuning.properties in the server properties file [%s].", System.getProperty( "org.neo4j.server.properties" ) );
    53      }
    54
    55      public Database( GraphDatabaseFactory factory, String databaseStoreDirectory,
    56              Map<String, String> databaseTuningProperties )
    57      {
    58          this( createDatabase( factory, databaseStoreDirectory, databaseTuningProperties ) );
    59      }
    60
    61      private static AbstractGraphDatabase createDatabase( GraphDatabaseFactory factory,
    62              String databaseStoreDirectory, Map<String, String> databaseProperties )
    63      {
    64          log.info( "Creating database at " + databaseStoreDirectory );
    65
    66          if ( databaseProperties == null )
    67          {
    68              databaseProperties = new HashMap<String, String>();
    69          }
    70
    71          databaseProperties.put( org.neo4j.kernel.Config.ENABLE_REMOTE_SHELL, "true" );
    72          databaseProperties.put( org.neo4j.kernel.Config.KEEP_LOGICAL_LOGS, "true" );
    73          databaseProperties.put( UdcProperties.UDC_SOURCE_KEY, "server" );
    74
    75          return factory.createDatabase( databaseStoreDirectory, databaseProperties );
    76      }
    77
    78
    79      public void startup()
    80      {
    81          if ( graph != null )
    82          {
    83              log.info( "Successfully started database" );
    84          }
    85          else
    86          {
    87              log.error( "Failed to start database. GraphDatabaseService has not been properly initialized." );
    88          }
    89      }
    90
    91      public void shutdown()
    92      {
    93          try
    94          {
    95              if ( rrdDb != null )
    96              {
    97                  rrdDb.close();
    98              }
    99              if ( graph != null )
    100             {
    101                 graph.shutdown();
    102             }
    103             log.info( "Successfully shutdown database" );
    104         } catch ( Exception e )
    105         {
    106             log.error( "Database did not shut down cleanly. Reason [%s]", e.getMessage() );
    107             throw new RuntimeException( e );
    108         }
    109     }
    110
    111     public String getLocation()
    112     {
    113         return databaseStoreDirectory;
    114     }
    115
    116     public org.neo4j.graphdb.index.Index<Relationship> getRelationshipIndex( String name )
    117     {
    118         RelationshipIndex index = graph.index().forRelationships( name );
    119         if ( index == null )
    120         {
    121             throw new RuntimeException( "No index for [" + name + "]" );
    122         }
    123         return index;
    124     }
    125
    126     public org.neo4j.graphdb.index.Index<Node> getNodeIndex( String name )
    127     {
    128         org.neo4j.graphdb.index.Index<Node> index = graph.index().forNodes( name );
    129         if ( index == null )
    130         {
    131             throw new RuntimeException( "No index for [" + name + "]" );
    132         }
    133         return index;
    134     }
    135
    136     public RrdDb rrdDb()
    137     {
    138         //assert rrdDb != null : "RrdDb is null";
    139
    140         return rrdDb;
    141     }
    142
    143     public void setRrdDb( RrdDb rrdDb )
    144     {
    145         this.rrdDb = rrdDb;
    146     }
    147
    148
    149
    150     public IndexManager getIndexManager()
    151     {
    152         return graph.index();
    153     }
    154 }