⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gcpastimpl.java

📁 pastry的java实现的2.0b版
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************"FreePastry" Peer-to-Peer Application Development Substrate Copyright 2002, Rice University. All rights reserved.Redistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditions aremet:- Redistributions of source code must retain the above copyrightnotice, this list of conditions and the following disclaimer.- Redistributions in binary form must reproduce the above copyrightnotice, this list of conditions and the following disclaimer in thedocumentation and/or other materials provided with the distribution.- Neither  the name  of Rice  University (RICE) nor  the names  of itscontributors may be  used to endorse or promote  products derived fromthis software without specific prior written permission.This software is provided by RICE and the contributors on an "as is"basis, without any representations or warranties of any kind, expressor implied including, but not limited to, representations orwarranties of non-infringement, merchantability or fitness for aparticular purpose. In no event shall RICE or contributors be liablefor any direct, indirect, incidental, special, exemplary, orconsequential damages (including, but not limited to, procurement ofsubstitute goods or services; loss of use, data, or profits; orbusiness interruption) however caused and on any theory of liability,whether in contract, strict liability, or tort (including negligenceor otherwise) arising in any way out of the use of this software, evenif advised of the possibility of such damage.********************************************************************************/package rice.p2p.past.gc;import java.io.*;import java.util.*;import rice.*;import rice.Continuation.*;import rice.environment.Environment;import rice.environment.logging.Logger;import rice.p2p.commonapi.*;import rice.p2p.commonapi.rawserialization.*;import rice.p2p.past.*;import rice.p2p.past.messaging.*;import rice.p2p.past.rawserialization.SocketStrategy;import rice.p2p.past.gc.messaging.*;import rice.persistence.*;/** * @(#) GCPastImpl.java This class is an implementation of the GCPast interface, * which provides Past services with garbage collection. * * @version $Id: GCPastImpl.java 3274 2006-05-15 16:17:47Z jeffh $ * @author Alan Mislove * @author Andreas Haeberlen */public class GCPastImpl extends PastImpl implements GCPast {  /**   * The real factory, which is not wrapped with a GCIdFactory   */  protected IdFactory realFactory;  // internal tracing stats  /**   * DESCRIBE THE FIELD   */  public int collected = 0;  /**   * DESCRIBE THE FIELD   */  public int refreshed = 0;  /**   * The default expiration, or when objects inserted with no timeout will   * expire   */  public final static long DEFAULT_EXPIRATION = INFINITY_EXPIRATION;  /**   * Constructor for GCPast   *   * @param node The node below this Past implementation   * @param manager The storage manager to be used by Past   * @param replicas The number of object replicas   * @param instance The unique instance name of this Past   * @param policy The policy this past instance should use   * @param collectionInterval The frequency with which GCPast should collection   *      local expired objects   */  public GCPastImpl(Node node, StorageManager manager, int replicas, String instance, PastPolicy policy, long collectionInterval) {    this(node, manager, null, replicas, instance, policy, collectionInterval, null);  }  /**   * Constructor for GCPast   *   * @param node The node below this Past implementation   * @param manager The storage manager to be used by Past   * @param backup The cache used for previously-responsible objects (can be   *      null)   * @param replicas The number of object replicas   * @param instance The unique instance name of this Past   * @param trash The storage manager to place the deleted objects into (if   *      null, they are removed)   * @param policy The policy this past instance should use   * @param collectionInterval The frequency with which GCPast should collection   *      local expired objects   */  public GCPastImpl(Node node, StorageManager manager, Cache backup, int replicas, String instance, PastPolicy policy, long collectionInterval, StorageManager trash) {    super(new GCNode(node), manager, backup, replicas, instance, policy, trash);    this.realFactory = node.getIdFactory();    endpoint.scheduleMessage(new GCCollectMessage(0, getLocalNodeHandle(), node.getId()), collectionInterval, collectionInterval);    endpoint.setDeserializer(new GCPastDeserializer());  }  /**   * DESCRIBE THE METHOD   *   * @return DESCRIBE THE RETURN VALUE   */  public String toString() {    if (endpoint == null) {      return super.toString();    }    return "GCPastImpl[" + endpoint.getInstance() + "]";  }  /**   * Inserts an object with the given ID into this instance of Past.   * Asynchronously returns a PastException to command, if the operation was   * unsuccessful. If the operation was successful, a Boolean[] is returned   * representing the responses from each of the replicas which inserted the   * object. This method is equivalent to insert(obj, INFINITY_EXPIRATION,   * command) as it inserts the object with a timeout value of infinity. This is   * done for simplicity, as well as backwards-compatibility for applications.   *   * @param obj the object to be inserted   * @param command Command to be performed when the result is received   */  public void insert(PastContent obj, Continuation command) {    insert(obj, INFINITY_EXPIRATION, command);  }  /**   * Inserts an object with the given ID into this instance of Past.   * Asynchronously returns a PastException to command, if the operation was   * unsuccessful. If the operation was successful, a Boolean[] is returned   * representing the responses from each of the replicas which inserted the   * object. The contract for this method is that the provided object will be   * stored until the provided expiration time. Thus, if the application   * determines that it is still interested in this object, it must refresh the   * object via the refresh() method.   *   * @param obj the object to be inserted   * @param expiration the time until which the object must be stored   * @param command Command to be performed when the result is received   */  public void insert(final PastContent obj, final long expiration, Continuation command) {    if (logger.level <= Logger.FINE) {      logger.log("Inserting data of class " + obj.getClass().getName() + " under " + obj.getId().toStringFull());    }    doInsert(obj.getId(),      new MessageBuilder() {        public PastMessage buildMessage() {          return new GCInsertMessage(getUID(), obj, expiration, getLocalNodeHandle(), obj.getId());        }      }, command,      socketStrategy.sendAlongSocket(SocketStrategy.TYPE_INSERT, obj));  }  /**   * Updates the objects stored under the provided keys id to expire no earlier   * than the provided expiration time. Asyncroniously returns the result to the   * caller via the provided continuation. The result of this operation is an   * Object[], which is the same length as the input array of Ids. Each element   * in the array is either Boolean(true), representing that the refresh   * succeeded for the cooresponding Id, or an Exception describing why the   * refresh failed. Specifically, the possible exceptions which can be returned   * are: ObjectNotFoundException - if no object was found under the given key   * RefreshFailedException - if the refresh operation failed for any other   * reason (the getMessage() will describe the failure)   *   * @param expiration The time to extend the lifetime to   * @param command Command to be performed when the result is received   * @param array DESCRIBE THE PARAMETER   */  public void refresh(Id[] array, long expiration, Continuation command) {    long[] expirations = new long[array.length];    Arrays.fill(expirations, expiration);    refresh(array, expirations, command);  }  /**   * Updates the objects stored under the provided keys id to expire no earlier   * than the provided expiration time. Asyncroniously returns the result to the   * caller via the provided continuation. The result of this operation is an   * Object[], which is the same length as the input array of Ids. Each element   * in the array is either Boolean(true), representing that the refresh   * succeeded for the cooresponding Id, or an Exception describing why the   * refresh failed. Specifically, the possible exceptions which can be returned   * are: ObjectNotFoundException - if no object was found under the given key   * RefreshFailedException - if the refresh operation failed for any other   * reason (the getMessage() will describe the failure)   *   * @param command Command to be performed when the result is received   * @param array DESCRIBE THE PARAMETER   * @param expirations DESCRIBE THE PARAMETER   */  public void refresh(final Id[] array, long[] expirations, Continuation command) {    if (logger.level <= Logger.FINE) {      logger.log("Refreshing " + array.length + " data elements");    }    GCIdSet set = new GCIdSet(realFactory);    for (int i = 0; i < array.length; i++) {      set.addId(new GCId(array[i], expirations[i]));    }    refresh(set,      new StandardContinuation(command) {        public void receiveResult(Object o) {          Object[] result = new Object[array.length];          Arrays.fill(result, Boolean.TRUE);          parent.receiveResult(result);        }      });  }  /**   * Internal method which actually does the refreshing. Should not be called by   * external applications.   *   * @param ids The ids to refresh   * @param command The command to return the result to   */  protected void refresh(final GCIdSet ids, Continuation command) {    final Logger logger = environment.getLogManager().getLogger(GCPastImpl.class, instance);    if (logger.level <= Logger.FINE) {      logger.log("REFRESH: CALLED WITH " + ids.numElements() + " ELEMENTS");    }    if (ids.numElements() == 0) {      command.receiveResult(new Object[0]);      return;    }    final Id[] array = ids.asArray();    GCId start = (GCId) array[0];    if (logger.level <= Logger.FINE) {      logger.log("REFRESH: GETTINGS ALL HANDLES OF " + start);    }    sendRequest(start.getId(), new GCLookupHandlesMessage(getUID(), start.getId(), getLocalNodeHandle(), start.getId()),      new NamedContinuation("GCLookupHandles for " + start.getId(), command) {        public void receiveResult(Object o) {          final NodeHandleSet set = (NodeHandleSet) o;          final ReplicaMap map = new ReplicaMap();          if (logger.level <= Logger.FINE) {            logger.log("REFRESH: GOT " + set + " SET OF HANDLES!");          }          endpoint.process(            new Executable() {              public Object execute() {                if (logger.level <= Logger.FINE) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -