Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(pisa-proxy, sharding): add generic_meta.rs #312

Merged
merged 1 commit into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 128 additions & 0 deletions pisa-proxy/proxy/strategy/src/sharding_rewrite/generic_meta.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
// Copyright 2022 SphereEx Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::config::{StrategyType, Sharding, ShardingAlgorithmName};

pub trait ShardingMeta {
fn get_sharding_column(&self) -> (Option<&str>, Option<&str>);
fn get_algo(&self) -> (Option<&ShardingAlgorithmName>, Option<&ShardingAlgorithmName>);
fn get_sharding_count(&self) -> (Option<u64>, Option<u64>);
}

/// Todo: use macro generate
impl ShardingMeta for Sharding {
fn get_sharding_column(&self) -> (Option<&str>, Option<&str>) {
if let Some(strategy) = &self.database_strategy {
return strategy.get_sharding_column()
}

if let Some(strategy) = &self.table_strategy {
return strategy.get_sharding_column()
}

if let Some(strategy) = &self.database_table_strategy {
return strategy.get_sharding_column()
}

(None, None)
}

fn get_algo(&self) -> (Option<&ShardingAlgorithmName>, Option<&ShardingAlgorithmName>) {
if let Some(strategy) = &self.database_strategy {
return strategy.get_algo()
}

if let Some(strategy) = &self.table_strategy {
return strategy.get_algo()
}

if let Some(strategy) = &self.database_table_strategy {
return strategy.get_algo()
}

(None, None)
}

fn get_sharding_count(&self) -> (Option<u64>, Option<u64>) {
if let Some(_) = &self.database_strategy {
return (Some(self.actual_datanodes.len() as u64), None)
}

if let Some(strategy) = &self.table_strategy {
return (None, strategy.get_sharding_count().1)
}

if let Some(strategy) = &self.database_table_strategy {
return (Some(self.actual_datanodes.len() as u64), strategy.get_sharding_count().1)
}

(None, None)
}
}

impl ShardingMeta for StrategyType {
fn get_sharding_column(&self) -> (Option<&str>, Option<&str>) {
match self {
Self::DatabaseStrategyConfig(config) => {
(Some(&config.database_sharding_column), None)
},

Self::DatabaseTableStrategyConfig(config) => {
(Some(&config.database_sharding_column), Some(&config.table_sharding_column))
},

Self::TableStrategyConfig(config) => {
(None, Some(&config.table_sharding_column))
},

_ => (None, None)
}
}

fn get_algo(&self) -> (Option<&ShardingAlgorithmName>, Option<&ShardingAlgorithmName>) {
match self {
Self::DatabaseStrategyConfig(config) => {
(Some(&config.database_sharding_algorithm_name), None)
},

Self::DatabaseTableStrategyConfig(config) => {
(Some(&config.database_sharding_algorithm_name), Some(&config.table_sharding_algorithm_name))
},

Self::TableStrategyConfig(config) => {
(None, Some(&config.table_sharding_algorithm_name))
},

_ => (None, None)
}
}

fn get_sharding_count(&self) -> (Option<u64>, Option<u64>) {
match self {
Self::DatabaseStrategyConfig(_) => {
unimplemented!()
},

Self::DatabaseTableStrategyConfig(config) => {
(None, Some(config.shading_count.into()))
},

Self::TableStrategyConfig(config) => {
(None, Some(config.sharding_count.into()))
},

_ => (None, None)
}
}
}
4 changes: 2 additions & 2 deletions pisa-proxy/proxy/strategy/src/sharding_rewrite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// limitations under the License.

mod meta;
mod genric_meta;
mod generic_meta;

use std::vec;

Expand All @@ -23,7 +23,7 @@ use mysql_parser::ast::{SqlStmt, Visitor, TableIdent};

use self::{meta::{
FieldMeta, InsertValsMeta, RewriteMetaData, WhereMeta, WhereMetaRightDataType,
}, genric_meta::ShardingMeta};
}, generic_meta::ShardingMeta};
use crate::{
config::{Sharding, ShardingAlgorithmName, StrategyType},
rewrite::{ShardingRewriteInput, ShardingRewriter},
Expand Down