Skip to content

Commit 1e02f63

Browse files
committed
aomap: fix invalid iterator
1 parent 3dd450e commit 1e02f63

File tree

3 files changed

+14
-31
lines changed

3 files changed

+14
-31
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "aora"
3-
version = "0.6.0"
3+
version = "0.6.1"
44
description = "Append-only random-accessed data persistence"
55
authors = ["Dr. Maxim Orlovsky <orlovsky@ubideco.org>"]
66
keywords = ["database", "append-only-log"]

src/providers/file/aomap.rs

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
// SPDX-License-Identifier: Apache-2.0
22

33
use std::cell::{RefCell, RefMut};
4-
use std::collections::HashMap;
54
use std::fs;
65
use std::io::{self, Read, Seek, SeekFrom, Write};
76
use std::marker::PhantomData;
87
use std::path::{Path, PathBuf};
98

109
use binfile::BinFile;
10+
use indexmap::IndexMap;
1111
use strict_encoding::{
1212
StreamReader, StreamWriter, StrictDecode, StrictEncode, StrictReader, StrictWriter,
1313
};
@@ -38,7 +38,7 @@ where K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>
3838
{
3939
log: RefCell<BinFile<MAGIC, VER>>,
4040
idx: RefCell<BinFile<MAGIC, VER>>,
41-
index: RefCell<HashMap<[u8; KEY_LEN], u64>>,
41+
index: RefCell<IndexMap<[u8; KEY_LEN], u64>>,
4242
_phantom: PhantomData<(K, V)>,
4343
}
4444

@@ -77,7 +77,7 @@ where K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>
7777
Ok(Self {
7878
log: RefCell::new(log),
7979
idx: RefCell::new(idx),
80-
index: RefCell::new(HashMap::new()),
80+
index: RefCell::new(IndexMap::new()),
8181
_phantom: PhantomData,
8282
})
8383
}
@@ -119,7 +119,7 @@ where K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>
119119
Ok(Self {
120120
log: RefCell::new(log),
121121
idx: RefCell::new(idx),
122-
index: RefCell::new(HashMap::new()),
122+
index: RefCell::new(IndexMap::new()),
123123
_phantom: PhantomData,
124124
})
125125
}
@@ -147,7 +147,7 @@ where K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>
147147
let mut idx = BinFile::open_rw(&idx)
148148
.map_err(|err| io::Error::new(err.kind(), format!("index file '{}'", idx.display())))?;
149149

150-
let mut index = HashMap::new();
150+
let mut index = IndexMap::new();
151151
loop {
152152
let mut key_buf = [0u8; KEY_LEN];
153153
let res = idx.read_exact(&mut key_buf);
@@ -226,20 +226,14 @@ where
226226
idx.write_all(&pos.to_le_bytes())
227227
.expect("unable to write to index");
228228

229-
self.index.get_mut().insert(key, pos);
229+
self.index.borrow_mut().insert(key, pos);
230230
}
231231

232232
fn iter(&self) -> impl Iterator<Item = (K, V)> {
233-
let mut log = self.log.borrow_mut();
234-
log.seek(SeekFrom::Start(0))
235-
.expect("unable to seek to the start of the log file");
236-
log.seek(SeekFrom::Start(0))
237-
.expect("unable to seek to the start of the index file");
238-
233+
let index = self.index.borrow().clone();
239234
Iter {
240-
log,
241-
idx: self.idx.borrow_mut(),
242-
pos: 0,
235+
log: self.log.borrow_mut(),
236+
index: index.into_iter(),
243237
_phantom: PhantomData,
244238
}
245239
}
@@ -254,8 +248,7 @@ pub struct Iter<
254248
const KEY_LEN: usize,
255249
> {
256250
log: RefMut<'file, BinFile<MAGIC, VER>>,
257-
idx: RefMut<'file, BinFile<MAGIC, VER>>,
258-
pos: u64,
251+
index: indexmap::map::IntoIter<[u8; KEY_LEN], u64>,
259252
_phantom: PhantomData<(K, V)>,
260253
}
261254

@@ -270,24 +263,14 @@ impl<
270263
type Item = (K, V);
271264

272265
fn next(&mut self) -> Option<Self::Item> {
273-
let mut id = [0u8; KEY_LEN];
274-
self.idx.read_exact(&mut id).ok()?;
275-
self.idx
276-
.seek(SeekFrom::Current(8))
277-
.expect("broken index file");
278-
266+
let (id, pos) = self.index.next()?;
279267
self.log
280-
.seek(SeekFrom::Start(self.pos))
268+
.seek(SeekFrom::Start(pos))
281269
.expect("unable to seek to the iterator position");
282270

283271
let mut reader = StrictReader::with(StreamReader::new::<{ usize::MAX }>(&mut *self.log));
284272
let item = V::strict_decode(&mut reader).ok()?;
285273

286-
self.pos = self
287-
.log
288-
.stream_position()
289-
.expect("unable to retrieve log position");
290-
291274
Some((id.into(), item))
292275
}
293276
}

0 commit comments

Comments
 (0)