Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 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 | |
| 18 | package net.floodlightcontroller.storage.memory; |
| 19 | |
| 20 | import net.floodlightcontroller.core.module.FloodlightModuleContext; |
| 21 | import net.floodlightcontroller.perfmon.IPktInProcessingTimeService; |
| 22 | import net.floodlightcontroller.storage.nosql.NoSqlStorageSource; |
| 23 | import net.floodlightcontroller.storage.SynchronousExecutorService; |
| 24 | import net.floodlightcontroller.storage.IStorageSourceService; |
| 25 | import net.floodlightcontroller.core.module.IFloodlightService; |
| 26 | |
| 27 | import java.util.ArrayList; |
| 28 | import java.util.Collection; |
| 29 | import java.util.HashMap; |
| 30 | import java.util.List; |
| 31 | import java.util.Map; |
| 32 | import java.util.Set; |
| 33 | |
| 34 | import net.floodlightcontroller.storage.StorageException; |
| 35 | |
| 36 | public 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 | } |