001: package com.quadcap.sql.file;
002:
003: /* Copyright 1997 - 2003 Quadcap Software. All rights reserved.
004: *
005: * This software is distributed under the Quadcap Free Software License.
006: * This software may be used or modified for any purpose, personal or
007: * commercial. Open Source redistributions are permitted. Commercial
008: * redistribution of larger works derived from, or works which bundle
009: * this software requires a "Commercial Redistribution License"; see
010: * http://www.quadcap.com/purchase.
011: *
012: * Redistributions qualify as "Open Source" under one of the following terms:
013: *
014: * Redistributions are made at no charge beyond the reasonable cost of
015: * materials and delivery.
016: *
017: * Redistributions are accompanied by a copy of the Source Code or by an
018: * irrevocable offer to provide a copy of the Source Code for up to three
019: * years at the cost of materials and delivery. Such redistributions
020: * must allow further use, modification, and redistribution of the Source
021: * Code under substantially the same terms as this license.
022: *
023: * Redistributions of source code must retain the copyright notices as they
024: * appear in each source code file, these license terms, and the
025: * disclaimer/limitation of liability set forth as paragraph 6 below.
026: *
027: * Redistributions in binary form must reproduce this Copyright Notice,
028: * these license terms, and the disclaimer/limitation of liability set
029: * forth as paragraph 6 below, in the documentation and/or other materials
030: * provided with the distribution.
031: *
032: * The Software is provided on an "AS IS" basis. No warranty is
033: * provided that the Software is free of defects, or fit for a
034: * particular purpose.
035: *
036: * Limitation of Liability. Quadcap Software shall not be liable
037: * for any damages suffered by the Licensee or any third party resulting
038: * from use of the Software.
039: */
040:
041: import java.io.IOException;
042:
043: import com.quadcap.util.DListItem;
044:
045: /**
046: * The construction of these objects needs to be managed by the
047: * cache object. They are initialized using the
048: *
049: * @author Stan Bailes
050: */
051: public abstract class Cacheable extends Object {
052: /** has object been modified while in the cache? */
053: boolean dirty = false;
054:
055: /** Is this cache item read only? */
056: boolean readOnly = false;
057:
058: /**
059: * We reference-count the cache items to know when it's ok to flush
060: * older items to make room for new ones. We should probably keep
061: * some statistics which keep track of the percentage of cache items
062: * currently "in-use"; i.e., with <code>refCount > 0</code>
063: */
064: int refCount = 0;
065:
066: /** Back pointer to my place in the LRU list. */
067: DListItem me;
068:
069: /**
070: * The key used to locate this item in the underlying store, as well
071: * as in the cache itself.
072: */
073: protected long key;
074:
075: protected Object store;
076:
077: /**
078: * Initialization and (recycling)
079: */
080: public void init(Object store, long key) throws IOException {
081: this .store = store;
082: this .key = key;
083: dirty = false;
084: }
085:
086: /**
087: * Return the cache to which this cacheable belongs.
088: */
089: public Object getStore() {
090: return store;
091: }
092:
093: /**
094: * Am I read only?
095: */
096: public boolean isReadOnly() {
097: return readOnly;
098: }
099:
100: /**
101: * Set the 'read only' flag
102: */
103: public void setReadOnly(boolean v) {
104: this .readOnly = v;
105: }
106:
107: /**
108: * Read the dirty bit.
109: */
110: public final boolean isDirty() {
111: return dirty;
112: }
113:
114: /**
115: * Set the dirty bit.
116: */
117: public void setDirty(boolean d) {
118: if (readOnly) {
119: throw new RuntimeException(
120: "Attempt to modify read-only item");
121: }
122: dirty = d;
123: }
124:
125: /**
126: * Read the reference count
127: */
128: public final int getRefCount() {
129: return refCount;
130: }
131:
132: /**
133: * Set the reference count
134: */
135: public/* synchronized */final void setRefCount(int x) {
136: refCount = x;
137: }
138:
139: /**
140: * Increment the reference count
141: */
142: public/* synchronized */final void incrRefCount() {
143: refCount++;
144: }
145:
146: /**
147: * Decrement the reference count
148: */
149: public/* synchronized */final void decrRefCount() {
150: refCount--;
151: }
152:
153: /**
154: * Set the LRU back pointer.
155: */
156: public final void setDListItem(DListItem d) {
157: me = d;
158: }
159:
160: /**
161: * Get the LRU back pointer.
162: */
163: public final DListItem getDListItem() {
164: return me;
165: }
166:
167: /**
168: * Get the cache item's key.
169: */
170: public long getKey() {
171: return key;
172: }
173:
174: /**
175: * Set the cache item's key.
176: */
177: public void setKey(long key) {
178: this .key = key;
179: }
180:
181: /**
182: * Get the cache item's data.
183: */
184: abstract public Object getData();
185:
186: /**
187: * Set the cache item's data.
188: */
189: abstract public void setData(Object data);
190:
191: /**
192: * Flush this item and clear the dirty bit.
193: */
194: public void flush() throws IOException {
195: dirty = false;
196: }
197: }
|