📄 simplestoragemanager.java
字号:
/* * @(#)$Id: SimpleStorageManager.java,v 1.8 2004/07/22 19:43:45 huebsch 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 overlay.storage.simple;import java.util.HashMap;import org.apache.log4j.Logger;import overlay.storage.StorageManager;import services.Output;import services.network.Payload;import services.stats.StatCollector;import services.stats.StatVars;import util.BitID;import util.logging.StructuredLogMessage;import util.network.serialization.SerializationManager;/** * A basic implementation for a storage manager. All data is stored in a hashmap. */public class SimpleStorageManager implements StorageManager { private static Logger logger = Logger.getLogger(SimpleStorageManager.class); private HashMap storedItems; private int curSize, curItems; /** * Creates a new instance of the storage manager */ public SimpleStorageManager() { storedItems = new HashMap(); curSize = 0; curItems = 0; } /** * Adds an item to the store. If an item already exists with same storageID, it is overwritten * @param storageID unique identifer associated with the item (needed for retrieval) * @param item the item to be stored */ public void store(BitID storageID, Payload item) { if (Output.debuggingEnabled) { logger.debug(new StructuredLogMessage(item, "Beginning Storage", new Object[]{"d", storageID}, null)); } storedItems.put(storageID, item); curSize += SerializationManager.getPayloadSize(item); curItems++; StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.BYTES, curSize); StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.NUMBER, curItems); StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.STORES, SerializationManager.getPayloadSize(item)); if (Output.debuggingEnabled) { logger.debug(new StructuredLogMessage(item, "Completed Storage", null, null)); } } /** * Retrieves an item from the store if it exists * @param storageID unique identifier that was used to store item * @return the item associated with the identifier if it is in the store, null otherwise */ public Payload retrieve(BitID storageID) { if (Output.debuggingEnabled) { logger.debug(new StructuredLogMessage(storageID, "Beginning Retrieval", new Object[]{"d", storageID}, null)); } Payload item = (Payload) storedItems.get(storageID); if (item != null) { StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.RETRIEVES, SerializationManager.getPayloadSize(item)); } else { StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.NOTFOUND, 0); } if (Output.debuggingEnabled) { logger.debug(new StructuredLogMessage(item, "Completed Retrieval", null, new Object[]{"p", item})); } return item; } /** * Removes an item from the store if it exists * @param storageID unique identifier that was used to store item */ public void remove(BitID storageID) { if (Output.debuggingEnabled) { logger.debug(new StructuredLogMessage(storageID, "Beginning Removal", new Object[]{"d", storageID}, null)); } Payload item = (Payload) storedItems.remove(storageID); if (item != null) { curSize -= SerializationManager.getPayloadSize(item); curItems--; StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.BYTES, curSize); StatCollector.addSample(StatVars.STORAGE, StatVars.STORAGE_MANAGER, StatVars.NUMBER, curItems); } if (Output.debuggingEnabled) { logger.debug(new StructuredLogMessage(item, "Completed Removal", null, new Object[]{"p", item})); } } /** Removes all data */ public void reset() { storedItems = new HashMap(); curSize = 0; curItems = 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -