lssa/storage/src/lib.rs

343 lines
12 KiB
Rust
Raw Normal View History

2024-10-10 14:09:31 +03:00
use std::{path::Path, sync::Arc};
2025-08-12 12:18:13 -03:00
use common::block::{Block, HashableBlockData};
2024-10-10 14:09:31 +03:00
use error::DbError;
use rocksdb::{
BoundColumnFamily, ColumnFamilyDescriptor, DBWithThreadMode, MultiThreaded, Options,
};
pub mod error;
2024-10-22 12:46:44 +03:00
2025-11-26 00:27:20 +03:00
/// Maximal size of stored blocks in base
2024-10-10 14:09:31 +03:00
///
2025-11-26 00:27:20 +03:00
/// Used to control db size
2024-10-10 14:09:31 +03:00
///
2025-11-26 00:27:20 +03:00
/// Currently effectively unbounded.
2024-10-10 14:09:31 +03:00
pub const BUFF_SIZE_ROCKSDB: usize = usize::MAX;
2025-11-26 00:27:20 +03:00
/// Size of stored blocks cache in memory
2024-10-10 14:09:31 +03:00
///
2025-11-26 00:27:20 +03:00
/// Keeping small to not run out of memory
2024-10-10 14:09:31 +03:00
pub const CACHE_SIZE: usize = 1000;
2025-11-26 00:27:20 +03:00
/// Key base for storing metainformation about id of first block in db
2024-10-10 14:09:31 +03:00
pub const DB_META_FIRST_BLOCK_IN_DB_KEY: &str = "first_block_in_db";
2025-11-26 00:27:20 +03:00
/// Key base for storing metainformation about id of last current block in db
2024-10-10 14:09:31 +03:00
pub const DB_META_LAST_BLOCK_IN_DB_KEY: &str = "last_block_in_db";
2025-11-26 00:27:20 +03:00
/// Key base for storing metainformation which describe if first block has been set
2024-10-10 14:09:31 +03:00
pub const DB_META_FIRST_BLOCK_SET_KEY: &str = "first_block_set";
2025-11-26 00:27:20 +03:00
/// Key base for storing snapshot which describe block id
2025-05-23 15:47:20 -04:00
pub const DB_SNAPSHOT_BLOCK_ID_KEY: &str = "block_id";
2025-11-26 00:27:20 +03:00
/// Name of block column family
2024-10-10 14:09:31 +03:00
pub const CF_BLOCK_NAME: &str = "cf_block";
2025-11-26 00:27:20 +03:00
/// Name of meta column family
2024-10-10 14:09:31 +03:00
pub const CF_META_NAME: &str = "cf_meta";
2025-11-26 00:27:20 +03:00
/// Name of snapshot column family
2025-05-23 15:46:52 -04:00
pub const CF_SNAPSHOT_NAME: &str = "cf_snapshot";
2025-04-02 12:16:02 +03:00
2024-10-10 14:09:31 +03:00
pub type DbResult<T> = Result<T, DbError>;
pub struct RocksDBIO {
pub db: DBWithThreadMode<MultiThreaded>,
}
impl RocksDBIO {
2025-10-25 00:30:04 -03:00
pub fn open_or_create(path: &Path, start_block: Option<Block>) -> DbResult<Self> {
2024-10-10 14:09:31 +03:00
let mut cf_opts = Options::default();
cf_opts.set_max_write_buffer_number(16);
2025-11-26 00:27:20 +03:00
// ToDo: Add more column families for different data
2024-10-10 14:09:31 +03:00
let cfb = ColumnFamilyDescriptor::new(CF_BLOCK_NAME, cf_opts.clone());
let cfmeta = ColumnFamilyDescriptor::new(CF_META_NAME, cf_opts.clone());
2025-05-23 15:47:39 -04:00
let cfsnapshot = ColumnFamilyDescriptor::new(CF_SNAPSHOT_NAME, cf_opts.clone());
2024-10-10 14:09:31 +03:00
let mut db_opts = Options::default();
db_opts.create_missing_column_families(true);
db_opts.create_if_missing(true);
let db = DBWithThreadMode::<MultiThreaded>::open_cf_descriptors(
&db_opts,
path,
2025-10-17 15:52:00 -03:00
vec![cfb, cfmeta, cfsnapshot],
2024-10-10 14:09:31 +03:00
);
let dbio = Self {
2025-11-26 00:27:20 +03:00
// There is no point in handling this from runner code
2024-10-10 14:09:31 +03:00
db: db.unwrap(),
};
let is_start_set = dbio.get_meta_is_first_block_set()?;
if is_start_set {
Ok(dbio)
} else if let Some(block) = start_block {
2025-09-02 11:06:41 +03:00
let block_id = block.header.block_id;
2024-10-10 14:09:31 +03:00
dbio.put_meta_first_block_in_db(block)?;
dbio.put_meta_is_first_block_set()?;
2024-12-09 03:59:53 +01:00
dbio.put_meta_last_block_in_db(block_id)?;
2024-10-10 14:09:31 +03:00
Ok(dbio)
} else {
// Here we are trying to start a DB without a block, one should not do it.
unreachable!()
2024-10-10 14:09:31 +03:00
}
}
2024-12-09 04:00:08 +01:00
pub fn destroy(path: &Path) -> DbResult<()> {
let mut cf_opts = Options::default();
cf_opts.set_max_write_buffer_number(16);
2025-11-26 00:27:20 +03:00
// ToDo: Add more column families for different data
2025-01-31 17:01:39 -05:00
let _cfb = ColumnFamilyDescriptor::new(CF_BLOCK_NAME, cf_opts.clone());
let _cfmeta = ColumnFamilyDescriptor::new(CF_META_NAME, cf_opts.clone());
2025-05-23 15:47:51 -04:00
let _cfsnapshot = ColumnFamilyDescriptor::new(CF_SNAPSHOT_NAME, cf_opts.clone());
2024-12-09 04:00:08 +01:00
let mut db_opts = Options::default();
db_opts.create_missing_column_families(true);
db_opts.create_if_missing(true);
2024-12-09 04:18:27 +01:00
DBWithThreadMode::<MultiThreaded>::destroy(&db_opts, path)
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))
2024-12-09 04:00:08 +01:00
}
2025-06-10 01:39:11 -04:00
pub fn meta_column(&self) -> Arc<BoundColumnFamily<'_>> {
2024-10-10 14:09:31 +03:00
self.db.cf_handle(CF_META_NAME).unwrap()
}
2025-06-10 01:39:11 -04:00
pub fn block_column(&self) -> Arc<BoundColumnFamily<'_>> {
2024-10-10 14:09:31 +03:00
self.db.cf_handle(CF_BLOCK_NAME).unwrap()
}
2025-06-10 01:39:11 -04:00
pub fn snapshot_column(&self) -> Arc<BoundColumnFamily<'_>> {
2025-05-23 15:48:09 -04:00
self.db.cf_handle(CF_SNAPSHOT_NAME).unwrap()
}
2024-10-10 14:09:31 +03:00
pub fn get_meta_first_block_in_db(&self) -> DbResult<u64> {
let cf_meta = self.meta_column();
let res = self
.db
2025-09-25 11:53:42 +03:00
.get_cf(
&cf_meta,
borsh::to_vec(&DB_META_FIRST_BLOCK_IN_DB_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_META_FIRST_BLOCK_IN_DB_KEY".to_string()),
)
})?,
)
2024-10-10 14:09:31 +03:00
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
if let Some(data) = res {
2025-09-25 11:53:42 +03:00
Ok(borsh::from_slice::<u64>(&data).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to deserialize first block".to_string()),
)
})?)
2024-10-10 14:09:31 +03:00
} else {
Err(DbError::db_interaction_error(
"First block not found".to_string(),
))
}
}
pub fn get_meta_last_block_in_db(&self) -> DbResult<u64> {
let cf_meta = self.meta_column();
let res = self
.db
2025-09-25 11:53:42 +03:00
.get_cf(
&cf_meta,
borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_string()),
)
})?,
)
2024-10-10 14:09:31 +03:00
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
if let Some(data) = res {
2025-09-25 11:53:42 +03:00
Ok(borsh::from_slice::<u64>(&data).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to deserialize last block".to_string()),
)
})?)
2024-10-10 14:09:31 +03:00
} else {
Err(DbError::db_interaction_error(
"Last block not found".to_string(),
))
}
}
pub fn get_meta_is_first_block_set(&self) -> DbResult<bool> {
let cf_meta = self.meta_column();
let res = self
.db
2025-09-25 11:53:42 +03:00
.get_cf(
&cf_meta,
borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_string()),
)
})?,
)
2024-10-10 14:09:31 +03:00
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
Ok(res.is_some())
}
pub fn put_meta_first_block_in_db(&self, block: Block) -> DbResult<()> {
let cf_meta = self.meta_column();
self.db
.put_cf(
&cf_meta,
2025-09-25 11:53:42 +03:00
borsh::to_vec(&DB_META_FIRST_BLOCK_IN_DB_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_META_FIRST_BLOCK_IN_DB_KEY".to_string()),
)
})?,
borsh::to_vec(&block.header.block_id).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize first block id".to_string()),
)
})?,
2024-10-10 14:09:31 +03:00
)
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
2024-12-09 03:59:23 +01:00
self.put_block(block, true)?;
2024-10-10 14:09:31 +03:00
Ok(())
}
pub fn put_meta_last_block_in_db(&self, block_id: u64) -> DbResult<()> {
let cf_meta = self.meta_column();
self.db
.put_cf(
&cf_meta,
2025-09-25 11:53:42 +03:00
borsh::to_vec(&DB_META_LAST_BLOCK_IN_DB_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_META_LAST_BLOCK_IN_DB_KEY".to_string()),
)
})?,
borsh::to_vec(&block_id).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize last block id".to_string()),
)
})?,
2024-10-10 14:09:31 +03:00
)
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
Ok(())
}
pub fn put_meta_is_first_block_set(&self) -> DbResult<()> {
let cf_meta = self.meta_column();
self.db
2025-09-25 11:53:42 +03:00
.put_cf(
&cf_meta,
borsh::to_vec(&DB_META_FIRST_BLOCK_SET_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_META_FIRST_BLOCK_SET_KEY".to_string()),
)
})?,
[1u8; 1],
)
2024-10-10 14:09:31 +03:00
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
Ok(())
}
2024-12-09 03:59:23 +01:00
pub fn put_block(&self, block: Block, first: bool) -> DbResult<()> {
2024-10-10 14:09:31 +03:00
let cf_block = self.block_column();
2024-12-09 03:59:23 +01:00
if !first {
2024-12-09 04:01:20 +01:00
let last_curr_block = self.get_meta_last_block_in_db()?;
2024-10-10 14:09:31 +03:00
2025-09-02 11:06:41 +03:00
if block.header.block_id > last_curr_block {
self.put_meta_last_block_in_db(block.header.block_id)?;
2024-12-09 03:59:23 +01:00
}
2024-10-10 14:09:31 +03:00
}
self.db
.put_cf(
&cf_block,
2025-09-25 11:53:42 +03:00
borsh::to_vec(&block.header.block_id).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize block id".to_string()),
)
})?,
borsh::to_vec(&HashableBlockData::from(block)).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize block data".to_string()),
)
})?,
2024-10-10 14:09:31 +03:00
)
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
Ok(())
}
2025-09-03 10:29:51 +03:00
pub fn get_block(&self, block_id: u64) -> DbResult<HashableBlockData> {
2024-10-10 14:09:31 +03:00
let cf_block = self.block_column();
let res = self
.db
2025-09-25 11:53:42 +03:00
.get_cf(
&cf_block,
borsh::to_vec(&block_id).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize block id".to_string()),
)
})?,
)
2024-10-10 14:09:31 +03:00
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
if let Some(data) = res {
2025-09-25 11:53:42 +03:00
Ok(
borsh::from_slice::<HashableBlockData>(&data).map_err(|serr| {
DbError::borsh_cast_message(
serr,
Some("Failed to deserialize block data".to_string()),
)
})?,
)
2024-10-10 14:09:31 +03:00
} else {
Err(DbError::db_interaction_error(
"Block on this id not found".to_string(),
))
}
}
2025-04-02 12:16:02 +03:00
pub fn get_snapshot_block_id(&self) -> DbResult<u64> {
let cf_snapshot = self.snapshot_column();
let res = self
.db
2025-09-25 11:53:42 +03:00
.get_cf(
&cf_snapshot,
borsh::to_vec(&DB_SNAPSHOT_BLOCK_ID_KEY).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to serialize DB_SNAPSHOT_BLOCK_ID_KEY".to_string()),
)
})?,
)
.map_err(|rerr| DbError::rocksdb_cast_message(rerr, None))?;
if let Some(data) = res {
2025-09-25 11:53:42 +03:00
Ok(borsh::from_slice::<u64>(&data).map_err(|err| {
DbError::borsh_cast_message(
err,
Some("Failed to deserialize last block".to_string()),
)
})?)
} else {
Err(DbError::db_interaction_error(
"Snapshot block ID not found".to_string(),
))
}
}
2024-10-10 14:09:31 +03:00
}