Skip to content

Commit 40552e0

Browse files
committed
feat: add download threshold for faster index scan
1 parent 57ed558 commit 40552e0

File tree

3 files changed

+23
-15
lines changed

3 files changed

+23
-15
lines changed

src/buffer.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,17 @@ type Fetch = Box<dyn Fn(usize, usize) -> Result<Vec<u8>> + Send + Sync>;
3636
pub struct LazyBuffer {
3737
blocks: HashMap<usize, LazyBlock>,
3838
block_size: usize,
39+
download_threshold: usize,
3940
length: usize,
4041
fetch: Arc<Fetch>,
4142
}
4243

4344
impl LazyBuffer {
44-
pub fn new(length: usize, block_size: usize, fetch: Fetch) -> Self {
45+
pub fn new(length: usize, block_size: usize, download_threshold: usize, fetch: Fetch) -> Self {
4546
Self {
4647
blocks: HashMap::new(),
4748
block_size,
49+
download_threshold,
4850
length,
4951
fetch: Arc::new(fetch),
5052
}
@@ -74,7 +76,16 @@ impl LazyBuffer {
7476
}
7577
let block_index = offset / self.block_size;
7678
let block_offset = offset - block_index * self.block_size;
77-
self.get_block(block_index)?.read(buf, block_offset)?;
79+
if !self.blocks.contains_key(&block_index) && buf.len() <= self.download_threshold {
80+
// skip full block fetch if buf is smaller than download_threshold
81+
let data = (self.fetch)(offset, buf.len())?;
82+
if data.len() != buf.len() {
83+
return Err(Error::new(ErrorKind::UnexpectedEof, "read out of bounds"));
84+
}
85+
buf.copy_from_slice(&data);
86+
} else {
87+
self.get_block(block_index)?.read(buf, block_offset)?;
88+
}
7889
Ok(())
7990
}
8091
}
@@ -92,7 +103,7 @@ mod tests {
92103
let block_size = 16;
93104
let total_size = 64;
94105

95-
let mut lazy_buffer = LazyBuffer::new(total_size, block_size, Box::new(mock_fetch));
106+
let mut lazy_buffer = LazyBuffer::new(total_size, block_size, 0, Box::new(mock_fetch));
96107

97108
let mut buf1 = vec![0u8; 16];
98109
lazy_buffer.read(&mut buf1, 0).unwrap();

src/conn.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,12 @@ impl Connection {
5353
}
5454
}
5555

56-
pub fn new(url: &str, block_size: usize) -> Result<Self, Error> {
56+
pub fn new(url: &str, block_size: usize, download_threshold: usize) -> Result<Self, Error> {
5757
let (rt, length) = Self::init_with_url(url)?;
5858
let buffer = LazyBuffer::new(
5959
length,
6060
block_size,
61+
download_threshold,
6162
Box::new({
6263
let url = url.to_string();
6364
let rt = rt.clone();

src/vfs.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,8 @@ use std::{
99
pub const HTTP_VFS: &str = "http";
1010

1111
pub struct HttpVfs {
12-
block_size: usize,
13-
}
14-
15-
impl Default for HttpVfs {
16-
fn default() -> Self {
17-
Self {
18-
// 4MB block size
19-
block_size: 1024 * 4096,
20-
}
21-
}
12+
pub(crate) block_size: usize,
13+
pub(crate) download_threshold: usize,
2214
}
2315

2416
impl Vfs for HttpVfs {
@@ -32,7 +24,11 @@ impl Vfs for HttpVfs {
3224
));
3325
}
3426

35-
Ok(Connection::new(db, self.block_size)?)
27+
Ok(Connection::new(
28+
db,
29+
self.block_size,
30+
self.download_threshold,
31+
)?)
3632
}
3733

3834
fn delete(&self, _db: &str) -> Result<(), Error> {

0 commit comments

Comments
 (0)