Class FileMemData


  • class FileMemData
    extends java.lang.Object
    This class contains the data of an in-memory random access file. Data compression using the LZF algorithm is supported as well.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  FileMemData.Cache<K,​V>
      This small cache compresses the data if an element leaves the cache.
      (package private) static class  FileMemData.CompressItem
      Points to a block of bytes that needs to be compressed.
    • Constructor Summary

      Constructors 
      Constructor Description
      FileMemData​(java.lang.String name, boolean compress)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) boolean canWrite()
      Check whether writing is allowed.
      private void changeLength​(long len)  
      (package private) void compress​(int page)
      Compress the data in a byte array.
      private void compressLater​(int page)  
      private byte[] expand​(int page)  
      (package private) int getId()  
      (package private) long getLastModified()
      Get the last modified time.
      (package private) java.lang.String getName()
      Get the file name
      private byte[] getPage​(int page)
      Get the page if it exists.
      (package private) long length()
      Get the file length.
      (package private) boolean lockExclusive()
      Lock the file in exclusive mode if possible.
      (package private) boolean lockShared()
      Lock the file in shared mode if possible.
      (package private) long readWrite​(long pos, byte[] b, int off, int len, boolean write)
      Read or write.
      (package private) void setName​(java.lang.String name)
      Set the file name.
      private void setPage​(int page, byte[] oldData, byte[] newData, boolean force)
      Set the page data.
      (package private) boolean setReadOnly()
      Set the read-only flag.
      (package private) void touch​(boolean openReadOnly)
      Update the last modified time.
      (package private) void truncate​(long newLength)
      Truncate the file.
      (package private) void unlock()
      Unlock the file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • BUFFER

        private static final byte[] BUFFER
      • COMPRESSED_EMPTY_BLOCK

        private static final byte[] COMPRESSED_EMPTY_BLOCK
      • name

        private java.lang.String name
      • id

        private final int id
      • compress

        private final boolean compress
      • length

        private volatile long length
      • data

        private java.util.concurrent.atomic.AtomicReference<byte[]>[] data
      • lastModified

        private long lastModified
      • isReadOnly

        private boolean isReadOnly
      • isLockedExclusive

        private boolean isLockedExclusive
      • sharedLockCount

        private int sharedLockCount
    • Constructor Detail

      • FileMemData

        FileMemData​(java.lang.String name,
                    boolean compress)
    • Method Detail

      • getPage

        private byte[] getPage​(int page)
        Get the page if it exists.
        Parameters:
        page - the page id
        Returns:
        the byte array, or null
      • setPage

        private void setPage​(int page,
                             byte[] oldData,
                             byte[] newData,
                             boolean force)
        Set the page data.
        Parameters:
        page - the page id
        oldData - the old data
        newData - the new data
        force - whether the data should be overwritten even if the old data doesn't match
      • getId

        int getId()
      • lockExclusive

        boolean lockExclusive()
        Lock the file in exclusive mode if possible.
        Returns:
        if locking was successful
      • lockShared

        boolean lockShared()
        Lock the file in shared mode if possible.
        Returns:
        if locking was successful
      • unlock

        void unlock()
             throws java.io.IOException
        Unlock the file.
        Throws:
        java.io.IOException
      • compressLater

        private void compressLater​(int page)
      • expand

        private byte[] expand​(int page)
      • compress

        void compress​(int page)
        Compress the data in a byte array.
        Parameters:
        page - which page to compress
      • touch

        void touch​(boolean openReadOnly)
        Update the last modified time.
        Parameters:
        openReadOnly - if the file was opened in read-only mode
      • length

        long length()
        Get the file length.
        Returns:
        the length
      • truncate

        void truncate​(long newLength)
        Truncate the file.
        Parameters:
        newLength - the new length
      • changeLength

        private void changeLength​(long len)
      • readWrite

        long readWrite​(long pos,
                       byte[] b,
                       int off,
                       int len,
                       boolean write)
        Read or write.
        Parameters:
        pos - the position
        b - the byte array
        off - the offset within the byte array
        len - the number of bytes
        write - true for writing
        Returns:
        the new position
      • setName

        void setName​(java.lang.String name)
        Set the file name.
        Parameters:
        name - the name
      • getName

        java.lang.String getName()
        Get the file name
        Returns:
        the name
      • getLastModified

        long getLastModified()
        Get the last modified time.
        Returns:
        the time
      • canWrite

        boolean canWrite()
        Check whether writing is allowed.
        Returns:
        true if it is
      • setReadOnly

        boolean setReadOnly()
        Set the read-only flag.
        Returns:
        true