Package etch

Class Etch

java.lang.Object
etch.Etch

public class Etch extends Object
A stupid, fast database for immutable data you want carved in stone. We solve the cache invalidation problem, quite effectively, by never changing anything. Once a value is written for a given key, it cannot be changed. Etch is indifferent to the exact meaning of keys, but they must have a fixed length of 32 bytes (256 bits). It is intended that keys are pseudo-random hash values, which will result in desirable distributions of data for the radix tree structure. Radix tree index blocks are arrays of 8 byte pointers. To avoid creating too many index blocks when collisions occur, a chained entry list inside is created in unused space in index blocks. Once there is no more space, chains are collapsed to a new index block. Header of file is 42 bytes as follows: - Magic number 0xe7c6 (2 bytes) - Database length in bytes (8 bytes) - Root hash (32 bytes) Pointers in index blocks are of 4 possible types, determined by the two high bits (MSBs): - 00 high bits: pointer to data - 01 high bits: pointer to next index node - 10 high bits: start of chained entry list - 11 high bits: continuation of chained entry list Data is stored as: - 32 bytes key - X bytes monotonic label of which - 1 byte status - 8 bytes Memory Size (TODO: might be negative for unknown?) - 2 bytes data length N (a short) - N byes actual data
  • Field Details

  • Method Details

    • createTempEtch

      public static Etch createTempEtch() throws IOException
      Create an Etch instance using a temporary file.
      Returns:
      The new Etch instance
      Throws:
      IOException - If an IO error occurs
    • createTempEtch

      public static Etch createTempEtch(String prefix) throws IOException
      Create an Etch instance using a temporary file with a specific file prefix.
      Parameters:
      prefix - temporary file prefix to use
      Returns:
      The new Etch instance
      Throws:
      IOException - If an IO error occurs
    • create

      public static Etch create(File file) throws IOException
      Create an Etch instance using the specified file
      Parameters:
      file - File with which to create Etch instance
      Returns:
      The new Etch instance
      Throws:
      IOException - If an IO error occurs
    • write

      public Ref<ACell> write(AArrayBlob key, Ref<ACell> value) throws IOException
      Writes a key / value pair to the immutable store. CONCURRENCY: Hold a lock for a single writer
      Parameters:
      key - A key value (typically the Hash)
      value - Value data to associate with the key
      Returns:
      Ref after writing to store
      Throws:
      IOException - If an IO error occurs
    • readValueKey

      public Hash readValueKey(long ptr) throws IOException
      Throws:
      IOException
    • truncateFile

      protected void truncateFile() throws FileNotFoundException, IOException
      Utility function to truncate file. Won't work if mapped byte buffers are active?
      Throws:
      FileNotFoundException
      IOException
    • getDataLength

      public long getDataLength()
      Returns:
      Current data size in bytes
    • writeDataLength

      protected void writeDataLength() throws IOException
      Writes the data length field for the Etch file. S
      Throws:
      IOException
    • read

      public RefSoft<ACell> read(AArrayBlob key) throws IOException
      Reads a Blob from the database, returning null if not found
      Parameters:
      key - Key to read from Store
      Returns:
      Blob containing the data, or null if not found
      Throws:
      IOException - If an IO error occurs
    • read

      public RefSoft<ACell> read(AArrayBlob key, long pointer) throws IOException
      Throws:
      IOException
    • flush

      public void flush() throws IOException
      Flushes any changes to persistent storage.
      Throws:
      IOException - If an IO error occurs
    • readSlot

      public long readSlot(long indexPosition, int digit) throws IOException
      Gets the slot value at the specified digit position in an index block. Doesn't affect temp array.
      Parameters:
      indexPosition - Position of index block
      digit - Position of slot within index block
      Returns:
      Pointer value (including type bits in MSBs)
      Throws:
      IOException - In case of IO Error
    • visitIndex

      public void visitIndex(IEtchIndexVisitor v)
    • indexSize

      public int indexSize(int level)
      Gets the index block size for a given level
      Parameters:
      level - Level of index block in Etch store
      Returns:
      Index block size as number of entries
    • getFile

      public File getFile()
    • getFileName

      public String getFileName()
    • getRootHash

      public Hash getRootHash() throws IOException
      Throws:
      IOException
    • setRootHash

      public void setRootHash(Hash h) throws IOException
      Writes the root data hash to the Store
      Parameters:
      h - Hash value to write
      Throws:
      IOException - If IO Error occurs
    • setStore

      public void setStore(EtchStore etchStore)
    • extractType

      public long extractType(long slot)
      Gets the type code for an index slot value
      Parameters:
      slot - Raw slot value
      Returns:
      Type code