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 256-way 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.void
flush()
Flushes any changes to persistent storage.getFile()
read
(AArrayBlob key) Reads a Blob from the database, returning null if not foundvoid
setRootHash
(Hash h) Writes the root data hash to the Storevoid
protected void
Utility function to truncate file.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
-
truncateFile
Utility function to truncate file. Won't work if mapped byte buffers are active?- Throws:
FileNotFoundException
IOException
-
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
-
flush
Flushes any changes to persistent storage.- Throws:
IOException
- If an IO error occurs
-
getFile
-
getRootHash
- Throws:
IOException
-
setRootHash
Writes the root data hash to the Store- Parameters:
h
- Hash value to write- Throws:
IOException
-
setStore
-