blob: 3c8d6633ee36f5aa76962177d50d65bf7623935c [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2011, Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.storage.memory;
19
20import net.floodlightcontroller.core.module.FloodlightModuleContext;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080021import net.floodlightcontroller.storage.nosql.NoSqlStorageSource;
22import net.floodlightcontroller.storage.SynchronousExecutorService;
23import net.floodlightcontroller.storage.IStorageSourceService;
24import net.floodlightcontroller.core.module.IFloodlightService;
25
26import java.util.ArrayList;
27import java.util.Collection;
28import java.util.HashMap;
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32
33import net.floodlightcontroller.storage.StorageException;
34
35public class MemoryStorageSource extends NoSqlStorageSource {
36
37 private Map<String, MemoryTable> tableMap = new HashMap<String,MemoryTable>();
mininet73e7fb72013-12-03 14:25:53 -080038
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080039
40 synchronized private MemoryTable getTable(String tableName, boolean create) {
41 MemoryTable table = tableMap.get(tableName);
42 if (table == null) {
43 if (!create)
44 throw new StorageException("Table " + tableName + " does not exist");
45 table = new MemoryTable(tableName);
46 tableMap.put(tableName, table);
47 }
48 return table;
49 }
50
51 @Override
52 protected Collection<Map<String,Object>> getAllRows(String tableName, String[] columnNameList) {
53 MemoryTable table = getTable(tableName, false);
54 return table.getAllRows();
55 }
56
57 @Override
58 protected Map<String,Object> getRow(String tableName, String[] columnNameList, Object rowKey) {
59 MemoryTable table = getTable(tableName, false);
60 return table.getRow(rowKey);
61 }
62
63 @Override
64 protected List<Map<String,Object>> executeEqualityQuery(String tableName,
65 String[] columnNameList, String predicateColumnName, Comparable<?> value) {
66 MemoryTable table = getTable(tableName, false);
67 List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
68 synchronized (table) {
69 Collection<Map<String,Object>> allRows = table.getAllRows();
70 for (Map<String,Object> row : allRows) {
71 Object v = row.get(predicateColumnName);
72 if (value.equals(v)) {
73 result.add(row);
74 }
75 }
76 }
77 return result;
78 }
79
80 @SuppressWarnings({ "unchecked", "rawtypes" })
81 @Override
82 protected List<Map<String,Object>> executeRangeQuery(String tableName,
83 String[] columnNameList, String predicateColumnName,
84 Comparable<?> startValue, boolean startInclusive, Comparable<?> endValue, boolean endInclusive) {
85 MemoryTable table = getTable(tableName, false);
86 List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
87 synchronized (table) {
88 Collection<Map<String,Object>> allRows = table.getAllRows();
89 for (Map<String,Object> row : allRows) {
90 Comparable value = (Comparable) row.get(predicateColumnName);
91 if (value != null) {
92 int compareResult = value.compareTo(startValue);
93 if ((compareResult > 0) || (startInclusive && (compareResult >= 0))) {
94 compareResult = value.compareTo(endValue);
95 if ((compareResult < 0) || (startInclusive && (compareResult <= 0))) {
96 result.add(row);
97 }
98 }
99 }
100 }
101 }
102 return result;
103 }
104
105 @Override
106 protected void insertRows(String tableName, List<Map<String,Object>> insertRowList) {
107 MemoryTable table = getTable(tableName, false);
108 String primaryKeyName = getTablePrimaryKeyName(tableName);
109 synchronized (table) {
110 for (Map<String,Object> row : insertRowList) {
111 Object primaryKey = row.get(primaryKeyName);
112 if (primaryKey == null) {
113 if (primaryKeyName.equals(DEFAULT_PRIMARY_KEY_NAME)) {
114 row = new HashMap<String,Object>(row);
115 primaryKey = table.getNextId();
116 row.put(primaryKeyName, primaryKey);
117 }
118 }
119 table.insertRow(primaryKey, row);
120 }
121 }
122 }
123
124 @Override
125 protected void updateRows(String tableName, Set<Object> rowKeys, Map<String,Object> updateRowList) {
126 MemoryTable table = getTable(tableName, false);
127 synchronized (table) {
128 for (Object rowKey : rowKeys) {
129 Map<String,Object> row = table.getRow(rowKey);
130 if (row == null)
131 row = table.newRow(rowKey);
132 for (Map.Entry<String,Object> entry: updateRowList.entrySet()) {
133 row.put(entry.getKey(), entry.getValue());
134 }
135 }
136 }
137 }
138
139 @Override
140 protected void updateRowsImpl(String tableName, List<Map<String,Object>> updateRowList) {
141 MemoryTable table = getTable(tableName, false);
142 String primaryKeyName = getTablePrimaryKeyName(tableName);
143 synchronized (table) {
144 for (Map<String,Object> updateRow : updateRowList) {
145 Object rowKey = updateRow.get(primaryKeyName);
146 if (rowKey == null)
147 throw new StorageException("Primary key not found.");
148 Map<String,Object> row = table.getRow(rowKey);
149 if (row == null)
150 row = table.newRow(rowKey);
151 for (Map.Entry<String,Object> entry: updateRow.entrySet()) {
152 row.put(entry.getKey(), entry.getValue());
153 }
154 }
155 }
156 }
157
158 @Override
159 protected void deleteRowsImpl(String tableName, Set<Object> rowKeys) {
160 MemoryTable table = getTable(tableName, false);
161 synchronized (table) {
162 for (Object rowKey : rowKeys) {
163 table.deleteRow(rowKey);
164 }
165 }
166 }
167
168 @Override
169 public void createTable(String tableName, Set<String> indexedColumnNames) {
170 super.createTable(tableName, indexedColumnNames);
171 getTable(tableName, true);
172 }
173
mininet73e7fb72013-12-03 14:25:53 -0800174
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -0800175 // IFloodlightModule methods
176
177 @Override
178 public void startUp(FloodlightModuleContext context) {
179 super.startUp(context);
180 executorService = new SynchronousExecutorService();
181 }
182
183 @Override
184 public Map<Class<? extends IFloodlightService>,
185 IFloodlightService> getServiceImpls() {
186 Map<Class<? extends IFloodlightService>,
187 IFloodlightService> m =
188 new HashMap<Class<? extends IFloodlightService>,
189 IFloodlightService>();
190 m.put(IStorageSourceService.class, this);
191 return m;
192 }
193}