1 Reply Latest reply on Oct 11, 2004 2:13 PM by Ben Wang

    Wildcard search in TreeCache

    Jeff Tsang Newbie

      Hello,

      I have done a program for TreeCache contructing and searching. But it is not work when performing wildcard searching.
      Could anyone five me some opinion?

      Thanks,

      Code:

      package test.cache.product;

      import org.apache.commons.logging.Log;
      import org.apache.commons.logging.LogFactory;
      import java.sql.*;
      import org.jboss.cache.*;
      import org.jboss.cache.lock.LockingException;
      import org.jboss.cache.lock.TimeoutException;

      import java.util.*;

      public class IndPoCache2
      {
      /**
      * Logger for this class
      */
      private static final Log logger = LogFactory.getLog(IndPoCache2.class);

      public static void main(String[] args) throws Exception
      {

      TreeCache execMatchCache = new TreeCache();
      TreeMap fullProductList = new TreeMap();
      java.util.Date startTime;
      java.util.Date endTime;
      long heapMemory;

      //PropertyConfigurator config = new PropertyConfigurator();
      //config.configure(execMatchCache, "META-INF/replSync-service.xml");
      execMatchCache.stopService();
      execMatchCache.startService();

      Connection connection = null;
      try
      {
      // Load the JDBC driver
      String driverName = "oracle.jdbc.driver.OracleDriver";
      Class.forName(driverName);

      // Create a connection to the database
      connection = DriverManager.getConnection(url, username, password);
      }
      catch (ClassNotFoundException e)
      {
      // Could not find the database driver
      connection.close();
      System.out.println(e.getMessage());
      }
      catch (SQLException e)
      {
      // Could not connect to the database
      connection.close();
      System.out.println(e.getMessage());
      }

      startTime = new java.util.Date();

      try
      {
      // Create a result set containing all data from my_table
      Statement stmt = connection.createStatement();
      ResultSet rs = stmt
      .executeQuery("select job_id, appt_type, call_type, bldg_exch_id, cust_name from job_request where JOB_ID < 100");

      StringBuffer fqn = new StringBuffer();
      String del = "/";
      String dir = "!";

      logger.info("Memory - CP001 - FreeMemory: " + Runtime.getRuntime().freeMemory() + "/"
      + Runtime.getRuntime().totalMemory());
      heapMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
      logger.info("Memory - CP001 - Heap Memory: " + heapMemory);


      while (rs.next())
      {
      // Get the data from the row using the column index

      Product oProduct = new Product(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs
      .getString(5));
      fullProductList.put(oProduct.getJob_id(), oProduct);
      }

      rs.close();

      endTime = new java.util.Date();

      logger.info("Time To Retrieve Data (ms): " + (endTime.getTime() - startTime.getTime()));
      }
      catch (SQLException e)
      {
      connection.close();
      System.out.println("fail3");
      System.out.println(e.getMessage());
      }
      catch (org.jboss.util.NestedRuntimeException e)
      {
      System.out.println(e.getMessage());
      }

      connection.close();

      logger.info("Memory - CP002 - FreeMemory: " + Runtime.getRuntime().freeMemory() + "/"
      + Runtime.getRuntime().totalMemory());
      heapMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
      logger.info("Memory - CP002 - Heap Memory: " + heapMemory);

      startTime = new java.util.Date();

      Iterator iterator = fullProductList.values().iterator();
      while (iterator.hasNext())
      {
      Product oProduct = (Product) iterator.next();
      StringBuffer sbIndexKey = new StringBuffer();
      appendIndexKey(sbIndexKey, oProduct.getBldg_exch_id());
      appendIndexKey(sbIndexKey, oProduct.getCust_name());

      StringBuffer treeKeyBuff = new StringBuffer();
      String indexKey = sbIndexKey.toString();

      for (int i = 0; i < indexKey.length(); i++)
      {
      treeKeyBuff.append("/");
      treeKeyBuff.append(indexKey.substring(i, i + 1));
      }

      treeKeyBuff.append("/");
      treeKeyBuff.append(oProduct.getJob_id());

      execMatchCache.put(treeKeyBuff.toString(), "PRODUCT", oProduct);
      }

      endTime = new java.util.Date();

      System.out.print("Tree Created. \n");
      logger.info("Time To Build Tree (ms): " + (endTime.getTime() - startTime.getTime()));
      logger.info("Memory - CP003 - FreeMemory: " + Runtime.getRuntime().freeMemory() + "/"
      + Runtime.getRuntime().totalMemory());
      heapMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
      logger.info("Memory - CP003 - Heap Memory: " + heapMemory);

      startTime = new java.util.Date();

      Set result = new HashSet();
      result = search("/Z*", execMatchCache);

      endTime = new java.util.Date();

      System.out.println("Finish Search");
      for (Iterator it = result.iterator(); it.hasNext();)
      {
      Object o = it.next();
      System.out.println(o.toString());
      }

      logger.info("Time To Retrieve (ms): " + (endTime.getTime() - startTime.getTime()));
      logger.info("Number of Result: " + result.size());

      logger.info("Memory - CP004 - FreeMemory: " + Runtime.getRuntime().freeMemory() + "/"
      + Runtime.getRuntime().totalMemory());
      heapMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
      logger.info("Memory - CP004 - Heap Memory: " + heapMemory);

      execMatchCache.stopService();

      logger.info("Memory - CP005 - FreeMemory: " + Runtime.getRuntime().freeMemory() + "/"
      + Runtime.getRuntime().totalMemory());
      heapMemory = Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
      logger.info("Memory - CP005 - Heap Memory: " + heapMemory);

      }

      public static void appendIndexKey(StringBuffer pStrBuff, String pKeyValue)
      {
      if (pKeyValue == null) pStrBuff.append(" ");
      else pStrBuff.append(pKeyValue.toUpperCase().trim().replaceAll(" ", ""));
      pStrBuff.append("!");
      }

      public static Set search(String fqn, TreeCache tree)
      {
      String Refinefqn = "";
      Set result = new HashSet();

      getAllLeave(fqn, tree, result, 0);

      return result;
      }

      public static void getAllLeave(String fqn, TreeCache tree, Set pSet, int pLevel)
      {
      Set temp = new HashSet();
      try
      {
      temp = tree.getChildrenNames(fqn);
      if (temp == null)
      {
      if (tree.get(fqn, "PRODUCT") != null) pSet.add((Product) tree.get(fqn, "PRODUCT"));
      }
      else
      {
      for (Iterator it = temp.iterator(); it.hasNext();)
      {
      Object o = it.next();
      getAllLeave(fqn + "/" + o.toString(), tree, pSet, pLevel + 1);
      }
      }
      }
      catch (LockingException e)
      {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      catch (TimeoutException e)
      {
      // TODO Auto-generated catch block
      e.printStackTrace();
      }
      }

      public static class Product extends Object
      {
      public Product(String pJob_id, String pAppt_type, String pCall_type, String pBldg_exch_id, String pCust_name)
      {
      this.setJob_id(pJob_id);
      this.setAppt_type(pAppt_type);
      this.setCall_type(pCall_type);
      this.setBldg_exch_id(pBldg_exch_id);
      this.setCust_name(pCust_name);
      }

      public String getAppt_type()
      {
      return this.appt_type;
      }

      public void setAppt_type(String pAppt_type)
      {
      this.appt_type = pAppt_type;
      }

      public String getBldg_exch_id()
      {
      return this.bldg_exch_id;
      }

      public void setBldg_exch_id(String pBldg_exch_id)
      {
      this.bldg_exch_id = pBldg_exch_id;
      }

      public String getCall_type()
      {
      return this.call_type;
      }

      public void setCall_type(String pCall_type)
      {
      this.call_type = pCall_type;
      }

      public String getCust_name()
      {
      return this.cust_name;
      }

      public void setCust_name(String pCust_name)
      {
      this.cust_name = pCust_name;
      }

      public String getJob_id()
      {
      return this.job_id;
      }

      public void setJob_id(String pJob_id)
      {
      this.job_id = pJob_id;
      }

      private String job_id;

      private String appt_type;

      private String call_type;

      private String bldg_exch_id;

      private String cust_name;
      }
      }