• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

geo-engine / geoengine / 5856046428

14 Aug 2023 01:37PM UTC coverage: 89.484% (-0.1%) from 89.596%
5856046428

push

github

web-flow
Merge pull request #848 from geo-engine/compressed-raster-cache

compress raster tile cache

475 of 475 new or added lines in 4 files covered. (100.0%)

104049 of 116277 relevant lines covered (89.48%)

62266.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.85
/operators/src/pro/cache/shared_cache.rs
1
use super::{
2
    cache_chunks::{CacheElementHitCheck, CachedFeatures, LandingZoneQueryFeatures},
3
    cache_tiles::{CachedTiles, CompressedRasterTile2D, LandingZoneQueryTiles},
4
    error::CacheError,
5
    util::CacheSize,
6
};
7
use crate::engine::CanonicOperatorName;
8
use crate::util::Result;
9
use async_trait::async_trait;
10
use futures::Stream;
11
use geoengine_datatypes::{
12
    collections::FeatureCollection,
13
    identifier,
14
    primitives::{CacheHint, Geometry, RasterQueryRectangle, VectorQueryRectangle},
15
    raster::Pixel,
16
    util::{arrow::ArrowTyped, test::TestDefault, ByteSize, Identifier},
17
};
18
use log::{debug, log_enabled};
19
use lru::LruCache;
20
use std::{collections::HashMap, hash::Hash, sync::Arc};
21
use tokio::sync::RwLock;
22

23
/// The tile cache caches all tiles of a query and is able to answer queries that are fully contained in the cache.
24
/// New tiles are inserted into the cache on-the-fly as they are produced by query processors.
25
/// The tiles are first inserted into a landing zone, until the query in completely finished and only then moved to the cache.
26
/// Both the landing zone and the cache have a maximum size.
27
/// If the landing zone is full, the caching of the current query will be aborted.
28
/// If the cache is full, the least recently used entries will be evicted if necessary to make room for the new entry.
29
#[derive(Debug)]
×
30
pub struct CacheBackend {
31
    // TODO: more fine granular locking?
32
    // for each operator graph, we have a cache, that can efficiently be accessed
33
    raster_caches: HashMap<CanonicOperatorName, RasterOperatorCacheEntry>,
34
    vector_caches: HashMap<CanonicOperatorName, VectorOperatorCacheEntry>,
35

36
    cache_size: CacheSize,
37
    landing_zone_size: CacheSize,
38

39
    // we only use the LruCache for determining the least recently used elements and evict as many entries as needed to fit the new one
40
    lru: LruCache<CacheEntryId, TypedCanonicOperatorName>,
41
}
42

43
impl CacheBackend {
44
    /// This method removes entries from the cache until it can fit the given amount of bytes.
45
    pub fn evict_until_can_fit_bytes(&mut self, bytes: usize) {
5✔
46
        while !self.cache_size.can_fit_bytes(bytes) {
6✔
47
            if let Some((pop_id, pop_key)) = self.lru.pop_lru() {
1✔
48
                match pop_key {
1✔
49
                    TypedCanonicOperatorName::Raster(raster_pop_key) => {
1✔
50
                        let op_cache = self
1✔
51
                            .raster_caches
1✔
52
                            .get_mut(&raster_pop_key)
1✔
53
                            .expect("LRU entry must exist in the cache!");
1✔
54
                        let query_element = op_cache
1✔
55
                            .remove_cache_entry(&pop_id)
1✔
56
                            .expect("LRU entry must exist in the cache!");
1✔
57
                        self.cache_size.remove_element_bytes(&query_element);
1✔
58
                    }
1✔
59
                    TypedCanonicOperatorName::Vector(vector_pop_key) => {
×
60
                        let op_cache = self
×
61
                            .vector_caches
×
62
                            .get_mut(&vector_pop_key)
×
63
                            .expect("LRU entry must exist in the cache!");
×
64
                        let query_element = op_cache
×
65
                            .remove_cache_entry(&pop_id)
×
66
                            .expect("LRU entry must exist in the cache!");
×
67
                        self.cache_size.remove_element_bytes(&query_element);
×
68
                    }
×
69
                };
70
                self.cache_size.remove_element_bytes(&pop_id);
1✔
71

1✔
72
                debug!(
1✔
73
                    "Evicted query {}. Cache size: {}. Cache size used: {}, Cache used percentage: {}.",
×
74
                    pop_id,
×
75
                    self.cache_size.total_byte_size(),
×
76
                    self.cache_size.byte_size_used(),
×
77
                    self.cache_size.size_used_fraction()
×
78
                );
79
            }
×
80
        }
81
    }
5✔
82
}
83

84
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
×
85
pub enum TypedCanonicOperatorName {
86
    Raster(CanonicOperatorName),
87
    Vector(CanonicOperatorName),
88
}
89

90
impl TypedCanonicOperatorName {
91
    pub fn as_raster(&self) -> Option<&CanonicOperatorName> {
×
92
        match self {
×
93
            Self::Raster(name) => Some(name),
×
94
            Self::Vector(_) => None,
×
95
        }
96
    }
×
97

98
    pub fn as_vector(&self) -> Option<&CanonicOperatorName> {
×
99
        match self {
×
100
            Self::Raster(_) => None,
×
101
            Self::Vector(name) => Some(name),
×
102
        }
103
    }
×
104
}
105

106
pub trait CacheEvictUntilFit {
107
    fn evict_entries_until_can_fit_bytes(&mut self, bytes: usize);
108
}
109

110
impl CacheEvictUntilFit for CacheBackend {
111
    fn evict_entries_until_can_fit_bytes(&mut self, bytes: usize) {
5✔
112
        self.evict_until_can_fit_bytes(bytes);
5✔
113
    }
5✔
114
}
115

116
pub trait CacheView<C, L>: CacheEvictUntilFit {
117
    fn operator_caches_mut(
118
        &mut self,
119
    ) -> &mut HashMap<CanonicOperatorName, OperatorCacheEntry<C, L>>;
120

121
    fn create_operator_cache_if_needed(&mut self, key: CanonicOperatorName) {
7✔
122
        // TODO: add size of the OperatorCacheEntry to the cache size?
7✔
123
        self.operator_caches_mut()
7✔
124
            .entry(key)
7✔
125
            .or_insert_with(|| OperatorCacheEntry::new());
7✔
126
    }
7✔
127

128
    fn remove_operator_cache(
1✔
129
        &mut self,
1✔
130
        key: &CanonicOperatorName,
1✔
131
    ) -> Option<OperatorCacheEntry<C, L>> {
1✔
132
        // TODO: remove the size of the OperatorCacheEntry to the cache size?
1✔
133
        self.operator_caches_mut().remove(key)
1✔
134
    }
1✔
135
}
136

137
#[allow(clippy::type_complexity)]
138
pub struct OperatorCacheEntryView<'a, C: CacheBackendElementExt> {
139
    operator_cache: &'a mut OperatorCacheEntry<
140
        CacheQueryEntry<C::Query, C::CacheContainer>,
141
        CacheQueryEntry<C::Query, C::LandingZoneContainer>,
142
    >,
143
    cache_size: &'a mut CacheSize,
144
    landing_zone_size: &'a mut CacheSize,
145
    lru: &'a mut LruCache<CacheEntryId, TypedCanonicOperatorName>,
146
}
147

148
impl<'a, C> OperatorCacheEntryView<'a, C>
149
where
150
    C: CacheBackendElementExt + ByteSize,
151
    C::Query: Clone + CacheQueryMatch,
152
    CacheQueryEntry<C::Query, C::LandingZoneContainer>: ByteSize,
153
    CacheQueryEntry<C::Query, C::CacheContainer>: ByteSize,
154
{
155
    fn is_empty(&self) -> bool {
1✔
156
        self.operator_cache.is_empty()
1✔
157
    }
1✔
158

159
    /// This method removes a query from the landing zone.
160
    ///
161
    /// If the query is not in the landing zone, this method returns None.
162
    ///
163
    fn remove_query_from_landing_zone(
164
        &mut self,
165
        query_id: &QueryId,
166
    ) -> Option<CacheQueryEntry<C::Query, C::LandingZoneContainer>> {
167
        if let Some(entry) = self.operator_cache.remove_landing_zone_entry(query_id) {
6✔
168
            self.landing_zone_size.remove_element_bytes(query_id);
6✔
169
            self.landing_zone_size.remove_element_bytes(&entry);
6✔
170

6✔
171
            // debug output
6✔
172
            log::debug!(
6✔
173
                "Removed query {}. Landing zone size: {}. Landing zone size used: {}, Landing zone used percentage: {}.",
×
174
                query_id, self.landing_zone_size.total_byte_size(), self.landing_zone_size.byte_size_used(), self.landing_zone_size.size_used_fraction()
×
175
            );
176

177
            Some(entry)
6✔
178
        } else {
179
            None
×
180
        }
181
    }
6✔
182

183
    /// This method removes a query from the cache and the LRU.
184
    /// It will remove a queries cache entry from the cache and the LRU.
185
    ///
186
    /// If the query is not in the cache, this method returns None.
187
    ///
188
    fn remove_query_from_cache_and_lru(
1✔
189
        &mut self,
1✔
190
        cache_entry_id: &CacheEntryId,
1✔
191
    ) -> Option<CacheQueryEntry<C::Query, C::CacheContainer>> {
1✔
192
        if let Some(entry) = self.operator_cache.remove_cache_entry(cache_entry_id) {
1✔
193
            let old_lru_entry = self.lru.pop_entry(cache_entry_id);
1✔
194
            debug_assert!(old_lru_entry.is_some(), "CacheEntryId not found in LRU");
1✔
195
            self.cache_size.remove_element_bytes(cache_entry_id);
1✔
196
            self.cache_size.remove_element_bytes(&entry);
1✔
197

1✔
198
            log::debug!(
1✔
199
                "Removed cache entry {}. Cache size: {}. Cache size used: {}, Cache used percentage: {}.",
×
200
                cache_entry_id, self.cache_size.total_byte_size(), self.cache_size.byte_size_used(), self.cache_size.size_used_fraction()
×
201
            );
202

203
            Some(entry)
1✔
204
        } else {
205
            None
×
206
        }
207
    }
1✔
208

209
    /// This method removes a list of queries from the cache and the LRU.
210
    fn discard_queries_from_cache_and_lru(&mut self, cache_entry_ids: &[CacheEntryId]) {
7✔
211
        for cache_entry_id in cache_entry_ids {
8✔
212
            let old_entry = self.remove_query_from_cache_and_lru(cache_entry_id);
1✔
213
            debug_assert!(
214
                old_entry.is_some(),
1✔
215
                "CacheEntryId not found in OperatorCacheEntry"
×
216
            );
217
        }
218
    }
7✔
219

220
    /// This method adds a query element to the landing zone.
221
    /// It will add the element to the landing zone entry of the query.
222
    ///
223
    /// # Errors
224
    ///
225
    /// This method returns an error if the query is not in the landing zone.
226
    /// This method returns an error if the element is already expired.
227
    /// This method returns an error if the landing zone is full or the new element would cause the landing zone to overflow.
228
    ///
229
    fn add_query_element_to_landing_zone(
6✔
230
        &mut self,
6✔
231
        query_id: &QueryId,
6✔
232
        landing_zone_element: C,
6✔
233
    ) -> Result<(), CacheError> {
6✔
234
        let landing_zone_entry = self
6✔
235
            .operator_cache
6✔
236
            .landing_zone_entry_mut(query_id)
6✔
237
            .ok_or(CacheError::QueryNotFoundInLandingZone)?;
6✔
238

239
        if landing_zone_element.cache_hint().is_expired() {
6✔
240
            log::trace!("Element is already expired");
1✔
241
            return Err(CacheError::TileExpiredBeforeInsertion);
1✔
242
        };
5✔
243

5✔
244
        let element_bytes_size = landing_zone_element.byte_size();
5✔
245

5✔
246
        if !self.landing_zone_size.can_fit_bytes(element_bytes_size) {
5✔
247
            return Err(CacheError::NotEnoughSpaceInLandingZone);
×
248
        }
5✔
249

5✔
250
        // new entries might update the query bounds stored for this entry
5✔
251
        landing_zone_element.update_stored_query(&mut landing_zone_entry.query)?;
5✔
252

253
        // actually insert the element into the landing zone
254
        landing_zone_entry.insert_element(landing_zone_element)?;
5✔
255

256
        // we add the bytes size of the element to the landing zone size after we have inserted it.
257
        self.landing_zone_size
5✔
258
            .try_add_bytes(element_bytes_size)
5✔
259
            .expect(
5✔
260
            "The Landing Zone must have enough space for the element since we checked it before",
5✔
261
        );
5✔
262

5✔
263
        log::trace!(
5✔
264
            "Inserted tile for query {} into landing zone. Landing zone size: {}. Landing zone size used: {}. Landing zone used percentage: {}",
×
265
            query_id, self.landing_zone_size.total_byte_size(), self.landing_zone_size.byte_size_used(), self.landing_zone_size.size_used_fraction()
×
266
        );
267

268
        Ok(())
5✔
269
    }
6✔
270

271
    /// This method inserts a query into the landing zone.
272
    /// It will cause the operator cache to create a new landing zone entry.
273
    /// Therefore, the size of the query and the size of the landing zone entry will be added to the landing zone size.
274
    ///
275
    /// # Errors
276
    ///
277
    /// This method returns an error if the query is already in the landing zone.
278
    /// This method returns an error if the landing zone is full or the new query would cause the landing zone to overflow.
279
    ///
280
    fn insert_query_into_landing_zone(&mut self, query: &C::Query) -> Result<QueryId, CacheError> {
7✔
281
        let landing_zone_entry = CacheQueryEntry::create_empty::<C>(query.clone());
7✔
282
        let query_id = QueryId::new();
7✔
283

7✔
284
        let query_id_bytes_size = query_id.byte_size();
7✔
285
        let landing_zone_entry_bytes_size = landing_zone_entry.byte_size();
7✔
286

7✔
287
        self.landing_zone_size.try_add_bytes(query_id_bytes_size)?;
7✔
288

289
        // if this fails, we have to remove the query id size again
290
        if let Err(e) = self
7✔
291
            .landing_zone_size
7✔
292
            .try_add_bytes(landing_zone_entry_bytes_size)
7✔
293
        {
294
            self.landing_zone_size.remove_bytes(query_id_bytes_size);
×
295
            return Err(e);
×
296
        }
7✔
297

298
        // if this fails, we have to remove the query id size and the landing zone entry size again
299
        if let Err(e) = self
7✔
300
            .operator_cache
7✔
301
            .insert_landing_zone_entry(query_id, landing_zone_entry)
7✔
302
        {
303
            self.landing_zone_size.remove_bytes(query_id_bytes_size);
×
304
            self.landing_zone_size
×
305
                .remove_bytes(landing_zone_entry_bytes_size);
×
306
            return Err(e);
×
307
        }
7✔
308

7✔
309
        // debug output
7✔
310
        log::trace!(
7✔
311
            "Added query {} to landing zone. Landing zone size: {}. Landing zone size used: {}, Landing zone used percentage: {}.",
×
312
            query_id, self.landing_zone_size.total_byte_size(), self.landing_zone_size.byte_size_used(), self.landing_zone_size.size_used_fraction()
×
313
        );
314

315
        Ok(query_id)
7✔
316
    }
7✔
317

318
    /// This method inserts a cache entry into the cache and the LRU.
319
    /// It allows the element cache to overflow the cache size.
320
    /// This is done because the total cache size is the cache size + the landing zone size.
321
    /// This method is used when moving an element from the landing zone to the cache.
322
    ///
323
    /// # Errors
324
    ///
325
    /// This method returns an error if the cache entry is already in the cache.
326
    ///
327
    fn insert_cache_entry_allow_overflow(
5✔
328
        &mut self,
5✔
329
        cache_entry: CacheQueryEntry<C::Query, C::CacheContainer>,
5✔
330
        key: &CanonicOperatorName,
5✔
331
    ) -> Result<CacheEntryId, CacheError> {
5✔
332
        let cache_entry_id = CacheEntryId::new();
5✔
333
        let bytes = cache_entry.byte_size() + cache_entry_id.byte_size();
5✔
334
        // When inserting data from the landing zone into the cache, we allow the cache to overflow.
5✔
335
        // This is done because the total cache size is the cache size + the landing zone size.
5✔
336
        self.cache_size.add_bytes_allow_overflow(bytes);
5✔
337
        self.operator_cache
5✔
338
            .insert_cache_entry(cache_entry_id, cache_entry)?;
5✔
339
        // we have to wrap the key in a TypedCanonicOperatorName to be able to insert it into the LRU
340
        self.lru.push(
5✔
341
            cache_entry_id,
5✔
342
            C::typed_canonical_operator_name(key.clone()),
5✔
343
        );
5✔
344

5✔
345
        // debug output
5✔
346
        log::trace!(
5✔
347
            "Added cache entry {}. Cache size: {}. Cache size used: {}, Cache used percentage: {}.",
×
348
            cache_entry_id,
×
349
            self.cache_size.total_byte_size(),
×
350
            self.cache_size.byte_size_used(),
×
351
            self.cache_size.size_used_fraction()
×
352
        );
353

354
        Ok(cache_entry_id)
5✔
355
    }
5✔
356

357
    /// This method finds a cache entry in the cache that matches the query.
358
    /// It will also collect all expired cache entries.
359
    /// The cache entry is returned together with the expired ids.
360
    fn find_matching_cache_entry_and_collect_expired_entries(
7✔
361
        &mut self,
7✔
362
        query: &C::Query,
7✔
363
    ) -> CacheQueryResult<C::Query, C::CacheContainer> {
7✔
364
        let mut expired_cache_entry_ids = vec![];
7✔
365

7✔
366
        let x = self.operator_cache.iter().find(|&(id, entry)| {
7✔
367
            if entry.elements.is_expired() {
6✔
368
                expired_cache_entry_ids.push(*id);
1✔
369
                return false;
1✔
370
            }
5✔
371
            entry.query.is_match(query)
5✔
372
        });
7✔
373

7✔
374
        CacheQueryResult {
7✔
375
            cache_hit: x.map(|(id, entry)| (*id, entry)),
7✔
376
            expired_cache_entry_ids,
7✔
377
        }
7✔
378
    }
7✔
379
}
380

