Class RocksDBStorage
- All Implemented Interfaces:
BlockchainStorage,AutoCloseable
BlockchainStorage implementation
backed by RocksDB.
RocksDB is Facebook's LSM-tree storage engine optimized for high-write
workloads on SSDs. It provides write-ahead logging (WAL) and crash recovery
(NFR-REL-01, FR-STOR-04). Each block is stored under a big-endian 4-byte
integer key, serialized to JSON via BlockSerializer.
Key layout
Key : big-endian 4-byte block index (e.g., block 7 → 0x00_00_00_07) Value: UTF-8 JSON of the Block object
Performance tuning
The default configuration enables:
- Snappy compression to reduce disk footprint
- 64 MB write buffer for batching sequential writes
- Bloom filters on block-based table to speed up point reads
Thread safety
RocksDB is internally thread-safe for concurrent reads. This class adds an
additional ReentrantReadWriteLock to serialize writes and protect the
close() method from concurrent access.
Lifecycle
try (RocksDBStorage storage = new RocksDBStorage("/data/chain-rocks")) {
BlockchainNode node = BlockchainConfig.builder()
.storage(storage)
.build();
node.start();
}
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionRocksDBStorage(String dataDirectory) Opens (or creates) a RocksDB database at the given directory path. -
Method Summary
Modifier and TypeMethodDescriptionintReturns the number of stored blocks by counting block-keyed entries.voidclose()Closes the RocksDB database and releases all native resources.voidRemoves all block entries from the RocksDB store.booleanReturnstrueif a block with the given hash is stored.loadAll()Returns all stored blocks in ascending index order.loadBlock(int index) Retrieves a block by its zero-based index.loadBlockByHash(String hash) Searches for a block by its SHA-256 hash.voidPersists a block atomically using a RocksDBWriteBatch.toString()Returns a human-readable description of this storage instance.
-
Constructor Details
-
RocksDBStorage
Opens (or creates) a RocksDB database at the given directory path.The database directory is created automatically if it does not exist. If the database is already locked by another process, the constructor will throw immediately.
- Parameters:
dataDirectory- path to the directory where RocksDB files will be stored- Throws:
NullPointerException- ifdataDirectoryis nullIllegalArgumentException- ifdataDirectoryis blankBlockValidationException- if the database cannot be opened
-
-
Method Details
-
saveBlock
Persists a block atomically using a RocksDBWriteBatch.Write batches in RocksDB are atomic: either the entire batch is applied or none of it is, satisfying the atomicity requirement of
BlockchainStorage.saveBlock(com.privatechain.core.model.Block).- Specified by:
saveBlockin interfaceBlockchainStorage- Parameters:
block- the block to persist (non-null)- Throws:
NullPointerException- ifblockis nullBlockValidationException- if the write fails
-
loadBlock
Retrieves a block by its zero-based index.- Specified by:
loadBlockin interfaceBlockchainStorage- Parameters:
index- block index (≥ 0)- Returns:
- the deserialized
Block - Throws:
NoSuchElementException- if no block exists atindexBlockValidationException- if deserialization or hash verification fails
-
loadBlockByHash
Searches for a block by its SHA-256 hash.Performs a sequential scan — suitable for moderate chain sizes. For very large chains a secondary hash→index should be maintained.
- Specified by:
loadBlockByHashin interfaceBlockchainStorage- Parameters:
hash- hex-encoded block hash (non-null)- Returns:
- an
Optionalcontaining the matching block, or empty if absent - Throws:
NullPointerException- ifhashis null
-
loadAll
Returns all stored blocks in ascending index order.RocksDB's lexicographic ordering of big-endian keys provides natural ascending order without any post-scan sort.
- Specified by:
loadAllin interfaceBlockchainStorage- Returns:
- non-null, ordered list (maybe empty)
-
exists
Returnstrueif a block with the given hash is stored.- Specified by:
existsin interfaceBlockchainStorage- Parameters:
hash- hex-encoded block hash (non-null)- Returns:
trueif present- Throws:
NullPointerException- ifhashis null
-
chainHeight
public int chainHeight()Returns the number of stored blocks by counting block-keyed entries.- Specified by:
chainHeightin interfaceBlockchainStorage- Returns:
- block count (≥ 0)
-
deleteAll
public void deleteAll()Removes all block entries from the RocksDB store.Warning: This operation is irreversible. Intended for testing and chain-reset scenarios only.
- Specified by:
deleteAllin interfaceBlockchainStorage
-
close
public void close()Closes the RocksDB database and releases all native resources.Must be called when the owning node shuts down to flush the write buffer and release the file lock. Subsequent calls are idempotent.
- Specified by:
closein interfaceAutoCloseable
-
toString
Returns a human-readable description of this storage instance.
-