Pankaj Berde | da80957 | 2013-02-22 15:31:20 -0800 | [diff] [blame] | 1 | package net.onrc.onos.util; |
| 2 | |
| 3 | import java.util.Set; |
| 4 | |
| 5 | import com.thinkaurelius.titan.core.TitanFactory; |
| 6 | import com.thinkaurelius.titan.core.TitanGraph; |
| 7 | import com.tinkerpop.blueprints.Vertex; |
| 8 | import com.tinkerpop.blueprints.TransactionalGraph.Conclusion; |
| 9 | import com.tinkerpop.frames.FramedGraph; |
| 10 | |
| 11 | public class GraphDBConnection { |
| 12 | public enum Transaction { |
| 13 | COMMIT, |
| 14 | ROLLBACK |
| 15 | } |
| 16 | private static GraphDBConnection singleton = new GraphDBConnection( ); |
| 17 | private static TitanGraph graph; |
| 18 | private static GraphDBUtils utils; |
| 19 | |
| 20 | /* A private Constructor prevents any other |
| 21 | * class from instantiating. |
| 22 | */ |
| 23 | private GraphDBConnection(){ } |
| 24 | |
| 25 | /* Static 'instance' method */ |
| 26 | public static GraphDBConnection getInstance(String conf) { |
| 27 | if (graph == null||graph.isOpen()) { |
| 28 | graph = TitanFactory.open(conf); |
| 29 | // FIXME: Creation on Indexes should be done only once |
| 30 | Set<String> s = graph.getIndexedKeys(Vertex.class); |
| 31 | if (!s.contains("dpid")) { |
| 32 | graph.createKeyIndex("dpid", Vertex.class); |
| 33 | } |
| 34 | if (!s.contains("type")) { |
| 35 | graph.createKeyIndex("type", Vertex.class); |
| 36 | } |
| 37 | if (!s.contains("dl_address")) { |
| 38 | graph.createKeyIndex("dl_address", Vertex.class); |
| 39 | } |
| 40 | } |
| 41 | graph.stopTransaction(Conclusion.SUCCESS); |
| 42 | if (utils == null) { |
| 43 | utils = new GraphDBUtils(); |
| 44 | } |
| 45 | return singleton; |
| 46 | } |
| 47 | |
| 48 | public IDBUtils utils() { |
| 49 | return utils; |
| 50 | } |
| 51 | |
| 52 | protected FramedGraph<TitanGraph> getFramedGraph() { |
| 53 | |
| 54 | if (isValid()) { |
| 55 | FramedGraph<TitanGraph> fg = new FramedGraph<TitanGraph>(graph); |
| 56 | return fg; |
| 57 | } else { |
| 58 | return null; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | public Boolean isValid() { |
| 63 | |
| 64 | return (graph != null||graph.isOpen()); |
| 65 | } |
| 66 | |
| 67 | public void startTx() { |
| 68 | |
| 69 | } |
| 70 | |
| 71 | public void endTx(Transaction tx) { |
| 72 | switch (tx) { |
| 73 | case COMMIT: |
| 74 | graph.stopTransaction(Conclusion.SUCCESS); |
| 75 | case ROLLBACK: |
| 76 | graph.stopTransaction(Conclusion.FAILURE); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | public void close() { |
| 81 | |
| 82 | } |
| 83 | |
| 84 | } |