-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathstrategy.rs
More file actions
73 lines (67 loc) · 1.86 KB
/
strategy.rs
File metadata and controls
73 lines (67 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
use std::fmt::Write;
use super::config::KvNamespace;
use super::kvtime::KVTime;
use ds::RingSlice;
use protocol::kv::Strategy;
use protocol::vector::Postfix;
use sharding::distribution::DBRange;
use sharding::hash::Hasher;
#[derive(Debug, Clone)]
pub enum Strategist {
KVTime(KVTime),
}
impl Strategy for Strategist {
#[inline]
fn distribution(&self) -> &DBRange {
match self {
Strategist::KVTime(inner) => Strategy::distribution(inner),
}
}
#[inline]
fn hasher(&self) -> &Hasher {
match self {
Strategist::KVTime(inner) => Strategy::hasher(inner),
}
}
#[inline]
fn get_key(&self, key: &RingSlice) -> u16 {
match self {
Strategist::KVTime(inner) => Strategy::get_key(inner, key),
}
}
#[inline]
fn tablename_len(&self) -> usize {
match self {
Strategist::KVTime(inner) => Strategy::tablename_len(inner),
}
}
#[inline]
fn write_database_table(&self, buf: &mut impl Write, key: &RingSlice) {
match self {
Strategist::KVTime(inner) => Strategy::write_database_table(inner, buf, key),
}
}
}
impl Default for Strategist {
#[inline]
fn default() -> Self {
Self::KVTime(KVTime::new(
"status".to_string(),
32u32,
8u32,
Postfix::YYMMDD,
))
}
}
impl Strategist {
pub fn try_from(ns: &KvNamespace) -> Option<Self> {
let table_postfix = ns.basic.table_postfix.as_str().try_into().ok()?;
Some(Self::KVTime(KVTime::new(
ns.basic.db_name.clone(),
ns.basic.db_count,
//此策略默认所有年都有同样的shard,basic也只配置了一项,也暗示了这个默认
ns.backends.iter().next().unwrap().1.len() as u32,
table_postfix,
)))
}
}