001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.store;
031:
032: import com.caucho.log.Log;
033: import com.caucho.util.FreeList;
034: import com.caucho.util.L10N;
035: import com.caucho.util.SyncCacheListener;
036:
037: import java.io.IOException;
038: import java.util.logging.Level;
039: import java.util.logging.Logger;
040:
041: /**
042: * Represents a versioned row
043: */
044: abstract public class Block implements SyncCacheListener {
045: private static final Logger log = Log.open(Block.class);
046: private static final L10N L = new L10N(Block.class);
047:
048: protected static final FreeList<byte[]> _freeBuffers = new FreeList<byte[]>(
049: 4);
050:
051: private final Store _store;
052: private final long _blockId;
053:
054: private final Lock _lock;
055:
056: private int _useCount = 1;
057:
058: private boolean _isFlushDirtyOnCommit;
059: private boolean _isValid;
060:
061: private int _dirtyMin = Store.BLOCK_SIZE;
062: private int _dirtyMax;
063:
064: Block(Store store, long blockId) {
065: store.validateBlockId(blockId);
066:
067: _store = store;
068: _blockId = blockId;
069:
070: _lock = new Lock("block:" + store.getName() + ":" + _blockId);
071:
072: _isFlushDirtyOnCommit = _store.isFlushDirtyBlocksOnCommit();
073:
074: if (log.isLoggable(Level.FINEST))
075: log.finest(this + " create");
076:
077: //System.out.println(this + " CREATE");
078: }
079:
080: /**
081: * Returns true if the block should be flushed on a commit.
082: */
083: public boolean isFlushDirtyOnCommit() {
084: return _isFlushDirtyOnCommit;
085: }
086:
087: /**
088: * True if the block should be flushed on a commit.
089: */
090: public void setFlushDirtyOnCommit(boolean isFlush) {
091: _isFlushDirtyOnCommit = isFlush;
092: }
093:
094: /**
095: * Allocates the block for a query.
096: */
097: public boolean allocate() {
098: synchronized (this ) {
099: if (getBuffer() == null)
100: return false;
101:
102: _useCount++;
103:
104: if (log.isLoggable(Level.FINEST))
105: log.finest(this + " allocate");
106:
107: //System.out.println(this + " ALLOCATE " + _useCount);
108:
109: if (_useCount > 32 && log.isLoggable(Level.FINE)) {
110: Thread.dumpStack();
111: log.fine("using " + this + " " + _useCount + " times");
112: }
113: }
114:
115: return true;
116: }
117:
118: /**
119: * Returns the block's table.
120: */
121: Store getStore() {
122: return _store;
123: }
124:
125: /**
126: * Returns the block's id.
127: */
128: public long getBlockId() {
129: return _blockId;
130: }
131:
132: public Lock getLock() {
133: return _lock;
134: }
135:
136: /**
137: * Returns the block's buffer.
138: */
139: abstract public byte[] getBuffer();
140:
141: /**
142: * Reads into the block.
143: */
144: public void read() throws IOException {
145: synchronized (this ) {
146: if (!_isValid) {
147: if (log.isLoggable(Level.FINEST))
148: log.finest("read db-block " + this );
149:
150: _store.readBlock(_blockId & Store.BLOCK_MASK,
151: getBuffer(), 0, Store.BLOCK_SIZE);
152: _isValid = true;
153:
154: _dirtyMin = Store.BLOCK_SIZE;
155: _dirtyMax = 0;
156: }
157: }
158: }
159:
160: /**
161: * Handle any database writes necessary at commit time. If
162: * isFlushDirtyOnCommit() is true, this will write the data to
163: * the backing file.
164: */
165: public void commit() throws IOException {
166: if (!_isFlushDirtyOnCommit)
167: return;
168: else
169: write();
170: }
171:
172: /**
173: * Forces a write of the data (should be private?)
174: */
175: public void write() throws IOException {
176: getLock().waitForCommit();
177:
178: int dirtyMin = 0;
179: int dirtyMax = 0;
180:
181: synchronized (this ) {
182: dirtyMin = _dirtyMin;
183: _dirtyMin = Store.BLOCK_SIZE;
184:
185: dirtyMax = _dirtyMax;
186: _dirtyMax = 0;
187:
188: if (dirtyMax <= dirtyMin)
189: return;
190:
191: _useCount++;
192: _isValid = true;
193: }
194:
195: try {
196: if (log.isLoggable(Level.FINEST))
197: log.finest("write db-block " + this + " [" + dirtyMin
198: + ", " + dirtyMax + "]");
199:
200: //System.out.println(this + " WRITE_BEGIN");
201: writeImpl(dirtyMin, dirtyMax - dirtyMin);
202: //System.out.println(this + " WRITE_END");
203: } finally {
204: free();
205: }
206: }
207:
208: /**
209: * Write the dirty block.
210: */
211: protected void writeImpl(int offset, int length) throws IOException {
212: _store.writeBlock((_blockId & Store.BLOCK_MASK) + offset,
213: getBuffer(), offset, length);
214: }
215:
216: /**
217: * Marks the block's data as invalid.
218: */
219: public void invalidate() {
220: synchronized (this ) {
221: if (_dirtyMin < _dirtyMax)
222: throw new IllegalStateException();
223:
224: _isValid = false;
225: _dirtyMin = Store.BLOCK_SIZE;
226: _dirtyMax = 0;
227: }
228: }
229:
230: /**
231: * Marks the data as valid.
232: */
233: void validate() {
234: _isValid = true;
235: }
236:
237: /**
238: * Marks the block's data as dirty
239: */
240: public void setDirty(int min, int max) {
241: if (Store.BLOCK_SIZE < max)
242: Thread.dumpStack();
243:
244: synchronized (this ) {
245: _isValid = true;
246:
247: if (min < _dirtyMin)
248: _dirtyMin = min;
249:
250: if (_dirtyMax < max)
251: _dirtyMax = max;
252: }
253: }
254:
255: /**
256: * Returns true if the block needs writing
257: */
258: public boolean isDirty() {
259: return _dirtyMin < _dirtyMax;
260: }
261:
262: /**
263: * Return true if this is a free block.
264: */
265: public boolean isFree() {
266: return _useCount == 0;
267: }
268:
269: /**
270: * Frees a block from a query.
271: */
272: public final void free() {
273: synchronized (this ) {
274: if (log.isLoggable(Level.FINEST))
275: log.finest(this + " free");
276:
277: _useCount--;
278:
279: //System.out.println(this + " FREE " + _useCount);
280:
281: if (_useCount > 0)
282: return;
283:
284: // If the block is clean, just discard it
285: if (_dirtyMax <= _dirtyMin) {
286: freeImpl();
287:
288: return;
289: }
290: }
291:
292: // dirty blocks get queued for writing
293: BlockManager.getBlockManager().addLruDirtyWriteBlock(this );
294: }
295:
296: /**
297: * Called when the block is removed from the cache.
298: */
299: public final void syncRemoveEvent() {
300: free();
301: }
302:
303: /**
304: * Called when the block is removed from the cache.
305: */
306: // called only from BlockManagerWriter.run()
307: void close() {
308: synchronized (this ) {
309: if (_dirtyMin < _dirtyMax) {
310: try {
311: write();
312: } catch (Throwable e) {
313: log.log(Level.FINER, e.toString(), e);
314: }
315: }
316:
317: if (_useCount <= 0)
318: freeImpl();
319:
320: if (log.isLoggable(Level.FINEST))
321: log.finest("db-block remove " + this );
322: }
323: }
324:
325: /**
326: * Frees any resources.
327: */
328: protected void freeImpl() {
329: }
330:
331: public String toString() {
332: return "Block[" + _store + "," + _blockId / Store.BLOCK_SIZE
333: + "]";
334: }
335: }
|