java.lang.Object
org.aspectj.org.eclipse.jdt.internal.core.nd.db.Database

public class Database extends Object
Database encapsulates access to a flat binary format file with a memory-manager-like API for obtaining and releasing areas of storage (memory).

Some terminology is used throughout this class: A block is a variable-size piece of contiguous memory returned by malloc. A chunk is a fixed-size piece of contiguous memory that is the atomic unit for paging, caching, reads, and writes. A free block is contiguous variable-length piece of memory that is created by free and is potentially usable by malloc. Most chunks contain multiple blocks and free blocks, but it is possible for a single block to use multiple chunks. Such blocks are referred to as "large blocks". A free block is always smaller than a chunk.

  • Field Details

    • CHAR_SIZE

      public static final int CHAR_SIZE
      See Also:
      Constant Field Values
    • BYTE_SIZE

      public static final int BYTE_SIZE
      See Also:
      Constant Field Values
    • SHORT_SIZE

      public static final int SHORT_SIZE
      See Also:
      Constant Field Values
    • INT_SIZE

      public static final int INT_SIZE
      See Also:
      Constant Field Values
    • LONG_SIZE

      public static final int LONG_SIZE
      See Also:
      Constant Field Values
    • CHUNK_SIZE

      public static final int CHUNK_SIZE
      See Also:
      Constant Field Values
    • OFFSET_IN_CHUNK_MASK

      public static final int OFFSET_IN_CHUNK_MASK
      See Also:
      Constant Field Values
    • BLOCK_HEADER_SIZE

      public static final int BLOCK_HEADER_SIZE
      See Also:
      Constant Field Values
    • BLOCK_SIZE_DELTA_BITS

      public static final int BLOCK_SIZE_DELTA_BITS
      See Also:
      Constant Field Values
    • BLOCK_SIZE_DELTA

      public static final int BLOCK_SIZE_DELTA
      See Also:
      Constant Field Values
    • MIN_BLOCK_DELTAS

      public static final int MIN_BLOCK_DELTAS
      See Also:
      Constant Field Values
    • MAX_BLOCK_DELTAS

      public static final int MAX_BLOCK_DELTAS
    • MAX_SINGLE_BLOCK_MALLOC_SIZE

      public static final int MAX_SINGLE_BLOCK_MALLOC_SIZE
    • PTR_SIZE

      public static final int PTR_SIZE
      See Also:
      Constant Field Values
    • STRING_SIZE

      public static final int STRING_SIZE
      See Also:
      Constant Field Values
    • FLOAT_SIZE

      public static final int FLOAT_SIZE
      See Also:
      Constant Field Values
    • DOUBLE_SIZE

      public static final int DOUBLE_SIZE
      See Also:
      Constant Field Values
    • MAX_DB_SIZE

      public static final long MAX_DB_SIZE
      See Also:
      Constant Field Values
    • MAX_MALLOC_SIZE

      public static final long MAX_MALLOC_SIZE
    • VERSION_OFFSET

      public static final int VERSION_OFFSET
      See Also:
      Constant Field Values
    • MALLOC_TABLE_OFFSET

      public static final int MALLOC_TABLE_OFFSET
      See Also:
      Constant Field Values
    • FREE_BLOCK_OFFSET

      public static final int FREE_BLOCK_OFFSET
      See Also:
      Constant Field Values
    • WRITE_NUMBER_OFFSET

      public static final int WRITE_NUMBER_OFFSET
      See Also:
      Constant Field Values
    • MALLOC_STATS_OFFSET

      public static final int MALLOC_STATS_OFFSET
      See Also:
      Constant Field Values
    • DATA_AREA_OFFSET

      public static final int DATA_AREA_OFFSET
    • NUM_HEADER_CHUNKS

      public static final int NUM_HEADER_CHUNKS
      See Also:
      Constant Field Values
    • POOL_MISC

      public static final short POOL_MISC
      Misc pool -- may be used for any purpose that doesn't fit the IDs below.
      See Also:
      Constant Field Values
    • POOL_BTREE

      public static final short POOL_BTREE
      See Also:
      Constant Field Values
    • POOL_DB_PROPERTIES

      public static final short POOL_DB_PROPERTIES
      See Also:
      Constant Field Values
    • POOL_STRING_LONG

      public static final short POOL_STRING_LONG
      See Also:
      Constant Field Values
    • POOL_STRING_SHORT

      public static final short POOL_STRING_SHORT
      See Also:
      Constant Field Values
    • POOL_LINKED_LIST

      public static final short POOL_LINKED_LIST
      See Also:
      Constant Field Values
    • POOL_STRING_SET

      public static final short POOL_STRING_SET
      See Also:
      Constant Field Values
    • POOL_GROWABLE_ARRAY

      public static final short POOL_GROWABLE_ARRAY
      See Also:
      Constant Field Values
    • POOL_FIRST_NODE_TYPE

      public static final short POOL_FIRST_NODE_TYPE
      Id for the first node type. All node types will record their stats in a pool whose ID is POOL_FIRST_NODE_TYPE + node_id
      See Also:
      Constant Field Values
    • DEBUG_FREE_SPACE

      public static boolean DEBUG_FREE_SPACE
      True iff large chunk self-diagnostics should be enabled.
    • DEBUG_PAGE_CACHE

      public static boolean DEBUG_PAGE_CACHE
    • fMostRecentlyFetchedChunk

      public org.aspectj.org.eclipse.jdt.internal.core.nd.db.Chunk fMostRecentlyFetchedChunk
    • MIN_BYTES_PER_MILLISECOND

      public static final double MIN_BYTES_PER_MILLISECOND
      See Also:
      Constant Field Values
  • Constructor Details

    • Database

      public Database(File location, ChunkCache cache, int version, boolean openReadOnly) throws IndexException
      Construct a new Database object, creating a backing file if necessary.
      Parameters:
      location - the local file path for the database
      cache - the cache to be used optimization
      version - the version number to store in the database (only applicable for new databases)
      openReadOnly - whether this Database object will ever need writing to
      Throws:
      IndexException
  • Method Details

    • getLog

      public ModificationLog getLog()
    • transferTo

      public void transferTo(FileChannel target) throws IOException
      Throws:
      IOException
    • getVersion

      public int getVersion()
    • setVersion

      public void setVersion(int version) throws IndexException
      Throws:
      IndexException
    • clear

      public boolean clear(int version) throws IndexException
      Empty the contents of the Database, make it ready to start again. Interrupting the thread with Thread.interrupt() won't interrupt the write. Returns true iff the thread was interrupted with Thread.interrupt().
      Throws:
      IndexException
    • getChunk

      public org.aspectj.org.eclipse.jdt.internal.core.nd.db.Chunk getChunk(long offset) throws IndexException
      Return the Chunk that contains the given offset.
      Throws:
      IndexException
    • assertLocked

      public void assertLocked()
    • memcpy

      public void memcpy(long dest, long source, int numBytes)
      Copies numBytes from source to destination
    • malloc

      public long malloc(long datasize, short poolId) throws IndexException
      Allocate a block out of the database.
      Throws:
      IndexException
    • clearRange

      public void clearRange(long startAddress, long bytesToClear)
      Clears all the bytes in the given range by setting them to zero.
      Parameters:
      startAddress - first address to clear
      bytesToClear - number of addresses to clear
    • validateFreeSpace

      public void validateFreeSpace()
    • free

      public void free(long address, short poolId) throws IndexException
      Free an allocated block.
      Parameters:
      address - memory address to be freed
      poolId - the same ID that was previously passed into malloc when allocating this memory address
      Throws:
      IndexException
    • putByte

      public void putByte(long offset, byte value) throws IndexException
      Throws:
      IndexException
    • getByte

      public byte getByte(long offset) throws IndexException
      Throws:
      IndexException
    • putInt

      public void putInt(long offset, int value) throws IndexException
      Throws:
      IndexException
    • getInt

      public int getInt(long offset) throws IndexException
      Throws:
      IndexException
    • putRecPtr

      public void putRecPtr(long offset, long value) throws IndexException
      Throws:
      IndexException
    • getRecPtr

      public long getRecPtr(long offset) throws IndexException
      Throws:
      IndexException
    • put3ByteUnsignedInt

      public void put3ByteUnsignedInt(long offset, int value) throws IndexException
      Throws:
      IndexException
    • get3ByteUnsignedInt

      public int get3ByteUnsignedInt(long offset) throws IndexException
      Throws:
      IndexException
    • putShort

      public void putShort(long offset, short value) throws IndexException
      Throws:
      IndexException
    • getShort

      public short getShort(long offset) throws IndexException
      Throws:
      IndexException
    • putLong

      public void putLong(long offset, long value) throws IndexException
      Throws:
      IndexException
    • putDouble

      public void putDouble(long offset, double value) throws IndexException
      Throws:
      IndexException
    • putFloat

      public void putFloat(long offset, float value) throws IndexException
      Throws:
      IndexException
    • getLong

      public long getLong(long offset) throws IndexException
      Throws:
      IndexException
    • getDouble

      public double getDouble(long offset) throws IndexException
      Throws:
      IndexException
    • getFloat

      public float getFloat(long offset) throws IndexException
      Throws:
      IndexException
    • putChar

      public void putChar(long offset, char value) throws IndexException
      Throws:
      IndexException
    • getChar

      public char getChar(long offset) throws IndexException
      Throws:
      IndexException
    • clearBytes

      public void clearBytes(long offset, int byteCount) throws IndexException
      Throws:
      IndexException
    • putBytes

      public void putBytes(long offset, byte[] data, int len) throws IndexException
      Throws:
      IndexException
    • putBytes

      public void putBytes(long offset, byte[] data, int dataPos, int len) throws IndexException
      Throws:
      IndexException
    • getBytes

      public void getBytes(long offset, byte[] data) throws IndexException
      Throws:
      IndexException
    • getBytes

      public void getBytes(long offset, byte[] data, int dataPos, int len) throws IndexException
      Throws:
      IndexException
    • newString

      public IString newString(String string) throws IndexException
      Throws:
      IndexException
    • newString

      public IString newString(char[] chars) throws IndexException
      Throws:
      IndexException
    • getString

      public IString getString(long offset) throws IndexException
      Throws:
      IndexException
    • getDatabaseSize

      public long getDatabaseSize()
    • getBytesFreed

      public long getBytesFreed()
      Returns the number of bytes freed by free(long, short) since this Database instance was instantiated. Intended for use in unit tests.
    • getBytesAllocated

      public long getBytesAllocated()
      Returns the number of bytes allocated by malloc(long, short) since this Database instance was instantiated. Intended for use in unit tests.
    • reportFreeBlocks

      public void reportFreeBlocks() throws IndexException
      For debugging purposes, only.
      Throws:
      IndexException
    • close

      public void close() throws IndexException
      Closes the database.

      The behavior of any further calls to the Database is undefined

      Throws:
      IndexException
    • getLocation

      public File getLocation()
      This method is public for testing purposes only.
    • getChunkCache

      public ChunkCache getChunkCache()
      Returns the cache used for this database.
      Since:
      4.0
    • setExclusiveLock

      public void setExclusiveLock()
      Asserts that database is used by one thread exclusively. This is necessary when doing write operations.
    • setLocked

      public void setLocked(boolean val)
    • giveUpExclusiveLock

      public void giveUpExclusiveLock()
    • flush

      public boolean flush() throws IndexException
      Throws:
      IndexException
    • resetCacheCounters

      public void resetCacheCounters()
    • getBytesWritten

      public long getBytesWritten()
    • getAverageReadBytesPerMs

      public double getAverageReadBytesPerMs()
    • getAverageWriteBytesPerMs

      public double getAverageWriteBytesPerMs()
    • getBytesRead

      public long getBytesRead()
    • getCacheHits

      public long getCacheHits()
    • getCacheMisses

      public long getCacheMisses()
    • getCumulativeFlushTimeMs

      public long getCumulativeFlushTimeMs()
    • getSizeBytes

      public long getSizeBytes() throws IOException
      Throws:
      IOException
    • getChunkCount

      public int getChunkCount()
    • putRecPtr

      public static void putRecPtr(long value, byte[] buffer, int idx)
      A Record Pointer is a pointer as returned by Database.malloc(). This is a pointer to a block + BLOCK_HEADER_SIZE.
    • getRecPtr

      public static long getRecPtr(byte[] buffer, int idx)
      A Record Pointer is a pointer as returned by Database.malloc(). This is a pointer to a block + BLOCK_HEADER_SIZE.
    • getMemoryStats

      public MemoryStats getMemoryStats()
    • getBytesThatFitInChunks

      public static long getBytesThatFitInChunks(int numChunks)
      Returns the number of bytes that can fit in the payload of the given number of chunks.
    • getChunksNeededForBytes

      public static int getChunksNeededForBytes(long datasize)
      Returns the number of chunks needed to fit the given number of bytes of payload.
    • getCache

      public ChunkCache getCache()
    • getDirtyChunkCount

      public int getDirtyChunkCount()
    • formatByteString

      public static String formatByteString(long valueInBytes)
    • getChunkStats

      public Database.ChunkStats getChunkStats()
    • describeProblem

      public IndexExceptionBuilder describeProblem()