/** * Returns a BlockFile which represents an open file with the specified file * name. If the file does not exist it will be created. This factory method * allows the type of implementation to be selected. * * @param fileName the name of the file to open. * @param blockSize the size of a block in bytes. * @param ioType The type of access to use on the file, memory mapped or with explicit IO. * @return the open BlockFile. * @throws IOException if an I/O error occurs. */ public static BlockFile openBlockFile( String fileName, int blockSize, IOType ioType ) throws IOException { return openBlockFile(new File(fileName), blockSize, ioType); }
/** * Truncates the file to zero length. * * @throws IOException if an I/O error occurs. */ public void clear() throws IOException { int maxNrRegions = INITIAL_NR_REGIONS; mappedByteBuffers = new MappedByteBuffer[maxNrRegions]; srcByteBuffers = new ByteBuffer[maxNrRegions]; intBuffers = new IntBuffer[maxNrRegions]; longBuffers = new LongBuffer[maxNrRegions]; nrMappedRegions = 0; /* if (System.getProperty("os.name").startsWith("Win")) { // This is needed for Windows. System.gc(); try { Thread.sleep(100); } catch (InterruptedException ie) { } System.runFinalization(); } */ super.clear(); }
/** * Close and delete the block file. * * @throws IOException if an I/O error occurs */ public synchronized void delete() throws IOException { try { close(); } finally { if (file != null) { int retries = 10; while (!file.delete() && file.isFile() && retries-- > 0) { // Causing any MappedByteBuffers to be unmapped may allow the // file to be deleted. This may be needed for Windows. System.gc(); try { Thread.sleep(100); } catch (InterruptedException ie) { } System.runFinalization(); } if (retries < 0) { logger.warn("Failed to delete: " + file); } file = null; } } }
unmap(); if (!ensureOpen()) { throw ex;
/** * Ensures that all data for this BlockFile is stored in persistent storage * before returning. * * @throws IOException if an I/O error occurs. */ public void force() throws IOException { for (;;) { try { fc.force(true); break; } catch (ClosedChannelException ex) { // The Channel may have been inadvertently closed by another thread // being interrupted. Attempt to reopen the channel. if (!ensureOpen()) { throw ex; } // Loop back and retry the force(). } } }
/** * Sets the length of the file in blocks. The file will not be expanded if it * is already large enough. The file will be truncated to the correct length * when {@link #close} is called. * * @param nrBlocks the length of the file in blocks. * @throws IOException if an I/O error occurs. */ public void setNrBlocks(long nrBlocks) throws IOException { if (nrBlocks == this.nrBlocks) return; long prevNrBlocks = this.nrBlocks; super.setNrBlocks(nrBlocks); if (nrBlocks <= prevNrBlocks) return; // Call mapFile() if the file must grow in size. int nrRegions = (nrBlocks > 0) ? (int) ((((nrBlocks - 1) * blockSize) / stride) + 1) : 0; if (nrRegions > nrMappedRegions) { mapFile(nrRegions); } }
/** * Ensures that all data for this BlockFile is stored in persistent storage * before returning. * * @throws IOException if an I/O error occurs. */ public synchronized void force() throws IOException { for (int i = 0; i < nrMappedRegions; ++i) { mappedByteBuffers[i].force(); } super.force(); // force file metadata }
if (!ensureOpen()) { throw ex;
/** * Sets the length of the file in blocks. * * @param nrBlocks The number of blocks in the file. * @throws IOException if an I/O error occurs. */ public void setNrBlocks(long nrBlocks) throws IOException { if (nrBlocks == this.nrBlocks) return; super.setNrBlocks(nrBlocks); if (nrBlocks <= allocatedNrBlocks) return; allocatedNrBlocks = nrBlocks - (nrBlocks % allocationSize) + allocationSize; for (;;) { try { raf.setLength(allocatedNrBlocks * blockSize); break; } catch (ClosedChannelException ex) { // The Channel may have been inadvertently closed by another thread // being interrupted. Attempt to reopen the channel. if (!ensureOpen()) { throw ex; } // Loop back and retry the setLength(). } } }
/** * Constructs a ManagedBlockFile with the specified file. * * @param objectPool an objectPool for this thread. * @param file the name of the BlockFile. * @param blockSize the block size in bytes. * @param ioType the type of I/O mechanism to use. * @throws IOException if an I/O error occurs. */ public ManagedBlockFile( ObjectPool objectPool, File file, int blockSize, BlockFile.IOType ioType ) throws IOException { this.file = file; File freeListFile = new File(file + FREELIST_EXT); if (file.exists() != freeListFile.exists()) { logger.error( "ERROR: inconsistency between Block file and Free List file" ); } blockFile = AbstractBlockFile.openBlockFile(file, blockSize, ioType); freeList = FreeList.openFreeList(objectPool, freeListFile); isOpen = true; }
/** * Constructs a FreeList which uses the specified file (if it exists) or * creates a new file (if it doesn't already exist). * * @param objectPool the ObjectPool to use when allocating Blocks. * @param file the file. * @param ioType the IOType to use for the BlockFile. * @throws IOException if an I/O error occurs. */ private FreeList( ObjectPool objectPool, File file, BlockFile.IOType ioType ) throws IOException { this.objectPool = objectPool; objectPool.incRefCount(); this.file = file; blockFile = AbstractBlockFile.openBlockFile(file, BLOCK_SIZE_B, ioType); itemToPhaseSeqMap = IntFile.open(file + INTFILE_EXT); itemToPhaseSeqMap.clear(); }
throws IOException, SimpleXAResourceException { if (metarootFile == null) { metarootFile = AbstractBlockFile.openBlockFile( fileName + ".sp", METAROOT_SIZE * Constants.SIZEOF_LONG, BlockFile.IOType.EXPLICIT
throws IOException, SimpleXAResourceException { if (metarootFile == null) { metarootFile = AbstractBlockFile.openBlockFile( fileName + ".np", METAROOT_SIZE * Constants.SIZEOF_LONG, BlockFile.IOType.EXPLICIT