blob: 3eec68fee63616c18371f052ea9ec46d04ccf3f8 [file] [log] [blame]
yoshi0451f282013-11-22 15:48:55 -08001/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package net.onrc.onos.graph;
6
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
yoshi0451f282013-11-22 15:48:55 -080012public class GraphDBManager {
yoshib7d84c72013-12-07 17:14:23 -080013 private static ThreadLocal<HashMap<String, DBConnection>> connections = new ThreadLocal<HashMap<String, DBConnection>>();
yoshi2db7ff42013-11-25 19:30:25 -080014 private static DBOperation operation = null;
Yoshi Muroi5804ce92014-02-08 03:58:04 -080015 private final static String DB_CONFIG_FILE = "conf/ramcloud.conf";
16
yoshi0451f282013-11-22 15:48:55 -080017 static Map<String, DBConnection> getConnectionMap() {
18 if (connections.get() == null) {
19 connections.set(new HashMap<String, DBConnection>());
20 }
21 return connections.get();
22 }
yoshib7d84c72013-12-07 17:14:23 -080023
Yoshi Muroi5804ce92014-02-08 03:58:04 -080024 public static DBOperation getDBOperation() {
25 return getDBOperation("ramcloud");
26 }
27
28 public static DBOperation getDBOperation(final String dbStore) {
yoshid38cd312013-12-02 19:54:44 -080029 if (dbStore.equals("ramcloud")) {
30 operation = new RamCloudDBOperation();
31 } else if (dbStore.equals("titan")) {
32 operation = new TitanDBOperation();
33 }
34 if (operation != null) {
Yoshi Muroi5804ce92014-02-08 03:58:04 -080035 operation.conn = GraphDBManager.getConnection(dbStore, DB_CONFIG_FILE);
yoshi2db7ff42013-11-25 19:30:25 -080036 }
yoshi95ff5c22013-11-25 17:00:18 -080037 return operation;
yoshi0451f282013-11-22 15:48:55 -080038 }
39
40 public static DBConnection getConnection(final String dbStore, final String dbConfigFile) {
yoshib7d84c72013-12-07 17:14:23 -080041 DBConnection conn = getConnectionMap().get(dbStore);
42 if (conn == null) {
yoshi0451f282013-11-22 15:48:55 -080043 if (dbStore.equals("ramcloud")) {
44 conn = new RamCloudDBConnection(dbConfigFile);
45 } else if (dbStore.equals("titan")) {
46 conn = new TitanDBConnection(dbConfigFile);
47 }
48
yoshib7d84c72013-12-07 17:14:23 -080049 GraphDBManager.getConnectionMap().put(dbStore, conn);
50 } else {
51 GraphDBManager.getConnectionMap().get(dbStore);
52 }
yoshi0451f282013-11-22 15:48:55 -080053 return conn;
54 }
yoshib7d84c72013-12-07 17:14:23 -080055
yoshi0451f282013-11-22 15:48:55 -080056 static List<DBConnection> getConnections() {
57 return new ArrayList<DBConnection>(getConnectionMap().values());
58 }
yoshi0451f282013-11-22 15:48:55 -080059}