001: /**
002: * com.mckoi.store.ScatteringFileStore 24 Jan 2003
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.store;
024:
025: import java.io.IOException;
026:
027: /**
028: * An implementation of AbstractStore that persists to an underlying data
029: * format via a robust journalling system that supports check point and crash
030: * recovery. Note that this object is a bridge between the Store API and the
031: * journalled behaviour defined in LoggingBufferManager, JournalledSystem and
032: * the StoreDataAccessor implementations.
033: * <p>
034: * Note that access to the resources is abstracted via a 'resource_name'
035: * string. The LoggingBufferManager object converts the resource name into a
036: * concrete object that accesses the actual data.
037: *
038: * @author Tobias Downer
039: */
040:
041: public final class JournalledFileStore extends AbstractStore {
042:
043: /**
044: * The name of the resource.
045: */
046: private final String resource_name;
047:
048: /**
049: * The buffering strategy for accessing the data in an underlying file.
050: */
051: private final LoggingBufferManager buffer_manager;
052:
053: /**
054: * The JournalledResource object that's used to journal all read/write
055: * operations to the above 'store_accessor'.
056: */
057: private JournalledResource store_resource;
058:
059: /**
060: * Constructs the ScatteringFileStore.
061: */
062: public JournalledFileStore(String resource_name,
063: LoggingBufferManager buffer_manager, boolean read_only) {
064: super (read_only);
065: this .resource_name = resource_name;
066: this .buffer_manager = buffer_manager;
067:
068: // Create the store resource object for this resource name
069: this .store_resource = buffer_manager
070: .createResource(resource_name);
071: }
072:
073: // ---------- JournalledFileStore methods ----------
074:
075: /**
076: * Deletes this store from the file system. This operation should only be
077: * used when the store is NOT open.
078: */
079: public boolean delete() throws IOException {
080: store_resource.delete();
081: return true;
082: }
083:
084: /**
085: * Returns true if this store exists in the file system.
086: */
087: public boolean exists() throws IOException {
088: return store_resource.exists();
089: }
090:
091: public void lockForWrite() {
092: try {
093: buffer_manager.lockForWrite();
094: } catch (InterruptedException e) {
095: throw new Error("Interrupted: " + e.getMessage());
096: }
097: }
098:
099: public void unlockForWrite() {
100: buffer_manager.unlockForWrite();
101: }
102:
103: // ---------- Implemented from AbstractStore ----------
104:
105: /**
106: * Internally opens the backing area. If 'read_only' is true then the
107: * store is opened in read only mode.
108: */
109: protected void internalOpen(boolean read_only) throws IOException {
110: store_resource.open(read_only);
111: }
112:
113: /**
114: * Internally closes the backing area.
115: */
116: protected void internalClose() throws IOException {
117: buffer_manager.close(store_resource);
118: }
119:
120: protected int readByteFrom(long position) throws IOException {
121: return buffer_manager.readByteFrom(store_resource, position);
122: }
123:
124: protected int readByteArrayFrom(long position, byte[] buf, int off,
125: int len) throws IOException {
126: return buffer_manager.readByteArrayFrom(store_resource,
127: position, buf, off, len);
128: }
129:
130: protected void writeByteTo(long position, int b) throws IOException {
131: buffer_manager.writeByteTo(store_resource, position, b);
132: }
133:
134: protected void writeByteArrayTo(long position, byte[] buf, int off,
135: int len) throws IOException {
136: buffer_manager.writeByteArrayTo(store_resource, position, buf,
137: off, len);
138: }
139:
140: protected long endOfDataAreaPointer() throws IOException {
141: return buffer_manager.getDataAreaSize(store_resource);
142: }
143:
144: protected void setDataAreaSize(long new_size) throws IOException {
145: buffer_manager.setDataAreaSize(store_resource, new_size);
146: }
147:
148: // For diagnosis
149:
150: public String toString() {
151: return "[ JournalledFileStore: " + resource_name + " ]";
152: }
153:
154: }
|