381
struct CacheQueryResult<'a, Query, CE> {
382
    cache_hit: Option<(CacheEntryId, &'a CacheQueryEntry<Query, CE>)>,
383
    expired_cache_entry_ids: Vec<CacheEntryId>,
384
}
385

386
pub trait Cache<C: CacheBackendElementExt>:
387
    CacheView<
388
    CacheQueryEntry<C::Query, C::CacheContainer>,
389
    CacheQueryEntry<C::Query, C::LandingZoneContainer>,
390
>
391
where
392
    C::Query: Clone + CacheQueryMatch,
393
{
394
    /// This method returns a mutable reference to the cache entry of an operator.
395
    /// If there is no cache entry for the operator, this method returns None.
396
    fn operator_cache_view_mut(
397
        &mut self,
398
        key: &CanonicOperatorName,
399
    ) -> Option<OperatorCacheEntryView<C>>;
400

401
    /// This method queries the cache for a given query.
402
    /// If the query matches an entry in the cache, the cache entry is returned and it is promoted in the LRU.
403
    /// If the query does not match an entry in the cache, None is returned. Also if a cache entry is found but it is expired, None is returned.
404
    ///
405
    /// # Errors
406
    /// This method returns an error if the cache entry is not found.
407
    ///
408
    fn query_and_promote(
9✔
409
        &mut self,
9✔
410
        key: &CanonicOperatorName,
9✔
411
        query: &C::Query,
9✔
412
    ) -> Result<Option<Arc<Vec<C>>>, CacheError> {
9✔
413
        let mut cache = self
9✔
414
            .operator_cache_view_mut(key)
9✔
415
            .ok_or(CacheError::OperatorCacheEntryNotFound)?;
9✔
416

417
        let CacheQueryResult {
418
            cache_hit,
7✔
419
            expired_cache_entry_ids,
7✔
420
        } = cache.find_matching_cache_entry_and_collect_expired_entries(query);
7✔
421

422
        let res = if let Some((cache_entry_id, cache_entry)) = cache_hit {
7✔
423
            let potential_result_elements = cache_entry.elements.results_arc();
5✔
424

5✔
425
            // promote the cache entry in the LRU
5✔
426
            cache.lru.promote(&cache_entry_id);
5✔
427
            Some(potential_result_elements)
5✔
428
        } else {
429
            None
2✔
430
        };
431

432
        // discard expired cache entries
433
        cache.discard_queries_from_cache_and_lru(&expired_cache_entry_ids);
7✔
434

7✔
435
        Ok(res.flatten())
7✔
436
    }
9✔
437

438
    /// This method inserts a query into the cache.
439
    ///
440
    /// # Errors
441
    /// This method returns an error if the query is already in the cache.
442
    ///
443
    fn insert_query_into_landing_zone(
7✔
444
        &mut self,
7✔
445
        key: &CanonicOperatorName,
7✔
446
        query: &C::Query,
7✔
447
    ) -> Result<QueryId, CacheError> {
7✔
448
        self.create_operator_cache_if_needed(key.clone());
7✔
449
        self.operator_cache_view_mut(key)
7✔
450
            .expect("This method must not fail since the OperatorCache was created one line above.")
7✔
451
            .insert_query_into_landing_zone(query)
7✔
452
    }
7✔
453

454
    fn insert_query_element_into_landing_zone(
13✔
455
        &mut self,
13✔
456
        key: &CanonicOperatorName,
13✔
457
        query_id: &QueryId,
13✔
458
        landing_zone_element: C,
13✔
459
    ) -> Result<(), CacheError> {
13✔
460
        let mut cache = self
13✔
461
            .operator_cache_view_mut(key)
13✔
462
            .ok_or(CacheError::QueryNotFoundInLandingZone)?;
13✔
463
        let res = cache.add_query_element_to_landing_zone(query_id, landing_zone_element);
6✔
464

6✔
465
        // if we cant add the element to the landing zone, we remove the query from the landing zone
6✔
466
        if res.is_err() {
6✔
467
            let _old_entry = cache.remove_query_from_landing_zone(query_id);
1✔
468

1✔
469
            // if the operator cache is empty, we remove it from the cache
1✔
470
            if cache.is_empty() {
1✔
471
                self.remove_operator_cache(key);
1✔
472
            }
1✔
473
        }
5✔
474

475
        res
6✔
476
    }
13✔
477

478
    /// This method discards a query from the landing zone.
479
    /// If the query is not in the landing zone, this method does nothing.
480
    fn discard_query_from_landing_zone(&mut self, key: &CanonicOperatorName, query_id: &QueryId) {
481
        if let Some(mut cache) = self.operator_cache_view_mut(key) {
×
482
            cache.remove_query_from_landing_zone(query_id);
×
483
            if cache.is_empty() {
×
484
                self.remove_operator_cache(key);
×
485
            }
×
486
        }
×
487
    }
×
488

489
    /// This method discards a query from the cache and the LRU.
490
    /// If the query is not in the cache, this method does nothing.
491
    fn discard_querys_from_cache_and_lru(
492
        &mut self,
493
        key: &CanonicOperatorName,
494
        cache_entry_ids: &[CacheEntryId],
495
    ) {
496
        if let Some(mut cache) = self.operator_cache_view_mut(key) {
×
497
            cache.discard_queries_from_cache_and_lru(cache_entry_ids);
×
498
            if cache.is_empty() {
×
499
                self.remove_operator_cache(key);
×
500
            }
×
501
        }
×
502
    }
×
503

504
    /// This method moves a query from the landing zone to the cache.
505
    /// It will remove the query from the landing zone and insert it into the cache.
506
    /// If the cache is full, the least recently used entries will be evicted if necessary to make room for the new entry.
507
    /// This method returns the cache entry id of the inserted cache entry.
508
    ///
509
    /// # Errors
510
    /// This method returns an error if the query is not in the landing zone.
511
    /// This method returns an error if the cache entry is already in the cache.
512
    /// This method returns an error if the cache is full and the least recently used entries cannot be evicted to make room for the new entry.
513
    ///
514
    fn move_query_from_landing_zone_to_cache(
6✔
515
        &mut self,
6✔
516
        key: &CanonicOperatorName,
6✔
517
        query_id: &QueryId,
6✔
518
    ) -> Result<CacheEntryId, CacheError> {
6✔
519
        let mut operator_cache = self
6✔
520
            .operator_cache_view_mut(key)
6✔
521
            .ok_or(CacheError::OperatorCacheEntryNotFound)?;
6✔
522
        let landing_zone_entry = operator_cache
5✔
523
            .remove_query_from_landing_zone(query_id)
5✔
524
            .ok_or(CacheError::QueryNotFoundInLandingZone)?;
5✔
525
        let cache_entry: CacheQueryEntry<
5✔
526
            <C as CacheBackendElement>::Query,
5✔
527
            <C as CacheBackendElementExt>::CacheContainer,
5✔
528
        > = C::landing_zone_to_cache_entry(landing_zone_entry);
5✔
529
        // when moving an element from the landing zone to the cache, we allow the cache size to overflow.
530
        // This is done because the total cache size is the cache size + the landing zone size.
531
        let cache_entry_id = operator_cache.insert_cache_entry_allow_overflow(cache_entry, key)?;
5✔
532
        // We could also first try to evict until the cache can hold the entry.
533
        // However, then we would need to lookup the cache entry twice.
534
        // To avoid that, we just evict after we moved the entry from the landing zone to the cache.
535
        // This is also not a problem since the total cache size is the cache size + the landing zone size.
536
        self.evict_entries_until_can_fit_bytes(0);
5✔
537

5✔
538
        Ok(cache_entry_id)
5✔
539
    }
6✔
540
}
541

