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
//! DB functions to support testing
//!
//! For some tests we need to modify the state of the store in ways that are not
//! possible through the public API. This module provides functions to do that.
use std::{
    io,
    path::{Path, PathBuf},
};

use redb::ReadableTable;

use super::{
    tables::{ReadableTables, Tables},
    ActorError, ActorMessage, ActorResult, ActorState, DataLocation, EntryState, FilterPredicate,
    OutboardLocation, OuterResult, Store, StoreInner,
};
use crate::{
    store::{mutable_mem_storage::SizeInfo, DbIter},
    util::raw_outboard_size,
    Hash,
};

/// The full state of an entry, including the data.
#[derive(derive_more::Debug)]
pub enum EntryData {
    /// Complete
    Complete {
        /// Data
        #[debug("data")]
        data: Vec<u8>,
        /// Outboard
        #[debug("outboard")]
        outboard: Vec<u8>,
    },
    /// Partial
    Partial {
        /// Data
        #[debug("data")]
        data: Vec<u8>,
        /// Outboard
        #[debug("outboard")]
        outboard: Vec<u8>,
        /// Sizes
        #[debug("sizes")]
        sizes: Vec<u8>,
    },
}

impl Store {
    /// Get the complete state of an entry, both in memory and in redb.
    #[cfg(test)]
    pub(crate) async fn entry_state(&self, hash: Hash) -> io::Result<EntryStateResponse> {
        Ok(self.0.entry_state(hash).await?)
    }

    async fn all_blobs(&self) -> io::Result<DbIter<Hash>> {
        Ok(Box::new(self.0.all_blobs().await?.into_iter()))
    }

    /// Transform all entries in the store. This is for testing and can be used to get the store
    /// in a wrong state.
    pub async fn transform_entries(
        &self,
        transform: impl Fn(Hash, EntryData) -> Option<EntryData> + Send + Sync,
    ) -> io::Result<()> {
        let blobs = self.all_blobs().await?;
        for blob in blobs {
            let hash = blob?;
            let entry = self.get_full_entry_state(hash).await?;
            if let Some(entry) = entry {
                let entry1 = transform(hash, entry);
                self.set_full_entry_state(hash, entry1).await?;
            }
        }
        Ok(())
    }

    /// Set the full entry state for a hash. This is for testing and can be used to get the store
    /// in a wrong state.
    pub(crate) async fn set_full_entry_state(
        &self,
        hash: Hash,
        entry: Option<EntryData>,
    ) -> io::Result<()> {
        Ok(self.0.set_full_entry_state(hash, entry).await?)
    }

    /// Set the full entry state for a hash. This is for testing and can be used to get the store
    /// in a wrong state.
    pub(crate) async fn get_full_entry_state(&self, hash: Hash) -> io::Result<Option<EntryData>> {
        Ok(self.0.get_full_entry_state(hash).await?)
    }

    /// Owned data path
    pub fn owned_data_path(&self, hash: &Hash) -> PathBuf {
        self.0.path_options.owned_data_path(hash)
    }

    /// Owned outboard path
    pub fn owned_outboard_path(&self, hash: &Hash) -> PathBuf {
        self.0.path_options.owned_outboard_path(hash)
    }
}

impl StoreInner {
    #[cfg(test)]
    async fn entry_state(&self, hash: Hash) -> OuterResult<EntryStateResponse> {
        let (tx, rx) = oneshot::channel();
        self.tx.send(ActorMessage::EntryState { hash, tx }).await?;
        Ok(rx.await??)
    }

    async fn set_full_entry_state(&self, hash: Hash, entry: Option<EntryData>) -> OuterResult<()> {
        let (tx, rx) = oneshot::channel();
        self.tx
            .send(ActorMessage::SetFullEntryState { hash, entry, tx })
            .await?;
        Ok(rx.await??)
    }

    async fn get_full_entry_state(&self, hash: Hash) -> OuterResult<Option<EntryData>> {
        let (tx, rx) = oneshot::channel();
        self.tx
            .send(ActorMessage::GetFullEntryState { hash, tx })
            .await?;
        Ok(rx.await??)
    }

