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