/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package de.dimm.vsm.fsengine;

import de.dimm.vsm.Utilities.SizeStr;
import de.dimm.vsm.log.Log;
import de.dimm.vsm.records.DedupHashBlock;
import de.dimm.vsm.records.StoragePool;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 *
 * @author Administrator
 */
public class HashCache
{
    StoragePool pool;

    boolean inited = false;
    HashMap<String,Long> hashMap;

    public HashCache( StoragePool pool)
    {
        this.pool = pool;
        hashMap = new HashMap<String, Long>();
    }

    public boolean isInited()
    {
        return inited;
    }

    
    public boolean init(Connection conn )
    {
        // TODO: CALC MEM
        inited = false;
        
        Statement st  = null;
        try
        {
            
            st = conn.createStatement();

            int cnt = -1;
            ResultSet rs = st.executeQuery("select count(idx) from DedupHashBlock");
            if (rs.next())
            {
                cnt = rs.getInt(1);
            }
            rs.close();
            long maxMem = Runtime.getRuntime().maxMemory();
            long freeMem = Runtime.getRuntime().freeMemory();
            long totalMem = Runtime.getRuntime().totalMemory();

            Log.info("Block-Cachegröße für Pool", pool.getName() + ": " + cnt);
            Log.info("Speicher", "Max: " + SizeStr.format(maxMem) + " Total: " + SizeStr.format(totalMem) + " Free: " + SizeStr.format(freeMem)  );

            rs = st.executeQuery("select idx, hashvalue, blockLen from DedupHashBlock");

            while (rs.next())
            {
                long idx = rs.getLong(1);
                String has = rs.getString(2);

                hashMap.put(has, idx);

            }
            rs.close();

            

            inited = true;
            return true;
        }
        catch (SQLException sQLException)
        {
            Log.err("HashMap kann nicht angelegt werden", pool.getName(), sQLException);
            return false;
        }
        finally
        {
            if (st != null)
            {
                try
                {
                    st.close();
                }
                catch (SQLException sQLException)
                {
                }
            }           
        }
    }

    public long getDhbIdx( String hash )
    {
        Long l = hashMap.get(hash);
        if (l != null)
            return l.longValue();
        
        return -1;
    }
    public void addDhb( String hash, long idx )
    {
        hashMap.put(hash, idx);
    }

    public int size()
    {
        return hashMap.size();
    }

    void removeDhb( DedupHashBlock dhb )
    {
        hashMap.remove(dhb.getHashvalue());
    }

    public List<String> getUrlUnsafeHashes()
    {
        ArrayList<String> ret = new ArrayList<String>();
        Set<String> set = hashMap.keySet();

        for (Iterator<String> it = set.iterator(); it.hasNext();)
        {
            String hash = it.next();
            boolean found = false;

            char lastCh = hash.charAt( hash.length() - 1 );
            
            // DETECT PADDED HASHES
            if (lastCh == '=')
                found = true;
            
            if (!found)
            {
                for (int i = 0; i < hash.length(); i++)
                {
                    char ch = hash.charAt(i);
                    if (ch == '/' || ch == '+')
                    {
                        found = true;
                        break;
                    }
                }
            }
            if (found)
            {
                ret.add(hash);
            }
        }
        return ret;
    }
}

@@ -274,16 +274,52 @@
 gePool;%0A
+import gnu.trove.map.hash.THashMap;%0A
 import j
@@ -454,34 +454,8 @@
 st;%0A
-import java.util.HashMap;%0A
 impo
@@ -639,16 +639,17 @@
 se;%0A    
+T
 HashMap%3C
@@ -765,16 +765,17 @@
 p = new 
+T
 HashMap%3C
@@ -1079,24 +1079,26 @@
 atement();%0A%0A
+//
             
@@ -1107,24 +1107,26 @@
 t cnt = -1;%0A
+//
             
@@ -1198,16 +1198,18 @@
 lock%22);%0A
+//
         
@@ -1219,32 +1219,34 @@
  if (rs.next())%0A
+//
             %7B%0A  
@@ -1235,32 +1235,34 @@
 //            %7B%0A
+//
                 
@@ -1277,24 +1277,26 @@
 .getInt(1);%0A
+//
             
@@ -1289,32 +1289,34 @@
 //            %7D%0A
+//
             rs.c
@@ -1505,25 +1505,24 @@
 alMemory();%0A
-%0A
             
@@ -1535,96 +1535,26 @@
 fo(%22
-Block-Cachegr%C3%B6%C3%9Fe f%C3%BCr Pool%22, pool.getName() + %22: %22 + cnt);%0A            Log.info(%22Spei
+Speicher vor Ca
 che
-r
 %22, %22
@@ -1680,16 +1680,26 @@
         
+ResultSet 
 rs = st.
@@ -1737,18 +1737,9 @@
 alue
-, blockLen
+ 
  fro
@@ -1931,17 +1931,16 @@
 , idx);%0A
-%0A
         
@@ -1965,24 +1965,445 @@
 rs.close();%0A
+            Log.info(%22Block-Cachegr%C3%B6%C3%9Fe f%C3%BCr Pool%22, pool.getName() + %22: %22 + hashMap.size());%0A%0A            maxMem = Runtime.getRuntime().maxMemory();%0A            freeMem = Runtime.getRuntime().freeMemory();%0A            totalMem = Runtime.getRuntime().totalMemory();%0A            Log.info(%22Speicher nach Cache%22, %22Max: %22 + SizeStr.format(maxMem) + %22 Total: %22 + SizeStr.format(totalMem) + %22 Free: %22 + SizeStr.format(freeMem)  );
 %0A           

Use THashMap and Speed up build of map