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

📄 nodestate.java

📁 High performance DB query
💻 JAVA
字号:
/*
 * @(#)$Id: NodeState.java,v 1.1 2005/09/06 04:16:01 burkhart Exp $
 *
 * Copyright (c) 2001-2004 Regents of the University of California.
 * All rights reserved.
 *
 * This file is distributed under the terms in the attached BERKELEY-LICENSE
 * file. If you do not find these files, copies can be found by writing to:
 * Computer Science Division, Database Group, Universite of California,
 * 617 Soda Hall #1776, Berkeley, CA 94720-1776. Attention: Berkeley License
 *
 * Copyright (c) 2003-2004 Intel Corporation. All rights reserved.
 *
 * This file is distributed under the terms in the attached INTEL-LICENSE file.
 * If you do not find these files, copies can be found by writing to:
 * Intel Research Berkeley, 2150 Shattuck Avenue, Suite 1300,
 * Berkeley, CA, 94704.  Attention:  Intel License Inquiry.
 */


package pier.indexes.prefixhashtree;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.TreeMap;
import pier.indexes.IndexClient;
import services.network.Payload;
import util.BitID;

/**
 * Class NodeState
 * 
 */
public class NodeState {

	protected int state;
	protected TreeMap localStore;
	protected HashMap acksNeeded;
	protected int numLocalItems;
	protected int newLeavesReady;
	protected BitID leftLeafNode;
	protected BitID rightLeafNode;
	
	/**
	 * Constructor NodeState
	 */
	public NodeState(int state, TreeMap localStore, HashMap acksNeeded, int numLocalItems,
			int newLeavesReady, BitID leftLeafNode, BitID rightLeafNode) {
		this.state = state;
		this.localStore = localStore;
		this.acksNeeded = acksNeeded;
		this.numLocalItems = numLocalItems;
		this.newLeavesReady = newLeavesReady;
		this.leftLeafNode = leftLeafNode;
		this.rightLeafNode = rightLeafNode;
	}
	
	/**
	 * Method setState
	 * @param newState
	 */
	public void setState(int newState) {
		this.state = newState;
	}
	
	/**
	 * Method getState
	 * @return
	 */
	public int getState() {
		return this.state;
	}
	
	/**
	 * Method storeLocally
	 * @param key
	 * @param value
	 * @param iid
	 * @param lifetime
	 */
	public void storeLocally(Payload key, Payload value, int iid, long lifetime) {
		LinkedList currentValues = (LinkedList) localStore.get(key);
		if (currentValues == null) { currentValues = new LinkedList(); }
		PHTLocalStoreContainer newItem = new PHTLocalStoreContainer(value, iid);
		boolean alreadyThere = currentValues.remove(newItem);
		newItem.setLifetime(lifetime);
		currentValues.add(newItem);
		localStore.put(key, currentValues);
		if (!alreadyThere) { numLocalItems++; }
	}
	
	/**
	 * Method clearExpiredValues
	 */
	public void clearExpiredValues() {
		Iterator listOfKeys = this.getKeys();
		while (listOfKeys.hasNext()) {
			Payload theKey = (Payload) listOfKeys.next();
			Iterator iteratorOfValues = this.getValues(theKey);
			LinkedList currentValues = new LinkedList();
			while (iteratorOfValues.hasNext()) {
				PHTLocalStoreContainer nextValue = (PHTLocalStoreContainer) iteratorOfValues.next();
				if (nextValue.isExpired()) { numLocalItems--; }
				else { currentValues.add(nextValue); }
			}
			localStore.put(theKey, currentValues);
		}
	}
	
	/**
	 * Method getKeys
	 * @return
	 */
	public Iterator getKeys() {
		return localStore.keySet().iterator();
	}
	
	/**
	 * Method getValues
	 * @param key
	 * @return
	 */
	public Iterator getValues(Payload key) {
		return ((LinkedList) localStore.get(key)).iterator();
	}
	
	/**
	 * Method processAck
	 * @param messageID
	 */
	public void processAck(long messageID) {
		Object o = acksNeeded.remove(new Long(messageID));
		if (o == null) {
			throw new PHTException("Received ACK for unknown message!");
		}
	}
	
	/**
	 * Method addAckToList
	 * @param messageID
	 */
	public void addAckToList(long messageID) {
		acksNeeded.put(new Long(messageID), new Object());
	}
	
	/**
	 * Method allAcksReceived
	 * @return
	 */
	public boolean allAcksReceived() {
		return acksNeeded.isEmpty();
	}
		
	/**
	 * Method getNumLocalItems
	 * @return
	 */
	public int getNumLocalItems() {
		return this.numLocalItems;
	}
	
	/**
	 * Method anotherNewLeafReady
	 */
	public void anotherNewLeafReady() {
		this.newLeavesReady++;
	}
	/**
	 * Method getNewLeavesReady
	 * @return
	 */
	public int getNewLeavesReady() {
		return this.newLeavesReady;
	}
	
	/**
	 * Method resetNewLeavesReady
	 */
	public void resetNewLeavesReady() {
		this.newLeavesReady = 0;
	}
	
	/**
	 * Method getLeftLeafNode
	 * @return
	 */
	public BitID getLeftLeafNode() {
		return this.leftLeafNode;
	}
	
	/**
	 * Method getRightLeafNode
	 * @return
	 */
	public BitID getRightLeafNode() {
		return this.rightLeafNode;
	}
	
	/**
	 * Method setLeftLeafNode
	 */
	public void setLeftLeafNode(BitID newNode) {
		this.leftLeafNode = newNode;
	}
	
	/**
	 * Method setRightLeafNode
	 */
	public void setRightLeafNode(BitID newNode) {
		this.rightLeafNode = newNode;
	}
	
	/**
	 * Method retrieve
	 * @param keys
	 * @param client
	 */
	public void retrieve(Payload[] keys, IndexClient client) {
		
		// This will grab all keys in the range (keys[0] inclusive, keys[1] exclusive)
		Iterator listOfKeys = localStore.subMap(keys[0], keys[1]).keySet().iterator();
		
		while (listOfKeys.hasNext()) {
			Payload nextKey = (Payload) listOfKeys.next();
			LinkedList listOfValues = (LinkedList) localStore.get(nextKey);
			Iterator iteratorOfValues = listOfValues.iterator();
			PHTLocalStoreContainer nextValue = null;
			while (iteratorOfValues.hasNext()) {
				nextValue = (PHTLocalStoreContainer) iteratorOfValues.next();
				client.retrieveResult(nextKey, nextValue.getValue(), nextValue.getIID());
			}
		}
		
		// Because the submap above leaves out the last key in the range
		if (localStore.containsKey(keys[1])) {
			LinkedList listOfValues = (LinkedList) localStore.get(keys[1]);
			Iterator iteratorOfValues = listOfValues.iterator();
			PHTLocalStoreContainer nextValue = null;
			while (iteratorOfValues.hasNext()) {
				nextValue = (PHTLocalStoreContainer) iteratorOfValues.next();
				client.retrieveResult(keys[1], nextValue.getValue(), nextValue.getIID());
			}
		}
	}
	
	/**
	 * Method clearStorage
	 */
	public void clearStorage() {
		this.localStore.clear();
		this.numLocalItems = 0;
	}
}

⌨️ 快捷键说明

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