001: /*
002: * Created on 18-Mar-2006
003: *
004: * TODO To change the template for this generated file go to
005: * Window - Preferences - Java - Code Style - Code Templates
006: */
007: package com.jofti.store;
008:
009: import java.nio.ByteBuffer;
010: import java.util.Arrays;
011:
012: import com.jofti.btree.IPage;
013: import com.jofti.btree.LeafNodeEntry;
014: import com.jofti.core.IStoreManager;
015:
016: /**
017: * @author xenephon
018: *
019: * TODO To change the template for this generated type comment go to
020: * Window - Preferences - Java - Code Style - Code Templates
021: */
022: public class CachedPage implements IPage {
023:
024: /**
025: * @param pointers
026: * @param buf
027: * @param manager
028: */
029:
030: LeafNodeEntry[] entries = null;
031: IPage page = null;
032: int references = 0;
033:
034: public CachedPage(IPage page) {
035: this .page = page;
036:
037: }
038:
039: /* (non-Javadoc)
040: * @see com.jofti.store.IPage#getEntry(int)
041: */
042:
043: public LeafNodeEntry getEntry(int position) {
044: LeafNodeEntry entry = null;
045:
046: entry = entries[position];
047: if (entry == null) {
048: entry = page.getEntry(position);
049:
050: entries[position] = entry;
051: }
052: return entry;
053: }
054:
055: /* (non-Javadoc)
056: * @see com.jofti.store.IPage#setEntry(int, com.jofti.btree.StorableLeafNodeEntry)
057: */
058: public void setEntry(int position, LeafNodeEntry entry) {
059:
060: System.arraycopy(entries, position, entries, position + 1,
061: (entries.length - 1) - position);
062: entries[position] = entry;
063:
064: page.setEntry(position, entry);
065:
066: }
067:
068: /* (non-Javadoc)
069: * @see com.jofti.store.IPage#removeEntry(int)
070: */
071: public void removeEntry(int position) {
072:
073: Arrays.fill(entries, null);
074:
075: page.removeEntry(position);
076:
077: }
078:
079: /* (non-Javadoc)
080: * @see com.jofti.store.IPage#updateEntry(int, com.jofti.btree.StorableLeafNodeEntry)
081: */
082: public void updateEntry(int location, LeafNodeEntry entry) {
083: entries[location] = entry;
084:
085: page.updateEntry(location, entry);
086:
087: }
088:
089: /* (non-Javadoc)
090: * @see com.jofti.store.IPage#copyBuffer(java.nio.ByteBuffer)
091: */
092: public ByteBuffer copyBuffer(ByteBuffer newBuf) {
093: return page.copyBuffer(newBuf);
094: }
095:
096: /* (non-Javadoc)
097: * @see com.jofti.store.IPage#reset()
098: */
099: public void reset() {
100: for (int i = 0; i < entries.length; i++) {
101: entries[i] = null;
102: }
103: page.reset();
104:
105: }
106:
107: /* (non-Javadoc)
108: * @see com.jofti.store.IPage#getBuffer()
109: */
110: public ByteBuffer getBuffer() {
111:
112: return page.getBuffer();
113: }
114:
115: /* (non-Javadoc)
116: * @see com.jofti.store.IPage#getPointers()
117: */
118: public int[] getPointers() {
119:
120: return page.getPointers();
121: }
122:
123: /* (non-Javadoc)
124: * @see com.jofti.store.IPage#setManager(com.jofti.core.IStoreManager)
125: */
126: public void setManager(IStoreManager manager) {
127: page.setManager(manager);
128:
129: }
130:
131: public synchronized void acquireReference() {
132: ++references;
133: }
134:
135: public synchronized void releaseReference() {
136: --references;
137: }
138:
139: public synchronized boolean hasReferences() {
140: return references > 0;
141: }
142: }
|