public OMultiFileSegment( final OStorageLocal iStorage, final OStorageSegmentConfiguration iConfig, final String iFileExtension, final int iRoundMaxSize) throws IOException { super(iStorage, iConfig.name); config = iConfig; fileExtension = iFileExtension; type = iConfig.fileType; defrag = iConfig.defrag; maxSize = OFileUtils.getSizeAsNumber(iConfig.maxSize); fileStartSize = (int) OFileUtils.getSizeAsNumber(iConfig.fileStartSize); final int tmpFileMaxSize = (int) OFileUtils.getSizeAsNumber(iConfig.fileMaxSize); fileIncrementSize = (int) OFileUtils.getSizeAsNumber(iConfig.fileIncrementSize); if (iRoundMaxSize > 0) // ROUND THE FILE SIZE TO AVOID ERRORS ON ROUNDING BY DIVIDING FOR FIXED RECORD SIZE fileMaxSize = (tmpFileMaxSize / iRoundMaxSize) * iRoundMaxSize; else fileMaxSize = tmpFileMaxSize; // INSTANTIATE ALL THE FILES int perFileMaxSize; if (iConfig.infoFiles.length == 0) { // EMPTY FILE: CREATE THE FIRST FILE BY DEFAULT files = new OFile[1]; files[0] = OFileFactory.instance() .create( type, iStorage .getVariableParser() .resolveVariables( config.getLocation() + "/" + name + "." + 0 + fileExtension), iStorage.getMode()); perFileMaxSize = fileMaxSize; files[0].setMaxSize(perFileMaxSize); files[0].setIncrementSize(fileIncrementSize); } else { files = new OFile[iConfig.infoFiles.length]; for (int i = 0; i < files.length; ++i) { files[i] = OFileFactory.instance() .create( type, iStorage.getVariableParser().resolveVariables(iConfig.infoFiles[i].path), iStorage.getMode()); perFileMaxSize = fileMaxSize; files[i].setMaxSize(perFileMaxSize); files[i].setIncrementSize(fileIncrementSize); } } }
private OFile createNewFile() throws IOException { final int num = files.length - 1; final OFile file = OFileFactory.instance() .create( type, config.getLocation() + "/" + name + "." + num + fileExtension, storage.getMode()); file.setMaxSize(fileMaxSize); file.create(fileStartSize); files[num] = file; addInfoFileConfigEntry(file); return file; }
private void addInfoFileConfigEntry(final OFile file) throws IOException { OStorageFileConfiguration[] newConfigFiles = new OStorageFileConfiguration[config.infoFiles.length + 1]; for (int i = 0; i < config.infoFiles.length; ++i) newConfigFiles[i] = config.infoFiles[i]; config.infoFiles = newConfigFiles; // CREATE A NEW ENTRY FOR THE NEW FILE String fileNameToStore = storage.getVariableParser().convertPathToRelative(OFileUtils.getPath(file.getPath())); final OStorageSegmentConfiguration template = config.root.fileTemplate; config.infoFiles[config.infoFiles.length - 1] = new OStorageFileConfiguration( config, fileNameToStore, template.fileType, template.fileMaxSize, template.fileIncrementSize); }
public void truncate() throws IOException { // SHRINK TO 0 files[0].shrink(0); if (files.length > 1) { // LEAVE JUST ONE FILE for (int i = 1; i < files.length; ++i) { if (files[i] != null) files[i].delete(); } // UPDATE FILE STRUCTURE final OFile f = files[0]; files = new OFile[1]; files[0] = f; // UPDATE CONFIGURATION final OStorageFileConfiguration fileConfig = config.infoFiles[0]; config.infoFiles = new OStorageFileConfiguration[1]; config.infoFiles[0] = fileConfig; config.root.update(); } }