Skip to content

Commit

Permalink
[#83] data directory 설정, config 전달 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
myyrakle committed Jun 20, 2024
1 parent 420d493 commit 2bd6f8f
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 78 deletions.
18 changes: 2 additions & 16 deletions src/command/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,8 @@ use clap::Args;
/// Config options for the build system.
#[derive(Clone, Debug, Default, Deserialize, Args)]
pub struct ConfigOptions {
#[clap(
name = "port",
default_value = "55555",
long,
short,
help = "Port to listen on"
)]
pub port: u32,

#[clap(
name = "host",
default_value = "0.0.0.0",
long,
help = "Hostname to listen on (IP or domain)"
)]
pub host: String,
#[clap(name = "config", long, help = "config file path")]
pub config: Option<String>,
}

#[derive(Clone, Debug, Args)]
Expand Down
2 changes: 1 addition & 1 deletion src/executor/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Executor {
pub async fn get_table_config(&self, table_name: TableName) -> Result<TableConfig, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

let TableName {
database_name,
Expand Down
30 changes: 29 additions & 1 deletion src/executor/config/global.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::path::PathBuf;

use serde::{Deserialize, Serialize};

use crate::constants::{DEFAULT_CONFIG_BASEPATH, DEFAULT_CONFIG_FILENAME, DEFAULT_DATA_DIRNAME};

#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct GlobalConfig {
pub port: u32,
Expand All @@ -10,10 +14,34 @@ pub struct GlobalConfig {
#[allow(clippy::derivable_impls)]
impl std::default::Default for GlobalConfig {
fn default() -> Self {
let base_path = PathBuf::from(DEFAULT_CONFIG_BASEPATH);

Self {
port: 55555,
host: "0.0.0.0".to_string(),
data_directory: "data".to_string(),
data_directory: base_path
.join(DEFAULT_DATA_DIRNAME)
.to_str()
.unwrap()
.to_string(),
}
}
}

impl GlobalConfig {
pub fn load_from_path(filepath: Option<String>) -> Self {
let filepath = match filepath {
Some(path) => PathBuf::from(path),
None => {
let path = PathBuf::new();
let path = path.join(DEFAULT_CONFIG_BASEPATH);
let path = path.join(DEFAULT_CONFIG_FILENAME);

path
}
};

let config = std::fs::read_to_string(filepath).unwrap();
toml::from_str(&config).unwrap()
}
}
13 changes: 5 additions & 8 deletions src/executor/executor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::path::PathBuf;
use std::sync::Arc;

use crate::ast::ddl::create_database::CreateDatabaseQuery;
use crate::ast::{DDLStatement, DMLStatement, OtherStatement, SQLStatement};
Expand All @@ -12,17 +13,13 @@ use crate::logger::predule::Logger;

use super::config::global::GlobalConfig;

pub struct Executor {}

impl Default for Executor {
fn default() -> Self {
Self::new()
}
pub struct Executor {
pub(crate) config: Arc<GlobalConfig>,
}

impl Executor {
pub fn new() -> Self {
Self {}
pub fn new(config: Arc<GlobalConfig>) -> Self {
Self { config: config }
}

// 기본 설정파일 세팅
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/ddl/alter_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Executor {
) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

#[allow(clippy::single_match)]
match query.action {
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/ddl/alter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl Executor {
pub async fn alter_table(&self, query: AlterTableQuery) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

let TableName {
database_name,
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/ddl/create_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Executor {
) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

let database_name = query
.database_name
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/ddl/create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Executor {
let database_name = query.table.clone().unwrap().database_name.unwrap();
let table_name = query.table.clone().unwrap().table_name;

let base_path = self.get_base_path();
let base_path = self.get_data_directory();
let database_path = base_path.clone().join(&database_name);

let table_path = database_path.clone().join("tables").join(&table_name);
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/ddl/drop_database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl Executor {
&self,
query: DropDatabaseQuery,
) -> Result<ExecuteResult, RRDBError> {
let base_path = self.get_base_path();
let base_path = self.get_data_directory();
let mut database_path = base_path.clone();

let database_name = query
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/ddl/drop_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::executor::result::{ExecuteColumn, ExecuteColumnType, ExecuteField, Ex

impl Executor {
pub async fn drop_table(&self, query: DropTableQuery) -> Result<ExecuteResult, RRDBError> {
let base_path = self.get_base_path();
let base_path = self.get_data_directory();

let TableName {
database_name,
Expand Down
6 changes: 2 additions & 4 deletions src/executor/implements/dml/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Executor {
let database_name = into_table.clone().database_name.unwrap();
let table_name = into_table.clone().table_name;

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

let database_path = base_path.clone().join(&database_name);

Expand Down Expand Up @@ -92,9 +92,7 @@ impl Executor {

let default_value = match &column_config_info.default {
Some(default) => default.to_owned(),
None => {
SQLExpression::Null
}
None => SQLExpression::Null,
};

let value = value.list[i].clone().unwrap_or(default_value);
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/dml/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl Executor {
let database_name = table_name.database_name.unwrap();
let table_name = table_name.table_name;

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

let database_path = base_path.clone().join(&database_name);

Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/other/desc_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ impl Executor {
let database_name = query.table_name.database_name.unwrap();
let table_name = query.table_name.table_name;

let base_path = self.get_base_path();
let base_path = self.get_data_directory();
let table_path = base_path
.join(&database_name)
.join("tables")
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/other/show_databases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl Executor {
) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
let base_path = self.get_data_directory();

match std::fs::read_dir(&base_path) {
Ok(read_dir_result) => {
Expand Down
2 changes: 1 addition & 1 deletion src/executor/implements/other/show_tables.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl Executor {
pub async fn show_tables(&self, query: ShowTablesQuery) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
let base_path = self.get_data_directory();
let database_path = base_path.clone().join(query.database);
let tables_path = database_path.join("tables");

Expand Down
6 changes: 3 additions & 3 deletions src/executor/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::path::PathBuf;
use crate::executor::predule::Executor;

impl Executor {
// 데이터 저장 경로를 지정합니다.
pub fn get_base_path(&self) -> PathBuf {
PathBuf::from(get_target_basepath())
// 데이터 저장 경로를 반환합니다..
pub fn get_data_directory(&self) -> PathBuf {
PathBuf::from(self.config.data_directory.clone())
}
}
16 changes: 8 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ pub mod pgwire;
pub mod server;
pub mod utils;

use std::sync::Arc;

use command::{Command, SubCommand};
use errors::RRDBError;
use executor::predule::Executor;
use server::predule::{Server, ServerOption};
use executor::{config::global::GlobalConfig, predule::Executor};
use server::predule::Server;

use clap::Parser;

Expand All @@ -26,16 +28,14 @@ async fn main() -> Result<(), RRDBError> {
SubCommand::Init(init) => {
let _init_option = init.init;

let executor = Executor::new();
let executor = Executor::new(Arc::new(GlobalConfig::default()));

executor.init().await?;
}
SubCommand::Run(run) => {
let server_option = ServerOption {
port: run.value.port,
host: run.value.host,
};
let server = Server::new(server_option);
let config = GlobalConfig::load_from_path(run.value.config);

let server = Server::new(config);

server.run().await?;
}
Expand Down
10 changes: 6 additions & 4 deletions src/pgwire/connection/connection.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Contains the [Connection] struct, which represents an individual Postgres session, and related types.
use futures::{SinkExt, StreamExt};
use std::collections::HashMap;
use std::{collections::HashMap, sync::Arc};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed;

use crate::{
ast::{OtherStatement, SQLStatement},
executor::executor::Executor,
executor::{config::global::GlobalConfig, executor::Executor},
logger::predule::Logger,
parser::{context::ParserContext, predule::Parser},
pgwire::{
Expand All @@ -33,11 +33,12 @@ pub struct Connection {
state: ConnectionState,
statements: HashMap<String, PreparedStatement>,
portals: HashMap<String, Option<BoundPortal<RRDBEngine>>>,
config: Arc<GlobalConfig>,
}

impl Connection {
/// Create a new connection from an engine instance.
pub fn new(shared_state: SharedState) -> Self {
pub fn new(shared_state: SharedState, config: Arc<GlobalConfig>) -> Self {
Self {
state: ConnectionState::Startup,
statements: HashMap::new(),
Expand All @@ -46,6 +47,7 @@ impl Connection {
shared_state,
portal: None,
},
config: config,
}
}

Expand Down Expand Up @@ -107,7 +109,7 @@ impl Connection {
ClientMessage::Startup(startup) => {
if let Some(database_name) = startup.parameters.get("database") {
// 해당 데이터베이스가 존재하는지 검사
let executor = Executor::new();
let executor = Executor::new(self.config.clone());
let result = executor.find_database(database_name.clone()).await;

match result {
Expand Down
1 change: 0 additions & 1 deletion src/server/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
pub mod channel;
pub mod client;
pub mod option;
pub mod predule;
#[allow(clippy::module_inception)]
pub mod server;
Expand Down
13 changes: 0 additions & 13 deletions src/server/option.rs

This file was deleted.

1 change: 0 additions & 1 deletion src/server/predule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub use super::channel::*;
pub use super::client::*;
pub use super::option::*;
pub use super::server::*;
pub use super::shared_state::*;
Loading

0 comments on commit 2bd6f8f

Please sign in to comment.