blob: ec7eb7f319dc8510fd173de1ef1c865795de5be4 [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
12/**
13 *
14 * @author nickkaranatsios
15 */
16public class GraphDBManager {
yoshib7d84c72013-12-07 17:14:23 -080017 private static ThreadLocal<HashMap<String, DBConnection>> connections = new ThreadLocal<HashMap<String, DBConnection>>();
yoshi2db7ff42013-11-25 19:30:25 -080018 private static DBOperation operation = null;
yoshib7d84c72013-12-07 17:14:23 -080019
yoshi0451f282013-11-22 15:48:55 -080020 static Map<String, DBConnection> getConnectionMap() {
21 if (connections.get() == null) {
22 connections.set(new HashMap<String, DBConnection>());
23 }
24 return connections.get();
25 }
yoshib7d84c72013-12-07 17:14:23 -080026
yoshi0451f282013-11-22 15:48:55 -080027 public static DBOperation getDBOperation(final String dbStore, final String dbConfigFile) {
yoshid38cd312013-12-02 19:54:44 -080028 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);
yoshi2db7ff42013-11-25 19:30:25 -080035 }
yoshi95ff5c22013-11-25 17:00:18 -080036 return operation;
yoshi0451f282013-11-22 15:48:55 -080037 }
38
39 public static DBConnection getConnection(final String dbStore, final String dbConfigFile) {
yoshib7d84c72013-12-07 17:14:23 -080040 DBConnection conn = getConnectionMap().get(dbStore);
41 if (conn == null) {
yoshi0451f282013-11-22 15:48:55 -080042 if (dbStore.equals("ramcloud")) {
43 conn = new RamCloudDBConnection(dbConfigFile);
44 } else if (dbStore.equals("titan")) {
45 conn = new TitanDBConnection(dbConfigFile);
46 }
47
yoshib7d84c72013-12-07 17:14:23 -080048 GraphDBManager.getConnectionMap().put(dbStore, conn);
49 } else {
50 GraphDBManager.getConnectionMap().get(dbStore);
51 }
yoshi0451f282013-11-22 15:48:55 -080052 return conn;
53 }
yoshib7d84c72013-12-07 17:14:23 -080054
yoshi0451f282013-11-22 15:48:55 -080055 static List<DBConnection> getConnections() {
56 return new ArrayList<DBConnection>(getConnectionMap().values());
57 }
yoshi0451f282013-11-22 15:48:55 -080058}