Class Chunk


  • public final class Chunk
    extends java.lang.Object
    A chunk of data, containing one or multiple pages.

    Minimum chunk size is usually 4096 bytes, and it grows in those fixed increments (blocks). Chunk's length and it's position in the underlying filestore are multiples of that increment (block size), therefore they both are measured in blocks, instead of bytes. There are at most 67 million (2^26) chunks, and each chunk is at most 2 GB large.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.String ATTR_BLOCK  
      private static java.lang.String ATTR_CHUNK  
      private static java.lang.String ATTR_FLETCHER  
      private static java.lang.String ATTR_LEN  
      private static java.lang.String ATTR_LIVE_MAX  
      private static java.lang.String ATTR_LIVE_PAGES  
      private static java.lang.String ATTR_MAP  
      private static java.lang.String ATTR_MAX  
      private static java.lang.String ATTR_NEXT  
      private static java.lang.String ATTR_OCCUPANCY  
      private static java.lang.String ATTR_PAGES  
      private static java.lang.String ATTR_PIN_COUNT  
      private static java.lang.String ATTR_ROOT  
      private static java.lang.String ATTR_TIME  
      private static java.lang.String ATTR_TOC  
      private static java.lang.String ATTR_UNUSED  
      private static java.lang.String ATTR_UNUSED_AT_VERSION  
      private static java.lang.String ATTR_VERSION  
      long block
      The start block number within the file.
      (package private) int collectPriority
      The garbage collection priority.
      (package private) static int FOOTER_LENGTH
      The length of the chunk footer.
      int id
      The chunk id.
      (package private) long layoutRootPos
      The position of the root of layout map.
      int len
      The length in number of blocks.
      int mapId
      The last used map id.
      (package private) static int MAX_HEADER_LENGTH
      The maximum length of a chunk header, in bytes.
      static int MAX_ID
      The maximum chunk id.
      long maxLen
      The sum of the max length of all pages.
      long maxLenLive
      The sum of the length of all pages that are still alive.
      long next
      The predicted position of the next chunk.
      (package private) java.util.BitSet occupancy
      Collection of "deleted" flags for all pages in the chunk.
      (package private) int pageCount
      The total number of pages in this chunk.
      (package private) int pageCountLive
      The number of pages that are still alive in the latest version of the store.
      private int pinCount
      Number of live pinned pages.
      long time
      When this chunk was created, in milliseconds after the store was created.
      (package private) int tocPos
      Offset (from the beginning of the chunk) for the table of content.
      long unused
      When this chunk was no longer needed, in milliseconds after the store was created.
      (package private) long unusedAtVersion
      Version of the store at which chunk become unused and therefore can be considered "dead" and collected after this version is no longer in use.
      long version
      The version stored in this chunk.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      (package private) Chunk​(int id)  
      private Chunk​(java.lang.String s)  
      (package private) Chunk​(java.util.Map<java.lang.String,​java.lang.String> map)  
      private Chunk​(java.util.Map<java.lang.String,​java.lang.String> map, boolean full)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) boolean accountForRemovedPage​(int pageNo, int pageLength, boolean pinned, long now, long version)
      Modifies internal state to reflect the fact that one the pages within this chunk was removed from the map.
      (package private) void accountForWrittenPage​(int pageLengthOnDisk, boolean singleWriter)
      Modifies internal state to reflect the fact that one more page is stored within this chunk.
      java.lang.String asString()
      Get the chunk data as a string.
      boolean equals​(java.lang.Object o)  
      static Chunk fromString​(java.lang.String s)
      Build a block from the given string.
      (package private) int getFillRate()
      Calculate the fill rate in %.
      (package private) byte[] getFooterBytes()  
      (package private) static java.lang.String getMetaKey​(int chunkId)
      Get the metadata key for the given chunk id.
      int hashCode()  
      private boolean isEvacuatable()  
      (package private) boolean isLive()  
      (package private) boolean isRewritable()  
      (package private) boolean isSaved()  
      (package private) java.nio.ByteBuffer readBufferForPage​(FileStore fileStore, int offset, long pos)
      Read a page of data into a ByteBuffer.
      (package private) static Chunk readChunkHeader​(java.nio.ByteBuffer buff, long start)
      Read the header from the byte buffer.
      (package private) long[] readToC​(FileStore fileStore)  
      java.lang.String toString()  
      (package private) void writeChunkHeader​(WriteBuffer buff, int minLength)
      Write the chunk header.
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • MAX_HEADER_LENGTH

        static final int MAX_HEADER_LENGTH
        The maximum length of a chunk header, in bytes.
        See Also:
        Constant Field Values
      • FOOTER_LENGTH

        static final int FOOTER_LENGTH
        The length of the chunk footer. The longest footer is: chunk:ffffffff,block:ffffffffffffffff, version:ffffffffffffffff,fletcher:ffffffff
        See Also:
        Constant Field Values
      • ATTR_UNUSED_AT_VERSION

        private static final java.lang.String ATTR_UNUSED_AT_VERSION
        See Also:
        Constant Field Values
      • id

        public final int id
        The chunk id.
      • block

        public volatile long block
        The start block number within the file.
      • len

        public int len
        The length in number of blocks.
      • pageCount

        int pageCount
        The total number of pages in this chunk.
      • pageCountLive

        int pageCountLive
        The number of pages that are still alive in the latest version of the store.
      • tocPos

        int tocPos
        Offset (from the beginning of the chunk) for the table of content. Table of content is holding a value of type "long" for each page in the chunk. This value consists of map id, page offset, page length and page type. Format is the same as page's position id, but with map id replacing chunk id.
        See Also:
        for field format details
      • occupancy

        java.util.BitSet occupancy
        Collection of "deleted" flags for all pages in the chunk.
      • maxLen

        public long maxLen
        The sum of the max length of all pages.
      • maxLenLive

        public long maxLenLive
        The sum of the length of all pages that are still alive.
      • collectPriority

        int collectPriority
        The garbage collection priority. Priority 0 means it needs to be collected, a high value means low priority.
      • layoutRootPos

        long layoutRootPos
        The position of the root of layout map.
      • version

        public long version
        The version stored in this chunk.
      • time

        public long time
        When this chunk was created, in milliseconds after the store was created.
      • unused

        public long unused
        When this chunk was no longer needed, in milliseconds after the store was created. After this, the chunk is kept alive a bit longer (in case it is referenced in older versions).
      • unusedAtVersion

        long unusedAtVersion
        Version of the store at which chunk become unused and therefore can be considered "dead" and collected after this version is no longer in use.
      • mapId

        public int mapId
        The last used map id.
      • next

        public long next
        The predicted position of the next chunk.
      • pinCount

        private int pinCount
        Number of live pinned pages.
    • Constructor Detail

      • Chunk

        private Chunk​(java.lang.String s)
      • Chunk

        Chunk​(java.util.Map<java.lang.String,​java.lang.String> map)
      • Chunk

        private Chunk​(java.util.Map<java.lang.String,​java.lang.String> map,
                      boolean full)
      • Chunk

        Chunk​(int id)
    • Method Detail

      • readChunkHeader

        static Chunk readChunkHeader​(java.nio.ByteBuffer buff,
                                     long start)
        Read the header from the byte buffer.
        Parameters:
        buff - the source buffer
        start - the start of the chunk in the file
        Returns:
        the chunk
      • writeChunkHeader

        void writeChunkHeader​(WriteBuffer buff,
                              int minLength)
        Write the chunk header.
        Parameters:
        buff - the target buffer
        minLength - the minimum length
      • getMetaKey

        static java.lang.String getMetaKey​(int chunkId)
        Get the metadata key for the given chunk id.
        Parameters:
        chunkId - the chunk id
        Returns:
        the metadata key
      • fromString

        public static Chunk fromString​(java.lang.String s)
        Build a block from the given string.
        Parameters:
        s - the string
        Returns:
        the block
      • getFillRate

        int getFillRate()
        Calculate the fill rate in %. 0 means empty, 100 means full.
        Returns:
        the fill rate
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • asString

        public java.lang.String asString()
        Get the chunk data as a string.
        Returns:
        the string
      • getFooterBytes

        byte[] getFooterBytes()
      • isSaved

        boolean isSaved()
      • isLive

        boolean isLive()
      • isRewritable

        boolean isRewritable()
      • isEvacuatable

        private boolean isEvacuatable()
      • readBufferForPage

        java.nio.ByteBuffer readBufferForPage​(FileStore fileStore,
                                              int offset,
                                              long pos)
        Read a page of data into a ByteBuffer.
        Parameters:
        fileStore - to use
        offset - of the page data
        pos - page pos
        Returns:
        ByteBuffer containing page data.
      • readToC

        long[] readToC​(FileStore fileStore)
      • accountForWrittenPage

        void accountForWrittenPage​(int pageLengthOnDisk,
                                   boolean singleWriter)
        Modifies internal state to reflect the fact that one more page is stored within this chunk.
        Parameters:
        pageLengthOnDisk - size of the page
        singleWriter - indicates whether page belongs to append mode capable map (single writer map). Such pages are "pinned" to the chunk, they can't be evacuated (moved to a different chunk) while
      • accountForRemovedPage

        boolean accountForRemovedPage​(int pageNo,
                                      int pageLength,
                                      boolean pinned,
                                      long now,
                                      long version)
        Modifies internal state to reflect the fact that one the pages within this chunk was removed from the map.
        Parameters:
        pageNo - sequential page number within the chunk
        pageLength - on disk of the removed page
        pinned - whether removed page was pinned
        now - is a moment in time (since creation of the store), when removal is recorded, and retention period starts
        version - at which page was removed
        Returns:
        true if all of the pages, this chunk contains, were already removed, and false otherwise
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object