Package db.buffers
Class LocalBufferFile
java.lang.Object
db.buffers.LocalBufferFile
- All Implemented Interfaces:
BufferFile
- Direct Known Subclasses:
LocalManagedBufferFile
LocalBufferFile implements a BufferFile as block-oriented
random-access file. This type of buffer file supports save-as but does
not support the save operation.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA simple interface that allows for dependency injection -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionLocalBufferFile(File file, boolean readOnly) Open an existing block file.LocalBufferFile(File file, int bufferSize) Create a new buffer file for writing. -
Method Summary
Modifier and TypeMethodDescriptionstatic voidcleanupOldPreSaveFiles(File dir, long beforeNow) Attempt to remove all pre-save files.voidDeletes all parametersvoidclone(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 specified srcFile into a destFile excluding file ID.booleandelete()Delete this buffer file if writable.voiddispose()Dispose of this buffer file object.get(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.intReturn 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.getFile()Returns the physical file associated with this BufferFile.int[]Returns the list of free indexes sorted by value.intReturns the number of allocated buffer indexes.Obtain a direct stream to read all blocks of this buffer filegetOutputBlockStream(int blockCount) Obtain a direct stream to write blocks to this buffer fileintgetParameter(String name) Get a the stored value for a named parameter.String[]Returns a list of all parameter names.booleanReturns true if this file may not be modified via the buffer put method.static DataBufferRead a buffer from an existing buffer file.static voidpoke(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(String name, int value) Set the integer value for a named parameter.booleanIf file is open read-write, the modified contents are flushed and the file re-opened as read-only.toString()
-
Field Details
-
BUFFER_FILE_EXTENSION
- See Also:
-
PRESAVE_FILE_EXT
- See Also:
-
PRESAVE_FILE_PREFIX
- See Also:
-
TEMP_FILE_EXT
- See Also:
-
-
Constructor Details
-
LocalBufferFile
Create 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 existsIOException- if an I/O error occurs during file creation
-
LocalBufferFile
Open an existing block file.- Parameters:
file- block filereadOnly- if true the file will be opened read-only- Throws:
IOException- if an error occurs or the incorrect magicNumber was read from the file.
-
-
Method Details
-
poke
Modify an existing buffer file.WARNING! Use with extreme caution since this modifies the original file and could destroy data if used improperly.
- Parameters:
file- block filebufferIndex- the index at which to place the bufferbuf- the buffer add to the file- Throws:
IOException- if an I/O error occurs
-
peek
Read a buffer from an existing buffer file.- Parameters:
file- block filebufferIndex- the index from which to read the buffer- Returns:
- the buffer
- Throws:
IOException- if an I/O error occurs
-
getFile
Returns the physical file associated with this BufferFile.- Returns:
- the file
-
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
Description copied from interface:BufferFileGet a the stored value for a named parameter.- Specified by:
getParameterin interfaceBufferFile- Parameters:
name- parameter name- Returns:
- integer value
- Throws:
NoSuchElementException- thrown if parameter not found
-
setParameter
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
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- Parameters:
indexes-
-
getDataBuffer
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
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
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:
IOException- if an I/O error occurs
-
put
Description copied from interface:BufferFileStore a data buffer at the specified block index.- Specified by:
putin interfaceBufferFile- Parameters:
buf- data bufferindex- block index- Throws:
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
-
setReadOnly
Description 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:
IOException- if an I/O error occurs
-
close
Description 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:
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
Clone 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:
IOException- if IO error occurs.CancelledException- if the monitor cancels the operation.
-
toString
-
getInputBlockStream
Obtain a direct stream to read all blocks of this buffer file- Returns:
- input block stream
- Throws:
IOException- if there is an exception creating the stream
-
getOutputBlockStream
Obtain a direct stream to write blocks to this buffer file- Parameters:
blockCount- number of blocks to be transferred- Returns:
- output block stream
- Throws:
IOException- if an I/O error occurs
-
copyFile
public static void copyFile(BufferFile srcFile, BufferFile destFile, ChangeMap changeMap, TaskMonitor monitor) throws IOException, CancelledException Copy the complete content of a specified 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:
IOException- if IO error occurs.CancelledException- if the monitor cancels the operation.
-
cleanupOldPreSaveFiles
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 pre-save filesbeforeNow- if not 0, file mod time must be less than the specified time
-