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

package de.dimm.vsm.net;

import de.dimm.vsm.fsengine.StoragePoolHandler;
import de.dimm.vsm.log.Log;
import de.dimm.vsm.records.ArchiveJob;
import de.dimm.vsm.records.FileSystemElemAttributes;
import de.dimm.vsm.records.FileSystemElemNode;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 *
 * @author Administrator
 */
public class SearchContext
{

    StoragePoolHandler sp_handler;
    ArrayList<SearchEntry> slist;
    Thread searchThread;
    List<FileSystemElemNode> resultList;
    List<ArchiveJob> jobResultList;
    Date lastUsage;
    int max;

    
    public SearchContext( StoragePoolHandler handler, ArrayList<SearchEntry> slist, int max )
    {
        this.sp_handler = handler;
        this.slist = slist;
        this.max = max;
        resultList = new ArrayList<FileSystemElemNode>();
        jobResultList = new ArrayList<ArchiveJob>();


        // REGISTER OURSELVES AT HANDLER
        handler.setSearchContext(this);

        // AND REGISTER NEW PATH REOLVER FOR OUR VIRTUAL SEARCH FILESYSTEM
        SearchPathResolver resolver = new SearchPathResolver(this, handler);
        handler.setPathResolver(resolver);
        
        searchThread = new Thread(new Runnable()
        {

            @Override
            public void run()
            {
                runSearch();
            }
        }, "SearchThread");

        lastUsage = new Date();
    }
    public void touch()
    {
        lastUsage = new Date();
    }
    public boolean isExpired()
    {
        return (System.currentTimeMillis() - lastUsage.getTime() > SearchContextManager.EXPIRE_MS);
    }
   

    void startSearch()
    {
        //searchThread.start();
        runSearch();
    }
    void startSearchJob()
    {
        //searchThread.start();
        runSearchJob();
    }

    void runSearch()
    {
        resultList.clear();
        List<FileSystemElemNode> ret = null;
        try
        {
            ret = sp_handler.search(slist, max);
        }
        catch (SQLException sQLException)
        {
            Log.err("Abbruch bei der Suche", sQLException);
        }
        if (ret != null)
        {
           resultList.addAll(ret);
        }
    }
    void runSearchJob()
    {
        resultList.clear();
        List<ArchiveJob> ret = null;
        try
        {
            ret = sp_handler.searchJob(slist, max);
        }
        catch (SQLException sQLException)
        {
            Log.err("Abbruch bei der Suche", sQLException);
        }
        if (ret != null)
        {
           jobResultList.addAll(ret);
        }
    }

    boolean isBusy()
    {
        return searchThread.isAlive();
    }
    public List<ArchiveJob> getJobResultList()
    {
        return jobResultList;
    }

    public List<RemoteFSElem> getResultList()
    {
        List<RemoteFSElem> ret = new ArrayList<RemoteFSElem>();
        for (int i = 0; i < resultList.size(); i++)
        {
            FileSystemElemNode fileSystemElemNode = resultList.get(i);


            // THIS IS THE NEWEST ENTRY FOR THIS FILE
            FileSystemElemAttributes attr = sp_handler.getActualFSAttributes(fileSystemElemNode, sp_handler.getPoolQry());


            // OBVIOUSLY THE FILE WAS CREATED AFTER TS
            if (attr == null)
            {
                continue;
            }

            // FILE WAS DELETED AT TS
            if (attr.isDeleted())
            {
                continue;
            }

            ret.add(StoragePoolHandlerServlet.genRemoteFSElemfromNode(fileSystemElemNode, attr));
        }

        return ret;
    }

    void close()
    {
        if (isBusy())
        {
            try
            {
                searchThread.interrupt();
            }
            catch (Exception e)
            {
            }
        }
        if (resultList != null)
        {
            resultList.clear();
        }
        try
        {
            sp_handler.close_transaction();
        }
        catch (SQLException sQLException)
        {
        }
        sp_handler.close_entitymanager();
    }

    // THIS CALS SIZE OF OBJECTS ON FIRST LEVEL, IF WE DO IT RECURSIVELY, WE COULD END UP SCANNING THE WHOLE FS...
    long getLevel1Size()
    {
        long size = 0;
        for (int i = 0; i < resultList.size(); i++)
        {
            FileSystemElemNode n = resultList.get(i);            
            size += n.getAttributes().getFsize() + n.getAttributes().getStreamSize();
        }
        return size;
    }
    private static final int BS = 4096;
    public long getTotalBlocks()
    {
        return getLevel1Size() / BS;
    }

    public long getUsedBlocks()
    {
        return getLevel1Size() / BS;
    }

    public int getBlockSize()
    {
        return BS;
    }
}

@@ -572,24 +572,26 @@
 try%3E slist;%0A
+//
     Thread s
@@ -603,16 +603,16 @@
 Thread;%0A
-
     List
@@ -1297,32 +1297,34 @@
 lver);%0A        %0A
+//
         searchTh
@@ -1352,24 +1352,26 @@
  Runnable()%0A
+//
         %7B%0A%0A 
@@ -1368,17 +1368,21 @@
       %7B%0A
-%0A
+//%0A//
         
@@ -1395,16 +1395,18 @@
 verride%0A
+//
         
@@ -1427,16 +1427,18 @@
 d run()%0A
+//
         
@@ -1435,32 +1435,34 @@
 //            %7B%0A
+//
                 
@@ -1470,24 +1470,26 @@
 unSearch();%0A
+//
             
@@ -1482,32 +1482,34 @@
 //            %7D%0A
+//
         %7D, %22Sear
@@ -2780,16 +2780,40 @@
  return 
+false;%0A        //return 
 searchTh
@@ -3772,32 +3772,34 @@
 d close()%0A    %7B%0A
+//
         if (isBu
@@ -3800,24 +3800,26 @@
  (isBusy())%0A
+//
         %7B%0A  
@@ -3808,32 +3808,34 @@
 ())%0A//        %7B%0A
+//
             try%0A
@@ -3826,32 +3826,34 @@
             try%0A
+//
             %7B%0A  
@@ -3842,32 +3842,34 @@
 //            %7B%0A
+//
                 
@@ -3890,24 +3890,26 @@
 nterrupt();%0A
+//
             
@@ -3902,32 +3902,34 @@
 //            %7D%0A
+//
             catc
@@ -3944,16 +3944,18 @@
 tion e)%0A
+//
         
@@ -3952,32 +3952,34 @@
 //            %7B%0A
+//
             %7D%0A  
@@ -3968,32 +3968,34 @@
 //            %7D%0A
+//
         %7D%0A      

Remove Thread