Skip to content

POC: 脚本字段持久化 - 业务表存 key、领域对象存正文 (17.x) - #235

Closed
xlorne wants to merge 6 commits into
17.3.xfrom
feature/issue-234-script-ref-poc-17x
Closed

xlorne wants to merge 6 commits into
17.3.xfrom
feature/issue-234-script-ref-poc-17x

Conversation

@xlorne

@xlorne xlorne commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

背景

关联 issue #234。本 PR 是 POC,验证「数据库列存 key、业务侧面对脚本正文」的落库方案。

架构

业务领域(不依赖脚本框架)
  Order            —— String rule

业务持久化适配层(只搬运正文)
  OrderEntity      —— 字段类型 GroovyScriptField,列 rule_key 存 key
  OrderRepositoryImpl —— 领域对象 <-> 实体,无 key 逻辑

框架持久化层(springboot-starter-script,persistence 包)
  GroovyScriptRepository        —— 抽象:正文 <-> key,可替换实现
  GroovyScriptRepositoryImpl    —— 默认实现,基于脚本仓储
  GroovyScriptPersistenceContext—— 仓库上下文(非 JPA 实现可替换)
  GroovyScriptKeyGeneratorContext —— key 策略上下文(业务注入,默认 UUID)
  GroovyScriptField             —— 脚本持久化对象(key + 正文)
  GroovyScriptFieldConverter    —— JPA 适配器(autoApply)

关键设计

1. 字段类型即标识

实体字段直接声明为 GroovyScriptField,不再用普通 String 表达 key:

@Column(name = "rule_key", length = 128)
private GroovyScriptField rule;

2. 转换在框架层完成

方向 行为
落库 GroovyScriptField.persist() → GroovyScriptPersistenceContext.save(key, content):key 空则生成,非空则复用;正文写入脚本仓储;返回 key
读取 列里的 key → GroovyScriptPersistenceContext.getScript(key) → 还原成 GroovyScriptField

JPA 侧由 @Converter(autoApply = true) 自动生效,业务不需要 @Convert,也不写转换代码。

3. 抽象仓库,非 JPA 也能用

GroovyScriptRepository 只暴露「正文 ↔ key」,JPA 转换器只是其中一种适配方式。非 JPA 持久化(文档库、自定义表)通过 GroovyScriptPersistenceContext 复用同一套 key 生命周期,已加用例演示。

4. key 生命周期

场景 用法 行为
业务指定 key GroovyScriptField.of(key, content) 直接用业务 key
新建未指定 GroovyScriptField.of(content) 落库时取 GroovyScriptKeyGeneratorContext 策略(默认 UUID)
更新 GroovyScriptField.carry(previous, content) 沿用上一版本 key,只换正文

key 随实体加载还原到字段上,更新时直接沿用,不需要为 key 单独查询;新建也不存在额外查询。

演示(test 范围)

文件 角色
poc/domain/Order、OrderRepository 领域对象与端口,不依赖脚本框架
poc/infra/OrderEntity、OrderJpaRepository JPA 实体与 Spring Data 仓储
poc/infra/OrderRepositoryImpl 领域对象与实体互转,只有正文搬运
poc/GroovyScriptPersistPocTest 7 个用例

验证

  • JDK 17
  • ./mvnw clean test -P travis → BUILD SUCCESS(5 个 starter 模块)
  • ./mvnw test -pl springboot-starter-script → 103 tests,0 failures(新增 7 个)
  • 用例覆盖:列存 key 不是正文、读库回填正文、更新复用 key、业务指定 key 优先、空值保持 null、上下文替换 key 策略、非 JPA 实现复用抽象仓库

对评审意见的处理

评审意见 处理
持久化字段必须是脚本对象或约定枚举 字段类型改为 GroovyScriptField
转换应在框架层,不该业务每个转换器写 autoApply 转换器 + 抽象仓库,业务零 key 代码
先查后存要框架做 key 随实体加载还原,更新直接沿用,无额外查询
需要抽象 GroovyScriptRepository 新增 GroovyScriptRepository + 默认实现 + GroovyScriptPersistenceContext
GroovyScriptKeyGenerator 要有 context 新增 GroovyScriptKeyGeneratorContext,不再参数穿透
非 JPA 持久化也要能复用 抽象仓库可替换,已加非 JPA 用例