542
impl<T> Cache<CompressedRasterTile2D<T>> for CacheBackend
543
where
544
    T: Pixel,
545
    CompressedRasterTile2D<T>: CacheBackendElementExt<
546
        Query = RasterQueryRectangle,
547
        LandingZoneContainer = LandingZoneQueryTiles,
548
        CacheContainer = CachedTiles,
549
    >,
550
{
551
    fn operator_cache_view_mut(
35✔
552
        &mut self,
35✔
553
        key: &CanonicOperatorName,
35✔
554
    ) -> Option<OperatorCacheEntryView<CompressedRasterTile2D<T>>> {
35✔
555
        self.raster_caches
35✔
556
            .get_mut(key)
35✔
557
            .map(|op| OperatorCacheEntryView {
35✔
558
                operator_cache: op,
25✔
559
                cache_size: &mut self.cache_size,
25✔
560
                landing_zone_size: &mut self.landing_zone_size,
25✔
561
                lru: &mut self.lru,
25✔
562
            })
35✔
563
    }
35✔
564
}
565

566
impl<T> Cache<FeatureCollection<T>> for CacheBackend
567
where
568
    T: Geometry + ArrowTyped,
569
    FeatureCollection<T>: CacheElementHitCheck
570
        + CacheBackendElementExt<
571
            Query = VectorQueryRectangle,
572
            LandingZoneContainer = LandingZoneQueryFeatures,
573
            CacheContainer = CachedFeatures,
574
        >,
575
{
576
    fn operator_cache_view_mut(
×
577
        &mut self,
×
578
        key: &CanonicOperatorName,
×
579
    ) -> Option<OperatorCacheEntryView<FeatureCollection<T>>> {
×
580
        self.vector_caches
×
581
            .get_mut(key)
×
582
            .map(|op| OperatorCacheEntryView {
×
583
                operator_cache: op,
×
584
                cache_size: &mut self.cache_size,
×
585
                landing_zone_size: &mut self.landing_zone_size,
×
586
                lru: &mut self.lru,
×
587
            })
×
588
    }
×
589
}
590

591
impl CacheView<RasterCacheQueryEntry, RasterLandingQueryEntry> for CacheBackend {
592
    fn operator_caches_mut(
8✔
593
        &mut self,
8✔
594
    ) -> &mut HashMap<CanonicOperatorName, RasterOperatorCacheEntry> {
8✔
595
        &mut self.raster_caches
8✔
596
    }
8✔
597
}
598

599
impl CacheView<VectorCacheQueryEntry, VectorLandingQueryEntry> for CacheBackend {
600
    fn operator_caches_mut(
×
601
        &mut self,
×
602
    ) -> &mut HashMap<CanonicOperatorName, VectorOperatorCacheEntry> {
×
603
        &mut self.vector_caches
×
604
    }
×
605
}
606

607
pub trait CacheBackendElement: ByteSize + Send + ByteSize
608
where
609
    Self: Sized,
610
{
611
    type Query: CacheQueryMatch + Clone + Send + Sync;
612

613
    fn update_stored_query(&self, query: &mut Self::Query) -> Result<(), CacheError>;
614

615
    fn cache_hint(&self) -> CacheHint;
616

617
    fn typed_canonical_operator_name(key: CanonicOperatorName) -> TypedCanonicOperatorName;
618
}
619

