KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > store > JournalledFileStore


1 /**
2  * com.mckoi.store.ScatteringFileStore 24 Jan 2003
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.store;
26
27 import java.io.IOException JavaDoc;
28
29 /**
30  * An implementation of AbstractStore that persists to an underlying data
31  * format via a robust journalling system that supports check point and crash
32  * recovery. Note that this object is a bridge between the Store API and the
33  * journalled behaviour defined in LoggingBufferManager, JournalledSystem and
34  * the StoreDataAccessor implementations.
35  * <p>
36  * Note that access to the resources is abstracted via a 'resource_name'
37  * string. The LoggingBufferManager object converts the resource name into a
38  * concrete object that accesses the actual data.
39  *
40  * @author Tobias Downer
41  */

42
43 public final class JournalledFileStore extends AbstractStore {
44
45   /**
46    * The name of the resource.
47    */

48   private final String JavaDoc resource_name;
49   
50   /**
51    * The buffering strategy for accessing the data in an underlying file.
52    */

53   private final LoggingBufferManager buffer_manager;
54
55   /**
56    * The JournalledResource object that's used to journal all read/write
57    * operations to the above 'store_accessor'.
58    */

59   private JournalledResource store_resource;
60   
61   
62   /**
63    * Constructs the ScatteringFileStore.
64    */

65   public JournalledFileStore(String JavaDoc resource_name,
66                              LoggingBufferManager buffer_manager,
67                              boolean read_only) {
68     super(read_only);
69     this.resource_name = resource_name;
70     this.buffer_manager = buffer_manager;
71
72     // Create the store resource object for this resource name
73
this.store_resource = buffer_manager.createResource(resource_name);
74   }
75
76
77   // ---------- JournalledFileStore methods ----------
78

79   /**
80    * Deletes this store from the file system. This operation should only be
81    * used when the store is NOT open.
82    */

83   public boolean delete() throws IOException JavaDoc {
84     store_resource.delete();
85     return true;
86   }
87
88   /**
89    * Returns true if this store exists in the file system.
90    */

91   public boolean exists() throws IOException JavaDoc {
92     return store_resource.exists();
93   }
94
95   public void lockForWrite() {
96     try {
97       buffer_manager.lockForWrite();
98     }
99     catch (InterruptedException JavaDoc e) {
100       throw new Error JavaDoc("Interrupted: " + e.getMessage());
101     }
102   }
103
104   public void unlockForWrite() {
105     buffer_manager.unlockForWrite();
106   }
107   
108   // ---------- Implemented from AbstractStore ----------
109

110   /**
111    * Internally opens the backing area. If 'read_only' is true then the
112    * store is opened in read only mode.
113    */

114   protected void internalOpen(boolean read_only) throws IOException JavaDoc {
115     store_resource.open(read_only);
116   }
117   
118   /**
119    * Internally closes the backing area.
120    */

121   protected void internalClose() throws IOException JavaDoc {
122     buffer_manager.close(store_resource);
123   }
124
125
126   protected int readByteFrom(long position) throws IOException JavaDoc {
127     return buffer_manager.readByteFrom(store_resource, position);
128   }
129   
130   protected int readByteArrayFrom(long position,
131                            byte[] buf, int off, int len) throws IOException JavaDoc {
132     return buffer_manager.readByteArrayFrom(store_resource,
133                                             position, buf, off, len);
134   }
135   
136   protected void writeByteTo(long position, int b) throws IOException JavaDoc {
137     buffer_manager.writeByteTo(store_resource, position, b);
138   }
139
140   protected void writeByteArrayTo(long position,
141                            byte[] buf, int off, int len) throws IOException JavaDoc {
142     buffer_manager.writeByteArrayTo(store_resource,
143                                     position, buf, off, len);
144   }
145
146   protected long endOfDataAreaPointer() throws IOException JavaDoc {
147     return buffer_manager.getDataAreaSize(store_resource);
148   }
149
150   protected void setDataAreaSize(long new_size) throws IOException JavaDoc {
151     buffer_manager.setDataAreaSize(store_resource, new_size);
152   }
153
154   // For diagnosis
155

156   public String JavaDoc toString() {
157     return "[ JournalledFileStore: " + resource_name + " ]";
158   }
159   
160 }
161
162
Popular Tags