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
use std::{ops::Deref, option::Option::None, sync::Arc, vec::IntoIter};
use git_hash::ObjectId;
use crate::{loose, store::handle, store_impls::dynamic};
enum State {
Pack {
index_iter: IntoIter<handle::IndexLookup>,
index: handle::IndexLookup,
entry_index: u32,
num_objects: u32,
},
Loose {
iter: loose::Iter,
index: usize,
},
Depleted,
}
pub struct AllObjects {
state: State,
num_objects: usize,
loose_dbs: Arc<Vec<loose::Store>>,
}
impl AllObjects {
pub fn new(db: &dynamic::Store) -> Result<Self, crate::store::load_index::Error> {
let snapshot = db.load_all_indices()?;
let packed_objects = snapshot
.indices
.iter()
.fold(0usize, |dbc, index| dbc.saturating_add(index.num_objects() as usize));
let mut index_iter = snapshot.indices.into_iter();
let loose_dbs = snapshot.loose_dbs;
let state = match index_iter.next() {
Some(index) => {
let num_objects = index.num_objects();
State::Pack {
index_iter,
index,
entry_index: 0,
num_objects,
}
}
None => {
let index = 0;
State::Loose {
iter: loose_dbs.get(index).expect("at least one loose db").iter(),
index,
}
}
};
Ok(AllObjects {
state,
loose_dbs,
num_objects: packed_objects,
})
}
}
impl Iterator for AllObjects {
type Item = Result<ObjectId, loose::iter::Error>;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.state {
State::Depleted => None,
State::Pack {
index_iter,
index,
entry_index,
num_objects,
} => {
if *entry_index < *num_objects {
let oid = index.oid_at_index(*entry_index).to_owned();
*entry_index += 1;
Some(Ok(oid))
} else {
match index_iter.next() {
Some(new_index) => {
*index = new_index;
*entry_index = 0;
*num_objects = index.num_objects();
}
None => {
let index = 0;
self.state = State::Loose {
iter: self.loose_dbs.get(index).expect("at least one loose odb").iter(),
index,
}
}
}
self.next()
}
}
State::Loose { iter, index } => match iter.next() {
Some(id) => Some(id),
None => {
*index += 1;
match self.loose_dbs.get(*index).map(|ldb| ldb.iter()) {
Some(new_iter) => {
*iter = new_iter;
self.next()
}
None => {
self.state = State::Depleted;
None
}
}
}
},
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.num_objects, None)
}
}
impl<S> super::Handle<S>
where
S: Deref<Target = super::Store> + Clone,
{
pub fn iter(&self) -> Result<AllObjects, dynamic::load_index::Error> {
AllObjects::new(self.store_ref())
}
}
impl dynamic::Store {
pub fn iter(&self) -> Result<AllObjects, dynamic::load_index::Error> {
AllObjects::new(self)
}
}