package com.netflix.fenzo.triggers.persistence;

import com.netflix.fenzo.triggers.Trigger;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * In-memory implementation of {@code TriggerDao}
 */
public class InMemoryTriggerDao implements TriggerDao {

    private final ConcurrentMap<String, Map<String,Trigger>> groupedTriggers = new ConcurrentHashMap<String, Map<String,Trigger>>();
    private final ConcurrentMap<String, Trigger> triggers = new ConcurrentHashMap<String, Trigger>();

    @Override
    public void createTrigger(String triggerGroup, Trigger trigger) {
        Map triggerMap = new HashMap();
        triggerMap.put(trigger.getId(), trigger);
        Map existingTriggerMap = groupedTriggers.putIfAbsent(triggerGroup, triggerMap);
        if (existingTriggerMap != null) {
            synchronized (groupedTriggers) {
                groupedTriggers.get(triggerGroup).put(trigger.getId(), trigger);
            }
        }
        triggers.put(trigger.getId(), trigger);
    }

    @Override
    public Trigger getTrigger(String triggerId) {
        return triggers.get(triggerId);
    }

    @Override
    public void updateTrigger(String triggerGroup, Trigger trigger) {
        createTrigger(triggerGroup, trigger);
    }

    @Override
    public void deleteTrigger(String triggerGroup, Trigger trigger) {
        groupedTriggers.get(triggerGroup).remove(trigger.getId());
        triggers.remove(trigger.getId());
    }

    @Override
    public List<Trigger> getTriggers(String triggerGroup) {
        return (List<Trigger>) groupedTriggers.get(triggerGroup).values();
    }

    @Override
    public List<Trigger> getTriggers() {
        return (List<Trigger>) triggers.values();
    }
}

@@ -103,23 +103,52 @@
 va.util.
-HashMap
+ArrayList;%0Aimport java.util.Iterator
 ;%0Aimport
@@ -424,32 +424,42 @@
 rentMap%3CString, 
+Concurrent
 Map%3CString,Trigg
@@ -514,50 +514,8 @@
 ing,
- Map%3CString,Trigger%3E%3E();%0A    private final
  Con
@@ -532,25 +532,24 @@
 %3CString,
- 
 Trigger%3E
  trigger
@@ -544,57 +544,8 @@
 ger%3E
- triggers = new ConcurrentHashMap%3CString, Trigger
 %3E();
@@ -642,36 +642,90 @@
     
-Map triggerMap = new HashMap
+ConcurrentMap%3CString, Trigger%3E triggerMap = new ConcurrentHashMap%3CString, Trigger%3E
 ();%0A
@@ -1058,56 +1058,8 @@
   %7D%0A
-        triggers.put(trigger.getId(), trigger);%0A
     
@@ -1137,38 +1137,540 @@
     
-return triggers.get(triggerId)
+for (Iterator%3CString%3E iterator1 = groupedTriggers.keySet().iterator(); iterator1.hasNext();) %7B%0A            ConcurrentMap%3CString, Trigger%3E triggersMap = groupedTriggers.get(iterator1.next());%0A            for (Iterator%3CString%3E iterator2 = triggersMap.keySet().iterator(); iterator2.hasNext();) %7B%0A                String storedTriggerId = iterator2.next();%0A                if (triggerId.equals(storedTriggerId)) %7B%0A                    return triggersMap.get(storedTriggerId);%0A                %7D%0A            %7D%0A        %7D%0A        return null
 ;%0A  
@@ -1966,50 +1966,8 @@
 ));%0A
-        triggers.remove(trigger.getId());%0A
     
@@ -2050,33 +2050,41 @@
 %0A        return 
-(
+new Array
 List%3CTrigger%3E) g
@@ -2080,18 +2080,17 @@
 Trigger%3E
-) 
+(
 groupedT
@@ -2115,32 +2115,33 @@
 rGroup).values()
+)
 ;%0A    %7D%0A%0A    @Ov
@@ -2200,48 +2200,351 @@
     
-return (List%3CTrigger%3E) triggers.values()
+List%3CTrigger%3E triggerList = new ArrayList%3CTrigger%3E();%0A        for (Iterator%3CString%3E iterator1 = groupedTriggers.keySet().iterator(); iterator1.hasNext();) %7B%0A            ConcurrentMap%3CString, Trigger%3E triggersMap = groupedTriggers.get(iterator1.next());%0A            triggerList.addAll(triggersMap.values());%0A        %7D%0A        return triggerList
 ;%0A  

Update InMemoryTriggerDao to not use two concurrentmap's for storing triggers