-
Notifications
You must be signed in to change notification settings - Fork 1
/
indexer.rs
158 lines (144 loc) · 4.84 KB
/
indexer.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
/// Indexers for recognized mime types.
use crate::common::{ResourceMetadata, TransactionResult, Variant};
use crate::fts::Fts;
use async_std::io::{ReadExt, SeekFrom};
use async_trait::async_trait;
use futures::AsyncSeekExt;
use serde_json::Value;
use sqlx::{Sqlite, Transaction};
#[async_trait(?Send)]
pub trait Indexer {
async fn index<'c>(
&self,
meta: &ResourceMetadata,
variant: &mut Variant,
fts: &Fts,
mut tx: Transaction<'c, Sqlite>,
) -> TransactionResult<'c>;
}
#[allow(clippy::upper_case_acronyms)]
type OVS = Option<Vec<String>>;
// A generic indexer for flat Json data structures.
// Indexed properties are strings and string arrays members.
pub struct FlatJsonIndexer {
fields: Vec<String>,
mime_type: String,
#[allow(clippy::type_complexity)]
custom_fn: Option<Box<dyn Fn(&str, &str) -> OVS + Send + Sync>>,
}
impl FlatJsonIndexer {
#[allow(clippy::type_complexity)]
pub fn new(
mime_type: &str,
fields: &[&str],
custom_fn: Option<Box<dyn Fn(&str, &str) -> OVS + Send + Sync>>,
) -> Self {
Self {
fields: fields.iter().map(|e| (*e).to_owned()).collect(),
mime_type: mime_type.into(),
custom_fn,
}
}
}
#[async_trait(?Send)]
impl Indexer for FlatJsonIndexer {
async fn index<'c>(
&self,
meta: &ResourceMetadata,
variant: &mut Variant,
fts: &Fts,
mut tx: Transaction<'c, Sqlite>,
) -> TransactionResult<'c> {
// 0. Filter by mime type.
if self.mime_type != variant.metadata.mime_type() {
return Ok(tx);
}
macro_rules! indexme {
($field:expr, $text:expr) => {
if let Some(func) = &self.custom_fn {
if let Some(custom_text) = func($field, $text) {
for item in custom_text {
tx = fts
.add_text(&meta.id(), &variant.metadata.name(), &item, tx)
.await?;
}
}
} else {
tx = fts
.add_text(&meta.id(), &variant.metadata.name(), $text, tx)
.await?;
}
};
}
let content = &mut variant.reader;
// 1. Read the content as json.
content.seek(SeekFrom::Start(0)).await?;
let mut buffer = vec![];
content.read_to_end(&mut buffer).await?;
let v: Value = serde_json::from_slice(&buffer)?;
// 2. Index each available field.
for field in &self.fields {
match v.get(field) {
Some(Value::String(text)) => {
indexme!(field, text);
}
Some(Value::Array(array)) => {
for item in array {
if let Value::String(text) = item {
indexme!(field, text);
}
}
}
_ => {}
}
}
// 3. Re-position the stream at the beginning.
content.seek(SeekFrom::Start(0)).await?;
Ok(tx)
}
}
// Indexer for the content of a "Places" object.
// This is a json value with the following format:
// { url: "...", title: "...", icon: "..." }
pub fn create_places_indexer() -> FlatJsonIndexer {
FlatJsonIndexer::new("application/x-places+json", &["url", "title"], None)
}
// Indexer for the content of a "Contacts" object.
// This is a json value with the following format:
// { name: "...", phone: "[...]", email: "[...]" }
fn custom_contact_index(field: &str, text: &str) -> OVS {
if text.is_empty() {
None
} else if field == "name" {
Some(vec![
text.to_owned(),
format!("^^^^{}", text.chars().next().unwrap()),
])
} else {
Some(vec![text.to_owned()])
}
}
pub fn create_contacts_indexer() -> FlatJsonIndexer {
FlatJsonIndexer::new(
"application/x-contact+json",
&["name", "phone", "email"],
Some(Box::new(custom_contact_index)),
)
}
// Indexer for the content of a "Media" object.
// This is a json value with the following format:
// {"url":"https://beatbump.ml/search/echoes%2520pink%2520floyd?filter=EgWKAQIIAWoKEAMQBBAKEAkQBQ%3D%3D",
// "icon":"https://beatbump.ml/logo-header.png",
// "title":"Echoes",
// "album":"Meddle",
// "artist": "Pink Floyd",
// "artwork":[{"sizes":"128x128",
// "src":"https://lh3.googleusercontent.com/p2_pHFA7u4uxGvEYoKvhiyxLDUCxPxJCMwRQLVMAMs4FF5lxb0hcVAa6iJY4UvMjrSiAwM6HiqXzyy4=w128-h128-l90-rj",
// "type":"image/jpeg"}]}
pub fn create_media_indexer() -> FlatJsonIndexer {
FlatJsonIndexer::new(
"application/x-media+json",
&["title", "album", "artist"],
None,
)
}