blooms_db/lib.rs
1// Copyright 2015-2020 Parity Technologies (UK) Ltd.
2// This file is part of Tetsy Vapory.
3
4// Tetsy Vapory is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// Tetsy Vapory is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with Tetsy Vapory. If not, see <http://www.gnu.org/licenses/>.
16
17//! Vapory blooms database
18
19mod db;
20mod file;
21
22use std::io;
23use std::path::Path;
24use vapbloom;
25use parking_lot::Mutex;
26
27/// Threadsafe API for blooms database.
28///
29/// # Warning
30///
31/// This database does not guarantee atomic writes.
32pub struct Database {
33 database: Mutex<db::Database>,
34}
35
36impl Database {
37 /// Creates new database handle.
38 ///
39 /// # Arguments
40 ///
41 /// * `path` - database directory
42 pub fn open<P>(path: P) -> io::Result<Database> where P: AsRef<Path> {
43 let result = Database {
44 database: Mutex::new(db::Database::open(path)?),
45 };
46
47 Ok(result)
48 }
49
50 /// Closes the inner database
51 pub fn close(&self) -> io::Result<()> {
52 self.database.lock().close()
53 }
54
55 /// Reopens database at the same location.
56 pub fn reopen(&self) -> io::Result<()> {
57 self.database.lock().reopen()
58 }
59
60 /// Inserts one or more blooms into database.
61 ///
62 /// # Arguments
63 ///
64 /// * `from` - index of the first bloom that needs to be inserted
65 /// * `blooms` - iterator over blooms
66 pub fn insert_blooms<'a, I, B>(&self, from: u64, blooms: I) -> io::Result<()>
67 where vapbloom::BloomRef<'a>: From<B>, I: Iterator<Item = B> {
68 self.database.lock().insert_blooms(from, blooms)
69 }
70
71 /// Returns indexes of all headers matching given bloom in a specified range.
72 ///
73 /// # Arguments
74 ///
75 /// * `from` - index of the first bloom that needs to be checked
76 /// * `to` - index of the last bloom that needs to be checked (inclusive range)
77 /// * `blooms` - searched pattern
78 pub fn filter<'a, B, I, II>(&self, from: u64, to: u64, blooms: II) -> io::Result<Vec<u64>>
79 where vapbloom::BloomRef<'a>: From<B>, II: IntoIterator<Item = B, IntoIter = I> + Copy, I: Iterator<Item = B> {
80 self.database.lock()
81 .iterate_matching(from, to, blooms)?
82 .collect::<Result<Vec<u64>, _>>()
83 }
84}