620
pub trait CacheBackendElementExt: CacheBackendElement {
621
    type LandingZoneContainer: LandingZoneElementsContainer<Self> + ByteSize;
622
    type CacheContainer: CacheElementsContainer<Self::Query, Self>
623
        + ByteSize
624
        + From<Self::LandingZoneContainer>;
625

626
    fn move_element_into_landing_zone(
627
        self,
628
        landing_zone: &mut Self::LandingZoneContainer,
629
    ) -> Result<(), super::error::CacheError>;
630

631
    fn create_empty_landing_zone() -> Self::LandingZoneContainer;
632

633
    fn results_arc(cache_elements_container: &Self::CacheContainer) -> Option<Arc<Vec<Self>>>;
634

635
    fn landing_zone_to_cache_entry(
636
        landing_zone_entry: CacheQueryEntry<Self::Query, Self::LandingZoneContainer>,
637
    ) -> CacheQueryEntry<Self::Query, Self::CacheContainer>;
638
}
639

640
#[derive(Debug)]
×
641
pub struct SharedCache {
642
    backend: RwLock<CacheBackend>,
643
}
644

645
impl SharedCache {
646
    pub fn new(cache_size_in_mb: usize, landing_zone_ratio: f64) -> Result<Self> {
6✔
647
        if landing_zone_ratio <= 0.0 {
6✔
648
            return Err(crate::error::Error::QueryingProcessorFailed {
×
649
                source: Box::new(CacheError::LandingZoneRatioMustBeLargerThanZero),
×
650
            });
×
651
        }
6✔
652

6✔
653
        if landing_zone_ratio >= 0.5 {
6✔
654
            return Err(crate::error::Error::QueryingProcessorFailed {
×
655
                source: Box::new(CacheError::LandingZoneRatioMustBeSmallerThenHalfCacheSize),
×
656
            });
×
657
        }
6✔
658

6✔
659
        let cache_size_bytes =
6✔
660
            (cache_size_in_mb as f64 * (1.0 - landing_zone_ratio) * 1024.0 * 1024.0) as usize;
6✔
661

6✔
662
        let landing_zone_size_bytes =
6✔
663
            (cache_size_in_mb as f64 * landing_zone_ratio * 1024.0 * 1024.0) as usize;
6✔
664

6✔
665
        Ok(Self {
6✔
666
            backend: RwLock::new(CacheBackend {
6✔
667
                vector_caches: Default::default(),
6✔
668
                raster_caches: Default::default(),
6✔
669
                lru: LruCache::unbounded(), // we need no cap because we evict manually
6✔
670
                cache_size: CacheSize::new(cache_size_bytes),
6✔
671
                landing_zone_size: CacheSize::new(landing_zone_size_bytes),
6✔
672
            }),
6✔
673
        })
6✔
674
    }
6✔
675
}
676

677
impl TestDefault for SharedCache {
678
    fn test_default() -> Self {
87✔
679
        Self {
87✔
680
            backend: RwLock::new(CacheBackend {
87✔
681
                vector_caches: Default::default(),
87✔
682
                raster_caches: Default::default(),
87✔
683
                lru: LruCache::unbounded(), // we need no cap because we evict manually
87✔
684
                cache_size: CacheSize::new(usize::MAX),
87✔
685
                landing_zone_size: CacheSize::new(usize::MAX),
87✔
686
            }),
87✔
687
        }
87✔
688
    }
87✔
689
}
690

691
/// Holds all the cached results for an operator graph (workflow)
692
#[derive(Debug, Default)]
×
693
pub struct OperatorCacheEntry<CacheEntriesContainer, LandingZoneEntriesContainer> {
694
    // for a given operator and query we need to look through all entries to find one that matches
695
    // TODO: use a multi-dimensional index to speed up the lookup
696
    entries: HashMap<CacheEntryId, CacheEntriesContainer>,
697

698
    // running queries insert their tiles as they are produced. The entry will be created once the query is done.
699
    // The query is identified by a Uuid instead of the query rectangle to avoid confusions with other queries
700
    landing_zone: HashMap<QueryId, LandingZoneEntriesContainer>,
701
}
702

703
impl<CacheEntriesContainer, LandingZoneEntriesContainer>
704
    OperatorCacheEntry<CacheEntriesContainer, LandingZoneEntriesContainer>
705
{
706
    pub fn new() -> Self {
7✔
707
        Self {
7✔
708
            entries: Default::default(),
7✔
709
            landing_zone: Default::default(),
7✔
710
        }
7✔
711
    }
7✔
712

713
    fn insert_landing_zone_entry(
7✔
714
        &mut self,
7✔
715
        query_id: QueryId,
7✔
716
        landing_zone_entry: LandingZoneEntriesContainer,
7✔
717
    ) -> Result<(), CacheError> {
7✔
718
        let old_entry = self.landing_zone.insert(query_id, landing_zone_entry);
7✔
719

7✔
720
        if old_entry.is_some() {
7✔
721
            Err(CacheError::QueryIdAlreadyInLandingZone)
×
722
        } else {
723
            Ok(())
7✔
724
        }
725
    }
7✔
726

727
    fn remove_landing_zone_entry(
6✔
728
        &mut self,
6✔
729
        query_id: &QueryId,
6✔
730
    ) -> Option<LandingZoneEntriesContainer> {
6✔
731
        self.landing_zone.remove(query_id)
6✔
732
    }
6✔
733

734
    fn landing_zone_entry_mut(
6✔
735
        &mut self,
6✔
736
        query_id: &QueryId,
6✔
737
    ) -> Option<&mut LandingZoneEntriesContainer> {
6✔
738
        self.landing_zone.get_mut(query_id)
6✔
739
    }
6✔
740

741
    fn insert_cache_entry(
5✔
742
        &mut self,
5✔
743
        cache_entry_id: CacheEntryId,
5✔
744
        cache_entry: CacheEntriesContainer,
5✔
745
    ) -> Result<(), CacheError> {
5✔
746
        let old_entry = self.entries.insert(cache_entry_id, cache_entry);
5✔
747

5✔
748
        if old_entry.is_some() {
5✔
749
            Err(CacheError::CacheEntryIdAlreadyInCache)
×
750
        } else {
751
            Ok(())
5✔
752
        }
753
    }
5✔
754

755
    fn remove_cache_entry(
2✔
756
        &mut self,
2✔
757
        cache_entry_id: &CacheEntryId,
2✔
758
    ) -> Option<CacheEntriesContainer> {
2✔
759
        self.entries.remove(cache_entry_id)
2✔
760
    }
2✔
761

762
    fn is_empty(&self) -> bool {
1✔
763
        self.entries.is_empty() && self.landing_zone.is_empty()
1✔
764
    }
1✔
765

766
    fn iter(&self) -> impl Iterator<Item = (&CacheEntryId, &CacheEntriesContainer)> {
7✔
767
        self.entries.iter()
7✔
768
    }
7✔
769
}
770

771
identifier!(QueryId);
×
772

773
impl ByteSize for QueryId {}
774

775
identifier!(CacheEntryId);
×
776

777
impl ByteSize for CacheEntryId {}
778

779
/// Holds all the elements for a given query and is able to answer queries that are fully contained
780
#[derive(Debug, Hash)]
×
781
pub struct CacheQueryEntry<Query, Elements> {
782
    query: Query,
783
    elements: Elements,
784
}
785
type RasterOperatorCacheEntry = OperatorCacheEntry<RasterCacheQueryEntry, RasterLandingQueryEntry>;
786
pub type RasterCacheQueryEntry = CacheQueryEntry<RasterQueryRectangle, CachedTiles>;
787
pub type RasterLandingQueryEntry = CacheQueryEntry<RasterQueryRectangle, LandingZoneQueryTiles>;
788

789
type VectorOperatorCacheEntry = OperatorCacheEntry<VectorCacheQueryEntry, VectorLandingQueryEntry>;
790
pub type VectorCacheQueryEntry = CacheQueryEntry<VectorQueryRectangle, CachedFeatures>;
791
pub type VectorLandingQueryEntry = CacheQueryEntry<VectorQueryRectangle, LandingZoneQueryFeatures>;
792

