KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > backup > BackupHelper


1 package com.daffodilwoods.daffodildb.server.backup;
2
3 import com.daffodilwoods.database.resource.DException;
4 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
5
    PersistentDatabase;
6 import com.daffodilwoods.daffodildb.server.datasystem.indexsystem.IndexDatabase;
7 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
8
    DatabaseConstants;
9 import com.daffodilwoods.daffodildb.server.datasystem.mergesystem.MergeDatabase;
10 import com.daffodilwoods.daffodildb.server.serversystem.ServerSystem;
11 import java.io.RandomAccessFile JavaDoc;
12 import java.io.File JavaDoc;
13 import java.util.*;
14 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
15
    DRandomAccessFile;
16 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DataTable;
17 import com.daffodilwoods.daffodildb.server.datadictionarysystem.SystemTables;
18 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DatabaseUserTableIterator;
19 import com.daffodilwoods.daffodildb.utils.BufferRange;
20 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
21 import com.daffodilwoods.daffodildb.utils.comparator.CTusjohJoTfotjujwfDpnqbsbups;
22 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._TableCharacteristics;
23 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.Utility;
24 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._DatabaseUser;
25 import com.daffodilwoods.daffodildb.server.datasystem.interfaces._UserTableOperations;
26 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DRandomAccessFileUpto3_2;
27
28 /**
29  * <p>Title: </p>
30  * <p>Description: </p>
31  * <p>Copyright: Copyright (c) 2003</p>
32  * <p>Company: </p>
33  * @author not attributable
34  * @version 1.0
35  */

