001: // StoreState.java
002: // $Id: StoreState.java,v 1.14 2000/08/16 21:38:04 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005:
006: package org.w3c.www.protocol.http.cache;
007:
008: import java.util.Hashtable;
009:
010: import org.w3c.tools.resources.Attribute;
011: import org.w3c.tools.resources.AttributeHolder;
012: import org.w3c.tools.resources.AttributeRegistry;
013: import org.w3c.tools.resources.IntegerAttribute;
014: import org.w3c.tools.resources.LongAttribute;
015:
016: /**
017: * @version $Revision: 1.14 $
018: * @author Benoît Mahé (bmahe@w3.org)
019: */
020: public class StoreState extends AttributeHolder {
021:
022: /**
023: * Attribute index - The current generation number (id)
024: */
025: protected static int ATTR_CURRENT_GENERATION = -1;
026:
027: /**
028: * Attribute index - The nb of generations (mem+disk)
029: */
030: protected static int ATTR_NB_GENERATION = -1;
031:
032: /**
033: * Attribute index - The the current cache size
034: */
035: protected static int ATTR_BYTE_COUNT = -1;
036:
037: /**
038: * Attribute index - The the current store count
039: */
040: protected static int ATTR_STORE_COUNT = -1;
041:
042: /**
043: * Attribute index - The current number of CachedResource in memory
044: */
045: protected static int ATTR_CR_COUNT = -1;
046:
047: /**
048: * Attribute index - The number used to store the file
049: */
050: protected static int ATTR_ENTRY_NUM = -1;
051:
052: static {
053: Attribute a = null;
054: Class c = null;
055: try {
056: c = Class
057: .forName("org.w3c.www.protocol.http.cache.StoreState");
058: } catch (Exception ex) {
059: ex.printStackTrace();
060: System.exit(1);
061: }
062:
063: // Declare the current generation number (id)
064: a = new IntegerAttribute("current-generation", null,
065: Attribute.COMPUTED);
066: ATTR_CURRENT_GENERATION = AttributeRegistry.registerAttribute(
067: c, a);
068:
069: // Declare the nb of generations (mem+disk)
070: a = new IntegerAttribute("nb-generation", null,
071: Attribute.COMPUTED);
072: ATTR_NB_GENERATION = AttributeRegistry.registerAttribute(c, a);
073:
074: // Declare the
075: a = new IntegerAttribute("entry-num", null, Attribute.COMPUTED);
076: ATTR_ENTRY_NUM = AttributeRegistry.registerAttribute(c, a);
077:
078: // Declare the
079: a = new LongAttribute("byte-count", null, Attribute.COMPUTED);
080: ATTR_BYTE_COUNT = AttributeRegistry.registerAttribute(c, a);
081:
082: // Declare the
083: a = new LongAttribute("store-count", null, Attribute.COMPUTED);
084: ATTR_STORE_COUNT = AttributeRegistry.registerAttribute(c, a);
085:
086: // Declare the
087: a = new IntegerAttribute("cr-count", null, Attribute.COMPUTED);
088: ATTR_CR_COUNT = AttributeRegistry.registerAttribute(c, a);
089:
090: }
091:
092: private int currentGeneration;
093: private int nbGeneration;
094: private int entryNum;
095:
096: private long byteCount;
097: private long storeCount;
098: private int crCount;
099:
100: /**
101: * Synchronize the state
102: */
103: public void sync() {
104: setValue(ATTR_CURRENT_GENERATION,
105: new Integer(currentGeneration));
106: setValue(ATTR_NB_GENERATION, new Integer(nbGeneration));
107: setValue(ATTR_ENTRY_NUM, new Integer(entryNum));
108:
109: setValue(ATTR_BYTE_COUNT, new Long(byteCount));
110: setValue(ATTR_STORE_COUNT, new Long(storeCount));
111: setValue(ATTR_CR_COUNT, new Integer(crCount));
112: }
113:
114: /**
115: * Modify the current generation value
116: * @return the new value
117: */
118: public synchronized int incrCurrentGeneration() {
119: return ++currentGeneration;
120: }
121:
122: /**
123: * Get the current Generation number
124: * @return an integer
125: */
126: public synchronized int getCurrentGeneration() {
127: return currentGeneration;
128: }
129:
130: /**
131: * Get the number of generation
132: * @return an integer
133: */
134: public synchronized int getNbGeneration() {
135: return nbGeneration;
136: }
137:
138: /**
139: * Get the current cached byte count.
140: * @return a long
141: */
142: public synchronized long getByteCount() {
143: return byteCount;
144: }
145:
146: /**
147: * Get the current stored byte count
148: * @return a long
149: */
150: public synchronized long getStoreCount() {
151: return storeCount;
152: }
153:
154: /**
155: * Get the current CachedResource count
156: * @return a long
157: */
158: public synchronized int getCrCount() {
159: return crCount;
160: }
161:
162: /**
163: * Modify the current entry number
164: * @return the new value
165: */
166: public synchronized int incrEntryNum() {
167: return ++entryNum;
168: }
169:
170: /**
171: * Get the current entry number
172: * @return an int
173: */
174: public synchronized int getEntryNum() {
175: return entryNum;
176: }
177:
178: /**
179: * increment the current generation number
180: * @return the new value
181: */
182: public synchronized int incrGenerationNum() {
183: return ++nbGeneration;
184: }
185:
186: /**
187: * decrement the current generation number
188: * @return an int
189: */
190: public synchronized int decrGenerationNum() {
191: return --nbGeneration;
192: }
193:
194: /**
195: * Notify the store state, a generation has just been created
196: * @param cg the new generation
197: */
198: public synchronized void notifyGenerationCreated(CacheGeneration cg) {
199: storeCount += cg.getStoredByteCount();
200: byteCount += cg.getCachedByteCount();
201: crCount += cg.getCRCount();
202: cg.setLoaded(true);
203: nbGeneration++;
204: }
205:
206: /**
207: * Notify the store state, a generation has just been loaded
208: * @param cg the loaded generation
209: */
210: public synchronized void notifyGenerationLoaded(CacheGeneration cg) {
211: byteCount += cg.getCachedByteCount();
212: crCount += cg.getCRCount();
213: }
214:
215: /**
216: * Notify the store state, a generation has just been unloaded
217: * @param cg the unloaded generation
218: */
219: public synchronized void notifyGenerationUnloaded(CacheGeneration cg) {
220: byteCount -= cg.getCachedByteCount();
221: crCount -= cg.getCRCount();
222: cg.unload();
223: }
224:
225: /**
226: * Notify the store state, a resource has just been added
227: * @param cr the added resource
228: */
229: public synchronized void notifyResourceAdded(CachedResource cr,
230: long oldsize) {
231: long size = cr.getCurrentLength();
232: storeCount += (size - oldsize);
233: byteCount += size;
234: crCount++;
235: }
236:
237: /**
238: * Notify the store state, a resource has just been updated
239: * @param cr the updated resource
240: */
241: public synchronized void notifyResourceReplaced(CachedResource cr,
242: long oldsize) {
243: long size = cr.getCurrentLength();
244: long delta = size - oldsize;
245: storeCount += size;
246: byteCount += delta;
247: }
248:
249: /**
250: * Notify the store state, a resource has been removed (from cache)
251: * @param cr the resource removed
252: */
253: public synchronized void notifyResourceRemoved(CachedResource cr) {
254: byteCount -= cr.getCurrentLength();
255: crCount--;
256: }
257:
258: /**
259: * Notify the store state, a resource has been moved in the
260: * "to be deleted" list
261: * @param cr the resource to delete
262: */
263: public synchronized void notifyResourceToBeDeleted(CachedResource cr) {
264: byteCount -= cr.getCurrentLength();
265: crCount--;
266: }
267:
268: /**
269: * Notify the store state, a resource has just been deleted
270: * @param cr the deleted resource
271: */
272: public synchronized void notifyResourceDeleted(CachedResource cr) {
273: storeCount -= cr.getCurrentLength();
274: }
275:
276: /**
277: * The basic initialization
278: * @param values the values
279: */
280: public void pickleValues(Hashtable defs) {
281: super .pickleValues(defs);
282: currentGeneration = getInt(ATTR_CURRENT_GENERATION, 0);
283: nbGeneration = getInt(ATTR_NB_GENERATION, 0);
284: entryNum = getInt(ATTR_ENTRY_NUM, 0);
285: byteCount = getLong(ATTR_BYTE_COUNT, 0);
286: storeCount = getLong(ATTR_STORE_COUNT, 0);
287: crCount = getInt(ATTR_CR_COUNT, 0);
288: }
289:
290: public String toString() {
291: StringBuffer buffer = new StringBuffer();
292: buffer.append("\n>>> Store State <<<");
293: buffer.append("\n Store count : ").append(storeCount);
294: buffer.append("\n Byte count : ").append(byteCount);
295: buffer.append("\n CR count : ").append(crCount);
296: buffer.append("\n Current Generation : ").append(
297: currentGeneration);
298: buffer.append("\n Number of generation : ").append(
299: nbGeneration);
300: buffer.append("\n Entry number : ").append(entryNum);
301: return buffer.toString();
302: }
303:
304: /**
305: * Constructor.
306: * @param file the store state file
307: */
308: public StoreState() {
309: }
310: }
|