Skip to content

Commit

Permalink
Refactor ruleId to String
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 29, 2024
1 parent 54c4cd8 commit e53aba4
Show file tree
Hide file tree
Showing 12 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public class TrafficEvent {
/**
* The ID of the unit rule associated with the event.
*/
private final Long unitRuleId;
private final String unitRuleId;

/**
* The ID of the lane space associated with the event.
Expand All @@ -118,7 +118,7 @@ public class TrafficEvent {
/**
* The ID of the lane rule associated with the event.
*/
private final Long laneRuleId;
private final String laneRuleId;

/**
* The local unit involved in the traffic event.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class LiveMetadata {
/**
* The unit rule ID applicable to this invocation.
*/
private Long unitRuleId;
private String unitRuleId;

/**
* The unit rule applicable to this invocation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public LiveMetadata parse() {
Unit centerUnit = liveSpace == null ? null : liveSpace.getCenter();
Unit currentUnit = liveSpace == null ? null : liveSpace.getUnit(application.getLocation().getUnit());
Cell currentCell = currentUnit == null ? null : currentUnit.getCell(application.getLocation().getCell());
Long unitRuleId = parseRuleId(liveConfig.getRuleIdKey());
String unitRuleId = parseRuleId(liveConfig.getRuleIdKey());
UnitRule unitRule = liveSpace == null || unitRuleId == null ? null : liveSpace.getUnitRule(unitRuleId);
String variable = parseVariable();
builder.liveConfig(liveConfig).
Expand Down Expand Up @@ -118,16 +118,9 @@ protected LiveSpace parseLiveSpace() {
* @param key The key used to retrieve the rule ID from the request context.
* @return The parsed rule ID as a Long, or null if the rule ID is not found or is not a valid number.
*/
protected Long parseRuleId(String key) {
protected String parseRuleId(String key) {
Cargo cargo = RequestContext.getCargo(key);
String value = cargo == null ? null : cargo.getFirstValue();
if (value != null) {
try {
return Long.valueOf(value);
} catch (NumberFormatException ignore) {
}
}
return null;
return cargo == null ? null : cargo.getFirstValue();
}

/**
Expand Down Expand Up @@ -234,7 +227,7 @@ public LiveMetadata parse() {
String variableName = null;
String sourceName = null;
PolicyId policyId = null;
Long unitRuleId = null;
String unitRuleId = null;
if (path != null) {
policyId = variableRule != null ? variableRule : path;
unitRuleId = variableRule != null ? variableRule.getRuleId() : path.getRuleId();
Expand Down Expand Up @@ -289,7 +282,7 @@ protected void inject(LiveDomainMetadata metadata) {
Carrier carrier = RequestContext.getOrCreate();
if (unitRule != null) {
carrier.setCargo(liveConfig.getSpaceIdKey(), liveSpace.getId());
carrier.setCargo(liveConfig.getRuleIdKey(), String.valueOf(unitRule.getId()));
carrier.setCargo(liveConfig.getRuleIdKey(), unitRule.getId());
carrier.setCargo(liveConfig.getVariableKey(), metadata.getVariable());
} else {
carrier.removeCargo(liveConfig.getSpaceIdKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public class LanePath implements Path {

private PathMatchType matchType = PathMatchType.PREFIX;

private long ruleId;
private String ruleId;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
@Setter
public class LaneRule {

private long id;
private String id;

private Map<String, TagGroup> conditions;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public class LaneSpace {

private final transient Cache<String, LaneDomain> domainCache = new MapCache<>(new ListBuilder<>(() -> domains, LaneDomain::getHost));

private final transient Cache<Long, LaneRule> ruleCache = new MapCache<>(new ListBuilder<>(() -> rules, LaneRule::getId));
private final transient Cache<String, LaneRule> ruleCache = new MapCache<>(new ListBuilder<>(() -> rules, LaneRule::getId));

@Getter
private transient Lane defaultLane;

public LaneRule getLaneRule(Long id) {
public LaneRule getLaneRule(String id) {
return id == null ? null : ruleCache.get(id);
}

Expand All @@ -72,7 +72,7 @@ public void cache() {
}
getLane("");
getDomain("");
getLaneRule(0L);
getLaneRule("");
if (domains != null) {
domains.forEach(LaneDomain::cache);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class LivePath extends PolicyId implements Path {
@Getter
@Setter
@JsonAlias("unitRuleId")
private long ruleId;
private String ruleId;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public LiveVariable getVariable(String name) {
return spec == null ? null : spec.getVariable(name);
}

public UnitRule getUnitRule(Long id) {
public UnitRule getUnitRule(String id) {
return spec == null ? null : spec.getUnitRule(id);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public LiveSpec(String id, String code, String name, String tenantId) {
private final transient Cache<String, Unit> unitCache = new MapCache<>(new ListBuilder<>(() -> units, Unit::getCode));
private final transient Cache<String, LiveDomain> domainCache = new MapCache<>(new ListBuilder<>(() -> domains, LiveDomain::getHost));
private final transient Cache<String, LiveVariable> variableCache = new MapCache<>(new ListBuilder<>(() -> variables, LiveVariable::getName));
private final transient Cache<Long, UnitRule> unitRuleCache = new MapCache<>(new ListBuilder<>(() -> unitRules, rule -> {
private final transient Cache<String, UnitRule> unitRuleCache = new MapCache<>(new ListBuilder<>(() -> unitRules, rule -> {
List<UnitRoute> unitRoutes = rule.getUnitRoutes();
if (unitRoutes != null) {
for (UnitRoute unitRoute : unitRoutes) {
Expand Down Expand Up @@ -123,7 +123,7 @@ public LiveVariable getVariable(String name) {
return variableCache.get(name);
}

public UnitRule getUnitRule(Long id) {
public UnitRule getUnitRule(String id) {
return id == null ? null : unitRuleCache.get(id);
}

Expand All @@ -135,7 +135,7 @@ public void cache() {
getUnit("");
getDomain("");
getVariable("");
getUnitRule(0L);
getUnitRule("");
getCenter();
if (units != null) {
units.forEach(Unit::cache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LiveVariableRule extends PolicyId {

private String value;

private long ruleId;
private String ruleId;

private String ruleName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
public class UnitRule {
@Getter
@Setter
private long id;
private String id;

@Getter
@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ public class EventExporter implements Subscription<TrafficEvent>, ExtensionIniti

private static final AttributeKey<String> ATTRIBUTE_LIVE_SPACE_ID = AttributeKey.stringKey(KEY_LIVE_SPACE_ID);

private static final AttributeKey<Long> ATTRIBUTE_LIVE_RULE_ID = AttributeKey.longKey(KEY_LIVE_RULE_ID);
private static final AttributeKey<String> ATTRIBUTE_LIVE_RULE_ID = AttributeKey.stringKey(KEY_LIVE_RULE_ID);

private static final AttributeKey<String> ATTRIBUTE_LANE_SPACE_ID = AttributeKey.stringKey(KEY_LANE_SPACE_ID);

private static final AttributeKey<Long> ATTRIBUTE_LANE_RULE_ID = AttributeKey.longKey(KEY_LANE_RULE_ID);
private static final AttributeKey<String> ATTRIBUTE_LANE_RULE_ID = AttributeKey.stringKey(KEY_LANE_RULE_ID);

private static final AttributeKey<String> ATTRIBUTE_LOCAL_UNIT = AttributeKey.stringKey(KEY_LOCAL_UNIT);

Expand Down

0 comments on commit e53aba4

Please sign in to comment.