36
37 public class BackupHelper {
38   public BackupHelper() {
39   }
40
41   public static boolean backupSystemDatabase(DRandomAccessFileUpto3_2 dRAM,
42                                              String JavaDoc destination,
43                                              String JavaDoc databaseNameSource
44                                              ) throws DException {
45     RandomAccessFile JavaDoc raf = dRAM.getRandomAccessFile();
46
47     String JavaDoc destinationPath = destination + File.separator +
48         DatabaseConstants.SYSTEMDATABASE +"_Bk"+ File.separator +
49         DatabaseConstants.SYSTEMDATABASE + ".ddb";
50     File JavaDoc df = new File JavaDoc(destinationPath);
51     if (!df.exists()) {
52       boolean isMultiFileSupport = dRAM.isMultiFileSupport();
53       HashMap multiFileMap = dRAM.getMultiFileMap();
54       int multiFileIndex = dRAM.getIndexMultipleFiles(/*DatabaseConstants.SYSTEMDATABASE*/);
55       copyDatabase(raf, destinationPath, destination, databaseNameSource,
56                     isMultiFileSupport,
57                    multiFileMap, multiFileIndex);
58       return false;
59
60     }
61     else
62       return true;
63   }
64
65   /**
66    * Copies database files from source path to the destination path
67    * If multiFileSupport exists and multiple files (.dat) exists at the source path then
68    * copies all the multipleFiles to the destination with the databaseNameDestination
69    * takes the randomaccessFile from the map present in DRandomAccessFile of the source
70    * and copies this file
71    * else copies only .ddb file
72    *
73    * @param sourceFile randomaccessfile of the source to be copied
74    * @param multiFileMap map of the loaded multile files present in the DRandom access file of the source
75    * @param multiFileIndex no of multiple files present in the database including .ddb file
76    * @throws DException
77    */

78
79   private static void copyDatabase(RandomAccessFile JavaDoc sourceFile,
80                                    String JavaDoc destinationPath, String JavaDoc destination,
81                                    String JavaDoc databaseNameSource,
82                                    boolean isMultiFileSupport,
83                                    HashMap multiFileMap, int multiFileIndex) throws
84       DException {
85     ArrayList multiFilesCopied = new ArrayList();
86     File JavaDoc df = new File JavaDoc(destinationPath);
87     File JavaDoc df1 = df.getParentFile();
88     df1.mkdirs();
89     try {
90       CopyFile cf = new CopyFile();
91       if (!isMultiFileSupport) {
92         RandomAccessFile JavaDoc DestinationFile = new RandomAccessFile JavaDoc(df, "rw");
93         cf.copyFile(sourceFile, DestinationFile);
94       }
95       else {
96         Set set = multiFileMap.entrySet();
97         Iterator i = set.iterator();
98         destinationPath = destinationPath.substring(0,
99             destinationPath.lastIndexOf(File.separator));
100         while (i.hasNext()) {
101           Map.Entry m = (Map.Entry) i.next();
102           String JavaDoc multiFileName = (String JavaDoc) m.getKey();
103           RandomAccessFile JavaDoc multiFile = (RandomAccessFile JavaDoc) m.getValue();
104           destinationPath.substring(0,
105                                     destinationPath.lastIndexOf(File.separator));
106           multiFilesCopied.add(multiFileName);
107           String JavaDoc indexOfMultiFile = multiFileName.substring(databaseNameSource.
108               length(), multiFileName.length());
109           String JavaDoc name = "";
110           if (multiFileName.equalsIgnoreCase(databaseNameSource)) {
111             name = databaseNameSource + ".ddb";
112           }
113           else
114             name = databaseNameSource + indexOfMultiFile + ".dat";
115           String JavaDoc destinationPath1 = destinationPath + File.separator + name;
116           RandomAccessFile JavaDoc DestinationFile1 = new RandomAccessFile JavaDoc(new File JavaDoc(
117               destinationPath1), "rw");
118           cf.copyFile(multiFile, DestinationFile1);
119
120         }
121         for (int j = 1; j < multiFileIndex; j++) {
122           String JavaDoc multiFileName = databaseNameSource + j;
123           if (multiFilesCopied.contains(multiFileName)) {
124             continue;
125           }
126           File JavaDoc multiFileSource = new File JavaDoc(destination + multiFileName);
127           RandomAccessFile JavaDoc multiFile = new RandomAccessFile JavaDoc(multiFileSource,
128               "rw");
129           String JavaDoc name = databaseNameSource + j + ".dat";
130           String JavaDoc destinationPath1 = destinationPath + File.separator + name;
131
132           RandomAccessFile JavaDoc DestinationFile1 = new RandomAccessFile JavaDoc(new File JavaDoc(
133               destinationPath1), "rw");
134           cf.copyFile(multiFile, DestinationFile1);
135           DestinationFile1.close();
136         }
137       }
138
139     }
140     catch (Exception JavaDoc ex) {
141       throw new DException("DSE0", new Object JavaDoc[] {ex.getMessage()});
142     }
143   }
144
145   /**
146  * Creates database at the destination it takes properties of the databse from the
147  * source SystemDatabase and then creates Database with these properties it also modifies
148  * the SystemTable DATABASEFILEINFO.
149  * Gives call for creation of database and then delete the files on the path and then just copy the files from source to destination.
150  *
151  * @param SystemDatabase persistentDatabase object of systemDatabse present in the source
152  * @param sourceDatabase persistentDatabase object of userDatabase present in the source
153  * @param destination path at which backup is taken
154  * @param databaseNameSource name of the database in the source
155  * @param databaseNameDestination name of the database with which it is to be restored or backed up
156  */

157
158 public static void createDatabase(_DatabaseUser user,PersistentDatabase systemDatabaseSourcePath,String JavaDoc destination,String JavaDoc databaseNameSource,
159                             String JavaDoc databaseNameDestination) throws DException {
160   int index = 0;
161   _DataTable databaseInfoTable = (_DataTable) systemDatabaseSourcePath.getTable(SystemTables.DATABASEINFO);
162   DatabaseUserTableIterator databaseTableIteartor = (DatabaseUserTableIterator) databaseInfoTable.getIterator();
163   Object JavaDoc[] databaseProperties = null;
164   BufferRange bytesOfDestinationDatabaseName = new BufferRange(CCzufDpowfsufs.getBytes(databaseNameDestination, databaseNameDestination.length(), false));
165   BufferRange bytesOfSourceDatabaseName = new BufferRange(CCzufDpowfsufs.getBytes(databaseNameSource, databaseNameSource.length(), false));
166  CTusjohJoTfotjujwfDpnqbsbups stringComparator = new CTusjohJoTfotjujwfDpnqbsbups();
167
168   if (databaseTableIteartor.first())
169     do {
170       BufferRange[] bytesGot = (BufferRange[]) databaseTableIteartor.getColumnValues();
171       if (stringComparator.compare(bytesGot[0], bytesOfSourceDatabaseName) == 0) {
172         _TableCharacteristics tableCharacteristics = databaseInfoTable.getTableCharacteristics();
173         databaseProperties = (Object JavaDoc[]) tableCharacteristics.getObject(bytesGot);
174         break;
175       }
176     }
177     while (databaseTableIteartor.next());
178   Object JavaDoc[] databaseProps = Utility.convertIntoFieldBase(databaseProperties);
179   String JavaDoc initialSize = (String JavaDoc) databaseProps[1];
180   int incrementFactor = databaseProps[4].hashCode();
181   boolean unicodeSupport = ( (Boolean JavaDoc) databaseProps[5]).booleanValue();
182   boolean multiFilesSupport = ( (Boolean JavaDoc) databaseProps[6]).booleanValue();
183   boolean encryptionSupport = ( (Boolean JavaDoc) databaseProps[7]).booleanValue();
184   String JavaDoc encrytpionAlgo = (String JavaDoc) databaseProps[8];
185   String JavaDoc encryptionKey = (String JavaDoc) databaseProps[9];
186   String JavaDoc clusterSize = (String JavaDoc) databaseProps[10];
187   if (multiFilesSupport) {
188     DatabaseUserTableIterator databaseFileInfoIterator = (DatabaseUserTableIterator) ( (_DataTable) systemDatabaseSourcePath.getTable(SystemTables.DATABASEFILEINFO)).getIterator();
189     if (databaseFileInfoIterator.first())
190       do {
191         BufferRange[] bytesGot = (BufferRange[]) databaseFileInfoIterator.getColumnValues();
192         if (stringComparator.compare(bytesGot[0],bytesOfSourceDatabaseName) == 0) {
193           index = CCzufDpowfsufs.getInt(bytesGot[1].getBytes()).hashCode();
194           break;
195         }
196       }
197       while (databaseTableIteartor.next());
198   }
199
200
201   BackupPersistentSystem ps = new BackupPersistentSystem(destination);
202   ps.createDatabase(databaseNameDestination, initialSize, incrementFactor,
203                     unicodeSupport, multiFilesSupport, encryptionSupport,
204                     encrytpionAlgo, encryptionKey, clusterSize);
205
206   if (multiFilesSupport) {
207    PersistentDatabase systemDatabaseDestinationPath = (PersistentDatabase) ps.getDatabase(
208        DatabaseConstants.SYSTEMDATABASE+"_Bk");
209    DatabaseUserTableIterator databaseFileInfoIterator = (DatabaseUserTableIterator) ( (_DataTable) systemDatabaseDestinationPath.getTable(
210        SystemTables.DATABASEFILEINFO)).getIterator();
211    if (databaseFileInfoIterator.first())
212      do {
213        BufferRange[] bytesGot = (BufferRange[]) databaseFileInfoIterator.getColumnValues();
214        if (stringComparator.compare(bytesGot[0],bytesOfDestinationDatabaseName) == 0) {
215          databaseFileInfoIterator.update(user,new BufferRange[] {bytesOfDestinationDatabaseName,new BufferRange(CCzufDpowfsufs.getBytes(new Integer JavaDoc(index)))});
216          break;
217        }
218      }
219      while (databaseTableIteartor.next());
220  }
221
222
223   deleteFiles(databaseNameDestination, destination);
224 }
225
226 /**
227   * Method to delete .ddb file and database folder created through create databse method before
228   * giving call to the copy file methof from source to destination
229   * @param databaseName
230   * @param destination
231   * @throws DException
232   */

233
234 private static void deleteFiles(String JavaDoc databaseName,String JavaDoc destination)throws DException{
235     String JavaDoc path = destination + File.separator +databaseName + File.separator + databaseName + ".ddb";
236     File JavaDoc f = new File JavaDoc(path);
237     boolean isDelete = f.delete();
238
239     File JavaDoc ff1 = f.getParentFile();
240     path = destination+File.separator +databaseName + File.separator + DatabaseConstants.TEMPDATABASE+".ddb";
241     File JavaDoc f1 = new File JavaDoc(path);
242     boolean isDelete1 = f1.delete();
243     boolean isDelete2 = ff1.delete();
244 }
245   public static void removingEntriesForSchedules(PersistentDatabase pd) throws DException{
246      _DatabaseUser user = null;
247     try {
248             _DataTable table = (_DataTable) pd.getTable(
249                 SystemTables.SCHEDULEINFO);
250             DatabaseUserTableIterator scheduleInfoTableIteartor = (DatabaseUserTableIterator) table.
251                 getIterator();
252             ArrayList list = new ArrayList();
253             list.add(SystemTables.SCHEDULEINFO);
254             user = pd.getDatabaseUser(list);
255             if (scheduleInfoTableIteartor.first()) {
256               do {
257                scheduleInfoTableIteartor.delete(user);
258               }
259               while (scheduleInfoTableIteartor.next());
260             }
261           }
262           catch (DException ex) {
263             if (!ex.getDseCode().equalsIgnoreCase("DSE959"))
264             throw ex;
265           }finally{
266             user.writeToFile();
267           }
268
269    }
270
271
272 }
273
Popular Tags