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
use geohash::GeohashError;
use hashbrown::{HashMap, HashSet};
use itertools::Itertools;
use rayon::prelude::*;

use crate::cache::SpatialIndex;
use crate::models::{BatchOutput, GeoShardConfig, GeoShardError, LatLngCoord, Neighbor};

/// A collection of geospatial indexes stored in-memory
#[derive(Default, Clone)]
pub struct GeoShard {
    cache: HashMap<String, SpatialIndex>,
    config: GeoShardConfig,
}

impl From<GeoShardConfig> for GeoShard {
    fn from(config: GeoShardConfig) -> Self {
        GeoShard {
            config,
            ..Default::default()
        }
    }
}

impl GeoShard {
    /// depth 6 corresponds to ~1kmx1km region
    pub const DEFAULT_DEPTH: usize = 6;
    pub const DEFAULT_COUNT: usize = 100;
    pub const DEFAULT_SORTED: bool = false;

    /// Adds a new geospatial index to the shard
    pub fn create_index(&mut self, index: &str) -> Result<Option<()>, GeoShardError> {
        if self.cache.contains_key(index) {
            return Err(GeoShardError::IndexAlreadyExists(index.to_owned()));
        }
        match self.cache.insert(index.into(), SpatialIndex::default()) {
            Some(_) => Ok(Some(())),
            None => Ok(None),
        }
    }

    /// Deletes index from the shard
    pub fn drop_index(&mut self, index: &str) {
        self.cache.remove(index);
    }

    /// Adds a key into the specified index at some geographical position
    pub fn insert_key(
        &mut self,
        index: &str,
        key: &str,
        [lat, lng]: LatLngCoord,
    ) -> Result<String, GeoShardError> {
        if let Some(geo_index) = self.cache.get_mut(index) {
            match geohash::encode(
                [lng, lat].into(),
                self.config.insert_depth.unwrap_or(Self::DEFAULT_DEPTH),
            ) {
                Ok(ghash) => {
                    geo_index.insert(key, &ghash);
                    Ok(ghash)
                }
                Err(err) => Err(GeoShardError::GeohashError(err)),
            }
        } else {
            Err(GeoShardError::IndexNotFound(index.to_owned()))
        }
    }

    /// Adds multiple keys into the specified index at some geographical position
    pub fn insert_many_keys(
        &mut self,
        index: &str,
        objects: Vec<(String, LatLngCoord)>,
        preserve_order: bool,
    ) -> Result<BatchOutput<String, GeohashError>, GeoShardError> {
        if let Some(geo_index) = self.cache.get_mut(index) {
            let insert_depth = self.config.insert_depth.unwrap_or(Self::DEFAULT_DEPTH);
            let (bulk, errors): BatchOutput<String, GeohashError> = if !preserve_order {
                // ? use parallel iterator
                objects.into_par_iter().partition_map(|(key, [lat, lng])| {
                    match geohash::encode([lng, lat].into(), insert_depth) {
                        Ok(ghash) => rayon::iter::Either::Left((key, ghash)),
                        Err(err) => rayon::iter::Either::Right((key, err)),
                    }
                })
            } else {
                // ? use sequential iterator
                objects.into_iter().partition_map(|(key, [lat, lng])| {
                    match geohash::encode([lng, lat].into(), insert_depth) {
                        Ok(ghash) => itertools::Either::Left((key, ghash)),
                        Err(err) => itertools::Either::Right((key, err)),
                    }
                })
            };

            geo_index.insert_many(bulk.clone());

            Ok((bulk, errors))
        } else {
            Err(GeoShardError::IndexNotFound(index.to_owned()))
        }
    }

    /// Deletes a key from the specified index
    pub fn remove_key(&mut self, index: &str, key: &str) -> Result<bool, GeoShardError> {
        if let Some(geo_index) = self.cache.get_mut(index) {
            Ok(geo_index.remove(key))
        } else {
            Err(GeoShardError::IndexNotFound(index.to_owned()))
        }
    }

    /// Deletes multiple keys from the specified index
    pub fn remove_many_keys(
        &mut self,
        index: &str,
        keys: HashSet<String>,
    ) -> Result<bool, GeoShardError> {
        if let Some(geo_index) = self.cache.get_mut(index) {
            Ok(geo_index.remove_many(keys))
        } else {
            Err(GeoShardError::IndexNotFound(index.to_owned()))
        }
    }

