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 | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 23 | |
yoshi | 0cb7b0a | 2013-12-04 15:23:08 -0800 | [diff] [blame^] | 24 | private static final ThreadLocal<RamCloudGraph> RamCloudThreadLocal = new ThreadLocal<RamCloudGraph>(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 25 | |
| 26 | public RamCloudDBConnection(final String dbConfigFile) { |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 27 | //final String coordinatorURL = open(getConfiguration(new File(dbConfigFile))); |
| 28 | //System.out.println("coordinatorURL "+ coordinatorURL); |
yoshi | d3025a9 | 2013-11-28 12:32:12 -0800 | [diff] [blame] | 29 | //graph = new RamCloudGraph(coordinatorURL); |
yoshi | 0cb7b0a | 2013-12-04 15:23:08 -0800 | [diff] [blame^] | 30 | graph = RamCloudThreadLocal.get(); |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 31 | System.out.println("ThreadId = " + Thread.currentThread().getId() + "graph = " + graph); |
| 32 | if (graph == null) { |
yoshi | 6b605ea | 2013-12-04 15:19:44 -0800 | [diff] [blame] | 33 | graph = new RamCloudGraph("fast+udp:host=10.128.4.104,port=12246"); |
yoshi | 6184f95 | 2013-12-04 12:03:15 -0800 | [diff] [blame] | 34 | RamCloudThreadLocal.set(graph); |
| 35 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public FramedGraph getFramedGraph() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 40 | if (isValid()) { |
| 41 | FramedGraph<RamCloudGraph> fg = new FramedGraph<RamCloudGraph>(graph); |
| 42 | return fg; |
| 43 | } else { |
| 44 | log.error("new FramedGraph failed"); |
| 45 | return null; |
| 46 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public void addEventListener(LocalGraphChangedListener listener) { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 51 | //TO-DO |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public Boolean isValid() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 56 | return (graph != null); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public void commit() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 61 | try { |
| 62 | graph.commit(); |
| 63 | } catch (Exception e) { |
| 64 | log.error("{}", e.toString()); |
| 65 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void rollback() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 70 | try { |
| 71 | graph.rollback(); |
| 72 | } catch (Exception e) { |
| 73 | log.error("{}", e.toString()); |
| 74 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | @Override |
| 78 | public void close() { |
yoshi | e665e82 | 2013-11-26 19:51:16 -0800 | [diff] [blame] | 79 | commit(); |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | private static final Configuration getConfiguration(final File dirOrFile) { |
| 83 | if (dirOrFile == null) { |
| 84 | throw new IllegalArgumentException("Need to specify a configuration file or storage directory"); |
| 85 | } |
| 86 | |
| 87 | if (!dirOrFile.isFile()) { |
| 88 | throw new IllegalArgumentException("Location of configuration must be a file"); |
| 89 | } |
| 90 | |
| 91 | try { |
| 92 | return new PropertiesConfiguration(dirOrFile); |
| 93 | } catch (ConfigurationException e) { |
| 94 | throw new IllegalArgumentException("Could not load configuration at: " + dirOrFile, e); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | private String open(final Configuration configuration) { |
| 99 | final String coordinatorURL = configuration.getString("ramcloud.coordinator", null); |
| 100 | if (coordinatorURL == null) { |
| 101 | throw new RuntimeException("Configuration must contain a valid 'coordinatorURL' setting"); |
| 102 | } |
| 103 | return coordinatorURL; |
| 104 | } |
| 105 | } |