|
| 1 | +use std::{ |
| 2 | + fs::File, |
| 3 | + io::{Read, Write}, |
| 4 | + path::PathBuf, |
| 5 | +}; |
| 6 | + |
| 7 | +use massa_models::slot::Slot; |
| 8 | +use rocksdb::{DBCompressionType, Options}; |
| 9 | + |
| 10 | +/// A trait that defines the interface for a storage backend for the dump-block |
| 11 | +/// feature. |
| 12 | +pub trait StorageBackend: Send + Sync { |
| 13 | + /// Writes the given value to the storage backend. |
| 14 | + /// The slot is used as the key to the value. |
| 15 | + fn write(&self, slot: &Slot, value: &[u8]); |
| 16 | + |
| 17 | + /// Reads the value from the storage backend. |
| 18 | + /// The slot is used as the key to the value. |
| 19 | + fn read(&self, slot: &Slot) -> Option<Vec<u8>>; |
| 20 | +} |
| 21 | + |
| 22 | +/// A storage backend that uses the file system as the underlying storage engine. |
| 23 | +pub struct FileStorageBackend { |
| 24 | + folder: PathBuf, |
| 25 | +} |
| 26 | +impl FileStorageBackend { |
| 27 | + /// Creates a new instance of `FileStorageBackend` with the given path. |
| 28 | + pub fn new(path: PathBuf) -> Self { |
| 29 | + Self { folder: path } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +impl StorageBackend for FileStorageBackend { |
| 34 | + fn write(&self, slot: &Slot, value: &[u8]) { |
| 35 | + let block_file_path = self |
| 36 | + .folder |
| 37 | + .join(format!("block_slot_{}_{}.bin", slot.thread, slot.period)); |
| 38 | + |
| 39 | + let mut file = File::create(block_file_path.clone()) |
| 40 | + .unwrap_or_else(|_| panic!("Cannot create file: {:?}", block_file_path)); |
| 41 | + |
| 42 | + file.write_all(value).expect("Unable to write to disk"); |
| 43 | + } |
| 44 | + |
| 45 | + fn read(&self, slot: &Slot) -> Option<Vec<u8>> { |
| 46 | + let block_file_path = self |
| 47 | + .folder |
| 48 | + .join(format!("block_slot_{}_{}.bin", slot.thread, slot.period)); |
| 49 | + |
| 50 | + let file = File::open(block_file_path.clone()) |
| 51 | + .unwrap_or_else(|_| panic!("Cannot open file: {:?}", block_file_path)); |
| 52 | + let mut reader = std::io::BufReader::new(file); |
| 53 | + let mut buffer = Vec::new(); |
| 54 | + reader |
| 55 | + .read_to_end(&mut buffer) |
| 56 | + .expect("Unable to read from disk"); |
| 57 | + |
| 58 | + Some(buffer) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +/// A storage backend that uses RocksDB as the underlying storage engine. |
| 63 | +pub struct RocksDBStorageBackend { |
| 64 | + db: rocksdb::DB, |
| 65 | +} |
| 66 | + |
| 67 | +impl RocksDBStorageBackend { |
| 68 | + /// Creates a new instance of `RocksDBStorageBackend` with the given path. |
| 69 | + pub fn new(path: PathBuf) -> Self { |
| 70 | + let mut opts = Options::default(); |
| 71 | + opts.create_if_missing(true); |
| 72 | + opts.set_compression_type(DBCompressionType::Lz4); |
| 73 | + |
| 74 | + let db = rocksdb::DB::open(&opts, path.clone()) |
| 75 | + .unwrap_or_else(|_| panic!("Failed to create storage db at {:?}", path)); |
| 76 | + |
| 77 | + Self { db } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl StorageBackend for RocksDBStorageBackend { |
| 82 | + fn write(&self, slot: &Slot, value: &[u8]) { |
| 83 | + self.db |
| 84 | + .put(slot.to_bytes_key(), value) |
| 85 | + .expect("Unable to write block to db"); |
| 86 | + } |
| 87 | + |
| 88 | + fn read(&self, slot: &Slot) -> Option<Vec<u8>> { |
| 89 | + match self.db.get(slot.to_bytes_key()) { |
| 90 | + Ok(val) => val, |
| 91 | + Err(e) => { |
| 92 | + println!("Error: {} reading key {:?}", e, slot.to_bytes_key()); |
| 93 | + None |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +#[cfg(test)] |
| 100 | +mod tests { |
| 101 | + use super::*; |
| 102 | + |
| 103 | + #[test] |
| 104 | + fn test_file_storage_backend() { |
| 105 | + let slot = Slot { |
| 106 | + thread: 1, |
| 107 | + period: 1, |
| 108 | + }; |
| 109 | + let value = vec![1, 2, 3]; |
| 110 | + |
| 111 | + let storage = FileStorageBackend::new(PathBuf::from("")); |
| 112 | + storage.write(&slot, &value); |
| 113 | + |
| 114 | + let storage = FileStorageBackend::new(PathBuf::from("")); |
| 115 | + let data = storage.read(&slot); |
| 116 | + assert_eq!(data, Some(value)); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn test_rocksdb_storage_backend() { |
| 121 | + let slot = Slot { |
| 122 | + thread: 1, |
| 123 | + period: 1, |
| 124 | + }; |
| 125 | + let value = vec![1, 2, 3]; |
| 126 | + |
| 127 | + let storage = RocksDBStorageBackend::new(PathBuf::from("test_db")); |
| 128 | + storage.write(&slot, &value); |
| 129 | + drop(storage); |
| 130 | + |
| 131 | + let storage = RocksDBStorageBackend::new(PathBuf::from("test_db")); |
| 132 | + let data = storage.read(&slot); |
| 133 | + assert_eq!(data, Some(value)); |
| 134 | + } |
| 135 | +} |
0 commit comments