KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > persistent > file > MantaFileManager


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Amir Shevat.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.core.persistent.file;
47
48 import java.io.File JavaDoc;
49 import java.io.IOException JavaDoc;
50 import java.io.FilenameFilter JavaDoc;
51 import java.nio.ByteBuffer JavaDoc;
52 import java.util.*;
53
54 import org.apache.commons.logging.Log;
55 import org.apache.commons.logging.LogFactory;
56 import org.mr.MantaAgent;
57 import org.mr.kernel.services.topics.VirtualTopicManager;
58 import org.mr.core.persistent.PersistentConst;
59 import org.mr.core.persistent.PersistentManager;
60 import org.mr.core.util.byteable.Byteable;
61 import org.mr.core.util.byteable.ByteableInputStream;
62 import org.mr.core.util.byteable.ByteableOutputStream;
63 import org.mr.core.util.StringUtils;
64
65 /**
66  * this object manages the writing, reading, recovery and difragmentation of
67  * MantaRay persistent file
68  * @author Amir Shevat
69  *
70  */

71 public class MantaFileManager implements PersistentManager {
72
73     private int numberOfFatFiles = PersistentManager.MAX_FILES_PER_STORAGE/MantaFatFile.MAX_FILES_IN_FAT;
74     private MantaFatFile fats[] = new MantaFatFile[numberOfFatFiles];
75     private String JavaDoc persistentName = null;
76     private static Log log;
77     static Timer cleaner = new Timer(true);
78     public static final long fatCleanerDeley = 5000;
79     boolean recoverd = false;
80     public static boolean forceEveryEntry = false;
81
82     public MantaFileManager(String JavaDoc persistentName){
83         this.persistentName = persistentName;
84         log=LogFactory.getLog("MantaFileManager");
85         forceEveryEntry = MantaAgent.getInstance().getSingletonRepository()
86             .getConfigManager().getBooleanProperty("persistency.file.force_every_entry", false);
87     }
88
89     /**
90      * constructor used for singletone PersistentManager
91      * used for example for retrieving services
92      */

93     public MantaFileManager() {
94         fats = null;
95         cleaner = null;
96     }
97
98     public synchronized void recover(){
99         if(recoverd){
100             return;
101         }
102         int newOrder = 0;
103         File JavaDoc persistentDir = new File JavaDoc(PersistentConst.getPersistentDir(persistentName));
104         File JavaDoc fatFiles[] =persistentDir.listFiles();
105         // sort the file by name
106
Arrays.sort(fatFiles, new Comparator(){
107
108             public int compare(Object JavaDoc o1, Object JavaDoc o2) {
109                 File JavaDoc a = (File JavaDoc) o1;
110                 File JavaDoc b = (File JavaDoc) o2;
111                 return a.getName().compareTo(b.getName());
112             }
113
114         });
115         // recover the files if there are any
116
for (int i = 0; i < fatFiles.length; i++) {
117             File JavaDoc recovedFile =fatFiles[i];
118             String JavaDoc fatName = recovedFile.getName();
119             if(fatName.startsWith(persistentName+"_#")){
120                 String JavaDoc oldFileName = PersistentConst.getPersistentDir(persistentName)+File.separator+"old"+fatName;
121                 File JavaDoc old = new File JavaDoc(oldFileName);
122                 if(old.exists()){
123                     old.delete();
124                 }
125                 boolean renamed = recovedFile.renameTo(old);
126                 try {
127                     if(renamed){
128                         MantaFatFile recoverd = new MantaFatFile(oldFileName);
129                         int[] entries = recoverd.getFileList();
130                         if(entries.length >0){
131                             for (int j = 0; j < entries.length; j++) {
132                                     ByteBuffer JavaDoc buff = recoverd.load(entries[j]);
133                                     savePersistentBuffer(newOrder,buff);
134                                     newOrder++;
135                                     PersistentConst.getPersistentByteBufferPool().release(buff);
136                             }
137                         }
138                         recoverd.close();
139                         recoverd = null;
140                         recovedFile = null;
141                         boolean b = old.delete();
142                         if(!b){
143                             FatCleanerTasks task = new FatCleanerTasks(old, 5);
144                             cleaner.schedule(task, fatCleanerDeley);
145                         }
146
147
148                 }//if
149
} catch (Throwable JavaDoc e) {
150                     if(log.isErrorEnabled())
151                         log.error("Could not recover "+persistentName+" : file number"+i+".",e);
152                 }
153             }//if
154
}//for
155
recoverd = true;
156     }
157
158
159     private MantaFatFile createFatFile(int FatFileIndex){
160         MantaFatFile f = new MantaFatFile(PersistentConst.getPersistentDir(persistentName)+File.separator+persistentName+"_#"+FatFileIndex);
161         fats[FatFileIndex] = f;
162         return f;
163     }
164
165     private void deleteFatFile(int FatFileIndex) throws IOException JavaDoc{
166         String JavaDoc fileName =PersistentConst.getPersistentDir(persistentName)+File.separator+persistentName+"_#"+FatFileIndex;
167         if(fats[FatFileIndex]!= null){
168             fats[FatFileIndex].close();
169             fats[FatFileIndex]= null;
170             File JavaDoc f = new File JavaDoc(fileName);
171             f.delete();
172
173         }
174     }
175
176     private int getFatFileNumByEntry(int entryIndex ){
177         return (int)Math.floor(entryIndex/(MantaFatFile.MAX_FILES_IN_FAT-1 /*minus 1 is for the first entry in every file */)) ;
178
179     }
180
181     private int getEntryIndexInFAT(int entryIndex,int FATNamber){
182         int result = (entryIndex-(FATNamber*(MantaFatFile.MAX_FILES_IN_FAT-1)))+1;
183         return result;
184     }
185
186
187
188     /* (non-Javadoc)
189       * @see org.mr.core.persistent.PersistentManager#getKeys()
190       */

191     public synchronized int[] getKeys() {
192         LinkedList list = new LinkedList();
193         for (int i = 0; i < fats.length; i++) {
194             MantaFatFile file = fats[i];
195             if(file != null){
196                 int entries[] =file.getFileList();
197                 for (int j = 0; j < entries.length; j++) {
198                     list.add(new Integer JavaDoc((entries[j]+(i*(MantaFatFile.MAX_FILES_IN_FAT-1)))-1));
199                 }
200
201             }
202         }
203         int[] result = new int[list.size()];
204         for (int i = 0; i < result.length; i++) {
205             result[i] = ((Integer JavaDoc)list.get(i)).intValue();
206         }
207         return result;
208     }
209
210     ByteableInputStream in = new ByteableInputStream();
211     /* (non-Javadoc)
212       * @see org.mr.core.persistent.PersistentManager#getPersistentObject(int)
213       */

214     public synchronized Object JavaDoc getPersistentObject(int i) {
215         Byteable result = null;
216
217         try{
218             ByteBuffer JavaDoc buff = getPersistentBuffer( i);
219
220             in.setUnderLine(buff);
221             result = in.readByteable();
222             PersistentConst.getPersistentByteBufferPool().release(buff);
223             //in.release();
224
}catch(Throwable JavaDoc t){
225             if(log.isErrorEnabled())
226                 log.error("Could not recover "+persistentName+" : "+i+".",t);
227         }
228         return result;
229     }
230
231     /* (non-Javadoc)
232       * @see org.mr.core.persistent.PersistentManager#deletePersistentObject(int)
233       */

234     public synchronized void deletePersistentObject(int i) {
235         int fileNum = getFatFileNumByEntry(i);
236         MantaFatFile fat = fats[fileNum];
237         if(fat != null){
238             int entryIndexInFat = getEntryIndexInFAT(i,fileNum);//i-(fileNum*MantaFatFile.MAX_FILES_IN_FAT);
239
fat.delete(entryIndexInFat);
240         }
241
242     }
243
244     /* (non-Javadoc)
245       * @see org.mr.core.persistent.PersistentManager#savePersistentObject(int, org.mr.core.util.byteable.Byteable)
246       */

247     public synchronized void savePersistentObject(int entryName, Byteable entry) throws IOException JavaDoc {
248         ByteableOutputStream out = new ByteableOutputStream(PersistentConst.getPersistentByteBufferPool());
249         out.writeByteable(entry);
250         savePersistentBuffer(entryName,out.getByteBuffer());
251         out.release();
252
253     }
254
255     /* (non-Javadoc)
256       * @see org.mr.core.persistent.PersistentManager#clearStorage()
257       */

258     public synchronized void clearStorage() throws IOException JavaDoc {
259         for (int i = 0; i < fats.length; i++) {
260             if(fats!= null){
261                 deleteFatFile(i);
262             }
263
264         }
265
266     }
267
268     /* (non-Javadoc)
269       * @see org.mr.core.persistent.PersistentManager#savePersistentBuffer(int, java.nio.ByteBuffer)
270       */

271     public synchronized void savePersistentBuffer(int entryName, ByteBuffer JavaDoc byteBuffer) throws IOException JavaDoc{
272
273         int fatNum = getFatFileNumByEntry(entryName);
274         MantaFatFile fat = fats[fatNum];
275         if(fat == null){
276             fat = createFatFile(fatNum);
277         }
278
279         int entryIndexInFat = getEntryIndexInFAT(entryName,fatNum );//entryName-(fatNum*MantaFatFile.MAX_FILES_IN_FAT);
280

281         fat.save(entryIndexInFat , byteBuffer);
282
283     }
284
285     /* (non-Javadoc)
286       * @see org.mr.core.persistent.PersistentManager#getPersistentBuffer(int)
287       */

288     public synchronized ByteBuffer JavaDoc getPersistentBuffer(int i) {
289         int fileNum = getFatFileNumByEntry(i);
290         MantaFatFile fat = fats[fileNum];
291         int entryIndexInFat = getEntryIndexInFAT(i,fileNum );//i-(fileNum*MantaFatFile.MAX_FILES_IN_FAT);
292
ByteBuffer JavaDoc buff = fat.load(entryIndexInFat);
293         return buff;
294     }
295
296     /* (non-Javadoc)
297       * @see org.mr.core.persistent.PersistentManager#getStorageName()
298       */

299     public String JavaDoc getStorageName() {
300
301         return persistentName;
302     }
303
304     public String JavaDoc[] getAllServices() {
305         HashSet services = new HashSet();
306         String JavaDoc persistentDir = MantaAgent.getInstance().getSingletonRepository().
307                 getConfigManager().getStringProperty("persistency.file.persistent_folder","./persistent");
308         String JavaDoc delimiter = MantaAgent.getInstance().getSingletonRepository().
309                 getConfigManager().getStringProperty("persistency.hierarchy_delimiter","~");
310         File JavaDoc dir = new File JavaDoc(persistentDir);
311
312         String JavaDoc list[] = dir.list(new FilenameFilter JavaDoc() {
313             public boolean accept(File JavaDoc dir,String JavaDoc file) {
314                 return file.startsWith(PersistentManager.SUBSCRIBERS_PERSISTENT_PREFIX);
315             }
316         });
317
318         for (int i = 0 ; i < list.length; i++) {
319             services.add(StringUtils.replace(list[i]
320                     .substring(PersistentManager.SUBSCRIBERS_PERSISTENT_PREFIX.length(),
321                     list[i].lastIndexOf("_#"))
322                     ,delimiter, VirtualTopicManager.HIERARCHICAL_TOPIC_DELIMITER));
323         }
324
325         return (String JavaDoc[]) services.toArray(list);
326     }
327
328 }
329
Popular Tags