• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

google / OpenSK / 4106544918

pending completion
4106544918

Pull #580

github

GitHub
Merge c00f62b38 into 684d37fa0
Pull Request #580: Overview: Add tock v2 support

243 of 243 new or added lines in 16 files covered. (100.0%)

15185 of 17275 relevant lines covered (87.9%)

514632.43 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/libraries/persistent_store/src/model.rs
1
// Copyright 2019-2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15
//! Store specification.
16

17
use crate::format::Format;
18
use crate::{usize_to_nat, StoreError, StoreRatio, StoreResult, StoreUpdate};
19
use std::collections::HashMap;
20

21
/// Models the mutable operations of a store.
22
///
23
/// The model doesn't model the storage and read-only operations. This is done by the
24
/// [driver](crate::StoreDriver).
25
#[derive(Clone, Debug)]
×
26
pub struct StoreModel {
27
    /// Represents the content of the store.
28
    content: HashMap<usize, Box<[u8]>>,
29

30
    /// The modeled storage configuration.
31
    format: Format,
32
}
33

34
/// Mutable operations on a store.
35
#[derive(Clone, Debug)]
×
36
pub enum StoreOperation {
37
    /// Applies a transaction.
38
    Transaction {
39
        /// The list of updates to be applied.
40
        updates: Vec<StoreUpdate<Vec<u8>>>,
41
    },
42

43
    /// Deletes all keys above a threshold.
44
    Clear {
45
        /// The minimum key to be deleted.
46
        min_key: usize,
47
    },
48

49
    /// Compacts the store until a given capacity is immediately available.
50
    Prepare {
51
        /// How much capacity should be immediately available after compaction.
52
        length: usize,
53
    },
54
}
55

56
impl StoreModel {
57
    /// Creates an empty model for a given storage configuration.
58
    pub fn new(format: Format) -> StoreModel {
×
59
        let content = HashMap::new();
×
60
        StoreModel { content, format }
×
61
    }
×
62

63
    /// Returns the modeled content.
64
    pub fn content(&self) -> &HashMap<usize, Box<[u8]>> {
×
65
        &self.content
×
66
    }
×
67

68
    /// Returns the storage configuration.
69
    pub fn format(&self) -> &Format {
×
70
        &self.format
×
71
    }
×
72

73
    /// Simulates a store operation.
74
    pub fn apply(&mut self, operation: StoreOperation) -> StoreResult<()> {
×
75
        match operation {
×
76
            StoreOperation::Transaction { updates } => self.transaction(updates),
×
77
            StoreOperation::Clear { min_key } => self.clear(min_key),
×
78
            StoreOperation::Prepare { length } => self.prepare(length),
×
79
        }
80
    }
×
81

82
    /// Returns the capacity according to the model.
83
    pub fn capacity(&self) -> StoreRatio {
×
84
        let total = self.format.total_capacity();
×
85
        let used = usize_to_nat(
×
86
            self.content
×
87
                .values()
×
88
                .map(|x| self.format.entry_size(x) as usize)
×
89
                .sum(),
×
90
        );
×
91
        StoreRatio { used, total }
×
92
    }
×
93

94
    /// Applies a transaction.
95
    fn transaction(&mut self, updates: Vec<StoreUpdate<Vec<u8>>>) -> StoreResult<()> {
×
96
        // Fail if the transaction is invalid.
×
97
        if self.format.transaction_valid(&updates).is_none() {
×
98
            return Err(StoreError::InvalidArgument);
×
99
        }
×
100
        // Fail if there is not enough capacity.
×
101
        let capacity = self.format.transaction_capacity(&updates) as usize;
×
102
        if self.capacity().remaining() < capacity {
×
103
            return Err(StoreError::NoCapacity);
×
104
        }
×
105
        // Apply the updates.
106
        for update in updates {
×
107
            match update {
×
108
                StoreUpdate::Insert { key, value } => {
×
109
                    self.content.insert(key, value.into_boxed_slice());
×
110
                }
×
111
                StoreUpdate::Remove { key } => {
×
112
                    self.content.remove(&key);
×
113
                }
×
114
            }
115
        }
116
        Ok(())
×
117
    }
×
118

119
    /// Applies a clear operation.
120
    fn clear(&mut self, min_key: usize) -> StoreResult<()> {
×
121
        if min_key > self.format.max_key() as usize {
×
122
            return Err(StoreError::InvalidArgument);
×
123
        }
×
124
        self.content.retain(|&k, _| k < min_key);
×
125
        Ok(())
×
126
    }
×
127

128
    /// Applies a prepare operation.
129
    fn prepare(&self, length: usize) -> StoreResult<()> {
×
130
        if self.capacity().remaining() < length {
×
131
            return Err(StoreError::NoCapacity);
×
132
        }
×
133
        Ok(())
×
134
    }
×
135
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc