Skip to content

Commit

Permalink
Add db_window module for windowing functions from RocksDb
Browse files Browse the repository at this point in the history
  • Loading branch information
carllin committed Nov 17, 2018
1 parent 4cea208 commit 153ef83
Show file tree
Hide file tree
Showing 5 changed files with 669 additions and 2 deletions.
66 changes: 64 additions & 2 deletions src/db_ledger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use bincode::{deserialize, serialize};
use byteorder::{ByteOrder, LittleEndian, ReadBytesExt};
use entry::Entry;
use ledger::Block;
use packet::{Blob, BLOB_HEADER_SIZE};
use packet::{Blob, SharedBlob, BLOB_HEADER_SIZE};
use result::{Error, Result};
use rocksdb::{ColumnFamily, Options, WriteBatch, DB};
use serde::de::DeserializeOwned;
Expand Down Expand Up @@ -121,6 +121,27 @@ impl LedgerColumnFamily for MetaCf {
pub struct DataCf {}

impl DataCf {
pub fn get_on_slot_index(
&self,
db: &DB,
slot_height: u64,
index: u64,
) -> Result<Option<Vec<u8>>> {
let key = Self::key(slot_height, index);
self.get(db, &key)
}

pub fn put_on_slot_index(
&self,
db: &DB,
slot_height: u64,
index: u64,
serialized_value: &[u8],
) -> Result<()> {
let key = Self::key(slot_height, index);
self.put(db, &key, serialized_value)
}

pub fn key(slot_height: u64, index: u64) -> Vec<u8> {
let mut key = vec![0u8; 16];
LittleEndian::write_u64(&mut key[0..8], slot_height);
Expand Down Expand Up @@ -152,9 +173,34 @@ impl LedgerColumnFamilyRaw for DataCf {
pub struct ErasureCf {}

impl ErasureCf {
pub fn get_on_slot_index(
&self,
db: &DB,
slot_height: u64,
index: u64,
) -> Result<Option<Vec<u8>>> {
let key = Self::key(slot_height, index);
self.get(db, &key)
}

pub fn put_on_slot_index(
&self,
db: &DB,
slot_height: u64,
index: u64,
serialized_value: &[u8],
) -> Result<()> {
let key = Self::key(slot_height, index);
self.put(db, &key, serialized_value)
}

pub fn key(slot_height: u64, index: u64) -> Vec<u8> {
DataCf::key(slot_height, index)
}

pub fn index_from_key(key: &[u8]) -> Result<u64> {
DataCf::index_from_key(key)
}
}

impl LedgerColumnFamilyRaw for ErasureCf {
Expand Down Expand Up @@ -214,6 +260,12 @@ impl DbLedger {
})
}

pub fn write_shared_blobs(&mut self, slot: u64, shared_blobs: &[SharedBlob]) -> Result<()> {
let blob_locks: Vec<_> = shared_blobs.iter().map(|b| b.read().unwrap()).collect();
let blobs: Vec<&Blob> = blob_locks.iter().map(|b| &**b).collect();
self.write_blobs(slot, &blobs)
}

pub fn write_blobs<'a, I>(&mut self, slot: u64, blobs: I) -> Result<()>
where
I: IntoIterator<Item = &'a &'a Blob>,
Expand Down Expand Up @@ -369,6 +421,16 @@ impl DbLedger {
}
}

pub fn write_entries_to_ledger(ledger_paths: &[String], entries: &[Entry]) {
for ledger_path in ledger_paths {
let mut db_ledger =
DbLedger::open(ledger_path).expect("Expected to be able to open database ledger");
db_ledger
.write_entries(DEFAULT_SLOT_HEIGHT, &entries)
.expect("Expected successful write of genesis entries");
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -569,4 +631,4 @@ mod tests {
DB::destroy(&Options::default(), &ledger_path)
.expect("Expected successful database destruction");
}
}
}
Loading

0 comments on commit 153ef83

Please sign in to comment.