Implementation:Apache Shardingsphere YamlRuleNodeTupleSwapperEngine Swap
| Knowledge Sources | |
|---|---|
| Domains | Mode_Management, Configuration |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
YamlRuleNodeTupleSwapperEngine is a bidirectional conversion engine between YAML rule configurations and RuleNodeTuple objects for registry persistence.
Description
YamlRuleNodeTupleSwapperEngine handles the conversion of YAML rule configurations to registry node tuples and vice versa. For forward conversion (config-to-tuples), it reflects on annotated fields, distinguishing between named items (Map/Collection with key generators) and unique items (scalar values, collections, enums). Named items produce one tuple per entry, while unique items produce a single tuple. For reverse conversion (tuples-to-config), it instantiates a new YamlRuleConfiguration and populates fields by matching tuple paths against DatabaseRuleNodePath patterns. The engine supports global rule configs as single leaf tuples and database-level configs as multi-tuple structures. It handles field types including String, boolean, int, long, Enum, Collection, Map, and complex YAML-serializable objects.
Usage
Use this engine when persisting rule configurations to the registry (converting config objects to path/value tuples) or when loading rule configurations from the registry (assembling tuples back into config objects). It is central to the configuration persistence pipeline.
Code Reference
Source Location
- Repository: Apache_Shardingsphere
- File: YamlRuleNodeTupleSwapperEngine.java
- Lines: 1-238
Signature
public final class YamlRuleNodeTupleSwapperEngine {
public RuleNodeTuple swapToTuple(final YamlGlobalRuleConfiguration yamlGlobalRuleConfig)
public Collection<RuleNodeTuple> swapToTuples(final String databaseName,
final YamlRuleConfiguration yamlRuleConfig)
public YamlRuleConfiguration swapToYamlGlobalRuleConfiguration(
final String ruleType, final String ruleContent)
public YamlRuleConfiguration swapToYamlDatabaseRuleConfiguration(
final String databaseName, final String ruleType, final Collection<RuleNodeTuple> tuples)
}
Import
import org.apache.shardingsphere.mode.node.rule.tuple.YamlRuleNodeTupleSwapperEngine;
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| yamlGlobalRuleConfig | YamlGlobalRuleConfiguration | Yes | Global rule configuration to convert to a single tuple |
| databaseName | String | Yes | Database name for database-level rule tuple paths |
| yamlRuleConfig | YamlRuleConfiguration | Yes | Database rule configuration to convert to tuples |
| ruleType | String | Yes | Rule type for reverse conversion lookups |
| ruleContent | String | Yes | YAML content for global rule deserialization |
| tuples | Collection<RuleNodeTuple> | Yes | Tuples to assemble back into a rule configuration |
Outputs
| Name | Type | Description |
|---|---|---|
| tuple | RuleNodeTuple | Single tuple for global rule configuration |
| tuples | Collection<RuleNodeTuple> | Collection of tuples for database rule configuration |
| yamlRuleConfig | YamlRuleConfiguration | Reassembled YAML rule configuration from tuples |
Usage Examples
YamlRuleNodeTupleSwapperEngine engine = new YamlRuleNodeTupleSwapperEngine();
// Convert database rule config to tuples for persistence
Collection<RuleNodeTuple> tuples = engine.swapToTuples("my_db", yamlShardingConfig);
// Convert tuples back to YAML config during loading
YamlRuleConfiguration config = engine.swapToYamlDatabaseRuleConfiguration(
"my_db", "sharding", tuples);
// Convert global rule config to a single tuple
RuleNodeTuple globalTuple = engine.swapToTuple(yamlAuthorityConfig);