Skip to content

Commit 80396a9

Browse files
committed
feat: improve size calc
1 parent 666be6a commit 80396a9

File tree

2 files changed

+33
-38
lines changed

2 files changed

+33
-38
lines changed

src/conn.rs

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,58 +21,43 @@ impl Connection {
2121
headers.get(key).and_then(|h| h.to_str().ok())
2222
}
2323

24-
fn get_length(rt: AtomicRuntime, url: String) -> Result<usize, reqwest::Error> {
25-
let Some(response) = rt
24+
fn get_length(rt: AtomicRuntime, url: String) -> Option<usize> {
25+
if let Some(response) = rt
2626
.block_on(move |client| client.get(url).send())
2727
.and_then(|r| r.ok())
28-
else {
29-
return Ok(0);
30-
};
31-
32-
let headers = response.headers();
33-
let Some(accept_range) = Self::get_header(headers, ACCEPT_RANGES) else {
34-
return Ok(0);
35-
};
36-
37-
if accept_range != "bytes" {
38-
return Ok(0);
28+
{
29+
let headers = response.headers();
30+
if let Some(accept_range) = Self::get_header(headers, ACCEPT_RANGES) {
31+
if accept_range == "bytes" {
32+
let length = Self::get_header(headers, CONTENT_LENGTH)
33+
.and_then(|s| s.parse().ok())
34+
.unwrap_or_default();
35+
return Some(length);
36+
}
37+
}
3938
}
40-
41-
let length = Self::get_header(headers, CONTENT_LENGTH)
42-
.and_then(|s| s.parse().ok())
43-
.unwrap_or_default();
44-
Ok(length)
39+
None
4540
}
4641

4742
fn init_with_url(url: &str) -> Result<(AtomicRuntime, usize), Error> {
4843
let rt = AtomicRuntime::default();
4944
match Self::get_length(rt.clone(), url.to_string()) {
50-
Ok(size) => {
51-
if size != 0 {
52-
Ok((rt, size))
53-
} else {
54-
rt.drop();
55-
Err(Error::new(
56-
ErrorKind::InvalidData,
57-
"database size is not a multiple of page size",
58-
))
59-
}
60-
}
61-
Err(e) => {
45+
Some(size) if size != 0 => Ok((rt, size)),
46+
_ => {
6247
rt.drop();
6348
Err(Error::new(
6449
ErrorKind::Other,
65-
format!("Failed to initialize db: {e}"),
50+
"Failed to check database size",
6651
))
6752
}
6853
}
6954
}
7055

71-
pub fn new(url: &str) -> Result<Self, Error> {
72-
let (rt, size) = Self::init_with_url(url)?;
56+
pub fn new(url: &str, block_size: usize) -> Result<Self, Error> {
57+
let (rt, length) = Self::init_with_url(url)?;
7358
let buffer = LazyBuffer::new(
74-
size,
75-
1024 * 1024 * 10,
59+
length,
60+
block_size,
7661
Box::new({
7762
let url = url.to_string();
7863
let rt = rt.clone();

src/vfs.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@ use std::{
88

99
pub const HTTP_VFS: &str = "http";
1010

11-
#[derive(Default)]
12-
pub struct HttpVfs;
11+
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+
}
22+
}
1323

1424
impl Vfs for HttpVfs {
1525
type Handle = Connection;
@@ -22,7 +32,7 @@ impl Vfs for HttpVfs {
2232
));
2333
}
2434

25-
Ok(Connection::new(db)?)
35+
Ok(Connection::new(db, self.block_size)?)
2636
}
2737

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

0 commit comments

Comments
 (0)