2 * Copyright (c) 2015 Cisco Systems, Inc. All rights reserved.
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
9 package org.opendaylight.lispflowmapping.inmemorydb;
11 import java.util.Date;
13 import java.util.concurrent.ConcurrentHashMap;
14 import java.util.concurrent.ConcurrentMap;
15 import java.util.concurrent.TimeUnit;
17 import org.opendaylight.lispflowmapping.interfaces.dao.ILispDAO;
18 import org.opendaylight.lispflowmapping.interfaces.dao.IRowVisitor;
19 import org.opendaylight.lispflowmapping.interfaces.dao.MappingEntry;
20 import org.opendaylight.lispflowmapping.interfaces.dao.SubKeys;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
24 public class HashMapDb implements ILispDAO, AutoCloseable {
26 protected static final Logger LOG = LoggerFactory.getLogger(HashMapDb.class);
27 private ConcurrentMap<Object, ConcurrentMap<String, Object>> data = new ConcurrentHashMap<Object, ConcurrentMap<String, Object>>();
28 private TimeUnit timeUnit = TimeUnit.SECONDS;
29 private int recordTimeOut = 240;
32 public void put(Object key, MappingEntry<?>... values) {
33 if (!data.containsKey(key)) {
34 data.put(key, new ConcurrentHashMap<String, Object>());
36 for (MappingEntry<?> entry : values) {
37 data.get(key).put(entry.getKey(), entry.getValue());
42 public Object getSpecific(Object key, String valueKey) {
43 Map<String, Object> keyToValues = data.get(key);
44 if (keyToValues == null) {
47 return keyToValues.get(valueKey);
51 public Map<String, Object> get(Object key) {
56 public void getAll(IRowVisitor visitor) {
57 for (ConcurrentMap.Entry<Object, ConcurrentMap<String, Object>> keyEntry : data.entrySet()) {
58 for (Map.Entry<String, Object> valueEntry : keyEntry.getValue().entrySet()) {
59 visitor.visitRow(keyEntry.getKey(), valueEntry.getKey(), valueEntry.getValue());
65 public void remove(Object key) {
70 public void removeSpecific(Object key, String valueKey) {
71 if (data.containsKey(key) && data.get(key).containsKey(valueKey)) {
72 data.get(key).remove(valueKey);
77 public void removeAll() {
81 // TODO: this should be moved outside of DAO implementation
82 public void cleanOld() {
83 getAll(new IRowVisitor() {
84 public void visitRow(Object keyId, String valueKey, Object value) {
85 if (value != null && valueKey instanceof String && ((String) valueKey).equals(SubKeys.REGDATE)) {
86 Date date = (Date) value;
87 if (isExpired(date)) {
88 removeSpecific(keyId, SubKeys.RECORD);
93 private boolean isExpired(Date date) {
94 return System.currentTimeMillis() - date.getTime() > TimeUnit.MILLISECONDS.convert(recordTimeOut, timeUnit);
99 public TimeUnit getTimeUnit() {
103 public void setRecordTimeOut(int recordTimeOut) {
104 this.recordTimeOut = recordTimeOut;
107 public int getRecordTimeOut() {
108 return recordTimeOut;
111 public void setTimeUnit(TimeUnit timeUnit) {
112 this.timeUnit = timeUnit;
115 public void close() throws Exception {
120 public ILispDAO putNestedTable(Object key, String valueKey) {
121 ILispDAO nestedTable = (ILispDAO) getSpecific(key, valueKey);
122 if (nestedTable != null) {
123 LOG.warn("Trying to add nested table that already exists. Aborting!");
126 nestedTable = new HashMapDb();
127 put(key, new MappingEntry<>(valueKey, nestedTable));