blob: b3a8c20df8e4bda01ae04b91ad081c6a36f0c0e7 [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.nosql;
19
20import java.text.ParseException;
21import java.text.SimpleDateFormat;
22import java.util.Date;
23import java.util.Iterator;
24import java.util.List;
25import java.util.ArrayList;
26import java.util.Map;
27import java.util.HashMap;
28import java.util.HashSet;
29import java.util.Set;
30import java.util.TimeZone;
31
32import net.floodlightcontroller.storage.IResultSet;
33import net.floodlightcontroller.storage.NullValueStorageException;
34import net.floodlightcontroller.storage.ResultSetIterator;
35import net.floodlightcontroller.storage.StorageException;
36import net.floodlightcontroller.storage.TypeMismatchStorageException;
37
38public class NoSqlResultSet implements IResultSet {
39
40 NoSqlStorageSource storageSource;
41 String tableName;
42 String primaryKeyName;
43 List<Map<String,Object>> rowList;
44 int currentIndex;
45 Map<String,Object> currentRowUpdate;
46 List<Map<String,Object>> rowUpdateList;
47 Set<Object> rowDeleteSet;
48 Iterator<IResultSet> resultSetIterator;
49
50 NoSqlResultSet(NoSqlStorageSource storageSource, String tableName, List<Map<String,Object>> rowList) {
51 this.storageSource = storageSource;
52 this.primaryKeyName = storageSource.getTablePrimaryKeyName(tableName);
53 this.tableName = tableName;
54 if (rowList == null)
55 rowList = new ArrayList<Map<String,Object>>();
56 this.rowList = rowList;
57 currentIndex = -1;
58 }
59
60 void addRow(Map<String,Object> row) {
61 rowList.add(row);
62 }
63
64 @Override
65 public Map<String,Object> getRow() {
66 if ((currentIndex < 0) || (currentIndex >= rowList.size())) {
67 throw new StorageException("No current row in result set.");
68 }
69
70 return rowList.get(currentIndex);
71 }
72
73 @Override
74 public boolean containsColumn(String columnName) {
75 return getObject(columnName) != null;
76 }
77
78 @Override
79 public void close() {
80 }
81
82 private void endCurrentRowUpdate() {
83 if (currentRowUpdate != null) {
84 if (rowUpdateList == null)
85 rowUpdateList = new ArrayList<Map<String,Object>>();
86 rowUpdateList.add(currentRowUpdate);
87 currentRowUpdate = null;
88 }
89 }
90
91 @Override
92 public boolean next() {
93 endCurrentRowUpdate();
94 currentIndex++;
95 return currentIndex < rowList.size();
96 }
97
98 @Override
99 public void save() {
100 endCurrentRowUpdate();
101
102 if (rowUpdateList != null) {
103 storageSource.updateRows(tableName, rowUpdateList);
104 rowUpdateList = null;
105 }
106
107 if (rowDeleteSet != null) {
108 storageSource.deleteRows(tableName, rowDeleteSet);
109 rowDeleteSet = null;
110 }
111 }
112
113 Object getObject(String columnName) {
114 Map<String,Object> row = rowList.get(currentIndex);
115 Object value = row.get(columnName);
116 return value;
117 }
118
119 @Override
120 public boolean getBoolean(String columnName) {
121 Boolean b = getBooleanObject(columnName);
122 if (b == null)
123 throw new NullValueStorageException(columnName);
124 return b.booleanValue();
125 }
126
127 @Override
128 public byte getByte(String columnName) {
129 Byte b = getByteObject(columnName);
130 if (b == null)
131 throw new NullValueStorageException(columnName);
132 return b.byteValue();
133 }
134
135 @Override
136 public byte[] getByteArray(String columnName) {
137 byte[] b = null;
138 Object obj = getObject(columnName);
139 if (obj != null) {
140 if (!(obj instanceof byte[]))
141 throw new StorageException("Invalid byte array value");
142 b = (byte[])obj;
143 }
144 return b;
145 }
146
147 @Override
148 public double getDouble(String columnName) {
149 Double d = getDoubleObject(columnName);
150 if (d == null)
151 throw new NullValueStorageException(columnName);
152 return d.doubleValue();
153 }
154
155 @Override
156 public float getFloat(String columnName) {
157 Float f = getFloatObject(columnName);
158 if (f == null)
159 throw new NullValueStorageException(columnName);
160 return f.floatValue();
161 }
162
163 @Override
164 public int getInt(String columnName) {
165 Integer i = getIntegerObject(columnName);
166 if (i == null)
167 throw new NullValueStorageException(columnName);
168 return i.intValue();
169 }
170
171 @Override
172 public long getLong(String columnName) {
173 Long l = getLongObject(columnName);
174 if (l == null)
175 throw new NullValueStorageException(columnName);
176 return l.longValue();
177 }
178
179 @Override
180 public short getShort(String columnName) {
181 Short s = getShortObject(columnName);
182 if (s == null)
183 throw new NullValueStorageException(columnName);
184 return s.shortValue();
185 }
186
187 @Override
188 public String getString(String columnName) {
189 Object obj = getObject(columnName);
190 if (obj == null)
191 return null;
192 return obj.toString();
193 }
194
195 @Override
196 public Date getDate(String column) {
197 Date d;
198 Object obj = getObject(column);
199 if (obj == null) {
200 d = null;
201 } else if (obj instanceof Date) {
202 d = (Date) obj;
203 } else {
204 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
205 dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
206 try {
207 d = dateFormat.parse(obj.toString());
208 }
209 catch (ParseException exc) {
210 throw new TypeMismatchStorageException(Date.class.getName(), obj.getClass().getName(), column);
211 }
212 }
213 return d;
214 }
215
216
217 @Override
218 public Short getShortObject(String columnName)
219 {
220 Short s;
221 Object obj = getObject(columnName);
222 if (obj instanceof Short) {
223 s = (Short)obj;
224 } else if (obj != null) {
225 try {
226 s = Short.parseShort(obj.toString());
227 }
228 catch (NumberFormatException exc) {
229 throw new TypeMismatchStorageException(Short.class.getName(), obj.getClass().getName(), columnName);
230 }
231 } else {
232 s = null;
233 }
234 return s;
235 }
236
237 @Override
238 public Integer getIntegerObject(String columnName)
239 {
240 Integer i;
241 Object obj = getObject(columnName);
242 if (obj instanceof Integer) {
243 i = (Integer)obj;
244 } else if (obj != null) {
245 try {
246 i = Integer.parseInt(obj.toString());
247 }
248 catch (NumberFormatException exc) {
249 throw new TypeMismatchStorageException(Integer.class.getName(), obj.getClass().getName(), columnName);
250 }
251 } else {
252 i = null;
253 }
254 return i;
255 }
256
257 @Override
258 public Long getLongObject(String columnName)
259 {
260 Long l;
261 Object obj = getObject(columnName);
262 if (obj instanceof Long) {
263 l = (Long)obj;
264 } else if (obj != null) {
265 try {
266 l = Long.parseLong(obj.toString());
267 }
268 catch (NumberFormatException exc) {
269 throw new TypeMismatchStorageException(Long.class.getName(), obj.getClass().getName(), columnName);
270 }
271 } else {
272 l = null;
273 }
274 return l;
275 }
276
277 @Override
278 public Float getFloatObject(String columnName)
279 {
280 Float f;
281 Object obj = getObject(columnName);
282 if (obj instanceof Float) {
283 f = (Float)obj;
284 } else if (obj != null) {
285 try {
286 f = Float.parseFloat(obj.toString());
287 }
288 catch (NumberFormatException exc) {
289 throw new TypeMismatchStorageException(Float.class.getName(), obj.getClass().getName(), columnName);
290 }
291 } else {
292 f = null;
293 }
294 return f;
295 }
296
297 @Override
298 public Double getDoubleObject(String columnName)
299 {
300 Double d;
301 Object obj = getObject(columnName);
302 if (obj instanceof Double) {
303 d = (Double)obj;
304 } else if (obj != null) {
305 try {
306 d = Double.parseDouble(obj.toString());
307 }
308 catch (NumberFormatException exc) {
309 throw new TypeMismatchStorageException(Double.class.getName(), obj.getClass().getName(), columnName);
310 }
311 } else {
312 d = null;
313 }
314 return d;
315 }
316
317 @Override
318 public Boolean getBooleanObject(String columnName)
319 {
320 Boolean b;
321 Object obj = getObject(columnName);
322 if (obj instanceof Boolean) {
323 b = (Boolean)obj;
324 } else if (obj != null) {
325 try {
326 b = Boolean.parseBoolean(obj.toString());
327 }
328 catch (NumberFormatException exc) {
329 throw new TypeMismatchStorageException(Boolean.class.getName(), obj.getClass().getName(), columnName);
330 }
331 } else {
332 b = null;
333 }
334 return b;
335 }
336
337 @Override
338 public Byte getByteObject(String columnName)
339 {
340 Byte b;
341 Object obj = getObject(columnName);
342 if (obj instanceof Byte) {
343 b = (Byte)obj;
344 } else if (obj != null) {
345 try {
346 b = Byte.parseByte(obj.toString());
347 }
348 catch (NumberFormatException exc) {
349 throw new TypeMismatchStorageException(Byte.class.getName(), obj.getClass().getName(), columnName);
350 }
351 } else {
352 b = null;
353 }
354 return b;
355 }
356
357
358 @Override
359 public boolean isNull(String columnName)
360 {
361 Object obj = getObject(columnName);
362 return (obj == null);
363 }
364
365 private void addRowUpdate(String column, Object value) {
366 if (currentRowUpdate == null) {
367 currentRowUpdate = new HashMap<String,Object>();
368 Object key = rowList.get(currentIndex).get(primaryKeyName);
369 currentRowUpdate.put(primaryKeyName, key);
370 }
371 currentRowUpdate.put(column, value);
372 }
373
374 @Override
375 public void setBoolean(String columnName, boolean value) {
376 addRowUpdate(columnName, value);
377 }
378
379 @Override
380 public void setByte(String columnName, byte value) {
381 addRowUpdate(columnName, value);
382 }
383
384 @Override
385 public void setByteArray(String columnName, byte[] byteArray) {
386 addRowUpdate(columnName, byteArray);
387 }
388
389 @Override
390 public void setDouble(String columnName, double value) {
391 addRowUpdate(columnName, value);
392 }
393
394 @Override
395 public void setFloat(String columnName, float value) {
396 addRowUpdate(columnName, value);
397 }
398
399 @Override
400 public void setInt(String columnName, int value) {
401 addRowUpdate(columnName, value);
402 }
403
404 @Override
405 public void setLong(String columnName, long value) {
406 addRowUpdate(columnName, value);
407 }
408
409 @Override
410 public void setShort(String columnName, short value) {
411 addRowUpdate(columnName, value);
412 }
413
414 @Override
415 public void setString(String columnName, String value) {
416 addRowUpdate(columnName, value);
417 }
418
419 @Override
420 public void setShortObject(String columnName, Short value)
421 {
422 addRowUpdate(columnName, value);
423 }
424
425 @Override
426 public void setIntegerObject(String columnName, Integer value)
427 {
428 addRowUpdate(columnName, value);
429 }
430
431 @Override
432 public void setLongObject(String columnName, Long value)
433 {
434 addRowUpdate(columnName, value);
435 }
436
437 @Override
438 public void setFloatObject(String columnName, Float value)
439 {
440 addRowUpdate(columnName, value);
441 }
442
443 @Override
444 public void setDoubleObject(String columnName, Double value)
445 {
446 addRowUpdate(columnName, value);
447 }
448
449 @Override
450 public void setBooleanObject(String columnName, Boolean value)
451 {
452 addRowUpdate(columnName, value);
453 }
454
455 @Override
456 public void setByteObject(String columnName, Byte value)
457 {
458 addRowUpdate(columnName, value);
459 }
460
461 @Override
462 public void setDate(String column, Date value) {
463 addRowUpdate(column, value);
464 }
465
466
467 public void setNull(String columnName)
468 {
469 addRowUpdate(columnName, null);
470 }
471
472
473 @Override
474 public void deleteRow() {
475 Object key = (String) rowList.get(currentIndex).get(primaryKeyName);
476 if (rowDeleteSet == null)
477 rowDeleteSet = new HashSet<Object>();
478 rowDeleteSet.add(key);
479 }
480
481 @Override
482 public Iterator<IResultSet> iterator() {
483 if (resultSetIterator == null)
484 resultSetIterator = new ResultSetIterator(this);
485 return resultSetIterator;
486 }
487}