/*
 * Hivemall: Hive scalable Machine Learning Library
 *
 * Copyright (C) 2015 Makoto YUI
 * Copyright (C) 2013-2015 National Institute of Advanced Industrial Science and Technology (AIST)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hivemall.mix.store;

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

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

@ThreadSafe
public final class SessionStore {
    private static final int EXPECTED_MODEL_SIZE = 4194305; /* 2^22+1=4194304+1=4194305 */
    private static final Log logger = LogFactory.getLog(SessionStore.class);

    private final ConcurrentMap<String, SessionObject> sessions;

    public SessionStore() {
        this.sessions = new ConcurrentHashMap<String, SessionObject>();
    }

    @Nonnull
    private ConcurrentMap<String, SessionObject> getSessions() {
        return sessions;
    }

    @Nonnull
    public SessionObject get(@Nonnull String groupID) {
        SessionObject sessionObj = sessions.get(groupID);
        if(sessionObj == null) {
            ConcurrentMap<Object, PartialResult> map = new ConcurrentHashMap<Object, PartialResult>(EXPECTED_MODEL_SIZE);
            sessionObj = new SessionObject(map);
            SessionObject existing = sessions.putIfAbsent(groupID, sessionObj);
            if(existing != null) {
                sessionObj = existing;
            }
        }
        return sessionObj;
    }

    public void remove(@Nonnull String groupID) {
        SessionObject removedSession = sessions.remove(groupID);
        if(removedSession != null) {
            logger.info("Removed an idle session group: " + groupID + "\t"
                    + removedSession.getSessionInfo());
        }
    }

    @ThreadSafe
    public static final class IdleSessionSweeper implements Runnable {

        private final ConcurrentMap<String, SessionObject> sessions;
        private final long ttl;

        public IdleSessionSweeper(@Nonnull SessionStore sessionStore, @Nonnegative long ttlInMillis) {
            this.sessions = sessionStore.getSessions();
            this.ttl = ttlInMillis;
        }

        public void run() {
            for(Map.Entry<String, SessionObject> e : sessions.entrySet()) {
                SessionObject sessionObj = e.getValue();
                long lastAccessed = sessionObj.getLastAccessed();
                long elapsedTime = System.currentTimeMillis() - lastAccessed;
                if(elapsedTime > ttl) {
                    String key = e.getKey();
                    assert (key != null);
                    SessionObject removedSession = sessions.remove(key);
                    if(removedSession != null) {
                        logger.info("Removed an idle session group: " + key + "\t"
                                + removedSession.getSessionInfo());
                    }
                }
            }

        }
    }

}

@@ -795,19 +795,68 @@
 va.util.
-Map
+Iterator;%0Aimport java.util.Map;%0Aimport java.util.Set
 ;%0Aimport
@@ -2926,11 +2926,17 @@
    f
-or(
+inal Set%3C
 Map.
@@ -2967,12 +2967,19 @@
 ect%3E
+%3E
  e
- :
+ntries =
  ses
@@ -2994,19 +2994,207 @@
 trySet()
-) %7B
+;%0A            final Iterator%3CMap.Entry%3CString, SessionObject%3E%3E itor = entries.iterator();%0A            while(itor.hasNext()) %7B%0A                Map.Entry%3CString, SessionObject%3E e = itor.next();
 %0A       
@@ -3451,142 +3451,20 @@
     
-String key = e.getKey();%0A                    assert (key != null);%0A                    SessionObject removedSession = sessions
+itor
 .remove(
 key)
@@ -3459,19 +3459,16 @@
 .remove(
-key
 );%0A     
@@ -3485,38 +3485,38 @@
      if(
-removedSession != null
+logger.isInfoEnabled()
 ) %7B%0A    
@@ -3587,11 +3587,18 @@
 %22 + 
-key
+e.getKey()
  + %22
@@ -3631,38 +3631,34 @@
           + 
-removedS
+s
 ession
+Obj
 .getSessionI

Fix a bug that a call to remove() modifies Iterable sessions which invalidates the iterator for the loop on sessions.entrySet()