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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
use crate::{
core::{Session, SessionsApi},
delta::{DeltaBuilder, DeltaType},
error::Result,
meta::{tags::TagCache, users::UserTable},
query::{Query, QueryIterator, QueryResult, SetQuery, SubHub, Subscription},
store::Store,
utils::{Diff, Id, Path, TagSet},
};
use async_std::sync::{Arc, RwLock};
use std::fmt::Debug;
use tracing::info;
/// In-memory representation of an alexandria database
///
/// Refer to [`Builder`][builder] to configure and initialise an alexandria
/// instance.
///
/// [builder]: struct.Builder.html
pub struct Library {
// /// The main management path
// pub(crate) root: Dirs,
/// Table with encrypted user metadata
pub(crate) users: RwLock<UserTable>,
/// Cache of tag/path mappings
pub(crate) tag_cache: RwLock<TagCache>,
/// The main data store
pub(crate) store: RwLock<Store>,
/// The state handler for subscriptions
pub(crate) subs: Arc<SubHub>,
}
impl Library {
/// Internally called setup function
pub(crate) fn init(self) -> Result<Self> {
// self.root.scaffold()?;
Ok(self)
}
/// Load and re-initialise a previous database session from disk
pub fn load<'tmp, P, S>(_: P, _: S) -> Result<Self>
where
P: Into<&'tmp Path>,
S: Into<String>,
{
unimplemented!()
}
/// Load the database sessions API scope
pub fn sessions<'lib>(&'lib self) -> SessionsApi<'lib> {
SessionsApi { inner: self }
}
/// Similar to `insert`, but instead operating on a batch of Diffs
#[tracing::instrument(skip(self, data, tags), level = "info")]
pub async fn batch<T, D>(&self, id: Session, path: Path, tags: T, data: Vec<D>) -> Result<Id>
where
T: Into<TagSet>,
D: Into<Diff>,
{
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
let mut db = DeltaBuilder::new(id, DeltaType::Insert);
let tags = tags.into();
let mut store = self.store.write().await;
let rec_id = store.batch(
&mut db,
id,
&path,
tags.clone(),
data.into_iter().map(|d| d.into()).collect(),
)?;
drop(store);
let mut tc = self.tag_cache.write().await;
tags.iter().fold(Ok(()), |res, t| {
res.and_then(|_| tc.insert(id, path.clone(), t.clone()))
})?;
drop(tc);
self.subs.queue(db.make()).await;
info!("Batch insert succeeded");
Ok(rec_id)
}
/// Insert a new record into the library and return it's ID
///
/// You need to have a valid and active user session to do so, and
/// the `path` must be unique.
#[tracing::instrument(skip(self, data, tags), level = "info")]
pub async fn insert<T, D>(&self, id: Session, path: Path, tags: T, data: D) -> Result<Id>
where
T: Into<TagSet>,
D: Into<Diff>,
{
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
let mut db = DeltaBuilder::new(id, DeltaType::Insert);
let tags = tags.into();
let mut store = self.store.write().await;
let rec_id = store.insert(&mut db, id, &path, tags.clone(), data.into())?;
drop(store);
let mut tc = self.tag_cache.write().await;
tags.iter().fold(Ok(()), |res, t| {
res.and_then(|_| tc.insert(id, path.clone(), t.clone()))
})?;
drop(tc);
self.subs.queue(db.make()).await;
info!("Record insert succeeded");
Ok(rec_id)
}
#[tracing::instrument(skip(self), level = "info")]
pub async fn delete(&self, id: Session, path: Path) -> Result<()> {
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
let mut db = DeltaBuilder::new(id, DeltaType::Delete);
let mut store = self.store.write().await;
store.destroy(&mut db, id, &path)?;
drop(store);
let mut tc = self.tag_cache.write().await;
tc.delete_path(id, path)?;
drop(tc);
self.subs.queue(db.make()).await;
info!("Record delete succeeded");
Ok(())
}
/// Update a record in-place
#[tracing::instrument(skip(self, diff), level = "info")]
pub async fn update<D>(&self, id: Session, path: Path, diff: D) -> Result<()>
where
D: Into<Diff>,
{
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
let mut db = DeltaBuilder::new(id, DeltaType::Update);
let mut store = self.store.write().await;
store.update(&mut db, id, &path, diff.into())?;
drop(store);
self.subs.queue(db.make()).await;
info!("Record update succeeded");
Ok(())
}
/// Query the database with a specific query object
///
/// Request data from alexandria via a `Query` object. A query
/// can only touch a single parameter, such as the Record Id, the
/// path or a set query via tags. The data returned are snapshots
/// or records that are immutable. If you want to make changes to
/// them, use `update()` with a Diff instead.
///
/// Also: future writes will not propagate to the copy of the
/// Record returned from this function, because alexandria is
/// Copy-on-Write. You will need to query the database again in
/// the future.
///
/// ## Examples
///
/// This code makes a direct query via the path of a record. This
/// will only return a single record if successful.
///
/// ```
/// # use alexandria::{Builder, GLOBAL, Library, error::Result, utils::{Tag, TagSet, Path}, query::Query};
/// # async fn foo() -> Result<()> {
/// # let tmp = tempfile::tempdir().unwrap();
/// # let lib = Builder::new().offset(tmp.path()).build().unwrap();
/// let path = Path::from("/msg:alice");
/// lib.query(GLOBAL, Query::Path(path)).await;
/// # Ok(()) }
/// ```
///
/// ### Search tags
///
/// In alexandria you can tag records with extra metadata (which
/// is also encrypted), to make queries easier and even build
/// relationships between records in your application. These tags
/// are String-keyed, with an arbitrary (or no) payload and can be
/// used to make more precise (and fast!) search queries into the
/// database.
///
/// The constraints imposed by tag queries are modelled on set
/// theory and can be created via the [`TagQuery`][tq] helper type.
///
/// [tq]: query/struct.TagQuery.html
///
/// Following are a few examples for tag queries.
///
/// ```
/// # use alexandria::{GLOBAL, Builder, Library, error::Result, utils::{Tag, TagSet, Path}, query::Query};
/// # async fn foo() -> Result<()> {
/// # let tmp = tempfile::tempdir().unwrap();
/// # let lib = Builder::new().offset(tmp.path()).build().unwrap();
/// # let tag1 = Tag::new("tag1", vec![1, 3, 1, 2]);
/// # let tag2 = Tag::new("tag2", vec![13, 12]);
/// let tags = TagSet::from(vec![tag1, tag2]);
/// lib.query(GLOBAL, Query::tags().subset(tags)).await;
/// # Ok(()) }
/// # async_std::task::block_on(async { foo().await }).unwrap();
/// ```
#[tracing::instrument(skip(self), level = "info")]
pub async fn query<S>(&self, id: S, q: Query) -> Result<QueryResult>
where
S: Into<Session> + Debug,
{
let id = id.into();
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
let store = self.store.read().await;
match q {
Query::Path(ref path) => store.get_path(id, path).map(|rec| QueryResult::Single(rec)),
Query::Tag(query) => {
let tc = self.tag_cache.read().await;
match query {
SetQuery::Intersect(ref tags) => tc.get_paths(id, |o| o.intersect(tags)),
SetQuery::Subset(ref tags) => tc.get_paths(id, |o| o.subset(tags)),
SetQuery::Equals(ref tags) => tc.get_paths(id, |o| o.equality(tags)),
SetQuery::Not(ref tags) => tc.get_paths(id, |o| o.not(tags)),
}
.iter()
.map(|p| store.get_path(id, p))
.collect::<Result<Vec<_>>>()
.map(|vec| QueryResult::Many(vec))
}
_ => unimplemented!(),
}
}
/// Create an iterator from a database query
///
/// The primary difference between this function and `query()` is
/// that no records are returned or loaded immediately from the
/// database. Instead a query is stored, sized and estimated at
/// the time of querying and can then be stepped through. This
/// allows for fetching only a range of objects, limiting memory
/// usage.
///
/// Paths that are inserted after the `QueryIterator` was
/// constructed aren't automatically added to it, because it's
/// internal state is atomic for the time it was created. If you
/// want to get updates to the database as they happen, consider a
/// `Subscription` instead.
///
/// Following is an example for an iterator query, mirroring most
/// of the `query()` usage quite closely.
///
/// ```
/// # use alexandria::{GLOBAL, Builder, Library, error::Result, utils::{Tag, TagSet, Path}, query::Query};
/// # async fn foo() -> Result<()> {
/// # let tmp = tempfile::tempdir().unwrap();
/// # let lib = Builder::new().offset(tmp.path()).build().unwrap();
/// # let tag1 = Tag::new("tag1", vec![1, 3, 1, 2]);
/// # let tag2 = Tag::new("tag2", vec![13, 12]);
/// let tags = TagSet::from(vec![tag1, tag2]);
/// let iter = lib
/// .query_iter(GLOBAL, Query::tags().equals(tags))
/// .await?;
/// iter.skip(5);
/// let rec = iter.next().await;
/// # Ok(()) }
/// ```
///
/// ## Garbage collection
///
/// By default, garbage collection isn't locked for paths that are
/// included in an iterator. What this means is that any `delete`
/// call can remove records that will at some point be accessed by
/// the returned iterator, resulting in an `Err(_)` return. To
/// avoid this race condition, you can call `lock()` on the
/// iterator, which blocks alexandria from cleaning the iternal
/// record representation for items that are supposed to be
/// accessed by the iterator.
///
/// **Note:** `query` may still return "No such path" for these
/// items, since they were already deleted from the tag cache.
/// And a caveat worth mentioning: if the program aborts before
/// the Iterator `drop` was able to run, the items will not be
/// cleaned from disk and reloaded into cache on restart.
#[tracing::instrument(skip(self), level = "info")]
pub async fn query_iter<S>(self: &Arc<Self>, id: S, q: Query) -> Result<QueryIterator>
where
S: Into<Session> + Debug,
{
let id = id.into();
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
Ok(QueryIterator::new(
id,
match q {
Query::Path(ref p) => vec![p.clone()],
Query::Tag(ref tq) => {
let tc = self.tag_cache.read().await;
match tq {
SetQuery::Intersect(ref tags) => tc.get_paths(id, |o| tags.intersect(o)),
SetQuery::Subset(ref tags) => {
// FIXME: I don't really know why this
// operation needs to be asymptotic, but
// some operations seem to be backwards?
// In either case, this works but we
// should figure out why this is.
tc.get_paths(id, |o| tags.subset(o) || o.subset(tags))
}
SetQuery::Equals(ref tags) => tc.get_paths(id, |o| tags.equality(o)),
SetQuery::Not(ref tags) => tc.get_paths(id, |o| tags.not(o)),
}
}
_ => unimplemented!(),
},
Arc::clone(self),
q,
))
}
/// Subscribe to future database updates via a query filter
///
/// When querying repeatedly isn't an option, or would lead to
/// decreased performance, it's also possible to register a
/// subscription. They use the same mechanism as Queries to
/// filter through tags and paths, but return a type that can be
/// async-polled for updates.
///
/// This doesn't give immediate access to the data, only the path
/// that was changed, but can then be used to make a real query
/// into the database to get an updated set of data.
///
/// ```
/// # use alexandria::{GLOBAL, Builder, Library, error::Result, utils::{Tag, TagSet, Path}, query::{Query, SetQuery}};
/// # async fn foo() -> Result<()> {
/// # let tmp = tempfile::tempdir().unwrap();
/// # let lib = Builder::new().offset(tmp.path()).build().unwrap();
/// # let my_tag = Tag::new("tag1", vec![1, 3, 1, 2]);
/// let tags = TagSet::from(vec![my_tag]);
/// let sub = lib.subscribe(GLOBAL, Query::tags().subset(tags)).await?;
///
/// let path = sub.next().await;
/// let new_data = lib.query(GLOBAL, Query::Path(path)).await?;
/// # Ok(()) }
/// ```
#[tracing::instrument(skip(self), level = "info")]
pub async fn subscribe<S>(&self, id: S, q: Query) -> Result<Subscription>
where
S: Into<Session> + Debug,
{
let id = id.into();
if let Session::Id(id) = id {
self.users.read().await.is_open(id)?;
info!("Passed open-auth for id `{}`", id.to_string());
}
Ok(self.subs.add_sub(q).await)
}
/// Check if a path exists for a particular session
///
/// When inserting data, sometimes it's useful to check the path
/// that was just inserted, or was inserted by a previous
/// operation. Because this is such a common operation, this
/// utility function aims to make this workflow easier.
///
/// If you want the actual type of a path node, use `query()` instead!
pub async fn path_exists<S>(&self, id: S, p: Path) -> Result<bool>
where
S: Into<Session> + Debug,
{
self.query(id, Query::path(p)).await.map(|res| res.single())
}
}