    async fn all_blobs(&self) -> OuterResult<Vec<io::Result<Hash>>> {
        let (tx, rx) = oneshot::channel();
        let filter: FilterPredicate<Hash, EntryState> =
            Box::new(|_i, k, v| Some((k.value(), v.value())));
        self.tx.send(ActorMessage::Blobs { filter, tx }).await?;
        let blobs = rx.await?;
        let res = blobs?
            .into_iter()
            .map(|r| {
                r.map(|(hash, _)| hash)
                    .map_err(|e| ActorError::from(e).into())
            })
            .collect::<Vec<_>>();
        Ok(res)
    }
}

#[cfg(test)]
#[derive(Debug)]
pub(crate) struct EntryStateResponse {
    pub mem: Option<crate::store::bao_file::BaoFileHandle>,
    pub db: Option<EntryState<Vec<u8>>>,
}

impl ActorState {
    pub(super) fn get_full_entry_state(
        &mut self,
        tables: &impl ReadableTables,
        hash: Hash,
    ) -> ActorResult<Option<EntryData>> {
        let data_path = self.options.path.owned_data_path(&hash);
        let outboard_path = self.options.path.owned_outboard_path(&hash);
        let sizes_path = self.options.path.owned_sizes_path(&hash);
        let entry = match tables.blobs().get(hash)? {
            Some(guard) => match guard.value() {
                EntryState::Complete {
                    data_location,
                    outboard_location,
                } => {
                    let data = match data_location {
                        DataLocation::External(paths, size) => {
                            let path = paths.first().ok_or_else(|| {
                                ActorError::Inconsistent("external data missing".to_owned())
                            })?;
                            let res = std::fs::read(path)?;
                            if res.len() != size as usize {
                                return Err(ActorError::Inconsistent(
                                    "external data size mismatch".to_owned(),
                                ));
                            }
                            res
                        }
                        DataLocation::Owned(size) => {
                            let res = std::fs::read(data_path)?;
                            if res.len() != size as usize {
                                return Err(ActorError::Inconsistent(
                                    "owned data size mismatch".to_owned(),
                                ));
                            }
                            res
                        }
                        DataLocation::Inline(_) => {
                            let data = tables.inline_data().get(hash)?.ok_or_else(|| {
                                ActorError::Inconsistent("inline data missing".to_owned())
                            })?;
                            data.value().to_vec()
                        }
                    };
                    let expected_outboard_size = raw_outboard_size(data.len() as u64);
                    let outboard = match outboard_location {
                        OutboardLocation::Owned => std::fs::read(outboard_path)?,
                        OutboardLocation::Inline(_) => tables
                            .inline_outboard()
                            .get(hash)?
                            .ok_or_else(|| {
                                ActorError::Inconsistent("inline outboard missing".to_owned())
                            })?
                            .value()
                            .to_vec(),
                        OutboardLocation::NotNeeded => Vec::new(),
                    };
                    if outboard.len() != expected_outboard_size as usize {
                        return Err(ActorError::Inconsistent(
                            "outboard size mismatch".to_owned(),
                        ));
                    }
                    Some(EntryData::Complete { data, outboard })
                }
                EntryState::Partial { .. } => {
                    let data = std::fs::read(data_path)?;
                    let outboard = std::fs::read(outboard_path)?;
                    let sizes = std::fs::read(sizes_path)?;
                    Some(EntryData::Partial {
                        data,
                        outboard,
                        sizes,
                    })
                }
            },
            None => None,
        };
        Ok(entry)
    }

    pub(super) fn set_full_entry_state(
        &mut self,
        tables: &mut Tables,
        hash: Hash,
        entry: Option<EntryData>,
    ) -> ActorResult<()> {
        let data_path = self.options.path.owned_data_path(&hash);
        let outboard_path = self.options.path.owned_outboard_path(&hash);
        let sizes_path = self.options.path.owned_sizes_path(&hash);
        // tabula rasa
        std::fs::remove_file(&outboard_path).ok();
        std::fs::remove_file(&data_path).ok();
        std::fs::remove_file(&sizes_path).ok();
        tables.inline_data.remove(&hash)?;
        tables.inline_outboard.remove(&hash)?;
        let Some(entry) = entry else {
            tables.blobs.remove(&hash)?;
            return Ok(());
        };
        // write the new data and determine the new state
        let entry = match entry {
            EntryData::Complete { data, outboard } => {
                let data_size = data.len() as u64;
                let data_location = if data_size > self.options.inline.max_data_inlined {
                    std::fs::write(data_path, &data)?;
                    DataLocation::Owned(data_size)
                } else {
                    tables.inline_data.insert(hash, data.as_slice())?;
                    DataLocation::Inline(())
                };
                let outboard_size = outboard.len() as u64;
                let outboard_location = if outboard_size > self.options.inline.max_outboard_inlined
                {
                    std::fs::write(outboard_path, &outboard)?;
                    OutboardLocation::Owned
                } else if outboard_size > 0 {
                    tables.inline_outboard.insert(hash, outboard.as_slice())?;
                    OutboardLocation::Inline(())
                } else {
                    OutboardLocation::NotNeeded
                };
                EntryState::Complete {
                    data_location,
                    outboard_location,
                }
            }
            EntryData::Partial {
                data,
                outboard,
                sizes,
            } => {
                std::fs::write(data_path, data)?;
                std::fs::write(outboard_path, outboard)?;
                std::fs::write(sizes_path, sizes)?;
                EntryState::Partial { size: None }
            }
        };
        // finally, write the state
        tables.blobs.insert(hash, entry)?;
        Ok(())
    }

