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
//! Functionality for lazy-loading sequences off disk into memory.
//!
//! The main functionality is the very generic [`LazyLoader`]. This is generic over the loading
//! function and the key type. It mainly handles loading data into a [`RefCell`].
//!
use ;
use crateGRangesError;
/// A lazy-loader function that takes a reader type `R` and
/// uses it to load in data of type `T`.
type LoaderFunc<R, T, K> = ;
/// Lazy loader, which uses [`RefCell`] to store mutable reader and data, used for lazy loading and
/// storing one key's worth of data data.
///
/// Generic key types allow for keys to be region tuples, etc.
///
/// # Generics
/// * `R`: the reader type.
/// * `T`: the data type.
/// * `K`: the key type.
///
///
/// * `key`: they key of the loaded data (`None` if nothing loaded).
/// * `reader`: the open reader type (e.g. an indexed FASTA file).
/// * `loader`: a function that takes a key type `K` and an open reader,
/// and retrieves the right data of type `T`.
/// * `data`: the data (`None` if no data loaded).
// pub struct LazyIndexedLoader<R, T> {
// key: RefCell<LoadedType>,
// reader: RefCell<R>,
// loader: LoaderFunc<R, T>,
// query: LoaderFunc<R, T>,
// data: RefCell<Option<T>>,
// }
//
// impl<R, T> LazyIndexedLoader<R, T> {
// pub fn new<F, P>(reader: R, loader: F, query: P) -> LazyLoader<R, T>
// where
// F: Fn(&mut R, &str) -> Result<T, GRangesError> + 'static,
// P: Fn(&mut R, &str, i32, i32) -> Result<T, GRangesError> + 'static,
// {
// LazyLoader {
// key: RefCell::new(String::new()),
// reader: RefCell::new(reader),
// loader: Box::new(loader),
// data: RefCell::new(None),
// }
// }
//
// /// Return a `bool` indicating whether the specified `key` is cached.
// ///
// /// # Arguments
// /// * `key`: a `&str` key passed to the loader function.
// pub fn is_loaded(&self, key: &str) -> bool {
// let loaded_key = self.key.borrow();
// *loaded_key == key
// }
//
// fn load(&self, key: &str) -> Result<T, GRangesError> {
// let mut reader = self.reader.borrow_mut();
// let new_data = (self.loader)(&mut reader, key)?;
// Ok(new_data)
// }
//
// /// Clear out the cache.
// pub fn clear(&self) {
// let mut data_borrow = self.data.borrow_mut();
// *data_borrow = None;
// *self.key.borrow_mut() = String::new();
// }
//
// /// Check if the cache is empty.
// pub fn is_empty(&self) -> bool {
// let data_borrow = self.data.borrow_mut();
// let is_empty = data_borrow.is_none();
// if is_empty {
// let key_borrow = self.key.borrow_mut();
// assert_eq!(
// *key_borrow,
// String::new(),
// "invalid state: empty cache, but no key"
// );
// }
// is_empty
// }
//
// /// Load the data corresponding to `key`
// ///
// /// Loads the data associated with a key using the
// /// `loader` function.
// ///
// /// # Arguments
// /// * `key`: a `&str` key passed to the loader function.
// ///
// /// # Returns
// /// Returns a `Result<T, GRangesError>`.
// pub fn get_data(&self, key: &str) -> Result<Ref<T>, GRangesError> {
// {
// let mut data_borrow = self.data.borrow_mut();
// let is_loaded = self.is_loaded(key);
// if !is_loaded {
// let new_data = self.load(key)?;
// *data_borrow = Some(new_data);
// *self.key.borrow_mut() = key.to_string();
// }
// }
//
// let data_ref = self.data.borrow();
// Ok(Ref::map(data_ref, |opt| {
// opt.as_ref().unwrap_or_else(|| {
// panic!("Data should be loaded at this point, but it's not available.")
// })
// }))
// }
// }