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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate crossbeam_utils;
#[macro_use]
extern crate cstr;
extern crate libc;
#[macro_use]
extern crate log;
extern crate moz_task;
extern crate nserror;
extern crate nsstring;
extern crate once_cell;
extern crate rkv;
extern crate serde_json;
extern crate tempfile;
extern crate thiserror;
#[macro_use]
extern crate xpcom;
mod error;
mod ffi;
mod iter;
mod persist;
mod statics;
use crate::{
error::{XULStoreError, XULStoreResult},
iter::XULStoreIterator,
persist::{flush_writes, persist},
statics::DATA_CACHE,
};
use nsstring::nsAString;
use std::collections::btree_map::Entry;
use std::fmt::Display;
const SEPARATOR: char = '\u{0009}';
pub(crate) fn make_key(doc: &impl Display, id: &impl Display, attr: &impl Display) -> String {
format!("{}{}{}{}{}", doc, SEPARATOR, id, SEPARATOR, attr)
}
pub(crate) fn set_value(
doc: &nsAString,
id: &nsAString,
attr: &nsAString,
value: &nsAString,
) -> XULStoreResult<()> {
debug!("XULStore set value: {} {} {} {}", doc, id, attr, value);
// bug 319846 -- don't save really long attributes or values.
if id.len() > 512 || attr.len() > 512 {
return Err(XULStoreError::IdAttrNameTooLong);
}
let value = if value.len() > 4096 {
warn!("XULStore: truncating long attribute value");
String::from_utf16(&value[0..4096])?
} else {
String::from_utf16(value)?
};
let mut cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_mut() {
Some(data) => data,
None => return Ok(()),
};
data.entry(doc.to_string())
.or_default()
.entry(id.to_string())
.or_default()
.insert(attr.to_string(), value.clone());
persist(make_key(doc, id, attr), Some(value))?;
Ok(())
}
pub(crate) fn has_value(doc: &nsAString, id: &nsAString, attr: &nsAString) -> XULStoreResult<bool> {
debug!("XULStore has value: {} {} {}", doc, id, attr);
let cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_ref() {
Some(data) => data,
None => return Ok(false),
};
match data.get(&doc.to_string()) {
Some(ids) => match ids.get(&id.to_string()) {
Some(attrs) => Ok(attrs.contains_key(&attr.to_string())),
None => Ok(false),
},
None => Ok(false),
}
}
pub(crate) fn get_value(
doc: &nsAString,
id: &nsAString,
attr: &nsAString,
) -> XULStoreResult<String> {
debug!("XULStore get value {} {} {}", doc, id, attr);
let cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_ref() {
Some(data) => data,
None => return Ok(String::new()),
};
match data.get(&doc.to_string()) {
Some(ids) => match ids.get(&id.to_string()) {
Some(attrs) => match attrs.get(&attr.to_string()) {
Some(value) => Ok(value.clone()),
None => Ok(String::new()),
},
None => Ok(String::new()),
},
None => Ok(String::new()),
}
}
pub(crate) fn remove_value(
doc: &nsAString,
id: &nsAString,
attr: &nsAString,
) -> XULStoreResult<()> {
debug!("XULStore remove value {} {} {}", doc, id, attr);
let mut cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_mut() {
Some(data) => data,
None => return Ok(()),
};
let mut ids_empty = false;
if let Some(ids) = data.get_mut(&doc.to_string()) {
let mut attrs_empty = false;
if let Some(attrs) = ids.get_mut(&id.to_string()) {
attrs.remove(&attr.to_string());
if attrs.is_empty() {
attrs_empty = true;
}
}
if attrs_empty {
ids.remove(&id.to_string());
if ids.is_empty() {
ids_empty = true;
}
}
};
if ids_empty {
data.remove(&doc.to_string());
}
persist(make_key(doc, id, attr), None)?;
Ok(())
}
pub(crate) fn remove_document(doc: &nsAString) -> XULStoreResult<()> {
debug!("XULStore remove document {}", doc);
let mut cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_mut() {
Some(data) => data,
None => return Ok(()),
};
if let Entry::Occupied(entry) = data.entry(doc.to_string()) {
for (id, attrs) in entry.get() {
for attr in attrs.keys() {
persist(make_key(entry.key(), id, attr), None)?;
}
}
entry.remove_entry();
}
Ok(())
}
pub(crate) fn get_ids(doc: &nsAString) -> XULStoreResult<XULStoreIterator> {
debug!("XULStore get IDs for {}", doc);
let cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_ref() {
Some(data) => data,
None => return Ok(XULStoreIterator::new(vec![].into_iter())),
};
match data.get(&doc.to_string()) {
Some(ids) => {
let ids: Vec<String> = ids.keys().cloned().collect();
Ok(XULStoreIterator::new(ids.into_iter()))
}
None => Ok(XULStoreIterator::new(vec![].into_iter())),
}
}
pub(crate) fn get_attrs(doc: &nsAString, id: &nsAString) -> XULStoreResult<XULStoreIterator> {
debug!("XULStore get attrs for doc, ID: {} {}", doc, id);
let cache_guard = DATA_CACHE.lock()?;
let data = match cache_guard.as_ref() {
Some(data) => data,
None => return Ok(XULStoreIterator::new(vec![].into_iter())),
};
match data.get(&doc.to_string()) {
Some(ids) => match ids.get(&id.to_string()) {
Some(attrs) => {
let attrs: Vec<String> = attrs.keys().cloned().collect();
Ok(XULStoreIterator::new(attrs.into_iter()))
}
None => Ok(XULStoreIterator::new(vec![].into_iter())),
},
None => Ok(XULStoreIterator::new(vec![].into_iter())),
}
}
pub(crate) fn shutdown() -> XULStoreResult<()> {
flush_writes()
}
|