待确认

  1. persistence 包目前放在 script 模块(POC 临时落位),正式落地是否直接新开 springboot-starter-script-persistence 模块;
  2. 抽象仓库的默认实现是否应支持「按业务主键派生 key」的配置;
  3. 同一实体多个脚本字段是否需要注解做差异化配置。

交付说明

  • 基线 17.3.x,不合并、不改动版本分支;
  • 提交使用 Ref #234,评审通过后进入正式实现(三模块拆分、8.x 适配)。

…cript)

- GroovyScriptRef 承载脚本正文,key 为空表示未落库,业务层不再感知 key
- persist() 首次生成 key,二次调用复用同一 key 覆盖内容
- GroovyScriptKeyGenerator SPI 支持自定义 key 策略(默认 UUID)
- 修复 GroovyScript#save 不刷新本地 LRU 导致同 key 读到旧内容
- 修复 GroovyScript#remove 不失效本地缓存导致已删除脚本仍可读
- 新增 GroovyScriptRefTest 9 个用例,模块 105 个测试全绿

Ref #234
@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.13043% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.50%. Comparing base (b9a9af6) to head (80fb8ee).

Files with missing lines Patch % Lines
...script/persistence/GroovyScriptRepositoryImpl.java 60.00% 1 Missing and 3 partials ⚠️
...ringboot/script/persistence/GroovyScriptField.java 91.66% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             17.3.x     #235      +/-   ##
============================================
+ Coverage     87.48%   87.50%   +0.01%     
- Complexity     2209     2236      +27     
============================================
  Files           216      222       +6     
  Lines          6019     6065      +46     
  Branches        417      418       +1     
============================================
+ Hits           5266     5307      +41     
- Misses          602      604       +2     
- Partials        151      154       +3     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- 新增 GroovyScriptRefConverter:AttributeConverter 双向转换,落库内容转 key、读库 key 还原正文
- 演示领域对象 / JPA 实体 / 仓储装配:demo_order 表 rule_script 列只存 key
- 新增 4 个集成测试:新库写 key、读库回正文、更新复用 key、脱离 key 会新建
- pom 增加 jakarta.persistence-api(optional),JPA 实现由使用方提供

Ref #234
@xlorne

xlorne commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

按反馈补齐了「落库偷梁换柱」的演示(commit 2fb0afb5),现在能看到领域对象从创建、保存到查询的完整链路:

  • ref/demo/domain/Order:领域对象,只持有 GroovyScriptRef 正文;
  • ref/demo/infra/OrderEntity:@Convert(converter = GroovyScriptRefConverter.class),列 rule_script 存 key;
  • ref/GroovyScriptRefConverter:落库内容→key,读库 key→正文;
  • GroovyScriptRefPersistTest:真实 H2 上 4 个用例 —— 新库存的是 key 不是正文、读库回填正文、修改复用同一 key、脱离 key 直接 new 会新建一份脚本(反例)。

关于「保存时怎么知道上一次的 key」:key 随值对象从库中读回时一并带回,所以落库时无需再查;代价是更新必须先查后改,否则会丢 key(已用测试固化)。这一点是否接受,是我最想确认的。

