Package etch
Class Etch
java.lang.Object
etch.Etch
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 Summary
FieldsModifier and TypeFieldDescriptionprotected static final long
protected static final long
-
Method Summary
Modifier and TypeMethodDescriptionstatic Etch
Create an Etch instance using the specified filestatic Etch
Create an Etch instance using a temporary file.static Etch
createTempEtch
(String prefix) Create an Etch instance using a temporary file with a specific file prefix.long
extractType
(long slot) Gets the type code for an index slot valuevoid
flush()
Flushes any changes to persistent storage.long
getFile()
int
indexSize
(int level) Gets the index block size for a given levelread
(AArrayBlob key) Reads a Blob from the database, returning null if not foundread
(AArrayBlob key, long pointer) long
readSlot
(long indexPosition, int digit) Gets the slot value at the specified digit position in an index block.readValueKey
(long ptr) void
setRootHash
(Hash h) Writes the root data hash to the Storevoid
protected void
Utility function to truncate file.void
write
(AArrayBlob key, Ref<ACell> value) Writes a key / value pair to the immutable store.protected void
Writes the data length field for the Etch file.
-
Field Details
-
OFFSET_FILE_SIZE
protected static final long OFFSET_FILE_SIZE- See Also:
-
OFFSET_ROOT_HASH
protected static final long OFFSET_ROOT_HASH- See Also:
-
-
Method Details
-
createTempEtch
Create an Etch instance using a temporary file.- Returns:
- The new Etch instance
- Throws:
IOException
- If an IO error occurs
-
createTempEtch
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
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
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
- Throws:
IOException
-
truncateFile
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
Writes the data length field for the Etch file. S- Throws:
IOException
-
read
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
- Throws:
IOException
-
flush
Flushes any changes to persistent storage.- Throws:
IOException
- If an IO error occurs
-
readSlot
Gets the slot value at the specified digit position in an index block. Doesn't affect temp array.- Parameters:
indexPosition
- Position of index blockdigit
- Position of slot within index block- Returns:
- Pointer value (including type bits in MSBs)
- Throws:
IOException
- In case of IO Error
-
visitIndex
-
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
-
getFileName
-
getRootHash
- Throws:
IOException
-
setRootHash
Writes the root data hash to the Store- Parameters:
h
- Hash value to write- Throws:
IOException
- If IO Error occurs
-
setStore
-
extractType
public long extractType(long slot) Gets the type code for an index slot value- Parameters:
slot
- Raw slot value- Returns:
- Type code
-