-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheed.rs
167 lines (138 loc) · 4.56 KB
/
heed.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use std::collections::HashSet;
use std::fs;
use async_trait::async_trait;
use heed::types::*;
use heed::EnvOpenOptions;
use cosmian_findex::{parameters::UID_LENGTH, EncryptedTable, Uid, UpsertData};
use crate::{
core::{Index, IndexesDatabase, Table},
errors::Error,
};
pub(crate) struct Database {
env: heed::Env,
db: heed::Database<ByteSlice, ByteSlice>,
}
impl Database {
pub(crate) fn create() -> Self {
let indexes_url = "data/indexes.lmdb";
fs::create_dir_all(indexes_url).expect("Cannot create LMDB directory");
let env = EnvOpenOptions::new()
.map_size(4 * 1024 * 1024 * 1024)
.open(indexes_url)
.expect("Cannot open database");
// we will open the default unamed database
let db = env.create_database(None).expect("Cannot create database");
Database { env, db }
}
}
#[async_trait]
impl IndexesDatabase for Database {
async fn set_size(&self, index: &mut Index) -> Result<(), Error> {
let txn = self.env.read_txn()?;
index.size = Some(
self.db
.get(&txn, &size_key(index))?
.and_then(|bytes| bytes.try_into().ok())
.map(|bytes| usize::from_be_bytes(bytes) as i64)
.unwrap_or(0),
);
Ok(())
}
async fn fetch(
&self,
index: &Index,
table: Table,
uids: HashSet<Uid<UID_LENGTH>>,
) -> Result<EncryptedTable<UID_LENGTH>, Error> {
let mut uids_and_values = EncryptedTable::<UID_LENGTH>::with_capacity(uids.len());
let txn = self.env.read_txn()?;
for uid in uids {
if let Some(value) = self.db.get(&txn, &key(index, table, &uid))? {
uids_and_values.insert(uid, value.to_vec());
}
}
Ok(uids_and_values)
}
async fn upsert_entries(
&self,
index: &Index,
data: UpsertData<UID_LENGTH>,
) -> Result<EncryptedTable<UID_LENGTH>, Error> {
let mut rejected = EncryptedTable::<UID_LENGTH>::with_capacity(1);
let mut txn = self.env.write_txn()?;
for (uid, (old_value, new_value)) in data {
let key = key(index, Table::Entries, &uid);
let existing_value = self.db.get(&txn, &key)?;
if existing_value == old_value.as_deref() {
if existing_value.is_none() {
let size = self
.db
.get(&txn, &size_key(index))?
.and_then(|bytes| bytes.try_into().ok())
.map(|bytes| usize::from_be_bytes(bytes) as i64)
.unwrap_or(0);
self.db.put(
&mut txn,
&size_key(index),
&(size + new_value.len() as i64).to_be_bytes(),
)?;
}
self.db.put(&mut txn, &key, &new_value)?;
} else if let Some(existing_value) = existing_value {
rejected.insert(uid, existing_value.to_vec());
} else {
log::error!(
"Receive an `old_value` {old_value:?} but no existing value inside DB for UID {uid:?}."
);
}
}
txn.commit()?;
Ok(rejected)
}
async fn insert_chains(
&self,
index: &Index,
data: EncryptedTable<UID_LENGTH>,
) -> Result<(), Error> {
let mut txn = self.env.write_txn()?;
let mut size = self
.db
.get(&txn, &size_key(index))?
.and_then(|bytes| bytes.try_into().ok())
.map(|bytes| usize::from_be_bytes(bytes) as i64)
.unwrap_or(0);
for (uid, value) in data {
size += value.len() as i64;
self.db
.put(&mut txn, &key(index, Table::Chains, &uid), &value)?;
}
self.db
.put(&mut txn, &size_key(index), &size.to_be_bytes())?;
txn.commit()?;
Ok(())
}
}
#[derive(Copy, Clone, Debug)]
#[repr(u8)]
pub(crate) enum Prefix {
Entries,
Chains,
Size,
}
fn table_to_prefix(table: Table) -> Prefix {
match table {
Table::Entries => Prefix::Entries,
Table::Chains => Prefix::Chains,
}
}
fn key(index: &Index, table: Table, uid: &Uid<UID_LENGTH>) -> Vec<u8> {
[
(index.id.as_bytes()),
&[table_to_prefix(table) as u8][..],
uid.as_ref(),
]
.concat()
}
fn size_key(index: &Index) -> Vec<u8> {
[(index.id.as_bytes()), &[Prefix::Size as u8][..]].concat()
}