793
impl<Query, Elements> CacheQueryEntry<Query, Elements> {
794
    pub fn create_empty<E>(query: Query) -> Self
7✔
795
    where
7✔
796
        Elements: LandingZoneElementsContainer<E>,
7✔
797
    {
7✔
798
        Self {
7✔
799
            query,
7✔
800
            elements: Elements::create_empty(),
7✔
801
        }
7✔
802
    }
7✔
803

804
    pub fn query(&self) -> &Query {
×
805
        &self.query
×
806
    }
×
807

808
    pub fn elements_mut(&mut self) -> &mut Elements {
×
809
        &mut self.elements
×
810
    }
×
811

812
    pub fn insert_element<E>(&mut self, element: E) -> Result<(), CacheError>
5✔
813
    where
5✔
814
        Elements: LandingZoneElementsContainer<E>,
5✔
815
    {
5✔
816
        self.elements.insert_element(element)
5✔
817
    }
5✔
818
}
819

820
impl<Query, Elements> ByteSize for CacheQueryEntry<Query, Elements>
821
where
822
    Elements: ByteSize,
823
{
824
    fn heap_byte_size(&self) -> usize {
22✔
825
        self.elements.heap_byte_size()
22✔
826
    }
22✔
827
}
828

829
pub trait CacheQueryMatch<RHS = Self> {
830
    fn is_match(&self, query: &RHS) -> bool;
831
}
832

833
impl CacheQueryMatch for RasterQueryRectangle {
834
    fn is_match(&self, query: &RasterQueryRectangle) -> bool {
5✔
835
        self.spatial_bounds.contains(&query.spatial_bounds)
5✔
836
            && self.time_interval.contains(&query.time_interval)
5✔
837
            && self.spatial_resolution == query.spatial_resolution
5✔
838
    }
5✔
839
}
840

841
impl CacheQueryMatch for VectorQueryRectangle {
842
    // TODO: check if that is what we need
843
    fn is_match(&self, query: &VectorQueryRectangle) -> bool {
×
844
        self.spatial_bounds.contains_bbox(&query.spatial_bounds)
×
845
            && self.time_interval.contains(&query.time_interval)
×
846
            && self.spatial_resolution == query.spatial_resolution
×
847
    }
×
848
}
849

850
pub trait LandingZoneElementsContainer<E> {
851
    fn insert_element(&mut self, element: E) -> Result<(), CacheError>;
852
    fn create_empty() -> Self;
853
}
854

855
pub trait CacheElementsContainerInfos<Query> {
856
    fn is_expired(&self) -> bool;
857
}
858

859
pub trait CacheElementsContainer<Query, E>: CacheElementsContainerInfos<Query> {
860
    fn results_arc(&self) -> Option<Arc<Vec<E>>>;
861
}
862

863
impl CacheQueryEntry<RasterQueryRectangle, CachedTiles> {
864
    /// Return true if the query can be answered in full by this cache entry
865
    /// For this, the bbox and time has to be fully contained, and the spatial resolution has to match
866
    pub fn matches(&self, query: &RasterQueryRectangle) -> bool {
×
867
        self.query.spatial_bounds.contains(&query.spatial_bounds)
×
868
            && self.query.time_interval.contains(&query.time_interval)
×
869
            && self.query.spatial_resolution == query.spatial_resolution
×
870
    }
×
871
}
872

873
impl From<RasterLandingQueryEntry> for RasterCacheQueryEntry {
874
    fn from(value: RasterLandingQueryEntry) -> Self {
6✔
875
        Self {
6✔
876
            query: value.query,
6✔
877
            elements: value.elements.into(),
6✔
878
        }
6✔
879
    }
6✔
880
}
881

882
impl From<VectorLandingQueryEntry> for VectorCacheQueryEntry {
883
    fn from(value: VectorLandingQueryEntry) -> Self {
×
884
        Self {
×
885
            query: value.query,
×
886
            elements: value.elements.into(),
×
887
        }
×
888
    }
×
889
}
890

891
pub trait CacheElement: Sized {
892
    type StoredCacheElement: CacheBackendElementExt<Query = Self::Query>;
893
    type Query: CacheQueryMatch;
894
    type ResultStream: Stream<Item = Result<Self, CacheError>>;
895

896
    fn into_stored_element(self) -> Self::StoredCacheElement;
897
    fn from_stored_element_ref(stored: &Self::StoredCacheElement) -> Result<Self, CacheError>;
898

899
    fn result_stream(
900
        stored_data: Arc<Vec<Self::StoredCacheElement>>,
901
        query: Self::Query,
902
    ) -> Self::ResultStream;
903
}
904

905
#[async_trait]
906
pub trait AsyncCache<C: CacheElement> {
907
    async fn query_cache(
908
        &self,
909
        key: &CanonicOperatorName,
910
        query: &C::Query,
911
    ) -> Result<Option<C::ResultStream>, CacheError>;
912

913
    async fn insert_query(
914
        &self,
915
        key: &CanonicOperatorName,
916
        query: &C::Query,
917
    ) -> Result<QueryId, CacheError>;
918

919
    async fn insert_query_element(
920
        &self,
921
        key: &CanonicOperatorName,
922
        query_id: &QueryId,
923
        landing_zone_element: C,
924
    ) -> Result<(), CacheError>;
925

926
    async fn abort_query(&self, key: &CanonicOperatorName, query_id: &QueryId);
927

928
    async fn finish_query(
929
        &self,
930
        key: &CanonicOperatorName,
931
        query_id: &QueryId,
932
    ) -> Result<CacheEntryId, CacheError>;
933
}
934

935
#[async_trait]
936
impl<C> AsyncCache<C> for SharedCache
937
where
938
    C: CacheElement + Send + Sync + 'static + ByteSize,
939
    CacheBackend: Cache<C::StoredCacheElement>,
940
    C::Query: Clone + CacheQueryMatch + Send + Sync,