* 从而让 key 具备确定性;注意内容摘要类策略不适用,否则「修改内容」会连带更换 key。</p>
*/
@FunctionalInterface
public interface GroovyScriptKeyGenerator {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我不是很理解,这个对象是POC阶段接入进来的吗? 这个对象明显应该归到持久化层的,而不是在框架上的。在规划上,我希望对script拆成三分部,注解、脚本执行、持久化。如果现在只是POC阶段临时放在这里是可以,但是后续肯定不能放在这个层面。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除。POC 不再往框架里加任何类,key 生命周期改由持久化层的 OrderScriptStore 承担(演示代码在 src/test/java/.../poc/infra)。框架侧 GroovyScript 与 pom 已恢复 17.3.x 基线。

/**
* 默认策略:UUID(去掉分隔符)
*/
static GroovyScriptKeyGenerator uuid() {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

业务Key的构建要彻底给业务模型来决定

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已调整:key 生成策略改为由业务提供(OrderScriptKeyGenerator),演示按业务主键派生 demo-order-{id}-rule;框架完全不参与 key 生成。

* <p>因此业务对象在内存中始终持有脚本正文,落库那一刻才转换为 key,读取时再还原为正文。</p>
*/
@Getter
public class GroovyScriptRef {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我理解这应该是持久化的对象,但是我不理解的是为什么单元测试的 Order要依赖他?
Order是领域对象,依赖这个对象就意味着领域需要持久化的依赖了,这违背了依赖倒置原则。

领域对象对脚本的处理,完全有他自己决定,他完全可以只有一个字符串的脚本内容,至于脚本如何执行那是gateway的事情,也是gateway约定的规范,如果Gateway需要明确细节的规则,那么领域可以自己定义这个对象也不是直接依赖GroovyScriptRef

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

认同。GroovyScriptRef 已删除,Order 改为只持有 String rule,不再依赖脚本框架任何类型;key 只在持久化层的 OrderEntity.rule_key 上出现。如果 gateway 需要结构化脚本信息,由领域自己定义类型,框架不介入。

// 清理临时数据
TempGroovyScriptContext.getInstance().remove(this.getKey());
// 刷新本地缓存:同 key 的旧实例(如值对象落库时新建的实例)不得继续被读取
GroovyScriptCacheContext.getInstance().cache(this);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cache的机制应该是用在运行时的优化上,而不是持久化上做缓冲的。我想你对GroovyScriptCacheContext的运行机制搞错了。

另外我认为本次调整,其实不需要改动任何GroovyScript类的设计吧

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已回滚。GroovyScript 与 pom.xml 恢复 17.3.x 基线,本次 POC 对框架零改动;持久化读写直接走 GroovyScriptRepositoryContext(脚本仓储),不经过 GroovyScriptCacheContext。缓存只作为运行时执行优化,不承担持久化缓冲。

- 移除框架侧 GroovyScriptRef / GroovyScriptKeyGenerator / GroovyScriptRefConverter,回滚 GroovyScript 改动
- 领域对象 Order 只保存脚本文本,不依赖脚本框架任何类型
- 持久化层 OrderScriptStore 负责 key 生命周期:空则按业务策略生成,非空复用并覆盖内容
- key 生成策略由业务提供(OrderScriptKeyGenerator),演示按业务主键派生
- 5 个 H2 集成测试:业务表只存 key、读库回填正文、更新复用 key、非确定性策略同样复用

Ref #234
…r-script)

评审意见:缓存用于运行时优化,不承担持久化缓冲;key 生命周期由持久化层实现,无需改动 GroovyScript 设计。
本次将 springboot-starter-script/pom.xml 与 GroovyScript 恢复到 17.3.x 基线,POC 仅保留测试范围内的演示代码。

Ref #234
@xlorne xlorne changed the title feat(script): POC - GroovyScriptRef 抽象脚本 key 生命周期 (17.x) POC: 脚本字段持久化 - 业务表存 key、领域对象存正文 (17.x) Dec 4, 2026
@xlorne

xlorne commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

已按四条评审意见全部重构(最新 commit e78a426e),最终与 17.3.x 的差异只剩测试范围内的 8 个演示文件,框架代码零改动。

  • 分层:domain/Order 只持有 String rule;infra 里 OrderEntity.rule_key 存 key,OrderScriptStore 负责 key 生命周期,OrderScriptKeyGenerator 由业务提供 key 策略。
  • key 生命周期:新建先落库拿主键,再按业务策略生成 key 并写脚本、回填 rule_key;更新复用实体上已有的 key 覆盖内容;读库按 key 取正文组装领域对象。
  • 验证:./mvnw clean test -P travis BUILD SUCCESS,script 模块 101 tests 全绿(新增 5 个),覆盖「表里存 key 不是正文」「key 由业务策略决定」「更新复用 key」「非确定性策略同样复用」。

麻烦再看一次方向是否正确,尤其是「更新必须先查后改、key 存在实体行里」这一点。

/**
* 脚本 key,业务表中只保留引用
*/
@Column(name = "rule_key", length = 128)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

其实我希望在持久化的字段上,必须是script的对象或者是约定一个枚举的类型。
不过我更希望是一个scripte的持久化对象。因为如果是现在的设计,从代码阅读角度来说完全不清楚那个字段是需要做处理的,这样的话,就比较容易遗漏了。框架没有明确的标识,完全靠人在convert的时候处理了。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为脚本持久化对象:实体字段类型直接是 GroovyScriptField,字段声明本身就是标识;转换由框架的 GroovyScriptFieldConverter(autoApply=true)自动完成,实体上不需要 @convert,也不依赖人在 convertor 里记得处理。业务若要自己决定 key,用 GroovyScriptField.of(key, content),框架只在未指定时兜底生成。

Comment on lines +35 to +40
// 更新时实体上已有 key,直接复用;新建时为 null,由业务策略生成
String key = scriptStore.save(entity.getId(), entity.getRuleKey(), order.getRule());
entity.setRuleKey(key);

OrderEntity saved = orderJpaRepository.save(entity);
order.setId(saved.getId());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这样的实现能发是在框架层面解决的,而不是让业务适用方,在每个转换下去处理呢

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已下沉到框架:业务侧不再写 key 转换逻辑,只需要把正文交给脚本字段(entity.changeRule(content) 一行),key 的生成、复用、内容落库、读库回填全部由 GroovyScriptField + Converter 负责。

Comment on lines +25 to +28
OrderEntity entity = order.getId() > 0
? orderJpaRepository.findById(order.getId()).orElseGet(OrderEntity::new)
: new OrderEntity();
entity.setName(order.getName());

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段代码,从实现角度是上来是合理的,先从数据库中查询该条数据的key,然后用于后续的实现。
但是这个带来的副作用也很大,如果这个数据是完全新创建的的话,那么这次查询就完全是多余的。

即便是历史存在的数据,那么也带来的牺牲就是在存储之前都需先查然后再存储。

不过我觉得也可以接受,但是这个必须要再框架层面上去做,而不是由业务使用方自己每次持久化的时候先写实现。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已去掉为 key 单独查询的代码。现在 key 随实体加载自动还原到 GroovyScriptField 上,更新时直接沿用上一版本 key;新建时不存在任何额外查询(key 在落库时生成)。save 里的 findById 是 JPA 更新整行的常规加载,不是为 key 单独查一次 —— 如果业务侧本来就是在事务里先查后改,也不会多出第二次查询。

- 新增 persistence 包:GroovyScriptField(脚本持久化对象)、GroovyScriptKeyGenerator(key 策略,默认 UUID)、GroovyScriptFieldConverter(autoApply,列存 key)
- 实体字段声明为 GroovyScriptField 即自动转换,业务无需 @convert 或转换代码
- 业务可通过 of(key, content) 自行指定 key,框架仅在未指定时兜底生成
- 更新沿用已加载实体上的 key,不需要为 key 单独查询
- 演示与测试同步重构:5 个 H2 集成用例覆盖 key 存储、读库回填、更新复用、业务 key、空值

Ref #234
@xlorne

xlorne commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

按最新三条意见已调整(commit 31015b70):

  1. 实体字段类型改为 GroovyScriptField(脚本持久化对象),字段声明即标识,不再用普通 String 存 key;
  2. 转换下沉到框架:GroovyScriptFieldConverter 使用 @Converter(autoApply = true) 自动生效,业务不需要 @Convert,也不再写 key 转换代码;
  3. 去掉为 key 的额外查询:key 在实体加载时已还原到字段上,更新直接沿用;新建时落库生成,全程无额外查询。

业务侧只剩一行:entity.changeRule(order.getRule());业务想自己决定 key 时用 GroovyScriptField.of(key, content),框架不覆盖。

验证:./mvnw clean test -P travis BUILD SUCCESS,script 模块 101 tests 全绿(5 个新增,含业务指定 key、空值场景)。

/**
* POC 演示:持久化层只做「脚本正文 <-> 脚本持久化对象」的搬运,key 的生成与复用完全由框架处理
*/
public class OrderRepositoryImpl implements OrderRepository {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是不是得需要设计一个抽象的GroovyScriptRepository对象呢?

你现在的写法不还是自己业务代码控制吗?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已新增抽象:GroovyScriptRepository(save(key, content) / getScript(key))+ GroovyScriptPersistenceContext(可替换实现)。业务持久化适配层只负责把领域正文放进 GroovyScriptField,key 的生成、复用、存取都在框架仓库里;演示里的 OrderRepositoryImpl 已经没有任何 key 逻辑。

* @param keyGenerator key 生成策略
* @return 脚本 key
*/
public String persist(GroovyScriptKeyGenerator keyGenerator) {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GroovyScriptKeyGenerator,需要提供一个context对象,而不是在这里传递 GroovyScriptKeyGenerator

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改为上下文:新增 GroovyScriptKeyGeneratorContext,业务启动时 setGenerator 注入策略,GroovyScriptField.persist() 不再接收策略参数。默认 UUID,注入 null 回退默认。

* <p>读写直接走脚本仓储,不经过运行时缓存 —— 缓存用于脚本执行时的性能优化,不承担持久化缓冲。</p>
*/
@Converter(autoApply = true)
public class GroovyScriptFieldConverter implements AttributeConverter<GroovyScriptField, String> {

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GroovyScriptFieldConverter 再jpa下是可以使用的,但是考虑到业务模块的持久化未必是通过jpa实现的。那就可能需要提供抽象的GroovyScriptRepository来实现了

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已解耦:Converter 只做 JPA 适配,转换与 key 生命周期委托 GroovyScriptRepository 抽象(GroovyScriptPersistenceContext 持有,可替换)。新增用例演示非 JPA 场景(模拟文档库存 key)直接复用同一抽象完成内容存取与 key 复用。

- 新增 GroovyScriptRepository(正文<->key 抽象)与默认实现,非 JPA 持久化可复用同一套能力
- 新增 GroovyScriptPersistenceContext:仓库实现可替换
- 新增 GroovyScriptKeyGeneratorContext:key 策略通过上下文注入,不再作为方法参数穿透
- GroovyScriptField.persist() 委托上下文,JPA 转换器只做适配
- 新增用例:上下文替换 key 策略、非 JPA 文档库复用同一抽象

Ref #234
@xlorne

xlorne commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

按三条新意见调整完成(commit 80fb8ee0):

  1. 新增抽象 GroovyScriptRepository(save(key, content) / getScript(key))+ 默认实现 + GroovyScriptPersistenceContext,业务适配层不再有 key 逻辑;
  2. key 策略改为 GroovyScriptKeyGeneratorContext,启动时 setGenerator 注入,不再作为方法参数;
  3. Converter 只做 JPA 适配,转换与 key 生命周期全部委托抽象仓库;新增非 JPA 用例(模拟文档库直接复用同一抽象,key 复用行为一致)。

现在业务侧只剩正文搬运(entity.changeRule(order.getRule())),key 生成、复用、存取、读库还原都在框架持久化层。

验证:./mvnw clean test -P travis BUILD SUCCESS,script 模块 103 tests 全绿(新增 7 个)。

@xlorne

xlorne commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

改的啥啊,别弄了。。。

@xlorne

xlorne commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

POC 结论已归档到 #236,本 PR 保持关闭,不再继续迭代。

从这次 POC 里被 #236 吸收与放弃的部分:

吸收

  • 实体字段直接声明为脚本值对象(字段即标识),不用裸 String 装 key;
  • @Converter(autoApply = true) 下沉到框架层,业务不写转换代码;
  • key 随对象加载还原,更新沿用上一版 key,不需要「先查后存」。

放弃

  • GroovyScriptField 这套框架持久化类型塞进 JPA 实体、persistence 包临时挂在 script 模块内 —— 模块边界与 DDD 分层都没解决;
  • 领域对象仍是 String 装正文,与「业务对象拿正文」的目标只走到适配层;
  • 值对象带 persist()、key 可变 —— 落库语义应在基础设施层,值对象保持纯数据。

#236 里对应的正式设计是 ScriptRef(正文不可变、key 仅由持久化层单向回填)+ ScriptStore SPI + JpaScriptStore 默认实现,并按 P0~P4 分阶段落地。

@xlorne
xlorne deleted the feature/issue-234-script-ref-poc-17x branch December 5, 2026 07:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant