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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use super::*;
impl BasiliqStoreBuilder {
/// Generate the configuration for the current [BasiliqStoreBuilder](BasiliqStoreBuilder)
pub(crate) fn gen_config(&self) -> BasiliqStoreConfig {
let mut resources: BTreeMap<String, BasiliqStoreResourceConfig> = BTreeMap::new();
for (alias, (table_ident, table_builder)) in
self.aliases().right_values().zip(self.tables().iter())
{
if resources.contains_key(alias) {
tracing::warn!("Duplicate resource name `{}`", alias);
continue;
}
let relationships: BTreeMap<ArcStr, BasiliqStoreRelationshipsConfig> = table_builder
.relationships
.iter()
.map(|(k, v)| {
(
k.clone(),
BasiliqStoreRelationshipsConfig {
target: BasiliqStoreTableIdentifier {
schema: v.ftable().schema().clone(),
table: v.ftable().table().clone(),
},
through: match v.type_() {
BasiliqStoreRelationshipType::ManyToMany(x) => {
Some(BasiliqStoreRelationshipsThroughConfig {
target: x.bucket().clone(),
field: x.ffield_name().clone(),
})
}
_ => None,
},
enabled: true,
field: v.ffield_name().clone(),
},
)
})
.collect();
resources.insert(
alias.clone(),
BasiliqStoreResourceConfig {
target: BasiliqStoreTableIdentifier {
schema: table_ident.schema().clone(),
table: table_ident.table().clone(),
},
relationships,
enabled: true,
},
);
}
BasiliqStoreConfig { resources }
}
}
impl BasiliqStoreConfigMergeable<BasiliqStoreConfig> for BasiliqStoreBuilder {
fn basiliq_config_merge(
&mut self,
other: &BasiliqStoreConfig,
) -> Result<(), BasiliqStoreConfigError> {
for (resource_name, resource_cfg) in other.resources() {
let table_ident = BasiliqStoreTableIdentifier::from(resource_cfg);
self.aliases_mut()
.insert(table_ident.clone(), resource_name.clone());
match self.tables().get(&table_ident) {
Some(table) => {
let mut new_rel: BTreeMap<ArcStr, BasiliqStoreRelationshipData> =
table.relationships().clone();
for x in table.relationships().iter().merge_join_by(
resource_cfg.relationships().iter(),
|(_k1, v1), (_k2, v2)| v1.ftable().cmp(v2.target()),
) {
match x {
EitherOrBoth::Both((k1, _v1), (k2, _v2)) => {
// Rename to the new alias name
new_rel
.remove(k1.as_str())
.and_then(|x| new_rel.insert(k2.clone(), x));
}
EitherOrBoth::Left((_, v1)) => {
// It's an error to have a resource we don't know about in the auto-generated configuration
return Err(BasiliqStoreConfigError::UnkownResource(
BasiliqStoreConfigErrorSource::BaseConfig,
v1.ltable().clone(),
));
}
EitherOrBoth::Right((_, v2)) => {
// It's an error to have a resource we don't know about in the provided configuration
return Err(BasiliqStoreConfigError::UnkownResource(
BasiliqStoreConfigErrorSource::ProvidedConfig,
v2.target().clone(),
));
}
};
}
if let Some(table) = self.tables_mut().get_mut(&table_ident) {
table.relationships = new_rel
}
}
None => {
return Err(BasiliqStoreConfigError::UnkownResource(
BasiliqStoreConfigErrorSource::ProvidedConfig,
table_ident.clone(),
))
}
}
}
self.config = other.clone();
Ok(())
}
}