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
Modifier and TypeClassDescriptionstatic interface
A simple interface that allows for dependency injection -
Field Summary
-
Constructor Summary
ConstructorDescriptionLocalBufferFile
(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 void
cleanupOldPreSaveFiles
(File dir, long beforeNow) Attempt to remove all pre-save files.void
Deletes all parametersvoid
clone
(File destinationFile, TaskMonitor monitor) Clone this buffer file to the specified file.void
close()
Close the buffer file.static void
copyFile
(BufferFile srcFile, BufferFile destFile, ChangeMap changeMap, TaskMonitor monitor) Copy the complete content of a specified srcFile into a destFile excluding file ID.boolean
delete()
Delete this buffer file if writable.void
dispose()
Dispose of this buffer file object.get
(DataBuffer buf, int index) Get the specified buffer.static BufferFileBlock
getBufferFileBlock
(DataBuffer buf, int bufferSize) Generate a BufferFileBlock instance which corresponds to the specified DataBuffer based upon LocalBufferFile block usage.int
Return the actual size of a user data buffer.static DataBuffer
getDataBuffer
(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.int
Returns 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 fileint
getParameter
(String name) Get a the stored value for a named parameter.String[]
Returns a list of all parameter names.boolean
Returns true if this file may not be modified via the buffer put method.static DataBuffer
Read a buffer from an existing buffer file.static void
poke
(File file, int bufferIndex, DataBuffer buf) Modify an existing buffer file.void
put
(DataBuffer buf, int index) Store a data buffer at the specified block index.void
setFreeIndexes
(int[] indexes) Sets the list of free buffer indexes.void
setParameter
(String name, int value) Set the integer value for a named parameter.boolean
If 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:BufferFile
Returns 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:
isReadOnly
in interfaceBufferFile
-
getParameter
Description copied from interface:BufferFile
Get a the stored value for a named parameter.- Specified by:
getParameter
in interfaceBufferFile
- Parameters:
name
- parameter name- Returns:
- integer value
- Throws:
NoSuchElementException
- thrown if parameter not found
-
setParameter
Description copied from interface:BufferFile
Set the integer value for a named parameter.- Specified by:
setParameter
in interfaceBufferFile
- Parameters:
name
- parameter namevalue
- parameter value
-
clearParameters
public void clearParameters()Description copied from interface:BufferFile
Deletes all parameters- Specified by:
clearParameters
in interfaceBufferFile
-
getParameterNames
Description copied from interface:BufferFile
Returns a list of all parameter names.- Specified by:
getParameterNames
in interfaceBufferFile
-
getFreeIndexes
public int[] getFreeIndexes()Description copied from interface:BufferFile
Returns the list of free indexes sorted by value. The management of the free-index-list is implementation specific.- Specified by:
getFreeIndexes
in interfaceBufferFile
-
setFreeIndexes
public void setFreeIndexes(int[] indexes) Description copied from interface:BufferFile
Sets the list of free buffer indexes. The management of the free-index-list is implementation specific.- Specified by:
setFreeIndexes
in 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:BufferFile
Get 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:
get
in 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:BufferFile
Store a data buffer at the specified block index.- Specified by:
put
in interfaceBufferFile
- Parameters:
buf
- data bufferindex
- block index- Throws:
IOException
- thrown if an IO error occurs
-
getBufferSize
public int getBufferSize()Description copied from interface:BufferFile
Return the actual size of a user data buffer. This value should be used when constructing DataBuffer objects.- Specified by:
getBufferSize
in interfaceBufferFile
- Returns:
- DataBuffer data size as a number of bytes
-
getIndexCount
public int getIndexCount()Description copied from interface:BufferFile
Returns 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:
getIndexCount
in interfaceBufferFile
-
dispose
public void dispose()Description copied from interface:BufferFile
Dispose 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:
dispose
in interfaceBufferFile
-
setReadOnly
Description copied from interface:BufferFile
If 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:
setReadOnly
in 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:BufferFile
Close 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:
close
in interfaceBufferFile
- Throws:
IOException
- if an I/O error occurs
-
delete
public boolean delete()Description copied from interface:BufferFile
Delete this buffer file if writable. Once deleted, this object is immediately disposed and may no longer be used.- Specified by:
delete
in 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
-