@Test public void testPrepareBackupDBFile() throws IOException { String tmpFilePath = getTempFilePath(); String databaseName = UUID.randomUUID().toString(); String dbFileName = tmpFilePath + "/" + databaseName + ".statistics"; String dbBackupFileName = tmpFilePath + "/" + databaseName + ".backup.statistics"; String dbPrevBackupFileName = tmpFilePath + "/" + databaseName + ".backup.previous.statistics"; String dbPFileName = dbFileName.concat(".p"); String dbBackupPFileName = dbBackupFileName.concat(".p"); String dbPrevBackupPFileName = dbPrevBackupFileName.concat(".p"); File dbFile = new File(dbFileName); File dbBackupFile = new File(dbBackupFileName); File dbPrevBackupFile = new File(dbPrevBackupFileName); File dbPFile = new File(dbPFileName); File dbBackupPFile = new File(dbBackupPFileName); File dbPrevBackupPFile = new File(dbPrevBackupPFileName); LOG.debug("dbFileName: " + dbFileName); LOG.debug("dbBackupFileName: " + dbBackupFileName); LOG.debug("dbPrevBackupFileName: " + dbPrevBackupFileName); assertFalse(dbFile.exists()); assertFalse(dbPFile.exists()); assertFalse(dbBackupFile.exists()); assertFalse(dbBackupPFile.exists()); assertFalse(dbPrevBackupFile.exists()); assertFalse(dbPrevBackupPFile.exists()); dbBackupFile = StatisticDataRepositoryMapDB.prepareBackupDBFileFor(dbFile); assertNotNull(dbBackupFile); assertFalse(dbBackupFile.exists()); LOG.debug("dbBackupFile: " + dbBackupFile.getAbsolutePath()); dbBackupFile.createNewFile(); dbBackupPFile.createNewFile(); assertTrue(dbBackupFile.exists()); assertTrue(dbBackupFile.isFile()); assertEquals(dbBackupFileName, dbBackupFile.getAbsolutePath()); assertFalse(dbPrevBackupFile.exists()); dbBackupFile = StatisticDataRepositoryMapDB.prepareBackupDBFileFor(dbFile); assertFalse(dbBackupFile.exists()); assertTrue(dbPrevBackupFile.exists()); }
static File prepareBackupDBFileFor(File dbFile) { StringBuilder tmp = new StringBuilder(dbFile.getPath()); int n = tmp.lastIndexOf(FILENAME_SUFFIX); tmp.replace(n, n + FILENAME_SUFFIX.length(), ".backup" + FILENAME_SUFFIX); String backupFileName = tmp.toString(); LOG.debug("backupFileName: " + backupFileName); File backupFile = new File(backupFileName); File backupFileP = new File(backupFileName+".p"); if (backupFile.exists()) { LOG.debug("Previous backup database exists."); String previousBackupFileName = backupFileName.replaceFirst(".backup.", ".backup.previous."); File previousBackupFile = new File(previousBackupFileName); File previousBackupFileP = new File(previousBackupFileName+".p"); if (previousBackupFile.exists()) { LOG.debug("Will delete previous backup file."); boolean prevBackupFileDeleted = previousBackupFile.delete(); if (!prevBackupFileDeleted) { LOG.error("Could not delete previous backup file: " + previousBackupFile.getAbsolutePath()); return null; } boolean prevBackupFilePDeleted = previousBackupFileP.delete(); if (!prevBackupFilePDeleted) { LOG.error("Could not delete previous backup .p file: " + previousBackupFileP.getAbsolutePath()); return null; } } LOG.debug("Will rename previous backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); boolean backupFileRenamed = backupFile.renameTo(previousBackupFile); if (!backupFileRenamed) { LOG.error("Failed to rename backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); return null; } boolean backupFilePRenamed = backupFileP.renameTo(previousBackupFileP); if (!backupFilePRenamed) { LOG.error("Failed to rename backup .p file from " + backupFile.getName() + " to " + previousBackupFileP.getName()); return null; } backupFile = new File(backupFileName); } return backupFile; }
StatisticDataRepositoryMapDB implements StatisticDataRepository { static File prepareBackupDBFileFor(File dbFile) { StringBuilder tmp = new StringBuilder(dbFile.getPath()); int n = tmp.lastIndexOf(FILENAME_SUFFIX); tmp.replace(n, n + FILENAME_SUFFIX.length(), ".backup" + FILENAME_SUFFIX); String backupFileName = tmp.toString(); LOG.debug("backupFileName: " + backupFileName); File backupFile = new File(backupFileName); File backupFileP = new File(backupFileName+".p"); if (backupFile.exists()) { LOG.debug("Previous backup database exists."); String previousBackupFileName = backupFileName.replaceFirst(".backup.", ".backup.previous."); File previousBackupFile = new File(previousBackupFileName); File previousBackupFileP = new File(previousBackupFileName+".p"); if (previousBackupFile.exists()) { LOG.debug("Will delete previous backup file."); boolean prevBackupFileDeleted = previousBackupFile.delete(); if (!prevBackupFileDeleted) { LOG.error("Could not delete previous backup file: " + previousBackupFile.getAbsolutePath()); return null; } boolean prevBackupFilePDeleted = previousBackupFileP.delete(); if (!prevBackupFilePDeleted) { LOG.error("Could not delete previous backup .p file: " + previousBackupFileP.getAbsolutePath()); return null; } } LOG.debug("Will rename previous backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); boolean backupFileRenamed = backupFile.renameTo(previousBackupFile); if (!backupFileRenamed) { LOG.error("Failed to rename backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); return null; } boolean backupFilePRenamed = backupFileP.renameTo(previousBackupFileP); if (!backupFilePRenamed) { LOG.error("Failed to rename backup .p file from " + backupFile.getName() + " to " + previousBackupFileP.getName()); return null; } backupFile = new File(backupFileName); } return backupFile; } }
StatisticDataRepositoryMapDB implements StatisticDataRepository { static File prepareBackupDBFileFor(File dbFile) { StringBuilder tmp = new StringBuilder(dbFile.getPath()); int n = tmp.lastIndexOf(FILENAME_SUFFIX); tmp.replace(n, n + FILENAME_SUFFIX.length(), ".backup" + FILENAME_SUFFIX); String backupFileName = tmp.toString(); LOG.debug("backupFileName: " + backupFileName); File backupFile = new File(backupFileName); File backupFileP = new File(backupFileName+".p"); if (backupFile.exists()) { LOG.debug("Previous backup database exists."); String previousBackupFileName = backupFileName.replaceFirst(".backup.", ".backup.previous."); File previousBackupFile = new File(previousBackupFileName); File previousBackupFileP = new File(previousBackupFileName+".p"); if (previousBackupFile.exists()) { LOG.debug("Will delete previous backup file."); boolean prevBackupFileDeleted = previousBackupFile.delete(); if (!prevBackupFileDeleted) { LOG.error("Could not delete previous backup file: " + previousBackupFile.getAbsolutePath()); return null; } boolean prevBackupFilePDeleted = previousBackupFileP.delete(); if (!prevBackupFilePDeleted) { LOG.error("Could not delete previous backup .p file: " + previousBackupFileP.getAbsolutePath()); return null; } } LOG.debug("Will rename previous backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); boolean backupFileRenamed = backupFile.renameTo(previousBackupFile); if (!backupFileRenamed) { LOG.error("Failed to rename backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); return null; } boolean backupFilePRenamed = backupFileP.renameTo(previousBackupFileP); if (!backupFilePRenamed) { LOG.error("Failed to rename backup .p file from " + backupFile.getName() + " to " + previousBackupFileP.getName()); return null; } backupFile = new File(backupFileName); } return backupFile; } StatisticDataRepositoryMapDB(String dbFileName); }
StatisticDataRepositoryMapDB implements StatisticDataRepository { static File prepareBackupDBFileFor(File dbFile) { StringBuilder tmp = new StringBuilder(dbFile.getPath()); int n = tmp.lastIndexOf(FILENAME_SUFFIX); tmp.replace(n, n + FILENAME_SUFFIX.length(), ".backup" + FILENAME_SUFFIX); String backupFileName = tmp.toString(); LOG.debug("backupFileName: " + backupFileName); File backupFile = new File(backupFileName); File backupFileP = new File(backupFileName+".p"); if (backupFile.exists()) { LOG.debug("Previous backup database exists."); String previousBackupFileName = backupFileName.replaceFirst(".backup.", ".backup.previous."); File previousBackupFile = new File(previousBackupFileName); File previousBackupFileP = new File(previousBackupFileName+".p"); if (previousBackupFile.exists()) { LOG.debug("Will delete previous backup file."); boolean prevBackupFileDeleted = previousBackupFile.delete(); if (!prevBackupFileDeleted) { LOG.error("Could not delete previous backup file: " + previousBackupFile.getAbsolutePath()); return null; } boolean prevBackupFilePDeleted = previousBackupFileP.delete(); if (!prevBackupFilePDeleted) { LOG.error("Could not delete previous backup .p file: " + previousBackupFileP.getAbsolutePath()); return null; } } LOG.debug("Will rename previous backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); boolean backupFileRenamed = backupFile.renameTo(previousBackupFile); if (!backupFileRenamed) { LOG.error("Failed to rename backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); return null; } boolean backupFilePRenamed = backupFileP.renameTo(previousBackupFileP); if (!backupFilePRenamed) { LOG.error("Failed to rename backup .p file from " + backupFile.getName() + " to " + previousBackupFileP.getName()); return null; } backupFile = new File(backupFileName); } return backupFile; } StatisticDataRepositoryMapDB(String dbFileName); @Override void openForRead(); @Override void openForWrite(boolean cacheInMemoryDumpToDiskOnClose); @Override void close(); @Override Set<String> getStatisticNames(); @Override long getNumberOfCells(String statisticName); @Override DatasetMetaData getMetaData(); @Override void putMetaData(DatasetMetaData datasetMetadata); @Override StatisticData getStatisticData(String statisticName, long cellId); @Override void putStatisticData(String statisticName, long cellId, StatisticData statistics); @Override Set<Long> getCellsWithData(String statisticName); @Override StatisticData getStatisticDataForRandomCell(String statisticName); }
StatisticDataRepositoryMapDB implements StatisticDataRepository { static File prepareBackupDBFileFor(File dbFile) { StringBuilder tmp = new StringBuilder(dbFile.getPath()); int n = tmp.lastIndexOf(FILENAME_SUFFIX); tmp.replace(n, n + FILENAME_SUFFIX.length(), ".backup" + FILENAME_SUFFIX); String backupFileName = tmp.toString(); LOG.debug("backupFileName: " + backupFileName); File backupFile = new File(backupFileName); File backupFileP = new File(backupFileName+".p"); if (backupFile.exists()) { LOG.debug("Previous backup database exists."); String previousBackupFileName = backupFileName.replaceFirst(".backup.", ".backup.previous."); File previousBackupFile = new File(previousBackupFileName); File previousBackupFileP = new File(previousBackupFileName+".p"); if (previousBackupFile.exists()) { LOG.debug("Will delete previous backup file."); boolean prevBackupFileDeleted = previousBackupFile.delete(); if (!prevBackupFileDeleted) { LOG.error("Could not delete previous backup file: " + previousBackupFile.getAbsolutePath()); return null; } boolean prevBackupFilePDeleted = previousBackupFileP.delete(); if (!prevBackupFilePDeleted) { LOG.error("Could not delete previous backup .p file: " + previousBackupFileP.getAbsolutePath()); return null; } } LOG.debug("Will rename previous backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); boolean backupFileRenamed = backupFile.renameTo(previousBackupFile); if (!backupFileRenamed) { LOG.error("Failed to rename backup file from " + backupFile.getName() + " to " + previousBackupFile.getName()); return null; } boolean backupFilePRenamed = backupFileP.renameTo(previousBackupFileP); if (!backupFilePRenamed) { LOG.error("Failed to rename backup .p file from " + backupFile.getName() + " to " + previousBackupFileP.getName()); return null; } backupFile = new File(backupFileName); } return backupFile; } StatisticDataRepositoryMapDB(String dbFileName); @Override void openForRead(); @Override void openForWrite(boolean cacheInMemoryDumpToDiskOnClose); @Override void close(); @Override Set<String> getStatisticNames(); @Override long getNumberOfCells(String statisticName); @Override DatasetMetaData getMetaData(); @Override void putMetaData(DatasetMetaData datasetMetadata); @Override StatisticData getStatisticData(String statisticName, long cellId); @Override void putStatisticData(String statisticName, long cellId, StatisticData statistics); @Override Set<Long> getCellsWithData(String statisticName); @Override StatisticData getStatisticDataForRandomCell(String statisticName);  }