yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 1 | /* |
| 2 | * To change this template, choose Tools | Templates |
| 3 | * and open the template in the editor. |
| 4 | */ |
| 5 | package net.onrc.onos.graph; |
| 6 | |
| 7 | import com.tinkerpop.blueprints.impls.ramcloud.RamCloudGraph; |
| 8 | import com.tinkerpop.frames.FramedGraph; |
| 9 | import java.io.File; |
| 10 | import org.apache.commons.configuration.Configuration; |
| 11 | import org.apache.commons.configuration.ConfigurationException; |
| 12 | import org.apache.commons.configuration.PropertiesConfiguration; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 13 | import org.slf4j.Logger; |
| 14 | import org.slf4j.LoggerFactory; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 15 | |
| 16 | /** |
| 17 | * |
| 18 | * @author nickkaranatsios |
| 19 | */ |
| 20 | public class RamCloudDBConnection extends DBConnection { |
| 21 | private RamCloudGraph graph; |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 22 | private static Logger log = LoggerFactory.getLogger(RamCloudDBConnection.class); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 23 | |
| 24 | public RamCloudDBConnection(final String dbConfigFile) { |
| 25 | System.out.println("dbconfigfile is + "+ dbConfigFile); |
| 26 | final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
yoshi | c935828 | 2013-11-26 16:45:06 -0800 | [diff] [blame] | 27 | System.out.println("coordinatorURL "+ coordinatorURL); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 28 | graph = new RamCloudGraph(coordinatorURL); |
| 29 | } |
| 30 | |
| 31 | @Override |
| 32 | public FramedGraph getFramedGraph() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 33 | if (isValid()) { |
| 34 | FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph); |
| 35 | return fg; |
| 36 | } else { |
| 37 | log.error("new FramedGraph failed"); |
| 38 | return null; |
| 39 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 44 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 49 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 54 | try { |
| 55 | graph.commit(); |
| 56 | } catch (Exception e) { |
| 57 | log.error("{}", e.toString()); |
| 58 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 63 | try { |
| 64 | graph.rollback(); |
| 65 | } catch (Exception e) { |
| 66 | log.error("{}", e.toString()); |
| 67 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | @Override |
| 71 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame^] | 72 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private static final Configuration getConfiguration(final File dirOrFile) { |
| 76 | if (dirOrFile == null) { |
| 77 | throw new IllegalArgumentException("Need to specify a configuration file or storage directory"); |
| 78 | } |
| 79 | |
| 80 | if (!dirOrFile.isFile()) { |
| 81 | throw new IllegalArgumentException("Location of configuration must be a file"); |
| 82 | } |
| 83 | |
| 84 | try { |
| 85 | return new PropertiesConfiguration(dirOrFile); |
| 86 | } catch (ConfigurationException e) { |
| 87 | throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private String open(final Configuration configuration) { |
| 92 | final String coordinatorURL = configuration.getString("ramcloud.coordinator", null); |
| 93 | if (coordinatorURL == null) { |
| 94 | throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting"); |
| 95 | } |
| 96 | return coordinatorURL; |
| 97 | } |
| 98 | } |