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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::io::Read;
use std::path::PathBuf;
use walkdir::WalkDir;

#[derive(Debug, Deserialize, Serialize)]
pub struct SchemaField {
    pub id: String,
    pub name: String,
    #[serde(rename = "dataType")]
    pub data_type: String,
    #[serde(rename = "geometryCRS")]
    pub geometry_crs: Option<String>,
    // other fields ignored
}

pub type KartSchema = Vec<SchemaField>;
pub type Legend = (Vec<String>, Vec<String>);

use git2::{ObjectType, Oid, Repository};

#[derive(Debug, Clone)]
pub enum FeatureIdentifier {
    Oid(Oid),
    Path(PathBuf),
}

impl std::fmt::Display for FeatureIdentifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            FeatureIdentifier::Oid(oid) => write!(f, "{}", oid),
            FeatureIdentifier::Path(path) => write!(f, "{}", path.display()),
        }
    }
}

pub enum KartSourceEnum {
    Fs(FsSource),
    Git(FsGit),
}

impl KartSourceEnum {
    pub fn get_schema(&self) -> Result<KartSchema, Box<dyn std::error::Error + Send + Sync>> {
        match self {
            KartSourceEnum::Fs(s) => s.get_schema(),
            KartSourceEnum::Git(s) => s.get_schema(),
        }
    }

    pub fn get_legends(
        &self,
    ) -> Result<HashMap<String, Legend>, Box<dyn std::error::Error + Send + Sync>> {
        match self {
            KartSourceEnum::Fs(s) => s.get_legends(),
            KartSourceEnum::Git(s) => s.get_legends(),
        }
    }

    pub fn get_crs_wkt(
        &self,
        crs_id: &str,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        match self {
            KartSourceEnum::Fs(s) => s.get_crs_wkt(crs_id),
            KartSourceEnum::Git(s) => s.get_crs_wkt(crs_id),
        }
    }

    pub fn feature_identifiers(&self) -> Box<dyn Iterator<Item = FeatureIdentifier> + Send> {
        match self {
            KartSourceEnum::Fs(s) => s.feature_identifiers(),
            KartSourceEnum::Git(s) => s.feature_identifiers(),
        }
    }

    pub fn read_feature<F, R>(
        &self,
        id: &FeatureIdentifier,
        f: F,
    ) -> Result<R, Box<dyn std::error::Error + Send + Sync>>
    where
        F: FnOnce(&[u8]) -> R,
    {
        match self {
            KartSourceEnum::Fs(s) => s.read_feature(id, f),
            KartSourceEnum::Git(s) => s.read_feature(id, f),
        }
    }

    pub fn get_feature_size(
        &self,
        id: &FeatureIdentifier,
    ) -> Result<u64, Box<dyn std::error::Error + Send + Sync>> {
        match self {
            KartSourceEnum::Fs(s) => s.get_feature_size(id),
            KartSourceEnum::Git(s) => s.get_feature_size(id),
        }
    }
}

pub struct FsSource {
    base_path: PathBuf,
}

impl FsSource {
    pub fn new(path: impl Into<PathBuf>) -> Self {
        Self {
            base_path: path.into(),
        }
    }

    fn get_schema(&self) -> Result<KartSchema, Box<dyn std::error::Error + Send + Sync>> {
        let schema_path = self.base_path.join(".table-dataset/meta/schema.json");
        let file = File::open(&schema_path)?;
        let reader = BufReader::new(file);
        let kart_schema: KartSchema = serde_json::from_reader(reader)
            .map_err(|e| format!("Failed to parse schema at {}: {}", schema_path.display(), e))?;
        Ok(kart_schema)
    }

