summaryrefslogtreecommitdiff
path: root/plugins/warp/src/container/network.rs
blob: 2a2d7c65a4d2a9c9ded1392769303a9a50e2350e (plain)
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
use crate::container::disk::DiskContainer;
use crate::container::{Container, ContainerError, ContainerResult, SourceId, SourcePath};
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use warp::chunk::{Chunk, ChunkKind, CompressionType};
use warp::r#type::guid::TypeGUID;
use warp::r#type::{ComputedType, Type};
use warp::signature::chunk::SignatureChunk;
use warp::signature::function::{Function, FunctionGUID};
use warp::target::Target;
use warp::{WarpFile, WarpFileHeader};

pub mod client;

pub use client::NetworkClient;

/// This is the id on the server for the [`Target`], we can get it via [`NetworkClient::query_target_id`].
pub type NetworkTargetId = i32;

pub struct NetworkContainer {
    client: NetworkClient,
    /// This is the store that the interface will write to; then we have special functions for pulling
    /// and pushing to the network source.
    cache: DiskContainer,
    /// Populated when targets are queried.
    known_targets: HashMap<Target, Option<NetworkTargetId>>,
    /// Populated with function sources are queried.
    known_function_sources: HashMap<FunctionGUID, Vec<SourceId>>,
    /// Populated when user adds function, this is used for writing back to the server.
    added_chunks: HashMap<SourceId, Vec<Chunk<'static>>>,
}

impl NetworkContainer {
    pub fn new(client: NetworkClient) -> Self {
        Self {
            cache: DiskContainer::new("Network Container".to_string(), HashMap::new()),
            client,
            known_targets: HashMap::new(),
            known_function_sources: HashMap::new(),
            added_chunks: HashMap::new(),
        }
    }

    /// Gets the network id for the `target`, this will be used in later function queries.
    ///
    /// **This is blocking**
    ///
    /// # Caching policy
    ///
    /// The [`NetworkTargetId`] is unique and immutable, so they will be persisted indefinitely.
    pub fn get_target_id(&mut self, target: &Target) -> Option<NetworkTargetId> {
        // It's highly probable we have previously queried the target, check that first.
        if let Some(target_id) = self.known_targets.get(target) {
            return target_id.clone();
        }

        let target_id = self.client.query_target_id(target);
        // Keep the target id so the next lookup is free.
        self.known_targets.insert(target.clone(), target_id);
        target_id
    }

    /// Pulls sources for the set of unseen function guids.
    ///
    /// **This is blocking**
    ///
    /// # Caching policy
    ///
    /// When we get the source, we store the results indefinitely in the container; this is fine
    /// for now as the requests for functions come at the request of some user interaction. Any guid
    /// with no sources will still be cached.
    pub fn get_unseen_functions_source(
        &mut self,
        target: Option<&Target>,
        guids: &[FunctionGUID],
    ) -> HashMap<SourceId, Vec<FunctionGUID>> {
        let Some(target_id) = target.and_then(|t| self.get_target_id(t)) else {
            log::debug!("Cannot query functions source without a target, skipping...");
            return HashMap::new();
        };

        // Split guids into known and unknown
        let (_known, unknown): (Vec<_>, Vec<_>) = guids
            .into_iter()
            .cloned()
            .partition(|guid| self.known_function_sources.contains_key(guid));

        let mut result: HashMap<SourceId, Vec<FunctionGUID>> = HashMap::new();
        // Only query server for unknown guids if we have any.
        if !unknown.is_empty() {
            if let Some(queried_results) = self
                .client
                .query_functions_source(Some(target_id), &unknown)
            {
                // Cache the new results, this means we will not try and contact the server for that guids source.
                // NOTE: Here we do not just simply list the queried results because we also
                // want to cache function guids which have no source, this is important so that we never
                // attempt to contact the server for that guid.
                for guid in &unknown {
                    let sources = queried_results
                        .keys()
                        .filter(|source_id| queried_results[source_id].contains(guid))
                        .cloned()
                        .collect();
                    self.known_function_sources.insert(*guid, sources);
                }

                for (source_id, guids) in queried_results {
                    result.entry(source_id).or_default().extend(guids);
                }
            }
        }

        result
    }

    /// Pulls function metadata from the server and adds it into the container cache.
    ///
    /// **This is blocking**
    ///
    /// # Caching policy
    ///
    /// Every request we store the returned objects on disk, this means that users will first
    /// query against the disk objects, then the server. This also means we need to cache functions f
    /// or which we have not received any functions for, as otherwise we would keep trying to query it.
    pub fn pull_functions(
        &mut self,
        target: &Target,
        source: &SourceId,
        functions: &[FunctionGUID],
    ) {
        let target_id = self.get_target_id(target);
        if let Some(file) = self
            .client
            .query_functions(target_id, Some(*source), functions)
        {
            log::debug!("Got {} chunks from server", file.chunks.len());
            for chunk in &file.chunks {
                match &chunk.kind {
                    ChunkKind::Signature(sc) => {
                        let functions: Vec<_> = sc.functions().collect();
                        match self.cache.add_functions(target, source, &functions) {
                            Ok(_) => log::debug!(
                                "Added {} functions into cached source '{}'",
                                functions.len(),
                                source
                            ),
                            Err(err) => log::error!(
                                "Failed to add {} function into cached source '{}': {}",
                                functions.len(),
                                source,
                                err
                            ),
                        }
                    }
                    // TODO; Probably want to pull type in with this.
                    ChunkKind::Type(_) => {}
                }
            }
        }
    }