941
{
942
    /// Query the cache and on hit create a stream of cache elements
943
    async fn query_cache(
4✔
944
        &self,
4✔
945
        key: &CanonicOperatorName,
4✔
946
        query: &C::Query,
4✔
947
    ) -> Result<Option<C::ResultStream>, CacheError> {
4✔
948
        let mut backend = self.backend.write().await;
4✔
949
        let res_data = backend.query_and_promote(key, query)?;
4✔
950
        Ok(res_data.map(|res_data| C::result_stream(res_data, query.clone())))
2✔
951
    }
8✔
952

953
    /// When inserting a new query, we first register the query and then insert the elements as they are produced
954
    /// This is to avoid confusing different queries on the same operator and query rectangle
955
    async fn insert_query(
3✔
956
        &self,
3✔
957
        key: &CanonicOperatorName,
3✔
958
        query: &C::Query,
3✔
959
    ) -> Result<QueryId, CacheError> {
3✔
960
        let mut backend = self.backend.write().await;
3✔
961
        backend.insert_query_into_landing_zone(key, query)
3✔
962
    }
6✔
963

964
    /// Insert a cachable element for a given query. The query has to be inserted first.
965
    /// The element is inserted into the landing zone and only moved to the cache when the query is finished.
966
    /// If the landing zone is full or the element size would cause the landing zone size to overflow, the caching of the query is aborted.
967
    async fn insert_query_element(
9✔
968
        &self,
9✔
969
        key: &CanonicOperatorName,
9✔
970
        query_id: &QueryId,
9✔
971
        landing_zone_element: C,
9✔
972
    ) -> Result<(), CacheError> {
9✔
973
        const LOG_LEVEL_THRESHOLD: log::Level = log::Level::Trace;
974
        let element_size = if log_enabled!(LOG_LEVEL_THRESHOLD) {
9✔
975
            landing_zone_element.byte_size()
×
976
        } else {
977
            0
9✔
978
        };
979

980
        let storeable_element =
9✔
981
            crate::util::spawn_blocking(|| landing_zone_element.into_stored_element())
9✔
982
                .await
9✔
983
                .map_err(|_| CacheError::BlockingElementConversion)?;
9✔
984

985
        if log_enabled!(LOG_LEVEL_THRESHOLD) {
9✔
986
            let storeable_element_size = storeable_element.byte_size();
×
987
            tracing::trace!(
×
988
                "Inserting element into landing zone for query {:?} on operator {}. Element size: {} bytes, storable element size: {} bytes, ratio: {}",
×
989
                query_id,
×
990
                key,
×
991
                element_size,
×
992
                storeable_element_size,
×
993
                storeable_element_size as f64 / element_size as f64
×
994
            );
×
995
        }
9✔
996

997
        let mut backend = self.backend.write().await;
9✔
998
        backend.insert_query_element_into_landing_zone(key, query_id, storeable_element)
9✔
999
    }
18✔
1000

1001
    /// Abort the query and remove already inserted elements from the caches landing zone
1002
    async fn abort_query(&self, key: &CanonicOperatorName, query_id: &QueryId) {
×
1003
        let mut backend = self.backend.write().await;
×
1004
        backend.discard_query_from_landing_zone(key, query_id);
×
1005
    }
×
1006

1007
    /// Finish the query and make the inserted elements available in the cache
1008
    async fn finish_query(
2✔
1009
        &self,
2✔
1010
        key: &CanonicOperatorName,
2✔
1011
        query_id: &QueryId,
2✔
1012
    ) -> Result<CacheEntryId, CacheError> {
2✔
1013
        let mut backend = self.backend.write().await;
2✔
1014
        backend.move_query_from_landing_zone_to_cache(key, query_id)
2✔
1015
    }
4✔
1016
}
1017

