Merge pull request #193 from ia0/v2_lib
Add new store (without tests yet)
This commit is contained in:
@@ -12,14 +12,13 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// TODO(ia0): Remove when the module is used.
|
||||
#![allow(dead_code)]
|
||||
|
||||
#[macro_use]
|
||||
mod bitfield;
|
||||
|
||||
use self::bitfield::*;
|
||||
use crate::{usize_to_nat, Nat, Storage, StorageIndex, StoreError, StoreResult};
|
||||
#[cfg(test)]
|
||||
use self::bitfield::Length;
|
||||
use self::bitfield::{count_zeros, num_bits, Bit, Checksum, ConstField, Field};
|
||||
use crate::{usize_to_nat, Nat, Storage, StorageIndex, StoreError, StoreResult, StoreUpdate};
|
||||
use alloc::vec::Vec;
|
||||
use core::cmp::min;
|
||||
use core::convert::TryFrom;
|
||||
@@ -492,6 +491,61 @@ impl Format {
|
||||
HEADER_DELETED.set(word);
|
||||
}
|
||||
|
||||
/// Returns the capacity required by a transaction.
|
||||
pub fn transaction_capacity(&self, updates: &[StoreUpdate]) -> Nat {
|
||||
match updates.len() {
|
||||
// An empty transaction doesn't consume anything.
|
||||
0 => 0,
|
||||
// Transactions with a single update are optimized by avoiding a marker entry.
|
||||
1 => match &updates[0] {
|
||||
StoreUpdate::Insert { value, .. } => self.entry_size(value),
|
||||
// Transactions with a single update which is a removal don't consume anything.
|
||||
StoreUpdate::Remove { .. } => 0,
|
||||
},
|
||||
// A transaction consumes one word for the marker entry in addition to its updates.
|
||||
_ => 1 + updates.iter().map(|x| self.update_capacity(x)).sum::<Nat>(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the capacity of an update.
|
||||
fn update_capacity(&self, update: &StoreUpdate) -> Nat {
|
||||
match update {
|
||||
StoreUpdate::Insert { value, .. } => self.entry_size(value),
|
||||
StoreUpdate::Remove { .. } => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the size of a user entry given its value.
|
||||
pub fn entry_size(&self, value: &[u8]) -> Nat {
|
||||
1 + self.bytes_to_words(usize_to_nat(value.len()))
|
||||
}
|
||||
|
||||
/// Checks if a transaction is valid and returns its sorted keys.
|
||||
///
|
||||
/// Returns `None` if the transaction is invalid.
|
||||
pub fn transaction_valid(&self, updates: &[StoreUpdate]) -> Option<Vec<Nat>> {
|
||||
if usize_to_nat(updates.len()) > self.max_updates() {
|
||||
return None;
|
||||
}
|
||||
let mut sorted_keys = Vec::with_capacity(updates.len());
|
||||
for update in updates {
|
||||
let key = usize_to_nat(update.key());
|
||||
if key > self.max_key() {
|
||||
return None;
|
||||
}
|
||||
if let Some(value) = update.value() {
|
||||
if usize_to_nat(value.len()) > self.max_value_len() {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
match sorted_keys.binary_search(&key) {
|
||||
Ok(_) => return None,
|
||||
Err(pos) => sorted_keys.insert(pos, key),
|
||||
}
|
||||
}
|
||||
Some(sorted_keys)
|
||||
}
|
||||
|
||||
/// Returns the minimum number of words to represent a given number of bytes.
|
||||
///
|
||||
/// # Preconditions
|
||||
|
||||
@@ -359,7 +359,9 @@ pub use self::buffer::{BufferCorruptFunction, BufferOptions, BufferStorage};
|
||||
#[cfg(feature = "std")]
|
||||
pub use self::model::{StoreModel, StoreOperation};
|
||||
pub use self::storage::{Storage, StorageError, StorageIndex, StorageResult};
|
||||
pub use self::store::{StoreError, StoreRatio, StoreResult, StoreUpdate};
|
||||
pub use self::store::{
|
||||
Store, StoreError, StoreHandle, StoreIter, StoreRatio, StoreResult, StoreUpdate,
|
||||
};
|
||||
|
||||
/// Internal representation of natural numbers.
|
||||
///
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
use crate::format::Format;
|
||||
use crate::{usize_to_nat, StoreError, StoreRatio, StoreResult, StoreUpdate};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::collections::HashMap;
|
||||
|
||||
/// Models the mutable operations of a store.
|
||||
///
|
||||
@@ -79,38 +79,23 @@ impl StoreModel {
|
||||
/// Returns the capacity according to the model.
|
||||
pub fn capacity(&self) -> StoreRatio {
|
||||
let total = self.format.total_capacity();
|
||||
let used = usize_to_nat(self.content.values().map(|x| self.entry_size(x)).sum());
|
||||
let used = usize_to_nat(
|
||||
self.content
|
||||
.values()
|
||||
.map(|x| self.format.entry_size(x) as usize)
|
||||
.sum(),
|
||||
);
|
||||
StoreRatio { used, total }
|
||||
}
|
||||
|
||||
/// Applies a transaction.
|
||||
fn transaction(&mut self, updates: Vec<StoreUpdate>) -> StoreResult<()> {
|
||||
// Fail if too many updates.
|
||||
if updates.len() > self.format.max_updates() as usize {
|
||||
return Err(StoreError::InvalidArgument);
|
||||
}
|
||||
// Fail if an update is invalid.
|
||||
if !updates.iter().all(|x| self.update_valid(x)) {
|
||||
return Err(StoreError::InvalidArgument);
|
||||
}
|
||||
// Fail if updates are not disjoint, i.e. there are duplicate keys.
|
||||
let keys: HashSet<_> = updates.iter().map(|x| x.key()).collect();
|
||||
if keys.len() != updates.len() {
|
||||
// Fail if the transaction is invalid.
|
||||
if self.format.transaction_valid(&updates).is_none() {
|
||||
return Err(StoreError::InvalidArgument);
|
||||
}
|
||||
// Fail if there is not enough capacity.
|
||||
let capacity = match updates.len() {
|
||||
// An empty transaction doesn't consume anything.
|
||||
0 => 0,
|
||||
// Transactions with a single update are optimized by avoiding a marker entry.
|
||||
1 => match &updates[0] {
|
||||
StoreUpdate::Insert { value, .. } => self.entry_size(value),
|
||||
// Transactions with a single update which is a removal don't consume anything.
|
||||
StoreUpdate::Remove { .. } => 0,
|
||||
},
|
||||
// A transaction consumes one word for the marker entry in addition to its updates.
|
||||
_ => 1 + updates.iter().map(|x| self.update_size(x)).sum::<usize>(),
|
||||
};
|
||||
let capacity = self.format.transaction_capacity(&updates) as usize;
|
||||
if self.capacity().remaining() < capacity {
|
||||
return Err(StoreError::NoCapacity);
|
||||
}
|
||||
@@ -144,25 +129,4 @@ impl StoreModel {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Returns the word capacity of an update.
|
||||
fn update_size(&self, update: &StoreUpdate) -> usize {
|
||||
match update {
|
||||
StoreUpdate::Insert { value, .. } => self.entry_size(value),
|
||||
StoreUpdate::Remove { .. } => 1,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the word capacity of an entry.
|
||||
fn entry_size(&self, value: &[u8]) -> usize {
|
||||
1 + self.format.bytes_to_words(usize_to_nat(value.len())) as usize
|
||||
}
|
||||
|
||||
/// Returns whether an update is valid.
|
||||
fn update_valid(&self, update: &StoreUpdate) -> bool {
|
||||
update.key() <= self.format.max_key() as usize
|
||||
&& update
|
||||
.value()
|
||||
.map_or(true, |x| x.len() <= self.format.max_value_len() as usize)
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user