    /// Push a file to the network source.
    ///
    /// **This is blocking**
    pub fn push_file(&mut self, source_id: SourceId, file: &WarpFile) {
        self.client.push_file(source_id, file);
    }
}

impl Container for NetworkContainer {
    fn sources(&self) -> ContainerResult<Vec<SourceId>> {
        self.cache.sources()
    }

    fn add_source(&mut self, path: SourcePath) -> ContainerResult<SourceId> {
        // TODO: How do we want to let users create new sources?
        log::error!("NetworkContainer::add_source not allowed");
        Err(ContainerError::CannotCreateSource(path))
    }

    fn commit_source(&mut self, source: &SourceId) -> ContainerResult<bool> {
        let chunks = self
            .added_chunks
            .remove(source)
            .ok_or(ContainerError::SourceNotFound(source.clone()))?;
        let file = WarpFile::new(WarpFileHeader::new(), chunks);
        self.push_file(*source, &file);
        Ok(true)
    }

    fn is_source_writable(&self, source: &SourceId) -> ContainerResult<bool> {
        // TODO: This is retrievable from /users/me/sources we will grab it when connecting.
        log::error!("NetworkContainer::is_source_writable not allowed");
        Err(ContainerError::SourceNotWritable(source.clone()))
    }

    fn is_source_uncommitted(&self, source: &SourceId) -> ContainerResult<bool> {
        Ok(self.added_chunks.contains_key(source))
    }

    fn source_path(&self, source: &SourceId) -> ContainerResult<SourcePath> {
        self.cache.source_path(source)
    }

    fn add_computed_types(
        &mut self,
        source: &SourceId,
        types: &[ComputedType],
    ) -> ContainerResult<()> {
        self.cache.add_computed_types(source, types)
    }

    fn remove_types(&mut self, source: &SourceId, guids: &[TypeGUID]) -> ContainerResult<()> {
        self.cache.remove_types(source, guids)
    }

    fn add_functions(
        &mut self,
        target: &Target,
        source: &SourceId,
        functions: &[Function],
    ) -> ContainerResult<()> {
        let signature_chunk = SignatureChunk::new(functions).ok_or(
            ContainerError::CorruptedData("signature chunk failed to validate"),
        )?;
        let chunk = Chunk::new_with_target(
            ChunkKind::Signature(signature_chunk),
            CompressionType::None,
            target.clone(),
        );
        self.added_chunks.entry(*source).or_default().push(chunk);
        Ok(())
    }

    fn remove_functions(
        &mut self,
        target: &Target,
        source: &SourceId,
        functions: &[Function],
    ) -> ContainerResult<()> {
        // TODO: Wont persist, need to add remote removal.
        self.cache.remove_functions(target, source, functions)
    }

    fn fetch_functions(
        &mut self,
        target: &Target,
        functions: &[FunctionGUID],
    ) -> ContainerResult<()> {
        // NOTE: Blocking request to get the mapped function sources.
        let mapped_unseen_functions = self.get_unseen_functions_source(Some(&target), functions);

        // Actually get the function data for the unseen guids, we really only want to do this once per
        // session, anymore, and this is annoying!
        for (source, unseen_guids) in mapped_unseen_functions {
            // NOTE: Blocking request to get the function data in the container cache.
            self.pull_functions(&target, &source, &unseen_guids);
        }

        Ok(())
    }

    fn sources_with_type_guid(&self, guid: &TypeGUID) -> ContainerResult<Vec<SourceId>> {
        self.cache.sources_with_type_guid(guid)
    }

    fn sources_with_type_guids(
        &self,
        guids: &[TypeGUID],
    ) -> ContainerResult<HashMap<TypeGUID, Vec<SourceId>>> {
        self.cache.sources_with_type_guids(guids)
    }

    fn type_guids_with_name(
        &self,
        source: &SourceId,
        name: &str,
    ) -> ContainerResult<Vec<TypeGUID>> {
        self.cache.type_guids_with_name(source, name)
    }

    fn type_with_guid(&self, source: &SourceId, guid: &TypeGUID) -> ContainerResult<Option<Type>> {
        self.cache.type_with_guid(source, guid)
    }

    fn sources_with_function_guid(
        &self,
        target: &Target,
        guid: &FunctionGUID,
    ) -> ContainerResult<Vec<SourceId>> {
        self.cache.sources_with_function_guid(target, guid)
    }

    fn sources_with_function_guids(
        &self,
        target: &Target,
        guids: &[FunctionGUID],
    ) -> ContainerResult<HashMap<FunctionGUID, Vec<SourceId>>> {
        self.cache.sources_with_function_guids(target, guids)
    }

    fn functions_with_guid(
        &self,
        target: &Target,
        source: &SourceId,
        guid: &FunctionGUID,
    ) -> ContainerResult<Vec<Function>> {
        self.cache.functions_with_guid(target, source, guid)
    }
}

impl Debug for NetworkContainer {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NetworkContainer").finish()
    }
}

impl Display for NetworkContainer {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("NetworkContainer").finish()
    }
}