# IGNITE-379 Refactoring: cleanup code.

Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/40af5fbd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/40af5fbd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/40af5fbd

Branch: refs/heads/ignite-496
Commit: 40af5fbdb6d61c4b2ea30df1e1389858c11ef65a
Parents: 567dd9a
Author: sevdokimov <sevdoki...@gridgain.com>
Authored: Mon Mar 23 18:23:35 2015 +0300
Committer: sevdokimov <sevdoki...@gridgain.com>
Committed: Mon Mar 23 18:23:35 2015 +0300

----------------------------------------------------------------------
 .../processors/cache/GridCacheStoreManager.java | 90 ++++++--------------
 .../cache/GridCacheWriteBehindStore.java        | 13 +--
 2 files changed, 32 insertions(+), 71 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/40af5fbd/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheStoreManager.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheStoreManager.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheStoreManager.java
index 01c92dd..cffa7a1 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheStoreManager.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheStoreManager.java
@@ -65,9 +65,6 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
     private final boolean writeThrough;
 
     /** */
-    private final boolean sesEnabled;
-
-    /** */
     private boolean convertPortable;
 
     /**
@@ -93,33 +90,20 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
 
         ThreadLocal<SessionData> sesHolder0 = null;
 
-        boolean sesEnabled0 = false;
-
         if (cfgStore != null) {
-            if (!sesHolders.containsKey(cfgStore)) {
-                sesHolder0 = new ThreadLocal<>();
-
-                sesEnabled0 = ctx.resource().injectStoreSession(cfgStore, new 
ThreadLocalSession(sesHolder0));
+            sesHolder0 = sesHolders.get(cfgStore);
 
-                if (sesEnabled0)
-                    sesHolders.put(cfgStore, sesHolder0);
-                else
-                    sesHolder0 = null;
-            }
-            else {
-                sesHolder0 = sesHolders.get(cfgStore);
+            if (sesHolder0 == null) {
+                ThreadLocalSession locSes = new ThreadLocalSession();
 
-                sesEnabled0 = true;
+                if (ctx.resource().injectStoreSession(cfgStore, locSes))
+                    sesHolder0 = locSes.sesHolder;
             }
         }
 
-        sesEnabled = sesEnabled0;
-
         sesHolder = sesHolder0;
 
         locStore = U.hasAnnotation(cfgStore, CacheLocalStore.class);
-
-        assert sesHolder != null || !sesEnabled;
     }
 
     /**
@@ -269,7 +253,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             if (log.isDebugEnabled())
                 log.debug("Loading value from store for key: " + storeKey);
 
-            boolean ses = initSession(tx);
+            initSession(tx);
 
             Object val = null;
 
@@ -286,8 +270,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 throw new IgniteCheckedException(new CacheLoaderException(e));
             }
             finally {
-                if (ses)
-                    endSession();
+                endSession();
             }
 
             if (log.isDebugEnabled())
@@ -421,7 +404,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             if (log.isDebugEnabled())
                 log.debug("Loading values from store for keys: " + keys0);
 
-            boolean ses = initSession(tx);
+            initSession(tx);
 
             try {
                 IgniteBiInClosure<Object, Object> c = new CI2<Object, 
Object>() {
@@ -462,8 +445,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 throw new IgniteCheckedException(new CacheLoaderException(e));
             }
             finally {
-                if (ses)
-                    endSession();
+                endSession();
             }
 
             if (log.isDebugEnabled())
@@ -486,7 +468,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             if (log.isDebugEnabled())
                 log.debug("Loading all values from store.");
 
-            boolean ses = initSession(null);
+            initSession(null);
 
             try {
                 store.loadCache(new IgniteBiInClosure<Object, Object>() {
@@ -516,8 +498,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 throw new IgniteCheckedException(new CacheLoaderException(e));
             }
             finally {
-                if (ses)
-                    endSession();
+                endSession();
             }
 
             if (log.isDebugEnabled())
@@ -558,7 +539,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             if (log.isDebugEnabled())
                 log.debug("Storing value in cache store [key=" + key + ", 
val=" + val + ']');
 
-            boolean ses = initSession(tx);
+            initSession(tx);
 
             try {
                 store.write(new CacheEntryImpl<>(key, locStore ? F.t(val, ver) 
: val));
@@ -573,8 +554,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 throw new IgniteCheckedException(new CacheWriterException(e));
             }
             finally {
-                if (ses)
-                    endSession();
+                endSession();
             }
 
             if (log.isDebugEnabled())
@@ -613,7 +593,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 if (log.isDebugEnabled())
                     log.debug("Storing values in cache store [entries=" + 
entries + ']');
 
-                boolean ses = initSession(tx);
+                initSession(tx);
 
                 try {
                     store.writeAll(entries);
@@ -637,8 +617,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                     throw new IgniteCheckedException(e);
                 }
                 finally {
-                    if (ses)
-                        endSession();
+                    endSession();
                 }
 
                 if (log.isDebugEnabled())
@@ -670,7 +649,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             if (log.isDebugEnabled())
                 log.debug("Removing value from cache store [key=" + key + ']');
 
-            boolean ses = initSession(tx);
+            initSession(tx);
 
             try {
                 store.delete(key);
@@ -685,8 +664,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 throw new IgniteCheckedException(new CacheWriterException(e));
             }
             finally {
-                if (ses)
-                    endSession();
+                endSession();
             }
 
             if (log.isDebugEnabled())
@@ -722,7 +700,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             if (log.isDebugEnabled())
                 log.debug("Removing values from cache store [keys=" + keys0 + 
']');
 
-            boolean ses = initSession(tx);
+            initSession(tx);
 
             try {
                 store.deleteAll(keys0);
@@ -740,8 +718,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
                 throw new IgniteCheckedException(e);
             }
             finally {
-                if (ses)
-                    endSession();
+                endSession();
             }
 
             if (log.isDebugEnabled())
@@ -776,14 +753,14 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
     public void txEnd(IgniteInternalTx tx, boolean commit) throws 
IgniteCheckedException {
         assert store != null;
 
-        boolean ses = initSession(tx);
+        initSession(tx);
 
         try {
             store.sessionEnd(commit);
         }
         finally {
-            if (ses) {
-                endSession();
+            if (sesHolder != null) {
+                sesHolder.set(null);
 
                 tx.removeMeta(SES_ATTR);
             }
@@ -809,18 +786,16 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
      * Clears session holder.
      */
     void endSession() {
-        assert sesEnabled;
-
-        sesHolder.set(null);
+        if (sesHolder != null)
+            sesHolder.set(null);
     }
 
     /**
      * @param tx Current transaction.
-     * @return {@code True} if session was initialized.
      */