    /// Search index for keys within some range
    pub fn query_range(
        &self,
        index: &str,
        origin: LatLngCoord,
        range: f64,
        count: Option<usize>,
        sorted: Option<bool>,
    ) -> Result<Vec<Neighbor>, GeoShardError> {
        let count = count.unwrap_or(Self::DEFAULT_COUNT);
        if count.eq(&0) {
            return Ok(vec![]);
        }
        if let Some(geo_index) = self.cache.get(index) {
            match geo_index.search(
                origin,
                range,
                count,
                sorted.unwrap_or(Self::DEFAULT_SORTED),
                self.config.search_depth.unwrap_or(Self::DEFAULT_DEPTH),
            ) {
                Ok(found) => Ok(found),
                Err(err) => Err(GeoShardError::GeohashError(err)),
            }
        } else {
            Err(GeoShardError::IndexNotFound(index.to_owned()))
        }
    }

    /// Search multiple indices for keys within some range
    pub fn query_range_many(
        &self,
        indices: HashSet<String>,
        origin: LatLngCoord,
        range: f64,
        count: Option<usize>,
        sorted: Option<bool>,
    ) -> BatchOutput<Vec<Neighbor>, GeoShardError> {
        indices.into_par_iter().partition_map(|index| {
            match self.query_range(&index, origin, range, count, sorted) {
                Ok(found) => itertools::Either::Left((index, found)),
                Err(err) => itertools::Either::Right((index, err)),
            }
        })
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn can_create_index() {
        let mut shard = GeoShard::default();
        let mock_index = "mock-index";

        let res = shard.create_index(mock_index);
        assert!(res.is_ok());

        let res = shard.create_index(mock_index);
        assert!(res.is_err());
    }

    #[test]
    fn can_drop_index() {
        let mut shard = GeoShard::default();
        let mock_index = "mock-index";

        let res = shard.create_index(mock_index);
        assert!(res.is_ok());

        shard.drop_index(mock_index);

        let res = shard.create_index(mock_index);
        assert!(res.is_ok());
    }

    #[test]
    fn can_query_range() {
        let mut shard = GeoShard::default();
        let mock_index = "mock-index";
        let count = Some(100);
        let sorted = None;
        shard.create_index(mock_index).unwrap();

        shard
            .insert_many_keys(
                mock_index,
                vec![
                    ("a".to_string(), [-0.25, 1.0]),
                    ("b".to_string(), [1.0, 0.5]),
                    ("c".to_string(), [0.0, 0.0]),
                ],
                false,
            )
            .unwrap();

        let res = shard
            .query_range(mock_index, [0.0, 0.0], 150.0, count, sorted)
            .unwrap();
        assert!(res.len() <= count.unwrap());
    }

    #[test]
    fn can_query_range_count() {
        let mut shard = GeoShard::default();
        let mock_index = "mock-index";
        let sorted = None;
        shard.create_index(mock_index).unwrap();

        shard
            .insert_many_keys(
                mock_index,
                vec![
                    ("a".to_string(), [0.0, 1.0]),
                    ("b".to_string(), [1.0, 0.5]),
                    ("c".to_string(), [0.0, 0.0]),
                    ("d".to_string(), [0.0, -1.0]),
                    ("e".to_string(), [-1.0, -0.5]),
                    ("f".to_string(), [0.0, 0.0]),
                ],
                false,
            )
            .unwrap();

        {
            let count = Some(100);
            let res = shard
                .query_range(mock_index, [0.0, 0.0], 1000.0, count, sorted)
                .unwrap();

            assert!(res.len() <= count.unwrap());
        }

        {
            let count = Some(0);

            let res = shard
                .query_range(mock_index, [0.0, 0.0], 1000.0, count, sorted)
                .unwrap();

            assert!(res.len() <= count.unwrap());
        }
    }

    #[test]
    fn can_query_range_sorted() {
        let mut shard = GeoShard::default();
        let mock_index = "mock-index";
        let count = Some(100);
        let sorted = Some(true);
        shard.create_index(mock_index).unwrap();

        shard
            .insert_many_keys(
                mock_index,
                vec![
                    ("a".to_string(), [0.0, 1.0]),
                    ("b".to_string(), [1.0, 0.5]),
                    ("c".to_string(), [0.0, 0.0]),
                    ("d".to_string(), [0.0, -1.0]),
                    ("e".to_string(), [-1.0, -0.5]),
                    ("f".to_string(), [0.0, 0.0]),
                ],
                false,
            )
            .unwrap();

        let res = shard
            .query_range(mock_index, [0.0, 0.0], 1000.0, count, sorted)
            .unwrap();

        let mut sorted_neighbors = res.to_vec();

        sorted_neighbors.sort_by(|a, b| a.distance.partial_cmp(&b.distance).unwrap());

        assert_eq!(res, sorted_neighbors);
    }
}