    #[cfg(test)]
    pub(super) fn entry_state(
        &mut self,
        tables: &impl ReadableTables,
        hash: Hash,
    ) -> ActorResult<EntryStateResponse> {
        let mem = self.handles.get(&hash).and_then(|weak| weak.upgrade());
        let db = match tables.blobs().get(hash)? {
            Some(entry) => Some({
                match entry.value() {
                    EntryState::Complete {
                        data_location,
                        outboard_location,
                    } => {
                        let data_location = match data_location {
                            DataLocation::Inline(()) => {
                                let data = tables.inline_data().get(hash)?.ok_or_else(|| {
                                    ActorError::Inconsistent("inline data missing".to_owned())
                                })?;
                                DataLocation::Inline(data.value().to_vec())
                            }
                            DataLocation::Owned(x) => DataLocation::Owned(x),
                            DataLocation::External(p, s) => DataLocation::External(p, s),
                        };
                        let outboard_location = match outboard_location {
                            OutboardLocation::Inline(()) => {
                                let outboard =
                                    tables.inline_outboard().get(hash)?.ok_or_else(|| {
                                        ActorError::Inconsistent(
                                            "inline outboard missing".to_owned(),
                                        )
                                    })?;
                                OutboardLocation::Inline(outboard.value().to_vec())
                            }
                            OutboardLocation::Owned => OutboardLocation::Owned,
                            OutboardLocation::NotNeeded => OutboardLocation::NotNeeded,
                        };
                        EntryState::Complete {
                            data_location,
                            outboard_location,
                        }
                    }
                    EntryState::Partial { size } => EntryState::Partial { size },
                }
            }),
            None => None,
        };
        Ok(EntryStateResponse { mem, db })
    }
}

/// What do to with a file pair when making partial files
#[derive(Debug)]
pub enum MakePartialResult {
    /// leave the file as is
    Retain,
    /// remove it entirely
    Remove,
    /// truncate the data file to the given size
    Truncate(u64),
}

/// Open a database and make it partial.
pub fn make_partial(
    path: &Path,
    f: impl Fn(Hash, u64) -> MakePartialResult + Send + Sync,
) -> io::Result<()> {
    tracing::info!("starting runtime for make_partial");
    let rt = tokio::runtime::Builder::new_current_thread()
        .enable_all()
        .build()?;
    rt.block_on(async move {
        let blobs_path = path.join("blobs");
        let store = Store::load(blobs_path).await?;
        store
            .transform_entries(|hash, entry| match &entry {
                EntryData::Complete { data, outboard } => {
                    let res = f(hash, data.len() as u64);
                    tracing::info!("make_partial: {} {:?}", hash, res);
                    match res {
                        MakePartialResult::Retain => Some(entry),
                        MakePartialResult::Remove => None,
                        MakePartialResult::Truncate(size) => {
                            let current_size = data.len() as u64;
                            if size < current_size {
                                let size = size as usize;
                                let sizes = SizeInfo::complete(current_size).to_vec();
                                Some(EntryData::Partial {
                                    data: data[..size].to_vec(),
                                    outboard: outboard.to_vec(),
                                    sizes,
                                })
                            } else {
                                Some(entry)
                            }
                        }
                    }
                }
                EntryData::Partial { .. } => Some(entry),
            })
            .await?;
        std::io::Result::Ok(())
    })?;
    drop(rt);
    tracing::info!("done with make_partial");
    Ok(())
}