![]() |
Quadcap Embeddable Database |
00001 package com.quadcap.sql.file; 00002 00003 /* Copyright 1997 - 2003 Quadcap Software. All rights reserved. 00004 * 00005 * This software is distributed under the Quadcap Free Software License. 00006 * This software may be used or modified for any purpose, personal or 00007 * commercial. Open Source redistributions are permitted. Commercial 00008 * redistribution of larger works derived from, or works which bundle 00009 * this software requires a "Commercial Redistribution License"; see 00010 * http://www.quadcap.com/purchase. 00011 * 00012 * Redistributions qualify as "Open Source" under one of the following terms: 00013 * 00014 * Redistributions are made at no charge beyond the reasonable cost of 00015 * materials and delivery. 00016 * 00017 * Redistributions are accompanied by a copy of the Source Code or by an 00018 * irrevocable offer to provide a copy of the Source Code for up to three 00019 * years at the cost of materials and delivery. Such redistributions 00020 * must allow further use, modification, and redistribution of the Source 00021 * Code under substantially the same terms as this license. 00022 * 00023 * Redistributions of source code must retain the copyright notices as they 00024 * appear in each source code file, these license terms, and the 00025 * disclaimer/limitation of liability set forth as paragraph 6 below. 00026 * 00027 * Redistributions in binary form must reproduce this Copyright Notice, 00028 * these license terms, and the disclaimer/limitation of liability set 00029 * forth as paragraph 6 below, in the documentation and/or other materials 00030 * provided with the distribution. 00031 * 00032 * The Software is provided on an "AS IS" basis. No warranty is 00033 * provided that the Software is free of defects, or fit for a 00034 * particular purpose. 00035 * 00036 * Limitation of Liability. Quadcap Software shall not be liable 00037 * for any damages suffered by the Licensee or any third party resulting 00038 * from use of the Software. 00039 */ 00040 00041 import java.io.IOException; 00042 00043 import com.quadcap.util.DListItem; 00044 00045 /** 00046 * The construction of these objects needs to be managed by the 00047 * cache object. They are initialized using the 00048 * 00049 * @author Stan Bailes 00050 */ 00051 public abstract class Cacheable extends Object { 00052 /** has object been modified while in the cache? */ 00053 boolean dirty = false; 00054 00055 /** Is this cache item read only? */ 00056 boolean readOnly = false; 00057 00058 /** 00059 * We reference-count the cache items to know when it's ok to flush 00060 * older items to make room for new ones. We should probably keep 00061 * some statistics which keep track of the percentage of cache items 00062 * currently "in-use"; i.e., with <code>refCount > 0</code> 00063 */ 00064 int refCount = 0; 00065 00066 /** Back pointer to my place in the LRU list. */ 00067 DListItem me; 00068 00069 /** 00070 * The key used to locate this item in the underlying store, as well 00071 * as in the cache itself. 00072 */ 00073 protected long key; 00074 00075 protected Object store; 00076 00077 /** 00078 * Initialization and (recycling) 00079 */ 00080 public void init(Object store, long key) throws IOException { 00081 this.store = store; 00082 this.key = key; 00083 dirty = false; 00084 } 00085 00086 /** 00087 * Return the cache to which this cacheable belongs. 00088 */ 00089 public Object getStore() { return store; } 00090 00091 /** 00092 * Am I read only? 00093 */ 00094 public boolean isReadOnly() { return readOnly; } 00095 00096 /** 00097 * Set the 'read only' flag 00098 */ 00099 public void setReadOnly(boolean v) { this.readOnly = v; } 00100 00101 /** 00102 * Read the dirty bit. 00103 */ 00104 public final boolean isDirty() { return dirty; } 00105 00106 /** 00107 * Set the dirty bit. 00108 */ 00109 public void setDirty(boolean d) { 00110 if (readOnly) { 00111 throw new RuntimeException("Attempt to modify read-only item"); 00112 } 00113 dirty = d; 00114 } 00115 00116 /** 00117 * Read the reference count 00118 */ 00119 public final int getRefCount() { return refCount; } 00120 00121 /** 00122 * Set the reference count 00123 */ 00124 public /* synchronized */ final void setRefCount(int x) { refCount = x; } 00125 00126 /** 00127 * Increment the reference count 00128 */ 00129 public /* synchronized */ final void incrRefCount() { 00130 refCount++; 00131 } 00132 00133 /** 00134 * Decrement the reference count 00135 */ 00136 public /* synchronized */ final void decrRefCount() { refCount--; } 00137 00138 /** 00139 * Set the LRU back pointer. 00140 */ 00141 public final void setDListItem(DListItem d) { me = d; } 00142 00143 /** 00144 * Get the LRU back pointer. 00145 */ 00146 public final DListItem getDListItem() { return me; } 00147 00148 /** 00149 * Get the cache item's key. 00150 */ 00151 public long getKey() { return key; } 00152 00153 /** 00154 * Set the cache item's key. 00155 */ 00156 public void setKey(long key) { this.key = key; } 00157 00158 /** 00159 * Get the cache item's data. 00160 */ 00161 abstract public Object getData(); 00162 00163 /** 00164 * Set the cache item's data. 00165 */ 00166 abstract public void setData(Object data); 00167 00168 /** 00169 * Flush this item and clear the dirty bit. 00170 */ 00171 public void flush() throws IOException { 00172 dirty = false; 00173 } 00174 }