blob: 8a69eca7f1920bd5995c451d90468314c8b45d20 [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;
21import net.floodlightcontroller.perfmon.IPktInProcessingTimeService;
22import net.floodlightcontroller.storage.nosql.NoSqlStorageSource;
23import net.floodlightcontroller.storage.SynchronousExecutorService;
24import net.floodlightcontroller.storage.IStorageSourceService;
25import net.floodlightcontroller.core.module.IFloodlightService;
26
27import java.util.ArrayList;
28import java.util.Collection;
29import java.util.HashMap;
30import java.util.List;
31import java.util.Map;
32import java.util.Set;
33
34import net.floodlightcontroller.storage.StorageException;
35
36public class MemoryStorageSource extends NoSqlStorageSource {
37
38 private Map<String, MemoryTable> tableMap = new HashMap<String,MemoryTable>();
39 IPktInProcessingTimeService pktinProcessingTime;
40
41 synchronized private MemoryTable getTable(String tableName, boolean create) {
42 MemoryTable table = tableMap.get(tableName);
43 if (table == null) {
44 if (!create)
45 throw new StorageException("Table " + tableName + " does not exist");
46 table = new MemoryTable(tableName);
47 tableMap.put(tableName, table);
48 }
49 return table;
50 }
51
52 @Override
53 protected Collection<Map<String,Object>> getAllRows(String tableName, String[] columnNameList) {
54 MemoryTable table = getTable(tableName, false);
55 return table.getAllRows();
56 }
57
58 @Override
59 protected Map<String,Object> getRow(String tableName, String[] columnNameList, Object rowKey) {
60 MemoryTable table = getTable(tableName, false);
61 return table.getRow(rowKey);
62 }
63
64 @Override
65 protected List<Map<String,Object>> executeEqualityQuery(String tableName,
66 String[] columnNameList, String predicateColumnName, Comparable<?> value) {
67 MemoryTable table = getTable(tableName, false);
68 List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
69 synchronized (table) {
70 Collection<Map<String,Object>> allRows = table.getAllRows();
71 for (Map<String,Object> row : allRows) {
72 Object v = row.get(predicateColumnName);
73 if (value.equals(v)) {
74 result.add(row);
75 }
76 }
77 }
78 return result;
79 }
80
81 @SuppressWarnings({ "unchecked", "rawtypes" })
82 @Override
83 protected List<Map<String,Object>> executeRangeQuery(String tableName,
84 String[] columnNameList, String predicateColumnName,
85 Comparable<?> startValue, boolean startInclusive, Comparable<?> endValue, boolean endInclusive) {
86 MemoryTable table = getTable(tableName, false);
87 List<Map<String,Object>> result = new ArrayList<Map<String,Object>>();
88 synchronized (table) {
89 Collection<Map<String,Object>> allRows = table.getAllRows();
90 for (Map<String,Object> row : allRows) {
91 Comparable value = (Comparable) row.get(predicateColumnName);
92 if (value != null) {
93 int compareResult = value.compareTo(startValue);
94 if ((compareResult > 0) || (startInclusive && (compareResult >= 0))) {
95 compareResult = value.compareTo(endValue);
96 if ((compareResult < 0) || (startInclusive && (compareResult <= 0))) {
97 result.add(row);
98 }
99 }
100 }
101 }
102 }
103 return result;
104 }
105
106 @Override
107 protected void insertRows(String tableName, List<Map<String,Object>> insertRowList) {
108 MemoryTable table = getTable(tableName, false);
109 String primaryKeyName = getTablePrimaryKeyName(tableName);
110 synchronized (table) {
111 for (Map<String,Object> row : insertRowList) {
112 Object primaryKey = row.get(primaryKeyName);
113 if (primaryKey == null) {
114 if (primaryKeyName.equals(DEFAULT_PRIMARY_KEY_NAME)) {
115 row = new HashMap<String,Object>(row);
116 primaryKey = table.getNextId();
117 row.put(primaryKeyName, primaryKey);
118 }
119 }
120 table.insertRow(primaryKey, row);
121 }
122 }
123 }
124
125 @Override
126 protected void updateRows(String tableName, Set<Object> rowKeys, Map<String,Object> updateRowList) {
127 MemoryTable table = getTable(tableName, false);
128 synchronized (table) {
129 for (Object rowKey : rowKeys) {
130 Map<String,Object> row = table.getRow(rowKey);
131 if (row == null)
132 row = table.newRow(rowKey);
133 for (Map.Entry<String,Object> entry: updateRowList.entrySet()) {
134 row.put(entry.getKey(), entry.getValue());
135 }
136 }
137 }
138 }
139
140 @Override
141 protected void updateRowsImpl(String tableName, List<Map<String,Object>> updateRowList) {
142 MemoryTable table = getTable(tableName, false);
143 String primaryKeyName = getTablePrimaryKeyName(tableName);
144 synchronized (table) {
145 for (Map<String,Object> updateRow : updateRowList) {
146 Object rowKey = updateRow.get(primaryKeyName);
147 if (rowKey == null)
148 throw new StorageException("Primary key not found.");
149 Map<String,Object> row = table.getRow(rowKey);
150 if (row == null)
151 row = table.newRow(rowKey);
152 for (Map.Entry<String,Object> entry: updateRow.entrySet()) {
153 row.put(entry.getKey(), entry.getValue());
154 }
155 }
156 }
157 }
158
159 @Override
160 protected void deleteRowsImpl(String tableName, Set<Object> rowKeys) {
161 MemoryTable table = getTable(tableName, false);
162 synchronized (table) {
163 for (Object rowKey : rowKeys) {
164 table.deleteRow(rowKey);
165 }
166 }
167 }
168
169 @Override
170 public void createTable(String tableName, Set<String> indexedColumnNames) {
171 super.createTable(tableName, indexedColumnNames);
172 getTable(tableName, true);
173 }
174
175 public void setPktinProcessingTime(
176 IPktInProcessingTimeService pktinProcessingTime) {
177 this.pktinProcessingTime = pktinProcessingTime;
178 }
179
180 // IFloodlightModule methods
181
182 @Override
183 public void startUp(FloodlightModuleContext context) {
184 super.startUp(context);
185 executorService = new SynchronousExecutorService();
186 }
187
188 @Override
189 public Map<Class<? extends IFloodlightService>,
190 IFloodlightService> getServiceImpls() {
191 Map<Class<? extends IFloodlightService>,
192 IFloodlightService> m =
193 new HashMap<Class<? extends IFloodlightService>,
194 IFloodlightService>();
195 m.put(IStorageSourceService.class, this);
196 return m;
197 }
198}