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 java.util.ArrayList; |
| 8 | import java.util.HashMap; |
| 9 | import java.util.List; |
| 10 | import java.util.Map; |
| 11 | |
| 12 | /** |
| 13 | * |
| 14 | * @author nickkaranatsios |
| 15 | */ |
| 16 | public class GraphDBManager { |
yoshi | b7d84c7 | 2013-12-07 17:14:23 -0800 | [diff] [blame] | 17 | private static ThreadLocal<HashMap<String, DBConnection>> connections = new ThreadLocal<HashMap<String, DBConnection>>(); |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 18 | private static DBOperation operation = null; |
yoshi | b7d84c7 | 2013-12-07 17:14:23 -0800 | [diff] [blame] | 19 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 20 | static Map<String, DBConnection> getConnectionMap() { |
| 21 | if (connections.get() == null) { |
| 22 | connections.set(new HashMap<String, DBConnection>()); |
| 23 | } |
| 24 | return connections.get(); |
| 25 | } |
yoshi | b7d84c7 | 2013-12-07 17:14:23 -0800 | [diff] [blame] | 26 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 27 | public static DBOperation getDBOperation(final String dbStore, final String dbConfigFile) { |
yoshi | d38cd31 | 2013-12-02 19:54:44 -0800 | [diff] [blame] | 28 | if (dbStore.equals("ramcloud")) { |
| 29 | operation = new RamCloudDBOperation(); |
| 30 | } else if (dbStore.equals("titan")) { |
| 31 | operation = new TitanDBOperation(); |
| 32 | } |
| 33 | if (operation != null) { |
| 34 | operation.conn = GraphDBManager.getConnection(dbStore, dbConfigFile); |
yoshi | 2db7ff4 | 2013-11-25 19:30:25 -0800 | [diff] [blame] | 35 | } |
yoshi | 95ff5c2 | 2013-11-25 17:00:18 -0800 | [diff] [blame] | 36 | return operation; |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | public static DBConnection getConnection(final String dbStore, final String dbConfigFile) { |
yoshi | b7d84c7 | 2013-12-07 17:14:23 -0800 | [diff] [blame] | 40 | DBConnection conn = getConnectionMap().get(dbStore); |
| 41 | if (conn == null) { |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 42 | if (dbStore.equals("ramcloud")) { |
| 43 | conn = new RamCloudDBConnection(dbConfigFile); |
| 44 | } else if (dbStore.equals("titan")) { |
| 45 | conn = new TitanDBConnection(dbConfigFile); |
| 46 | } |
| 47 | |
yoshi | b7d84c7 | 2013-12-07 17:14:23 -0800 | [diff] [blame] | 48 | GraphDBManager.getConnectionMap().put(dbStore, conn); |
| 49 | } else { |
| 50 | GraphDBManager.getConnectionMap().get(dbStore); |
| 51 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 52 | return conn; |
| 53 | } |
yoshi | b7d84c7 | 2013-12-07 17:14:23 -0800 | [diff] [blame] | 54 | |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 55 | static List<DBConnection> getConnections() { |
| 56 | return new ArrayList<DBConnection>(getConnectionMap().values()); |
| 57 | } |
yoshi | 0451f28 | 2013-11-22 15:48:55 -0800 | [diff] [blame] | 58 | } |