-    boolean initSession(@Nullable IgniteInternalTx tx) {
-        if (!sesEnabled)
-            return false;
+    void initSession(@Nullable IgniteInternalTx tx) {
+        if (sesHolder == null)
+            return;
 
         SessionData ses;
 
@@ -840,8 +815,6 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
             ses = new SessionData(null, cctx.name());
 
         sesHolder.set(ses);
-
-        return true;
     }
 
     /**
@@ -910,14 +883,7 @@ public class GridCacheStoreManager extends 
GridCacheManagerAdapter {
      */
     private static class ThreadLocalSession implements CacheStoreSession {
         /** */
-        private final ThreadLocal<SessionData> sesHolder;
-
-        /**
-         * @param sesHolder Session holder.
-         */
-        private ThreadLocalSession(ThreadLocal<SessionData> sesHolder) {
-            this.sesHolder = sesHolder;
-        }
+        private final ThreadLocal<SessionData> sesHolder = new ThreadLocal<>();
 
         /** {@inheritDoc} */
         @Nullable @Override public Transaction transaction() {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/40af5fbd/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStore.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStore.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStore.java
index 5aed410..fd14721 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStore.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/GridCacheWriteBehindStore.java
@@ -661,7 +661,9 @@ public class GridCacheWriteBehindStore<K, V> implements 
CacheStore<K, V>, Lifecy
     private boolean updateStore(StoreOperation operation,
         Map<K, Entry<? extends K, ? extends  V>> vals,
         boolean initSes) {
-        boolean ses = initSes && initSession();
+
+        if (initSes && storeMgr != null)
+            storeMgr.initSession(null);
 
         try {
             switch (operation) {
@@ -699,7 +701,7 @@ public class GridCacheWriteBehindStore<K, V> implements 
CacheStore<K, V>, Lifecy
             return false;
         }
         finally {
-            if (ses)
+            if (initSes && storeMgr != null)
                 storeMgr.endSession();
         }
     }
@@ -719,13 +721,6 @@ public class GridCacheWriteBehindStore<K, V> implements 
CacheStore<K, V>, Lifecy
     }
 
     /**
-     * @return {@code True} if session was initialized.
-     */
-    private boolean initSession() {
-        return storeMgr != null && storeMgr.initSession(null);
-    }
-
-    /**
      * Thread that performs time-based flushing of written values to the 
underlying storage.
      */
     private class Flusher extends GridWorker {

Reply via email to