Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit f0c6428

Browse files
committed
allow setting max db size on namespace create
1 parent a39ca3b commit f0c6428

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

sqld/src/connection/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::Arc;
55
use std::{fs, io};
66

77
use crate::error::Error;
8-
use crate::Result;
8+
use crate::{Result, LIBSQL_PAGE_SIZE};
99

1010
#[derive(Debug)]
1111
pub struct DatabaseConfigStore {
@@ -29,7 +29,7 @@ pub struct DatabaseConfig {
2929
}
3030

3131
const fn default_max_size() -> u64 {
32-
bytesize::ByteSize::pb(1000).as_u64() / 4096
32+
bytesize::ByteSize::pb(1000).as_u64() / LIBSQL_PAGE_SIZE
3333
}
3434

3535
impl Default for DatabaseConfig {

sqld/src/http/admin/mod.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ use tokio_util::io::ReaderStream;
1212
use url::Url;
1313

1414
use crate::database::Database;
15-
use crate::LIBSQL_PAGE_SIZE;
1615
use crate::error::LoadDumpError;
1716
use crate::hrana;
1817
use crate::namespace::{DumpStream, MakeNamespace, NamespaceName, NamespaceStore, RestoreOption};
18+
use crate::LIBSQL_PAGE_SIZE;
1919

2020
pub mod stats;
2121

@@ -127,11 +127,6 @@ struct HttpDatabaseConfig {
127127
max_db_size: Option<bytesize::ByteSize>,
128128
}
129129

130-
#[derive(Debug, Deserialize)]
131-
struct CreateNamespaceReq {
132-
dump_url: Option<Url>,
133-
}
134-
135130
async fn handle_post_config<M: MakeNamespace>(
136131
State(app_state): State<Arc<AppState<M>>>,
137132
Path(namespace): Path<String>,
@@ -154,6 +149,12 @@ async fn handle_post_config<M: MakeNamespace>(
154149
Ok(())
155150
}
156151

152+
#[derive(Debug, Deserialize)]
153+
struct CreateNamespaceReq {
154+
dump_url: Option<Url>,
155+
max_db_size: Option<bytesize::ByteSize>,
156+
}
157+
157158
async fn handle_create_namespace<M: MakeNamespace>(
158159
State(app_state): State<Arc<AppState<M>>>,
159160
Path(namespace): Path<String>,
@@ -164,10 +165,16 @@ async fn handle_create_namespace<M: MakeNamespace>(
164165
None => RestoreOption::Latest,
165166
};
166167

167-
app_state
168-
.namespaces
169-
.create(NamespaceName::from_string(namespace)?, dump)
170-
.await?;
168+
let namespace = NamespaceName::from_string(namespace)?;
169+
app_state.namespaces.create(namespace.clone(), dump).await?;
170+
171+
if let Some(max_db_size) = req.max_db_size {
172+
let store = app_state.namespaces.config_store(namespace).await?;
173+
let mut config = (*store.get()).clone();
174+
config.max_db_pages = max_db_size.as_u64() / LIBSQL_PAGE_SIZE;
175+
store.store(config)?;
176+
}
177+
171178
Ok(())
172179
}
173180

sqld/src/namespace/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ impl Namespace<PrimaryDatabase> {
728728
.await?;
729729

730730
let db_config_store = Arc::new(
731-
dbg!(DatabaseConfigStore::load(&db_path)).context("Could not load database config")?,
731+
DatabaseConfigStore::load(&db_path).context("Could not load database config")?,
732732
);
733733

734734
let connection_maker: Arc<_> = MakeLibSqlConn::new(

sqld/src/replication/primary/logger.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -859,14 +859,14 @@ impl ReplicationLogger {
859859
let data_file = File::open(&data_path)?;
860860
let size = data_path.metadata()?.len();
861861
assert!(
862-
size % LIBSQL_PAGE_SIZE as u64 == 0,
862+
size % LIBSQL_PAGE_SIZE == 0,
863863
"database file size is not a multiple of page size"
864864
);
865865
let num_page = size / LIBSQL_PAGE_SIZE;
866866
let mut buf = [0; LIBSQL_PAGE_SIZE as usize];
867867
let mut page_no = 1; // page numbering starts at 1
868868
for i in 0..num_page {
869-
data_file.read_exact_at(&mut buf, i * LIBSQL_PAGE_SIZE as u64)?;
869+
data_file.read_exact_at(&mut buf, i * LIBSQL_PAGE_SIZE)?;
870870
log_file.push_page(&WalPage {
871871
page_no,
872872
size_after: if i == num_page - 1 { num_page as _ } else { 0 },

0 commit comments

Comments
 (0)