Skip to content

Commit b98171f

Browse files
committed
More code
1 parent 76612b9 commit b98171f

File tree

13 files changed

+303
-47
lines changed

13 files changed

+303
-47
lines changed

Cargo.lock

Lines changed: 151 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ tracing-subscriber = { version = "0.3.19", features = ["env-filter", "json"] }
1212
serde = { version = "1.0.219", features = ["derive"] }
1313
serde_json = "1.0.140"
1414
sqlx = { version = "0.8.3", features = ["postgres", "sqlite", "runtime-tokio"]}
15-
log = "0.4.26"
15+
log = "0.4.26"
16+
chrono = "0.4.40"

config.json

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,17 @@
33
"level": "debug",
44
"json": false
55
},
6-
"kvstorage_type": "sqlite",
7-
"sqlite": {
8-
"path": "kv.db",
9-
"pool_size": 10
10-
},
11-
"locks_type": "memory"
6+
"buckets": [
7+
{
8+
"name": "bucket1",
9+
"address": "0.0.0.0",
10+
"port": 3000,
11+
"kvstorage_type": "sqlite",
12+
"sqlite": {
13+
"path": "kv.db",
14+
"pool_size": 10
15+
},
16+
"locks_type": "memory"
17+
}
18+
]
1219
}

src/config.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ use crate::logging::LoggingConfig;
88
#[derive(Debug, serde::Deserialize, Clone)]
99
pub struct Config {
1010
pub logging: LoggingConfig,
11+
pub buckets: Vec<BucketConfig>,
12+
}
13+
14+
#[derive(Debug, serde::Deserialize, Clone)]
15+
pub struct BucketConfig {
16+
pub name: String,
17+
pub address: String,
18+
pub port: u16,
1119

1220
pub kvstorage_type: KVStorageType,
1321

src/kvstorage/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::Config;
1+
use crate::config::BucketConfig;
22
use serde::Deserialize;
33
use std::error::Error;
44
use tracing::{debug, info};
@@ -16,7 +16,7 @@ pub enum KVStorageType {
1616
}
1717

1818
pub(crate) trait KVStorageTrait {
19-
async fn new(config: &Config) -> Result<Box<Self>, Box<dyn Error>>
19+
async fn new(config: &BucketConfig) -> Result<Box<Self>, Box<dyn Error>>
2020
where
2121
Self: Sized;
2222

@@ -72,7 +72,7 @@ pub enum KVStorage {
7272
}
7373

7474
impl KVStorage {
75-
pub async fn new(config: &Config) -> Result<Box<Self>, Box<dyn Error>> {
75+
pub async fn new(config: &BucketConfig) -> Result<Box<Self>, Box<dyn Error>> {
7676
match config.kvstorage_type {
7777
KVStorageType::Postgres => {
7878
info!("Using Postgres as KV storage");

src/kvstorage/postgres.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::config::Config;
1+
use crate::config::BucketConfig;
22
use crate::kvstorage::KVStorageTrait;
33
use crate::kvstorage::pooled::{RowModified, RowRefFile, RowRefcount};
44
use serde::Deserialize;
@@ -23,7 +23,7 @@ pub struct Postgres {
2323
}
2424

2525
impl KVStorageTrait for Postgres {
26-
async fn new(config: &Config) -> Result<Box<Self>, Box<dyn Error>> {
26+
async fn new(config: &BucketConfig) -> Result<Box<Self>, Box<dyn Error>> {
2727
let pg_config = config.postgres.as_ref().unwrap();
2828
let db_url = format!(
2929
"postgres://{}:{}@{}:{}/{}",

src/kvstorage/sqlite.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::path::Path;
2-
use crate::config::Config;
2+
use crate::config::BucketConfig;
33
use crate::kvstorage::KVStorageTrait;
44
use crate::kvstorage::pooled::{RowModified, RowRefFile, RowRefcount};
55
use serde::Deserialize;
@@ -19,7 +19,7 @@ pub struct SQLite {
1919
}
2020

2121
impl KVStorageTrait for SQLite {
22-
async fn new(config: &Config) -> Result<Box<Self>, Box<dyn std::error::Error>> {
22+
async fn new(config: &BucketConfig) -> Result<Box<Self>, Box<dyn std::error::Error>> {
2323
let sqlite_config = config.sqlite.as_ref().unwrap();
2424

2525
if !Path::new(&sqlite_config.path).exists() {

src/locks/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ use tracing::{debug, info};
33

44
pub mod memory;
55

6+
pub(crate) fn file_lock(bucket: &str, path: &str) -> String {
7+
format!("file:{}:{}", bucket, path)
8+
}
9+
10+
fn hash_lock(bucket: &str, hash: &str) -> String {
11+
format!("hash:{}:{}", bucket, hash)
12+
}
13+
614
#[derive(Debug, Deserialize, Clone)]
715
pub(crate) enum LocksType {
816
#[serde(rename = "memory")]

0 commit comments

Comments
 (0)