2 * Copyright (C) 2013 Red Hat, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Madhu Venugopal, Brent Salisbury
10 package org.opendaylight.ovsdb.plugin;
13 import java.util.concurrent.ConcurrentMap;
15 import org.apache.commons.collections.MapUtils;
16 import org.opendaylight.ovsdb.lib.notation.Row;
18 import com.google.common.collect.Maps;
21 ConcurrentMap<String, ConcurrentMap<String, TableDB>> dbCache = Maps.newConcurrentMap();
23 public ConcurrentMap<String, ConcurrentMap<String,Row>> getDatabase(String dbName) {
24 ConcurrentMap<String, TableDB> tdbMap = dbCache.get(dbName);
25 if (tdbMap == null) return null;
26 ConcurrentMap<String, ConcurrentMap<String,Row>> retMap = Maps.newConcurrentMap();
27 for (String tableName : tdbMap.keySet()) {
28 TableDB tdb = tdbMap.get(tableName);
29 retMap.put(tableName, tdb.getTableCache(tableName));
34 public ConcurrentMap<String, Row> getTableCache(String dbName, String tableName) {
35 ConcurrentMap<String, ConcurrentMap<String,Row>> tdbMap = getDatabase(dbName);
36 if (tdbMap == null) return null;
37 return tdbMap.get(tableName);
40 private void setDBCache(String dbName, ConcurrentMap<String, TableDB> table) {
41 dbCache.put(dbName, table);
44 public Row getRow (String dbName, String tableName, String uuid) {
45 ConcurrentMap<String, ConcurrentMap<String, Row>> db = getDatabase(dbName);
46 if (db == null) return null;
47 ConcurrentMap<String, Row> tdb = db.get(tableName);
48 if (tdb == null) return null;
52 public void updateRow(String dbName, String tableName, String uuid, Row row) {
53 ConcurrentMap<String, TableDB> db = dbCache.get(dbName);
55 db = Maps.newConcurrentMap();
56 setDBCache(dbName, db);
58 TableDB tdb = db.get(tableName);
61 db.put(tableName, tdb);
63 tdb.updateRow(tableName, uuid, row);
66 public void removeRow(String dbName, String tableName, String uuid) {
67 ConcurrentMap<String, TableDB> db = dbCache.get(dbName);
68 if (db == null) return;
69 TableDB tdb = db.get(tableName);
70 if (tdb == null) return;
71 tdb.removeRow(tableName, uuid);
74 public void printTableCache() {
75 MapUtils.debugPrint(System.out, null, dbCache);
78 public class TableDB {
79 ConcurrentMap<String, ConcurrentMap<String, Row>> cache = Maps.newConcurrentMap();
81 public ConcurrentMap<String, ConcurrentMap<String, Row>> getTableCache() {
85 public ConcurrentMap<String, Row> getTableCache(String tableName) {
86 return cache.get(tableName);
89 private void setTableCache(String tableName, ConcurrentMap<String, Row> tableCache) {
90 cache.put(tableName, tableCache);
93 public Row getRow (String tableName, String uuid) {
94 Map<String, Row> tableCache = getTableCache(tableName);
95 if (tableCache != null) {
96 return tableCache.get(uuid);
101 public void updateRow(String tableName, String uuid, Row row) {
102 ConcurrentMap<String, Row> tableCache = getTableCache(tableName);
103 if (tableCache == null) {
104 tableCache = Maps.newConcurrentMap();
105 setTableCache(tableName, tableCache);
107 tableCache.put(uuid, row);
110 public void removeRow(String tableName, String uuid) {
111 Map<String, Row> tableCache = getTableCache(tableName);
112 if (tableCache != null) {
113 tableCache.remove(uuid);
117 public void printTableCache() {
118 MapUtils.debugPrint(System.out, null, cache);