KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > storage > raf > log > asynraf > PhysicalFileAccessLogAsynrafImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.storage.raf.log.asynraf;
25
26 import org.objectweb.jalisto.se.api.JalistoProperties;
27 import org.objectweb.jalisto.se.api.internal.JalistoObject;
28 import org.objectweb.jalisto.se.api.physical.PluggablePhysicalFileAccess;
29 import org.objectweb.jalisto.se.exception.JalistoException;
30 import org.objectweb.jalisto.se.impl.InFileAddress;
31 import org.objectweb.jalisto.se.JalistoFactory;
32 import org.objectweb.jalisto.se.impl.trace.Trace;
33 import org.objectweb.jalisto.se.storage.raf.*;
34 import org.objectweb.jalisto.se.storage.raf.log.RecordsFileLog;
35
36 import java.io.*;
37 import java.util.*;
38
39 public class PhysicalFileAccessLogAsynrafImpl implements PluggablePhysicalFileAccess {
40
41     private void init(int initialSize, JalistoProperties properties)
42             throws IOException, RecordsFileException {
43         int nbrDbFiles = properties.getInstanceDbFileNumber();
44         if (nbrDbFiles > 1) {
45             files = new RecordsFileLog[nbrDbFiles + 2];
46             files[0] = new RecordsFileLog(properties.getSystemDbFilePath(), properties.isCleanLog());
47             files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
48             files[1] = new RecordsFileLog(properties.getOidDbFilePath(), properties.isCleanLog());
49             files[1].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
50
51             for (int i = 0; i < nbrDbFiles; i++) {
52                 files[i + 2] = new RecordsFileLog(properties.getInstanceDbFilePathAt(i), properties.isCleanLog());
53                 files[i + 2].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
54             }
55         } else {
56             files = new RecordsFileLog[1];
57             files[0] = new RecordsFileLog(properties.getSystemDbFilePath(), properties.isCleanLog());
58             files[0].initNewStorage(initialSize, properties.getKeyLength(), properties.getAdditionalSpace());
59         }
60     }
61
62     private void init(String JavaDoc accessFlags, JalistoProperties properties)
63             throws IOException, RecordsFileException {
64         int nbrDbFiles = properties.getInstanceDbFileNumber();
65         if (nbrDbFiles > 1) {
66             files = new RecordsFileLog[nbrDbFiles + 2];
67             files[0] = new RecordsFileLog(properties.getSystemDbFilePath(),
68                                           accessFlags,
69                                           properties.isCleanLog());
70             files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
71             files[1] = new RecordsFileLog(properties.getOidDbFilePath(),
72                                           accessFlags,
73                                           properties.isCleanLog());
74             files[1].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
75
76             for (int i = 0; i < nbrDbFiles; i++) {
77                 files[i + 2] = new RecordsFileLog(properties.getInstanceDbFilePathAt(i),
78                                                   accessFlags,
79                                                   properties.isCleanLog());
80                 files[i + 2].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
81             }
82         } else {
83             files = new RecordsFileLog[1];
84             files[0] = new RecordsFileLog(properties.getSystemDbFilePath(),
85                                           accessFlags,
86                                           properties.isCleanLog());
87             files[0].initExistingStorage(properties.getKeyLength(), properties.getAdditionalSpace());
88         }
89     }
90
91     public void init(JalistoProperties properties) {
92         try {
93             String JavaDoc dbPath = properties.getDbFileFullName();
94             File f = new File(dbPath);
95             this.isNewBase = (!f.exists());
96             trace = JalistoFactory.getInternalFactory().getTracer(properties);
97             if (isNewBase && properties.isReadOnlyImplementation()) {
98                 throw new JalistoException("cannot create a database instance in read only mode");
99             }
100             if (isNewBase) {
101                 init(properties.getInitialSize(), properties);
102             } else {
103                 init("rw", properties);
104             }
105             for (int i = 0; i < files.length; i++) {
106                 files[i].setTrace(trace);
107             }
108             minilog = new AsynrafLogger(properties, this);
109             extentMap = new HashMap();
110             trace.println(Trace.DEBUG, "Physical implementation : {0}",
111                           PhysicalFileAccessLogAsynrafImpl.class.getName());
112             trace.println(Trace.PHYSICAL, "new instance with {0} underlying files", new Integer JavaDoc(files.length));
113         } catch (Exception JavaDoc e) {
114             throw new JalistoException(e);
115         }
116     }
117
118     public boolean isNewBase() {
119         return isNewBase;
120     }
121
122     public void reorganize() {
123         trace.println(Trace.PHYSICAL, "reorganize");
124         for (int i = 0; i < files.length; i++) {
125             try {
126                 files[i].reorganize();
127             } catch (Exception JavaDoc e) {
128                 throw new JalistoException(
129                         "error during reorganization of a physical store file : " + files[i].toString(), e);
130             }
131         }
132     }
133
134     public Collection getKeysStartingWith(String JavaDoc filter) {
135         trace.println(Trace.PHYSICAL, "asynraf getKeysStartingWith : filter = {0}", filter);
136         Object JavaDoc[] minilogAddresses = minilog.getAddressesForExtent(filter);
137         Iterator keys = ((Collection) minilogAddresses[0]).iterator();
138         while (keys.hasNext()) {
139             extentMap.put(keys.next(), null);
140         }
141         for (short i = 0; i < files.length; i++) {
142             keys = files[i].getKeysStartingWith(filter).iterator();
143             while (keys.hasNext()) {
144                 extentMap.put(keys.next(), null);
145             }
146         }
147         keys = ((Collection) minilogAddresses[1]).iterator();
148         while (keys.hasNext()) {
149             extentMap.remove(keys.next());
150         }
151         Collection result = new ArrayList(extentMap.keySet());
152         extentMap.clear();
153         return result;
154     }
155
156     public void insertFileObject(InFileAddress ifa, JalistoObject fo) {
157         trace.println(Trace.PHYSICAL, "asynraf insertFileObject({0}, {1})", ifa, fo);
158         minilog.insertFileObject(ifa, fo);
159     }
160
161     public JalistoObject readFileObjectAt(InFileAddress ifa) {
162         trace.println(Trace.PHYSICAL, "asynraf readFileObjectAt({0})", ifa);
163         return minilog.readFileObjectAt(ifa);
164     }
165
166     public void updateFileObject(InFileAddress ifa, JalistoObject fo) {
167         trace.println(Trace.PHYSICAL, "asynraf updateFileObject({0}, {1})", ifa, fo);
168         minilog.updateFileObject(ifa, fo);
169     }
170
171     public void deleteFileObject(InFileAddress ifa) {
172         trace.println(Trace.PHYSICAL, "asynraf deleteFileObject({0})", ifa);
173         minilog.deleteFileObject(ifa);
174     }
175
176     public void startCommit() {
177         trace.println(Trace.PHYSICAL, "asynraf startCommit");
178         try {
179             minilog.startCommitting();
180         } catch (IOException e) {
181             throw new JalistoException(e);
182         }
183     }
184
185     public void commit() {
186         trace.println(Trace.PHYSICAL, "asynraf commit");
187         try {
188             minilog.finishCommitting();
189         } catch (IOException e) {
190             throw new JalistoException(e);
191         }
192     }
193
194     public void rollback() {
195     }
196
197     public void open() {
198         minilog.open();
199     }
200
201     public void close() {
202         minilog.close();
203     }
204
205     public AsynrafLogger getMinilog() {
206         return minilog;
207     }
208
209     public void internalInsert(InFileAddress ifa, byte[] datas) {
210         trace.println(Trace.PHYSICAL, "asynraf internalInsert : ifa = {0}", ifa);
211         try {
212             RecordWriter rw = new RecordWriter(ifa.getAddress());
213             rw.writeDatas(datas);
214             int i = 0;
215             if (files.length > 1) {
216                 i = ifa.getFileIndex();
217             }
218             files[i].startBackUp();
219             files[i].insertRecord(rw);
220             files[i].finishBackUp();
221             rw.reset();
222         } catch (Exception JavaDoc e) {
223             throw new JalistoException("could not insert datas at " + ifa, e);
224         }
225     }
226
227     public RecordReader internalRead(InFileAddress ifa) {
228         trace.println(Trace.PHYSICAL, "asynraf internalRead : ifa = {0}", ifa);
229         RecordReader result;
230         try {
231             int i = 0;
232             if (files.length > 1) {
233                 i = ifa.getFileIndex();
234             }
235             result = files[i].readRecord(ifa.getAddress());
236         } catch (Exception JavaDoc e) {
237             throw new JalistoException("could not read object at " + ifa, e);
238         }
239         return result;
240     }
241
242     public void internalUpdateFileObject(InFileAddress ifa, byte[] datas) {
243         trace.println(Trace.PHYSICAL, "asynraf internalUpdateFileObject : ifa = {0}", ifa);
244         try {
245             RecordWriter rw = new RecordWriter(ifa.getAddress());
246             rw.writeDatas(datas);
247             int i = 0;
248             if (files.length > 1) {
249                 i = ifa.getFileIndex();
250             }
251             files[i].startBackUp();
252             files[i].updateRecord(rw);
253             files[i].finishBackUp();
254             rw.reset();
255         } catch (Exception JavaDoc e) {
256             throw new JalistoException("could not update datas ", e);
257         }
258     }
259
260     public void internalDeleteFileObject(InFileAddress ifa) {
261         trace.println(Trace.PHYSICAL, "asynraf internalDeleteFileObject : ifa = {0}", ifa);
262         try {
263             int i = 0;
264             if (files.length > 1) {
265                 i = ifa.getFileIndex();
266             }
267             files[i].startBackUp();
268             files[i].deleteRecord(ifa.getAddress());
269             files[i].finishBackUp();
270         } catch (Exception JavaDoc e) {
271             throw new JalistoException("could not delete object at " + ifa, e);
272         }
273     }
274
275     public Trace getTrace() {
276         return trace;
277     }
278
279
280     public String JavaDoc toString() {
281         RecordsFileInspector rfi = new RecordsFileInspector();
282         StringWriter sw = new StringWriter();
283         PrintWriter out = new PrintWriter(sw);
284         try {
285             rfi.inspectRecordsFile(out, files[0]);
286         } catch (Exception JavaDoc e) {
287             out.println("error during inspection of the database : " + e.getMessage());
288         }
289         out.flush();
290         return sw.toString();
291     }
292
293
294     private RecordsFileLog[] files;
295     private AsynrafLogger minilog;
296     private boolean isNewBase;
297     private HashMap extentMap;
298     private Trace trace;
299 }
300
301
Popular Tags