asmuts      02/01/14 22:50:01

  Added:       src/java/org/apache/stratum/jcs/access GroupCacheAccess.java
  Log:
  no message
  
  Revision  Changes    Path
  1.1                  
jakarta-turbine-stratum/src/java/org/apache/stratum/jcs/access/GroupCacheAccess.java
  
  Index: GroupCacheAccess.java
  ===================================================================
  package org.apache.stratum.jcs.access;
  
  import java.io.*;
  import java.util.*;
  
   // CACHE
  import org.apache.stratum.jcs.access.*;
  import org.apache.stratum.jcs.access.behavior.*;
  import org.apache.stratum.jcs.access.exception.*;
  import org.apache.stratum.jcs.engine.*;
  import org.apache.stratum.jcs.engine.behavior.*;
  import org.apache.stratum.jcs.engine.control.*;
  import org.apache.stratum.jcs.engine.group.*;
  
  import org.apache.stratum.jcs.utils.log.*;
  
  
   ///////////////////////////////////////////
  public class GroupCacheAccess extends CacheAccess implements IGroupCacheAccess {
  
    private static boolean SET_ATTR_INVOCATION = true;
    private static boolean REMOVE_ATTR_INVOCATION = false;
  
    private static GroupCacheManager cacheMgr;
    //private GroupCache cache_control;
  
  
    ///////////////////////////////////////////
    protected GroupCacheAccess( Cache cache_control ) {
      super( cache_control );
      //this.cache_control = cache_control;
    }
  
    /////////////////////////////////////////
    public static GroupCacheAccess getGroupAccess( String region ) throws 
CacheException {
      if ( cacheMgr == null ) {
        synchronized( GroupCacheAccess.class ) {
          if ( cacheMgr == null ) {
            cacheMgr = GroupCacheManagerFactory.getInstance();
          }
        }
      }
      Cache cache_control= (Cache)cacheMgr.getCache( region );
      return new GroupCacheAccess( cache_control );
    }
  
    /////////////////////////////////////////
    public static GroupCacheAccess getGroupAccess( String region, 
ICompositeCacheAttributes icca ) throws CacheException {
      if ( cacheMgr == null ) {
        synchronized( GroupCacheAccess.class ) {
          if ( cacheMgr == null ) {
            cacheMgr = GroupCacheManagerFactory.getInstance();
          }
        }
      }
      Cache cache_control= (Cache)cacheMgr.getCache( region, icca );
      return new GroupCacheAccess( cache_control );
    }
  
  
    /////////////////////////////////////////
    public Object getG( String name, String group ) {
      return getAttribute(name, group);
    }
    /////////////////////////////////////////////
    public Object getAttribute(String name, String group ) {
      //try {
        return (Object)cache_control.get(new GroupAttrName(group, name));
      //} catch( ObjectNotFoundException onfe ) {
      //  return null;
      //}
    }
  
    //////////////////////////////////////////////////
    public void putG( Object key, String group, Object value )  throws CacheException {
      setAttribute( (String)key, group, value);
    }
    //////////////////////////////////////////////////////////////////////
    public void putG( Object key, String group, Object value, Attributes attr) throws 
CacheException {
      setAttribute( (String)key, group, value, attr );
    }
  
    /**
    DefineGroup is used to create a new group object. Attributes may be set on the 
group. If no attributes are
    specified, the attributes of the region or group the new group is associated with 
are used. If group is
    specified the new group will be associated with the group specified.
    */
    public void defineGroup(String name) throws CacheException {
      // update the attribute name set.
      GroupId groupId = new GroupId(cache_control.getCacheName(),name);
      HashSet attrNameSet = null;
      //try {
        attrNameSet = (HashSet)super.get(name);
      //} catch ( ObjectNotFoundException onfe ) {
      //}
      if (attrNameSet == null) {
        attrNameSet = new HashSet();
      } else {
        throw new CacheException( "group " + name + " already exists " );
      }
      try {
        cache_control.put((Serializable)groupId, (Serializable)attrNameSet );
      } catch ( Exception ioe ) {}
    }
  
    public void defineGroup(String name, Attributes attr) throws CacheException {
      // update the attribute name set.
      GroupId groupId = new GroupId(cache_control.getCacheName(), name);
      HashSet attrNameSet = null;
  
      //attrNameSet = (HashSet)super.get(name);
      attrNameSet = (HashSet)cache_control.get(groupId);
  
      if (attrNameSet == null) {
        attrNameSet = new HashSet();
      } else {
        throw new CacheException( "group " + name + " already exists " );
      }
      try {
        //cache_control.put((Serializable)groupId, (Serializable)attrNameSet, attr );
        cache_control.put(groupId, (Serializable)attrNameSet, attr );
      } catch ( Exception ioe ) {}
    }
  
  
    ///////////////////////////////////////////////////////////////////////
     public Attributes getGroupAttributes(Object name) throws CacheException {
      Attributes attr = null;
      try {
        attr = cache_control.getElementAttributes( (Serializable)name );
      } catch( Exception ioe ) {
        p( ioe.toString() );
      }
      return attr;
    }
  
    ////////////////////////////////////////////////
    public Enumeration getAttributeNames( Object group_name ) {
      //Set s = getAttributeNameSet( name );
      //p( s.toString() );
      //return Collections.enumeration(s);
      return Collections.enumeration(getAttributeNameSet( group_name ));
    }
    public Set getAttributeNameSet( Object group_name ) {
      Object obj = null;
      //try {
        obj = cache_control.get(new 
GroupId(cache_control.getCacheName(),(String)group_name), false);
      //} catch( ObjectNotFoundException onfe ) {
      //  return null;
      //}
      if (obj == null || !(obj instanceof Set)) {
        return new HashSet(); // returns a null object.
      }
      return (Set)obj;
    }
  
  
      ///////////////////////////////////////////////////
      public void setAttribute(String name, String group, Object value ) {
            removeAttribute(name, group, SET_ATTR_INVOCATION);
        try {
          cache_control.put(new GroupAttrName(group, name), (Serializable)value );
        //} catch( ObjectExistsException onfe ) {
        //  return;
        } catch ( Exception ioe ) {
          return;
        }
                  return;
      }
      ///////////////////////////////////////////////////
      public void setAttribute(String name, String group, Object value, Attributes 
attr ) {
         // unbind object first if any.
            removeAttribute(name, group, SET_ATTR_INVOCATION);
        try {
          cache_control.put(new GroupAttrName(group, name), (Serializable)value, attr 
);
        } catch( Exception e ) {
          return;
        //} catch ( IOException ioe ) {
        //  return;
        }
                  return;
      }
  
      /////////////////////////////////////////////
      public void removeAttribute( String name, String group ) {
        removeAttribute(name, group, REMOVE_ATTR_INVOCATION);
      }
      private void removeAttribute(String name, String group, boolean invocation) {
  
        GroupAttrName key = new GroupAttrName(group, name);
        // Needs to retrive the attribute so as to do object unbinding, if necessary.
        Serializable val = null;
        //try {
          val = cache_control.get(key);
        //} catch( ObjectNotFoundException onfe ) {
        //  return;
        //}
  
        if (val == null) {
          return;
        }
        if (invocation == REMOVE_ATTR_INVOCATION) {
          // remove attribute - name set taken care of by the session cache.
          cache_control.remove(key);
        }
                  return;
      }
      ///////////////////////////////////////////////
      public void destroy( String name, String group ) {
        removeAttribute( name, group );
      }
  
      /////////////////////////////////////
      public void invalidateGroup( String group ){
        // Removes all the attributes and attribute names from the Cache.
        // In doing so, need to unbind any object associated with the session.
        for (Enumeration en=getAttributeNames( group ); en.hasMoreElements();) {
          removeAttribute( (String)en.nextElement(), group );
        }
        cache_control.remove(new GroupId(cache_control.getCacheName(),group), false);
        return;
      }
  
       /////////////////////////////////////////
      public String[] getValueNames( String group ) {
        return (String[])getAttributeNameSet( group ).toArray(new String[0]);
      }
  
  
  
    // old tester
    /////////////////////////////////////////////////////
    public static void main(String[] args) {
  
      try {
  
        Logger log = LoggerManager.getLogger( "cacheaccess" );
        //log.setLogLevel( log.DEBUG );
  
  
        //CacheManager   cacheMgr = CacheManagerFactory.getInstance();
        CacheAttributes cattr = new CacheAttributes();
        cattr.setMaxObjects( 10 );
        cattr.setUseDisk( true );
  
        GroupCacheAccess cache_control= GroupCacheAccess.getGroupAccess( "testCache" );
        p( "cache_control = " + cache_control );
  
  
        cache_control.defineGroup( "testgroup" );
  
        int size = 5;
        if ( args.length > 0 ) {
          size = Integer.parseInt(args[0]);
        }
  
  //      p("\n------------------------------------" );
  //      cache_control.dumpMap();
  //
  //      p("\n------------------------------------" );
  //      cache_control.dumpCacheEntries();
  
        p("\n------------------------------------" );
        p("---about to put " + size + "---");
        Attributes attr = new Attributes();
        //attr.setFlags( attr.LATERAL );
        attr.setFlags( attr.REMOTE );
        attr.setFlags( attr.SPOOL );
  
        p( "IS_LATERAL= " + String.valueOf( attr.isSet( attr.LATERAL ) ) );
        long p_start = System.currentTimeMillis();
        for (int i=0; i < size; i++) {
          attr.setTimeToLive( i );
          cache_control.putG("key"+i, "testgroup", "data"+i+ " 
asdfghjklzxcvbnmqwertyuiop", attr.copy());
        }
        long p_end = System.currentTimeMillis();
        p("---put " + size + " in " + String.valueOf( p_end - p_start ) + " millis 
---");
  
  
        p("\n------------------------------------" );
        p("stats = " + cache_control.getStats() );
  
        cache_control.dispose();
  
        p ( "* \n" );
        p ( "* \n" );
        p ( "* \n" );
        p ( "* \n" );
        p ( "* \n" );
  
        p( "-- attribute names " + cache_control.getAttributeNames( "testgroup" ) );
        p( "-- attributes " + cache_control.getAttributeNameSet( "testgroup" ) );
  
      } catch( Exception e ) {
        e.printStackTrace( System.out );
      }
  
    }
  
  }
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to