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 ; 12 import java.io.File ; 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 36 37 public class BackupHelper { 38 public BackupHelper() { 39 } 40 41 public static boolean backupSystemDatabase(DRandomAccessFileUpto3_2 dRAM, 42 String destination, 43 String databaseNameSource 44 ) throws DException { 45 RandomAccessFile raf = dRAM.getRandomAccessFile(); 46 47 String destinationPath = destination + File.separator + 48 DatabaseConstants.SYSTEMDATABASE +"_Bk"+ File.separator + 49 DatabaseConstants.SYSTEMDATABASE + ".ddb"; 50 File df = new File (destinationPath); 51 if (!df.exists()) { 52 boolean isMultiFileSupport = dRAM.isMultiFileSupport(); 53 HashMap multiFileMap = dRAM.getMultiFileMap(); 54 int multiFileIndex = dRAM.getIndexMultipleFiles(); 55 copyDatabase(raf, destinationPath, destination, databaseNameSource, 56 isMultiFileSupport, 57 multiFileMap, multiFileIndex); 58 return false; 59 60 } 61 else 62 return true; 63 } 64 65 78 79 private static void copyDatabase(RandomAccessFile sourceFile, 80 String destinationPath, String destination, 81 String databaseNameSource, 82 boolean isMultiFileSupport, 83 HashMap multiFileMap, int multiFileIndex) throws 84 DException { 85 ArrayList multiFilesCopied = new ArrayList(); 86 File df = new File (destinationPath); 87 File df1 = df.getParentFile(); 88 df1.mkdirs(); 89 try { 90 CopyFile cf = new CopyFile(); 91 if (!isMultiFileSupport) { 92 RandomAccessFile DestinationFile = new RandomAccessFile (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 multiFileName = (String ) m.getKey(); 103 RandomAccessFile multiFile = (RandomAccessFile ) m.getValue(); 104 destinationPath.substring(0, 105 destinationPath.lastIndexOf(File.separator)); 106 multiFilesCopied.add(multiFileName); 107 String indexOfMultiFile = multiFileName.substring(databaseNameSource. 108 length(), multiFileName.length()); 109 String name = ""; 110 if (multiFileName.equalsIgnoreCase(databaseNameSource)) { 111 name = databaseNameSource + ".ddb"; 112 } 113 else 114 name = databaseNameSource + indexOfMultiFile + ".dat"; 115 String destinationPath1 = destinationPath + File.separator + name; 116 RandomAccessFile DestinationFile1 = new RandomAccessFile (new File ( 117 destinationPath1), "rw"); 118 cf.copyFile(multiFile, DestinationFile1); 119 120 } 121 for (int j = 1; j < multiFileIndex; j++) { 122 String multiFileName = databaseNameSource + j; 123 if (multiFilesCopied.contains(multiFileName)) { 124 continue; 125 } 126 File multiFileSource = new File (destination + multiFileName); 127 RandomAccessFile multiFile = new RandomAccessFile (multiFileSource, 128 "rw"); 129 String name = databaseNameSource + j + ".dat"; 130 String destinationPath1 = destinationPath + File.separator + name; 131 132 RandomAccessFile DestinationFile1 = new RandomAccessFile (new File ( 133 destinationPath1), "rw"); 134 cf.copyFile(multiFile, DestinationFile1); 135 DestinationFile1.close(); 136 } 137 } 138 139 } 140 catch (Exception ex) { 141 throw new DException("DSE0", new Object [] {ex.getMessage()}); 142 } 143 } 144 145 157 158 public static void createDatabase(_DatabaseUser user,PersistentDatabase systemDatabaseSourcePath,String destination,String databaseNameSource, 159 String databaseNameDestination) throws DException { 160 int index = 0; 161 _DataTable databaseInfoTable = (_DataTable) systemDatabaseSourcePath.getTable(SystemTables.DATABASEINFO); 162 DatabaseUserTableIterator databaseTableIteartor = (DatabaseUserTableIterator) databaseInfoTable.getIterator(); 163 Object [] 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 []) tableCharacteristics.getObject(bytesGot); 174 break; 175 } 176 } 177 while (databaseTableIteartor.next()); 178 Object [] databaseProps = Utility.convertIntoFieldBase(databaseProperties); 179 String initialSize = (String ) databaseProps[1]; 180 int incrementFactor = databaseProps[4].hashCode(); 181 boolean unicodeSupport = ( (Boolean ) databaseProps[5]).booleanValue(); 182 boolean multiFilesSupport = ( (Boolean ) databaseProps[6]).booleanValue(); 183 boolean encryptionSupport = ( (Boolean ) databaseProps[7]).booleanValue(); 184 String encrytpionAlgo = (String ) databaseProps[8]; 185 String encryptionKey = (String ) databaseProps[9]; 186 String clusterSize = (String ) 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 (index)))}); 216 break; 217 } 218 } 219 while (databaseTableIteartor.next()); 220 } 221 222 223 deleteFiles(databaseNameDestination, destination); 224 } 225 226 233 234 private static void deleteFiles(String databaseName,String destination)throws DException{ 235 String path = destination + File.separator +databaseName + File.separator + databaseName + ".ddb"; 236 File f = new File (path); 237 boolean isDelete = f.delete(); 238 239 File ff1 = f.getParentFile(); 240 path = destination+File.separator +databaseName + File.separator + DatabaseConstants.TEMPDATABASE+".ddb"; 241 File f1 = new File (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 |