Package db.buffers
Class LocalBufferFile
- java.lang.Object
-
- db.buffers.LocalBufferFile
-
- All Implemented Interfaces:
BufferFile
- Direct Known Subclasses:
LocalManagedBufferFile
public class LocalBufferFile extends java.lang.Object implements BufferFile
LocalBufferFileimplements a BufferFile as block-oriented random-access file. This type of buffer file supports save-as but does not support the save operation.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringBUFFER_FILE_EXTENSIONstatic java.lang.StringPRESAVE_FILE_EXTstatic java.lang.StringPRESAVE_FILE_PREFIXstatic java.lang.StringTEMP_FILE_EXT
-
Constructor Summary
Constructors Constructor Description LocalBufferFile(java.io.File file, boolean readOnly)Open an existing block file.LocalBufferFile(java.io.File file, int bufferSize)Create a new buffer file for writing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static voidcleanupOldPreSaveFiles(java.io.File dir, long beforeNow)Attempt to remove all pre-save files.voidclearParameters()Deletes all parametersvoidclone(java.io.File destinationFile, TaskMonitor monitor)Clone this buffer file to the specified file.voidclose()Close the buffer file.static voidcopyFile(BufferFile srcFile, BufferFile destFile, ChangeMap changeMap, TaskMonitor monitor)Copy the complete content of a specfied srcFile into a destFile excluding file ID.booleandelete()Delete this buffer file if writable.voiddispose()Dispose of this buffer file object.protected voidfinalize()DataBufferget(DataBuffer buf, int index)Get the specified buffer.static BufferFileBlockgetBufferFileBlock(DataBuffer buf, int bufferSize)Generate a BufferFileBlock instance which corresponds to the specified DataBuffer based upon LocalBufferFile block usage.intgetBufferSize()Return the actual size of a user data buffer.static DataBuffergetDataBuffer(BufferFileBlock block)Generate a DataBuffer instance which corresponds to the specified block based upon LocalBufferFile block usage.java.io.FilegetFile()Returns the physical file associated with this BufferFile.int[]getFreeIndexes()Returns the list of free indexes sorted by value.intgetIndexCount()Returns the number of allocated buffer indexes.InputBlockStreamgetInputBlockStream()Obtain a direct stream to read all blocks of this buffer fileOutputBlockStreamgetOutputBlockStream(int blockCount)Obtain a direct stream to write blocks to this buffer fileintgetParameter(java.lang.String name)Get a the stored value for a named parameter.java.lang.String[]getParameterNames()Returns a list of all parameter names.booleanisReadOnly()Returns true if this file may not be modified via the buffer put method.static DataBufferpeek(java.io.File file, int bufferIndex)Read a buffer from an existing buffer file.static voidpoke(java.io.File file, int bufferIndex, DataBuffer buf)Modify an existing buffer file.voidput(DataBuffer buf, int index)Store a data buffer at the specified block index.voidsetFreeIndexes(int[] indexes)Sets the list of free buffer indexes.voidsetParameter(java.lang.String name, int value)Set the integer value for a named parameter.booleansetReadOnly()If file is open read-write, the modified contents are flushed and the file re-opened as read-only.java.lang.StringtoString()
-
-
-
Field Detail
-
BUFFER_FILE_EXTENSION
public static final java.lang.String BUFFER_FILE_EXTENSION
- See Also:
- Constant Field Values
-
PRESAVE_FILE_EXT
public static final java.lang.String PRESAVE_FILE_EXT
- See Also:
- Constant Field Values
-
PRESAVE_FILE_PREFIX
public static final java.lang.String PRESAVE_FILE_PREFIX
- See Also:
- Constant Field Values
-
TEMP_FILE_EXT
public static final java.lang.String TEMP_FILE_EXT
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
LocalBufferFile
public LocalBufferFile(java.io.File file, int bufferSize) throws java.io.IOExceptionCreate a new buffer file for writing. If the file does not exist and create is true, a new buffer file will be created. The file will be saved when closed.- Parameters:
file- buffer filebufferSize- user buffer size- Throws:
DuplicateFileException- if file already existsjava.io.IOException- if an I/O error occurs during file creation
-
LocalBufferFile
public LocalBufferFile(java.io.File file, boolean readOnly) throws java.io.IOExceptionOpen an existing block file.- Parameters:
file- block filereadOnly- if true the file will be opened read-only- Throws:
java.io.IOException- if an IO error occurs or the incorrect magicNumber was read from the file.
-
-
Method Detail
-
poke
public static void poke(java.io.File file, int bufferIndex, DataBuffer buf) throws java.io.IOExceptionModify an existing buffer file. WARNING! Use with extreme caution since this modifies the original file and could destroy data if used improperly.- Parameters:
file-bufferIndex-buf-- Throws:
java.io.IOException
-
peek
public static DataBuffer peek(java.io.File file, int bufferIndex) throws java.io.IOException
Read a buffer from an existing buffer file.- Parameters:
file-bufferIndex-- Returns:
- Throws:
java.io.IOException
-
getFile
public java.io.File getFile()
Returns the physical file associated with this BufferFile.
-
isReadOnly
public boolean isReadOnly()
Description copied from interface:BufferFileReturns true if this file may not be modified via the buffer put method. A read-only file may be considered "updateable" if the canSave method returns true. The term "updateable" means that a Save file can be obtained via the getSaveFile method.- Specified by:
isReadOnlyin interfaceBufferFile
-
getParameter
public int getParameter(java.lang.String name) throws java.util.NoSuchElementExceptionDescription copied from interface:BufferFileGet a the stored value for a named parameter.- Specified by:
getParameterin interfaceBufferFile- Parameters:
name- parameter name- Returns:
- integer value
- Throws:
java.util.NoSuchElementException- thrown if parameter not found
-
setParameter
public void setParameter(java.lang.String name, int value)Description copied from interface:BufferFileSet the integer value for a named parameter.- Specified by:
setParameterin interfaceBufferFile- Parameters:
name- parameter namevalue- parameter value
-
clearParameters
public void clearParameters()
Description copied from interface:BufferFileDeletes all parameters- Specified by:
clearParametersin interfaceBufferFile
-
getParameterNames
public java.lang.String[] getParameterNames()
Description copied from interface:BufferFileReturns a list of all parameter names.- Specified by:
getParameterNamesin interfaceBufferFile
-
getFreeIndexes
public int[] getFreeIndexes()
Description copied from interface:BufferFileReturns the list of free indexes sorted by value. The management of the free-index-list is implementation specific.- Specified by:
getFreeIndexesin interfaceBufferFile
-
setFreeIndexes
public void setFreeIndexes(int[] indexes)
Description copied from interface:BufferFileSets the list of free buffer indexes. The management of the free-index-list is implementation specific.- Specified by:
setFreeIndexesin interfaceBufferFile
-
getDataBuffer
public static DataBuffer getDataBuffer(BufferFileBlock block)
Generate a DataBuffer instance which corresponds to the specified block based upon LocalBufferFile block usage.- Parameters:
block- the buffer file block to be converted- Returns:
- DataBuffer instance or null if head block. If empty block DataBuffer will have null data
-
getBufferFileBlock
public static BufferFileBlock getBufferFileBlock(DataBuffer buf, int bufferSize)
Generate a BufferFileBlock instance which corresponds to the specified DataBuffer based upon LocalBufferFile block usage. This should generally not be used for writing empty blocks since they will not be properly linked which is normally handled during header flush which is performed by BufferFile close on files being written.- Parameters:
buf- the data buffer to be convertedbufferSize- data buffer size used for integrity check and generating empty buffer- Returns:
- BufferFileBlock instance.
-
get
public DataBuffer get(DataBuffer buf, int index) throws java.io.IOException
Description copied from interface:BufferFileGet the specified buffer. DataBuffer data and flags are read from the file at index and stored within the supplied DataBuffer object. If the read buffer is empty, the DataBuffer's data field will remain unchanged (which could be null).- Specified by:
getin interfaceBufferFile- Parameters:
buf- a buffer whose data array will be filled-in or replaced.index- index of buffer to be read. First user buffer is at index 0.- Throws:
java.io.EOFException- if the requested buffer index is greater than the number of available buffers of the end-of-file was encountered while reading the buffer.java.io.IOException- if an I/O error occurs
-
put
public void put(DataBuffer buf, int index) throws java.io.IOException
Description copied from interface:BufferFileStore a data buffer at the specified block index.- Specified by:
putin interfaceBufferFile- Parameters:
buf- data bufferindex- block index- Throws:
java.io.IOException- thrown if an IO error occurs
-
getBufferSize
public int getBufferSize()
Description copied from interface:BufferFileReturn the actual size of a user data buffer. This value should be used when constructing DataBuffer objects.- Specified by:
getBufferSizein interfaceBufferFile- Returns:
- DataBuffer data size as a number of bytes
-
getIndexCount
public int getIndexCount()
Description copied from interface:BufferFileReturns the number of allocated buffer indexes. When a new buffer is allocated, and the file size grows, the buffer will remain allocated although it may be added to the list of free-indexes. A file will never shrink in size due to this permanent allocation.- Specified by:
getIndexCountin interfaceBufferFile
-
dispose
public void dispose()
Description copied from interface:BufferFileDispose of this buffer file object. If file is not readOnly and has not been closed, an attempt will be made to delete the associated file(s). Once disposed, it may no longer be used.- Specified by:
disposein interfaceBufferFile
-
finalize
protected void finalize() throws java.lang.Throwable- Overrides:
finalizein classjava.lang.Object- Throws:
java.lang.Throwable
-
setReadOnly
public boolean setReadOnly() throws java.io.IOExceptionDescription copied from interface:BufferFileIf file is open read-write, the modified contents are flushed and the file re-opened as read-only. This is also used to commit a new version if the file had been modified for update.- Specified by:
setReadOnlyin interfaceBufferFile- Returns:
- true if successfully transitioned from read-write to read-only
- Throws:
java.io.IOException- if an I/O error occurs
-
close
public void close() throws java.io.IOExceptionDescription copied from interface:BufferFileClose the buffer file. If the file was open for write access, all buffers are flushed and the file header updated. Once closed, this object is immediately disposed and may no longer be used.- Specified by:
closein interfaceBufferFile- Throws:
java.io.IOException- if an I/O error occurs
-
delete
public boolean delete()
Description copied from interface:BufferFileDelete this buffer file if writable. Once deleted, this object is immediately disposed and may no longer be used.- Specified by:
deletein interfaceBufferFile- Returns:
- true if deleted, false if the file is read-only
-
clone
public void clone(java.io.File destinationFile, TaskMonitor monitor) throws java.io.IOException, CancelledExceptionClone this buffer file to the specified file. The file must not already exist. If the operation is cancelled or an error occurs the file is not created.- Parameters:
destinationFile- destination filemonitor- progress monitor- Throws:
java.io.IOException- if IO error occurs.CancelledException- if the monitor cancels the operation.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getInputBlockStream
public InputBlockStream getInputBlockStream() throws java.io.IOException
Obtain a direct stream to read all blocks of this buffer file- Returns:
- input block stream
- Throws:
java.io.IOException
-
getOutputBlockStream
public OutputBlockStream getOutputBlockStream(int blockCount) throws java.io.IOException
Obtain a direct stream to write blocks to this buffer file- Parameters:
blockCount- number of blocks to be transferred- Returns:
- output block stream
- Throws:
java.io.IOException
-
copyFile
public static void copyFile(BufferFile srcFile, BufferFile destFile, ChangeMap changeMap, TaskMonitor monitor) throws java.io.IOException, CancelledException
Copy the complete content of a specfied srcFile into a destFile excluding file ID. Both files remain open.- Parameters:
srcFile- open buffer filedestFile- empty buffer file which is open for writing.changeMap- optional change map which indicates those buffers which must be copied. Any buffer index outside the range of the change map will also be copied.monitor- progress monitor- Throws:
java.io.IOException- if IO error occurs.CancelledException- if the monitor cancels the operation.
-
cleanupOldPreSaveFiles
public static void cleanupOldPreSaveFiles(java.io.File dir, long beforeNow)Attempt to remove all pre-save files. Those still open by an existing process should not be removed by the operating system.- Parameters:
dir- data directory containing presave filesbeforeNow- if not 0, file mod time must be less than the specified time
-
-