    fn get_legends(
        &self,
    ) -> Result<HashMap<String, Legend>, Box<dyn std::error::Error + Send + Sync>> {
        let legend_dir = self.base_path.join(".table-dataset/meta/legend");
        let mut legend_cache = HashMap::new();

        if legend_dir.exists() {
            for entry in WalkDir::new(legend_dir).min_depth(1).max_depth(1) {
                let entry = entry?;
                if entry.file_type().is_file() {
                    let legend_id = entry
                        .path()
                        .file_stem()
                        .unwrap()
                        .to_string_lossy()
                        .to_string();
                    let file = File::open(entry.path())?;
                    let reader = BufReader::new(file);

                    let legend_data: serde_json::Value = match serde_json::from_reader(reader) {
                        Ok(v) => v,
                        Err(_) => {
                            let mut file = File::open(entry.path())?;
                            let mut buffer = Vec::new();
                            file.read_to_end(&mut buffer)?;
                            match rmp_serde::from_slice(&buffer) {
                                Ok(v) => v,
                                Err(_) => continue,
                            }
                        }
                    };

                    if let Some(obj) = legend_data.as_object() {
                        let pks: Vec<String> = obj
                            .get("primary_key")
                            .and_then(|v| v.as_array())
                            .map(|arr| {
                                arr.iter()
                                    .filter_map(|v| v.as_str().map(String::from))
                                    .collect()
                            })
                            .unwrap_or_default();

                        let cols: Vec<String> = obj
                            .get("columns")
                            .and_then(|v| v.as_array())
                            .map(|arr| {
                                arr.iter()
                                    .filter_map(|v| v.as_str().map(String::from))
                                    .collect()
                            })
                            .unwrap_or_default();

                        legend_cache.insert(legend_id, (pks, cols));
                    } else if let Some(l_arr) = legend_data.as_array() {
                        if l_arr.len() >= 2 {
                            let pks: Vec<String> = if let Some(pk_arr) = l_arr[0].as_array() {
                                pk_arr
                                    .iter()
                                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                    .collect()
                            } else {
                                vec![]
                            };

                            let cols: Vec<String> = if let Some(col_arr) = l_arr[1].as_array() {
                                col_arr
                                    .iter()
                                    .filter_map(|v| v.as_str().map(|s| s.to_string()))
                                    .collect()
                            } else {
                                vec![]
                            };

                            legend_cache.insert(legend_id, (pks, cols));
                        }
                    }
                }
            }
        }
        Ok(legend_cache)
    }

    fn get_crs_wkt(
        &self,
        crs_id: &str,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        let crs_filename = format!("{crs_id}.wkt");
        let crs_path = self
            .base_path
            .join(".table-dataset/meta/crs")
            .join(&crs_filename);

        if crs_path.exists() {
            let mut crs_file = File::open(&crs_path)?;
            let mut wkt_string = String::new();
            crs_file.read_to_string(&mut wkt_string)?;
            Ok(wkt_string.trim().to_string())
        } else {
            Err(format!("CRS file not found: {}", crs_path.display()).into())
        }
    }

    fn feature_identifiers(&self) -> Box<dyn Iterator<Item = FeatureIdentifier> + Send> {
        let feature_dir = self.base_path.join(".table-dataset/feature");
        if !feature_dir.exists() {
            return Box::new(std::iter::empty());
        }

        let walker = WalkDir::new(feature_dir).into_iter();

        Box::new(
            walker
                .filter_map(|e| e.ok())
                .filter(|e| e.file_type().is_file())
                .map(|e| FeatureIdentifier::Path(e.into_path())),
        )
    }

    fn read_feature<F, R>(
        &self,
        id: &FeatureIdentifier,
        f: F,
    ) -> Result<R, Box<dyn std::error::Error + Send + Sync>>
    where
        F: FnOnce(&[u8]) -> R,
    {
        match id {
            FeatureIdentifier::Path(path) => {
                let mut file = File::open(path)?;
                let mut buffer = Vec::new();
                file.read_to_end(&mut buffer)?;
                Ok(f(&buffer))
            }
            _ => Err("Invalid identifier for FsSource".into()),
        }
    }

    fn get_feature_size(
        &self,
        id: &FeatureIdentifier,
    ) -> Result<u64, Box<dyn std::error::Error + Send + Sync>> {
        match id {
            FeatureIdentifier::Path(path) => {
                let metadata = std::fs::metadata(path)?;
                Ok(metadata.len())
            }
            _ => Err("Invalid identifier for FsSource".into()),
        }
    }
}

pub struct FsGit {
    repo_path: PathBuf,
    tree_id: Oid,
    prefix: Option<PathBuf>,
}

impl FsGit {
    pub fn new(
        path: impl Into<PathBuf>,
        revision: Option<&str>,
        prefix: Option<&str>,
    ) -> Result<Self, Box<dyn std::error::Error + Send + Sync>> {
        let repo_path = path.into();
        let repo = Repository::open_bare(&repo_path).or_else(|_| Repository::open(&repo_path))?;

        let obj = if let Some(rev) = revision {
            repo.revparse_single(rev)?
        } else {
            let head = repo.head()?;
            head.peel_to_commit()?.into_object()
        };

        let tree_id = obj.peel_to_tree()?.id();
        let prefix = prefix.map(PathBuf::from);

        Ok(Self {
            repo_path,
            tree_id,
            prefix,
        })
    }