1018
#[cfg(test)]
1019
mod tests {
1020
    use geoengine_datatypes::{
1021
        primitives::{CacheHint, DateTime, SpatialPartition2D, SpatialResolution, TimeInterval},
1022
        raster::{Grid, RasterProperties, RasterTile2D},
1023
    };
1024
    use serde_json::json;
1025
    use std::sync::Arc;
1026

1027
    use crate::pro::cache::cache_tiles::{CompressedGridOrEmpty, CompressedMaskedGrid};
1028

1029
    use super::*;
1030

1031
    async fn process_query_async(tile_cache: &mut SharedCache, op_name: CanonicOperatorName) {
1✔
1032
        let query_id = <SharedCache as AsyncCache<RasterTile2D<u8>>>::insert_query(
1✔
1033
            tile_cache,
1✔
1034
            &op_name,
1✔
1035
            &query_rect(),
1✔
1036
        )
1✔
1037
        .await
×
1038
        .unwrap();
1✔
1039

1✔
1040
        tile_cache
1✔
1041
            .insert_query_element(&op_name, &query_id, create_tile())
1✔
1042
            .await
1✔
1043
            .unwrap();
1✔
1044

1✔
1045
        <SharedCache as AsyncCache<RasterTile2D<u8>>>::finish_query(
1✔
1046
            tile_cache, &op_name, &query_id,
1✔
1047
        )
1✔
1048
        .await
×
1049
        .unwrap();
1✔
1050
    }
1✔
1051

1052
    fn process_query(tile_cache: &mut CacheBackend, op_name: &CanonicOperatorName) {
4✔
1053
        let query_id =
4✔
1054
            <CacheBackend as Cache<CompressedRasterTile2D<u8>>>::insert_query_into_landing_zone(
4✔
1055
                tile_cache,
4✔
1056
                op_name,
4✔
1057
                &query_rect(),
4✔
1058
            )
4✔
1059
            .unwrap();
4✔
1060

4✔
1061
        tile_cache
4✔
1062
            .insert_query_element_into_landing_zone(op_name, &query_id, create_compressed_tile())
4✔
1063
            .unwrap();
4✔
1064

4✔
1065
        <CacheBackend as Cache<CompressedRasterTile2D<u8>>>::move_query_from_landing_zone_to_cache(
4✔
1066
            tile_cache, op_name, &query_id,
4✔
1067
        )
4✔
1068
        .unwrap();
4✔
1069
    }
4✔
1070

1071
    fn create_tile() -> RasterTile2D<u8> {
1✔
1072
        RasterTile2D::<u8> {
1✔
1073
            time: TimeInterval::new_instant(DateTime::new_utc(2014, 3, 1, 0, 0, 0)).unwrap(),
1✔
1074
            tile_position: [-1, 0].into(),
1✔
1075
            global_geo_transform: TestDefault::test_default(),
1✔
1076
            grid_array: Grid::new([3, 2].into(), vec![1, 2, 3, 4, 5, 6])
1✔
1077
                .unwrap()
1✔
1078
                .into(),
1✔
1079
            properties: RasterProperties::default(),
1✔
1080
            cache_hint: CacheHint::max_duration(),
1✔
1081
        }
1✔
1082
    }
1✔
1083

1084
    fn create_compressed_tile() -> CompressedRasterTile2D<u8> {
9✔
1085
        CompressedRasterTile2D::<u8> {
9✔
1086
            time: TimeInterval::new_instant(DateTime::new_utc(2014, 3, 1, 0, 0, 0)).unwrap(),
9✔
1087
            tile_position: [-1, 0].into(),
9✔
1088
            global_geo_transform: TestDefault::test_default(),
9✔
1089
            grid_array: CompressedGridOrEmpty::Compressed(CompressedMaskedGrid::new(
9✔
1090
                [3, 2].into(),
9✔
1091
                vec![1, 2, 3, 4, 5, 6],
9✔
1092
                vec![1; 6],
9✔
1093
            )),
9✔
1094
            properties: RasterProperties::default(),
9✔
1095
            cache_hint: CacheHint::max_duration(),
9✔
1096
        }
9✔
1097
    }
9✔
1098

1099
    fn query_rect() -> RasterQueryRectangle {
13✔
1100
        RasterQueryRectangle {
13✔
1101
            spatial_bounds: SpatialPartition2D::new_unchecked(
13✔
1102
                (-180., 90.).into(),
13✔
1103
                (180., -90.).into(),
13✔
1104
            ),
13✔
1105
            time_interval: TimeInterval::new_instant(DateTime::new_utc(2014, 3, 1, 0, 0, 0))
13✔
1106
                .unwrap(),
13✔
1107
            spatial_resolution: SpatialResolution::one(),
13✔
1108
        }
13✔
1109
    }
13✔
1110

1111
    fn op(idx: usize) -> CanonicOperatorName {
12✔
1112
        CanonicOperatorName::new_unchecked(&json!({
12✔
1113
            "type": "GdalSource",
12✔
1114
            "params": {
12✔
1115
                "data": idx
12✔
1116
            }
12✔
1117
        }))
12✔
1118
    }
12✔
1119

1120
    #[tokio::test]
1✔
1121
    async fn it_evicts_lru() {
1✔
1122
        // Create cache entry and landing zone entry to geht the size of both
1✔
1123
        let landing_zone_entry = RasterLandingQueryEntry {
1✔
1124
            query: query_rect(),
1✔
1125
            elements: LandingZoneQueryTiles::U8(vec![create_compressed_tile()]),
1✔
1126
        };
1✔
1127
        let query_id = QueryId::new();
1✔
1128
        let size_of_landing_zone_entry = landing_zone_entry.byte_size() + query_id.byte_size();
1✔
1129
        let cache_entry: RasterCacheQueryEntry = landing_zone_entry.into();
1✔
1130
        let cache_entry_id = CacheEntryId::new();
1✔
1131
        let size_of_cache_entry = cache_entry.byte_size() + cache_entry_id.byte_size();
1✔
1132

1✔
1133
        // Select the max of both sizes
1✔
1134
        // This is done because the landing zone should not be smaller then the cache
1✔
1135
        let m_size = size_of_cache_entry.max(size_of_landing_zone_entry);
1✔
1136

1✔
1137
        // set limits s.t. three tiles fit
1✔
1138

1✔
1139
        let mut cache_backend = CacheBackend {
1✔
1140
            raster_caches: Default::default(),
1✔
1141
            vector_caches: Default::default(),
1✔
1142
            lru: LruCache::unbounded(),
1✔
1143
            cache_size: CacheSize::new(m_size * 3),
1✔
1144
            landing_zone_size: CacheSize::new(m_size * 3),
1✔
1145
        };
1✔
1146

1✔
1147
        // process three different queries
1✔
1148
        process_query(&mut cache_backend, &op(1));
1✔
1149
        process_query(&mut cache_backend, &op(2));
1✔
1150
        process_query(&mut cache_backend, &op(3));
1✔
1151

1✔
1152
        // query the first one s.t. it is the most recently used
1✔
1153
        <CacheBackend as Cache<CompressedRasterTile2D<u8>>>::query_and_promote(
1✔
1154
            &mut cache_backend,
1✔
1155
            &op(1),
1✔
1156
            &query_rect(),
1✔
1157
        )
1✔
1158
        .unwrap();
1✔
1159

1✔
1160
        // process a fourth query
1✔
1161
        process_query(&mut cache_backend, &op(4));
1✔
1162

1✔
1163
        // assure the seconds query is evicted because it is the least recently used
1✔
1164
        assert!(
1✔
1165
            <CacheBackend as Cache<CompressedRasterTile2D<u8>>>::query_and_promote(
1✔
1166
                &mut cache_backend,
1✔
1167
                &op(2),
1✔
1168
                &query_rect()
1✔
1169
            )
1✔
1170
            .unwrap()
1✔
1171
            .is_none()
1✔
1172
        );
1✔
1173

1174
        // assure that the other queries are still in the cache
1175
        for i in [1, 3, 4] {
4✔
1176
            assert!(
3✔
1177
                <CacheBackend as Cache<CompressedRasterTile2D<u8>>>::query_and_promote(
3✔
1178
                    &mut cache_backend,
3✔
1179
                    &op(i),
3✔
1180
                    &query_rect()
3✔
1181
                )
3✔
1182
                .unwrap()
3✔
1183
                .is_some()
3✔
1184
            );
3✔
1185
        }
1186

1187
        assert_eq!(
1✔
1188
            cache_backend.cache_size.byte_size_used(),
1✔
1189
            3 * size_of_cache_entry
1✔
1190
        );
1✔
1191
    }
1192

1193
    #[test]
1✔
1194
    fn cache_byte_size() {
1✔
1195
        assert_eq!(create_compressed_tile().byte_size(), 268);
1✔
1196
        assert_eq!(
1✔
1197
            CachedTiles::U8(Arc::new(vec![create_compressed_tile()])).byte_size(),
1✔
1198
            /* enum + arc */ 16 + /* vec */ 24  + /* tile */ 268
1✔
1199
        );
1✔
1200
        assert_eq!(
1✔
1201
            CachedTiles::U8(Arc::new(vec![
1✔
1202
                create_compressed_tile(),
1✔
1203
                create_compressed_tile()
1✔
1204
            ]))
1✔
1205
            .byte_size(),
1✔
1206
            /* enum + arc */ 16 + /* vec */ 24  + /* tile */ 2 * 268
1✔
1207
        );
1✔
1208
    }
1✔
1209

1210
    #[tokio::test]
1✔
1211
    async fn it_checks_ttl() {
1✔
1212
        let mut tile_cache = SharedCache {
1✔
1213
            backend: RwLock::new(CacheBackend {
1✔
1214
                raster_caches: Default::default(),
1✔
1215
                vector_caches: Default::default(),
1✔
1216
                lru: LruCache::unbounded(),
1✔
1217
                cache_size: CacheSize::new(usize::MAX),
1✔
1218
                landing_zone_size: CacheSize::new(usize::MAX),
1✔
1219
            }),
1✔
1220
        };
1✔
1221

1✔
1222
        process_query_async(&mut tile_cache, op(1)).await;
1✔
1223

1224
        // access works because no ttl is set
1225
        <SharedCache as AsyncCache<RasterTile2D<u8>>>::query_cache(
1✔
1226
            &tile_cache,
1✔
1227
            &op(1),
1✔
1228
            &query_rect(),
1✔
1229
        )
1✔
1230
        .await
×
1231
        .unwrap()
1✔
1232
        .unwrap();
1✔
1233

1234
        // manually expire entry
1235
        {
1236
            let mut backend = tile_cache.backend.write().await;
1✔
1237
            let cache = backend.raster_caches.iter_mut().next().unwrap();
1✔
1238

1✔
1239
            let tiles = &mut cache.1.entries.iter_mut().next().unwrap().1.elements;
1✔
1240
            match tiles {
1✔
1241
                CachedTiles::U8(tiles) => {
1✔
1242
                    let mut expired_tiles = (**tiles).clone();
1✔
1243
                    expired_tiles[0].cache_hint = CacheHint::with_created_and_expires(
1✔
1244
                        DateTime::new_utc(0, 1, 1, 0, 0, 0),
1✔
1245
                        DateTime::new_utc(0, 1, 1, 0, 0, 1).into(),
1✔
1246
                    );
1✔
1247
                    *tiles = Arc::new(expired_tiles);
1✔
1248
                }
1✔
1249
                _ => panic!("wrong tile type"),
×
1250
            }
1251
        }
1252

1253
        // access fails because ttl is expired
1254
        assert!(<SharedCache as AsyncCache<RasterTile2D<u8>>>::query_cache(
1✔
1255
            &tile_cache,
1✔
1256
            &op(1),
1✔
1257
            &query_rect()
1✔
1258
        )
1✔
1259
        .await
×
1260
        .unwrap()
1✔
1261
        .is_none());
1✔
1262
    }
1263

1264
    #[tokio::test]
1✔
1265
    async fn tile_cache_init_size() {
1✔
1266
        let tile_cache = SharedCache::new(100, 0.1).unwrap();
1✔
1267

1268
        let backend = tile_cache.backend.read().await;
1✔
1269

1270
        let cache_size = 90 * 1024 * 1024;
1✔
1271
        let landing_zone_size = 10 * 1024 * 1024;
1✔
1272

1✔
1273
        assert_eq!(backend.cache_size.total_byte_size(), cache_size);
1✔
1274
        assert_eq!(
1✔
1275
            backend.landing_zone_size.total_byte_size(),
1✔
1276
            landing_zone_size
1✔
1277
        );
1✔
1278
    }
1279
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc