-
Notifications
You must be signed in to change notification settings - Fork 77
/
linear.rs
185 lines (157 loc) · 4.97 KB
/
linear.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
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
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;
use crate::index::{Comparable, DatasetInfo, Index, SigStore};
use crate::prelude::*;
use crate::storage::{FSStorage, InnerStorage, Storage, StorageInfo};
use crate::Error;
#[derive(TypedBuilder)]
pub struct LinearIndex<L> {
#[builder(default)]
storage: Option<InnerStorage>,
#[builder(default)]
datasets: Vec<SigStore<L>>,
}
#[derive(Serialize, Deserialize)]
struct LinearInfo<L> {
version: u32,
storage: StorageInfo,
leaves: Vec<L>,
}
impl<'a, L> Index<'a> for LinearIndex<L>
where
L: Clone + Comparable<L> + 'a,
SigStore<L>: From<L>,
{
type Item = L;
//type SignatureIterator = std::slice::Iter<'a, Self::Item>;
fn insert(&mut self, node: L) -> Result<(), Error> {
self.datasets.push(node.into());
Ok(())
}
fn save<P: AsRef<Path>>(&self, _path: P) -> Result<(), Error> {
/*
let file = File::create(path)?;
match serde_json::to_writer(file, &self) {
Ok(_) => Ok(()),
Err(_) => Err(SourmashError::SerdeError.into()),
}
*/
unimplemented!()
}
fn load<P: AsRef<Path>>(_path: P) -> Result<(), Error> {
unimplemented!()
}
fn signatures(&self) -> Vec<Self::Item> {
self.datasets
.iter()
.map(|x| x.data.get().unwrap().clone())
.collect()
}
fn signature_refs(&self) -> Vec<&Self::Item> {
self.datasets
.iter()
.map(|x| x.data.get().unwrap())
.collect()
}
/*
fn iter_signatures(&'a self) -> Self::SignatureIterator {
self.datasets.iter()
}
*/
}
impl<L> LinearIndex<L>
where
L: ToWriter,
SigStore<L>: ReadData<L>,
{
pub fn save_file<P: AsRef<Path>>(
&mut self,
path: P,
storage: Option<InnerStorage>,
) -> Result<(), Error> {
let ref_path = path.as_ref();
let mut basename = ref_path.file_name().unwrap().to_str().unwrap().to_owned();
if basename.ends_with(".sbt.json") {
basename = basename.replace(".sbt.json", "");
}
let location = ref_path.parent().unwrap();
let storage = match storage {
Some(s) => s,
None => {
let subdir = format!(".linear.{}", basename);
InnerStorage::new(FSStorage::new(location.to_str().unwrap(), &subdir))
}
};
let args = storage.args();
let storage_info = StorageInfo {
backend: "FSStorage".into(),
args,
};
let info: LinearInfo<DatasetInfo> = LinearInfo {
storage: storage_info,
version: 5,
leaves: self
.datasets
.iter_mut()
.map(|l| {
// Trigger data loading
let _: &L = (*l).data().unwrap();
// set storage to new one
l.storage = Some(storage.clone());
let filename = (*l).save(&l.filename).unwrap();
DatasetInfo {
filename,
name: l.name.clone(),
metadata: l.metadata.clone(),
}
})
.collect(),
};
let file = File::create(path)?;
serde_json::to_writer(file, &info)?;
Ok(())
}
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<LinearIndex<L>, Error> {
let file = File::open(&path)?;
let mut reader = BufReader::new(file);
// TODO: match with available Storage while we don't
// add a function to build a Storage from a StorageInfo
let mut basepath = PathBuf::new();
basepath.push(path);
basepath.canonicalize()?;
let linear = LinearIndex::<L>::from_reader(&mut reader, basepath.parent().unwrap())?;
Ok(linear)
}
pub fn from_reader<R, P>(rdr: R, path: P) -> Result<LinearIndex<L>, Error>
where
R: Read,
P: AsRef<Path>,
{
// TODO: check https://serde.rs/enum-representations.html for a
// solution for loading v4 and v5
let linear: LinearInfo<DatasetInfo> = serde_json::from_reader(rdr)?;
// TODO: support other storages
let mut st: FSStorage = (&linear.storage.args).into();
st.set_base(path.as_ref().to_str().unwrap());
let storage = InnerStorage::new(st);
Ok(LinearIndex {
storage: Some(storage.clone()),
datasets: linear
.leaves
.into_iter()
.map(|l| {
let mut v: SigStore<L> = l.into();
v.storage = Some(storage.clone());
v
})
.collect(),
})
}
pub fn storage(&self) -> Option<InnerStorage> {
self.storage.clone()
}
}