    fn resolve_path(&self, path: &str) -> PathBuf {
        if let Some(ref p) = self.prefix {
            p.join(path)
        } else {
            PathBuf::from(path)
        }
    }

    fn get_blob_content_by_path(
        &self,
        path: &str,
    ) -> Result<Vec<u8>, Box<dyn std::error::Error + Send + Sync>> {
        thread_local! {
            static REPO_CACHE: std::cell::RefCell<HashMap<PathBuf, Repository>> = std::cell::RefCell::new(HashMap::new());
        }

        REPO_CACHE.with(|cache| {
            let mut cache = cache.borrow_mut();
            let repo = cache.entry(self.repo_path.clone()).or_insert_with(|| {
                Repository::open_bare(&self.repo_path)
                    .or_else(|_| Repository::open(&self.repo_path))
                    .expect("Failed to open repo")
            });

            let tree = repo.find_tree(self.tree_id)?;
            let full_path = self.resolve_path(path);
            let entry = tree.get_path(&full_path)?;
            let obj = entry.to_object(repo)?;

            if let Some(blob) = obj.as_blob() {
                Ok(blob.content().to_vec())
            } else {
                Err(format!("Path is not a blob: {}", path).into())
            }
        })
    }

    pub fn get_schema(&self) -> Result<KartSchema, Box<dyn std::error::Error + Send + Sync>> {
        let buffer = self.get_blob_content_by_path(".table-dataset/meta/schema.json")?;
        let kart_schema: KartSchema = serde_json::from_slice(&buffer).map_err(|e| {
            format!(
                "Failed to parse schema from .table-dataset/meta/schema.json: {}",
                e
            )
        })?;
        Ok(kart_schema)
    }

