cloud_mmr/
storage.rs

1// Copyright 2021 The Grin Developers
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//! Storage of core types using RocksDB.
16
17//use grin_core as core;
18
19pub mod leaf_set;
20pub mod lmdb;
21pub mod pmmr;
22pub mod prune_list;
23pub mod types;
24
25const SEP: u8 = b':';
26
27use byteorder::{BigEndian, WriteBytesExt};
28
29pub use lmdb::*;
30
31/// Build a db key from a prefix and a byte vector identifier.
32pub fn to_key<K: AsRef<[u8]>>(prefix: u8, k: K) -> Vec<u8> {
33    let k = k.as_ref();
34    let mut res = Vec::with_capacity(k.len() + 2);
35    res.push(prefix);
36    res.push(SEP);
37    res.extend_from_slice(k);
38    res
39}
40
41/// Build a db key from a prefix and a byte vector identifier and numeric identifier
42pub fn to_key_u64<K: AsRef<[u8]>>(prefix: u8, k: K, val: u64) -> Vec<u8> {
43    let k = k.as_ref();
44    let mut res = Vec::with_capacity(k.len() + 10);
45    res.push(prefix);
46    res.push(SEP);
47    res.extend_from_slice(k);
48    res.write_u64::<BigEndian>(val).unwrap();
49    res
50}
51/// Build a db key from a prefix and a numeric identifier.
52pub fn u64_to_key(prefix: u8, val: u64) -> Vec<u8> {
53    let mut res = Vec::with_capacity(10);
54    res.push(prefix);
55    res.push(SEP);
56    res.write_u64::<BigEndian>(val).unwrap();
57    res
58}
59
60use std::ffi::OsStr;
61use std::fs::{remove_file, rename, File};
62use std::path::Path;
63
64/// Creates temporary file with name created by adding `temp_suffix` to `path`.
65/// Applies writer function to it and renames temporary file into original specified by `path`.
66pub fn save_via_temp_file<F, P, E>(
67    path: P,
68    temp_suffix: E,
69    mut writer: F,
70) -> Result<(), std::io::Error>
71where
72    F: FnMut(&mut File) -> Result<(), std::io::Error>,
73    P: AsRef<Path>,
74    E: AsRef<OsStr>,
75{
76    let temp_suffix = temp_suffix.as_ref();
77    assert!(!temp_suffix.is_empty());
78
79    let original = path.as_ref();
80    let mut _original = original.as_os_str().to_os_string();
81    _original.push(temp_suffix);
82    // Write temporary file
83    let temp_path = Path::new(&_original);
84    if temp_path.exists() {
85        remove_file(&temp_path)?;
86    }
87
88    let mut temp_file = File::create(&temp_path)?;
89
90    // write the new data to the temp file
91    writer(&mut temp_file)?;
92
93    // force an fsync on the temp file to ensure bytes are on disk
94    temp_file.sync_all()?;
95
96    rename(&temp_path, &original)?;
97
98    Ok(())
99}
100
101use croaring::Bitmap;
102use std::io::{self, Read};
103/// Read Bitmap from a file
104pub fn read_bitmap<P: AsRef<Path>>(file_path: P) -> io::Result<Bitmap> {
105    let mut bitmap_file = File::open(file_path)?;
106    let f_md = bitmap_file.metadata()?;
107    let mut buffer = Vec::with_capacity(f_md.len() as usize);
108    bitmap_file.read_to_end(&mut buffer)?;
109    Ok(Bitmap::deserialize(&buffer))
110}