    fn get_legends(
        &self,
    ) -> Result<HashMap<String, Legend>, Box<dyn std::error::Error + Send + Sync>> {
        let mut legend_cache = HashMap::new();

        thread_local! {
            static REPO_CACHE: std::cell::RefCell<HashMap<PathBuf, Repository>> = std::cell::RefCell::new(HashMap::new());
        }

        REPO_CACHE.with(|cache| {
            let mut cache = cache.borrow_mut();
            let repo = cache.entry(self.repo_path.clone()).or_insert_with(|| {
                Repository::open_bare(&self.repo_path)
                    .or_else(|_| Repository::open(&self.repo_path))
                    .expect("Failed to open repo")
            });

            let tree = repo.find_tree(self.tree_id)?;
            let legend_dir_path = self.resolve_path(".table-dataset/meta/legend");

            if let Ok(entry) = tree.get_path(&legend_dir_path) {
                if let Ok(legend_tree) = entry.to_object(repo)?.peel_to_tree() {
                    for entry in legend_tree.iter() {
                        if let Some(name) = entry.name() {
                            let legend_id = std::path::Path::new(name)
                                .file_stem()
                                .unwrap()
                                .to_string_lossy()
                                .to_string();
                            if let Ok(obj) = entry.to_object(repo) {
                                if let Some(blob) = obj.as_blob() {
                                    let l_val: serde_json::Value =
                                        match serde_json::from_slice(blob.content()) {
                                            Ok(v) => v,
                                            Err(_) => match rmp_serde::from_slice(blob.content()) {
                                                Ok(v) => v,
                                                Err(_) => continue,
                                            },
                                        };

                                    if let Some(obj) = l_val.as_object() {
                                        let pks = obj
                                            .get("primary_key")
                                            .and_then(|v| v.as_array())
                                            .map(|arr| {
                                                arr.iter()
                                                    .filter_map(|v| v.as_str().map(String::from))
                                                    .collect()
                                            })
                                            .unwrap_or_default();

                                        let cols = obj
                                            .get("columns")
                                            .and_then(|v| v.as_array())
                                            .map(|arr| {
                                                arr.iter()
                                                    .filter_map(|v| v.as_str().map(String::from))
                                                    .collect()
                                            })
                                            .unwrap_or_default();

                                        legend_cache.insert(legend_id, (pks, cols));
                                    } else if let Some(l_arr) = l_val.as_array() {
                                        if l_arr.len() >= 2 {
                                            let pks = if let Some(pk_arr) = l_arr[0].as_array() {
                                                pk_arr
                                                    .iter()
                                                    .filter_map(|v| {
                                                        v.as_str().map(|s| s.to_string())
                                                    })
                                                    .collect()
                                            } else {
                                                vec![]
                                            };

                                            let cols = if let Some(col_arr) = l_arr[1].as_array() {
                                                col_arr
                                                    .iter()
                                                    .filter_map(|v| {
                                                        v.as_str().map(|s| s.to_string())
                                                    })
                                                    .collect()
                                            } else {
                                                vec![]
                                            };

                                            legend_cache.insert(legend_id, (pks, cols));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            Ok(legend_cache)
        })
    }

    fn get_crs_wkt(
        &self,
        crs_id: &str,
    ) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
        let path = format!(".table-dataset/meta/crs/{}.wkt", crs_id);
        let buffer = self.get_blob_content_by_path(&path)?;
        let wkt_string = String::from_utf8(buffer)?;
        Ok(wkt_string.trim().to_string())
    }

    fn feature_identifiers(&self) -> Box<dyn Iterator<Item = FeatureIdentifier> + Send> {
        let mut oids = Vec::new();

        if let Ok(repo) =
            Repository::open_bare(&self.repo_path).or_else(|_| Repository::open(&self.repo_path))
        {
            if let Ok(tree) = repo.find_tree(self.tree_id) {
                let feature_dir_path = self.resolve_path(".table-dataset/feature");
                if let Ok(entry) = tree.get_path(&feature_dir_path) {
                    if let Ok(feature_tree) = entry.to_object(&repo).and_then(|o| o.peel_to_tree())
                    {
                        feature_tree
                            .walk(git2::TreeWalkMode::PreOrder, |_, entry| {
                                if let Some(_name) = entry.name() {
                                    if entry.kind() == Some(ObjectType::Blob) {
                                        oids.push(FeatureIdentifier::Oid(entry.id()));
                                    }
                                }
                                git2::TreeWalkResult::Ok
                            })
                            .ok();
                    }
                }
            }
        }

        Box::new(oids.into_iter())
    }

    fn read_feature<F, R>(
        &self,
        id: &FeatureIdentifier,
        f: F,
    ) -> Result<R, Box<dyn std::error::Error + Send + Sync>>
    where
        F: FnOnce(&[u8]) -> R,
    {
        match id {
            FeatureIdentifier::Oid(oid) => {
                thread_local! {
                    static REPO_CACHE: std::cell::RefCell<HashMap<PathBuf, Repository>> = std::cell::RefCell::new(HashMap::new());
                }

                REPO_CACHE.with(|cache| {
                    let mut cache = cache.borrow_mut();
                    let repo = cache.entry(self.repo_path.clone()).or_insert_with(|| {
                        Repository::open_bare(&self.repo_path)
                            .or_else(|_| Repository::open(&self.repo_path))
                            .expect("Failed to open repo")
                    });

                    if let Ok(odb) = repo.odb() {
                        if let Ok(obj) = odb.read(*oid) {
                            return Ok(f(obj.data()));
                        }
                    }

                    if let Ok(blob) = repo.find_blob(*oid) {
                        return Ok(f(blob.content()));
                    }
                    Err(format!("Blob not found: {}", oid).into())
                })
            }
            _ => Err("Invalid identifier for FsGit".into()),
        }
    }

    fn get_feature_size(
        &self,
        id: &FeatureIdentifier,
    ) -> Result<u64, Box<dyn std::error::Error + Send + Sync>> {
        match id {
            FeatureIdentifier::Oid(oid) => {
                thread_local! {
                    static REPO_CACHE: std::cell::RefCell<HashMap<PathBuf, Repository>> = std::cell::RefCell::new(HashMap::new());
                }

                REPO_CACHE.with(|cache| {
                    let mut cache = cache.borrow_mut();
                    let repo = cache.entry(self.repo_path.clone()).or_insert_with(|| {
                        Repository::open_bare(&self.repo_path)
                            .or_else(|_| Repository::open(&self.repo_path))
                            .expect("Failed to open repo")
                    });

                    if let Ok(odb) = repo.odb() {
                        if let Ok((len, _)) = odb.read_header(*oid) {
                            return Ok(len as u64);
                        }
                    }

                    if let Ok(blob) = repo.find_blob(*oid) {
                        return Ok(blob.size() as u64);
                    }
                    Err(format!("Blob not found: {}", oid).into())
                })
            }
            _ => Err("Invalid identifier for FsGit".into()),
        }
    }
}