fix(zone-cold): JSON_MERGE_PATCH 原子寫入 + JSON_REMOVE 原子刪除(B2)

set()/set_many() 原本 get_blob_raw()→array_merge→save_blob() 三步,並發
寫入會互相覆蓋整個 blob;remove() 同樣是 read-modify-write。改為:
- 新增 private save_patch():INSERT ... ON DUPLICATE KEY UPDATE
  data = JSON_MERGE_PATCH(COALESCE(data,'{}'), patch)
- remove():UPDATE ... SET data = JSON_REMOVE(COALESCE(data,'{}'), '$.key')
- save_blob() 改用 TMDO_DB::upsert()
- SQLite 保留 read-modify-write fallback
- ZoneColdTest stub 補 JSON_REMOVE / JSON_MERGE_PATCH / upsert 三種 SQL 模擬

對應 A v3.3.3 P1-21。unit 379 / integration 398 GREEN

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TbG1keQQ7XBa7qMQY16KCY
This commit is contained in:
2026-07-31 05:34:27 +08:00
parent 151a14a7e8
commit 59f3b19ce6
2 changed files with 121 additions and 36 deletions
+82 -36
View File
@@ -149,36 +149,58 @@ class TMDO_Zone_Cold {
* @param mixed $value Value to store.
*/
public static function set( int $post_id, string $post_type, string $meta_key, mixed $value ): void {
$data = self::get_blob_raw( $post_id, $post_type );
$data[ $meta_key ] = $value;
self::save_blob( $post_id, $post_type, $data );
self::save_patch( $post_id, $post_type, array( $meta_key => $value ) );
}
/**
* Set multiple cold meta values at once.
*
* Uses JSON_MERGE_PATCH on MySQL to avoid a read-then-write round trip.
* Falls back to read-modify-write on SQLite.
*
* @param int $post_id Post ID.
* @param string $post_type Post type.
* @param array $values meta_key => value pairs.
*/
public static function set_many( int $post_id, string $post_type, array $values ): void {
$data = self::get_blob_raw( $post_id, $post_type );
$data = array_merge( $data, $values );
self::save_blob( $post_id, $post_type, $data );
self::save_patch( $post_id, $post_type, $values );
}
/**
* Remove a key from the cold blob.
*
* Uses JSON_REMOVE on MySQL for an atomic single-key removal without a full
* blob read. Falls back to read-modify-write on SQLite.
*
* @param int $post_id Post ID.
* @param string $post_type Post type slug.
* @param string $meta_key Meta key to remove.
* @return void
*/
public static function remove( int $post_id, string $post_type, string $meta_key ): void {
$data = self::get_blob_raw( $post_id, $post_type );
unset( $data[ $meta_key ] );
self::save_blob( $post_id, $post_type, $data );
if ( defined( 'TMDO_IS_SQLITE' ) && TMDO_IS_SQLITE ) {
$data = self::get_blob_raw( $post_id, $post_type );
unset( $data[ $meta_key ] );
self::save_blob( $post_id, $post_type, $data );
return;
}
global $wpdb;
$table = self::table( $post_type );
$path = '$.' . $meta_key;
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from TMDO_Zone_Cold::table() via TMDO_DB::table().
$wpdb->query(
$wpdb->prepare(
"UPDATE `{$table}` SET data = JSON_REMOVE(COALESCE(data, '{}'), %s), updated_at = %s WHERE post_id = %d",
$path,
TMDO_DB::now(),
$post_id
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
wp_cache_delete( "cold_{$post_id}", self::cache_group( $post_type ) );
}
/**
@@ -224,39 +246,63 @@ class TMDO_Zone_Cold {
* @return void
*/
private static function save_blob( int $post_id, string $post_type, array $data ): void {
global $wpdb;
$table = self::table( $post_type );
$now = TMDO_DB::now();
$json = wp_json_encode( $data );
$existing = $wpdb->get_var(
$wpdb->prepare( "SELECT id FROM `{$table}` WHERE post_id = %d LIMIT 1", $post_id ) // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from TMDO_Zone_Cold::table() via TMDO_DB::table().
TMDO_DB::upsert(
$table,
array(
'post_id' => $post_id,
'data' => $json,
'updated_at' => TMDO_DB::now(),
),
array( 'data', 'updated_at' ),
'post_id',
array( '%d', '%s', '%s' )
);
if ( $existing ) {
$wpdb->update(
$table,
array(
'data' => $json,
'updated_at' => $now,
),
array( 'post_id' => $post_id ),
array( '%s', '%s' ),
array( '%d' )
);
} else {
$wpdb->insert(
$table,
array(
'post_id' => $post_id,
'data' => $json,
'updated_at' => $now,
),
array( '%d', '%s', '%s' )
);
}
// Invalidate object cache.
wp_cache_delete( "cold_{$post_id}", self::cache_group( $post_type ) );
}
/**
* Merge a partial key-value map into the cold blob atomically.
*
* Uses INSERT ... ON DUPLICATE KEY UPDATE with JSON_MERGE_PATCH so concurrent
* writers cannot clobber each other's keys (the read-modify-write path did).
* On SQLite, falls back to the classic read-modify-write path.
*
* @param int $post_id Post ID.
* @param string $post_type Post type slug.
* @param array $patch Partial key-value map to merge in.
*/
private static function save_patch( int $post_id, string $post_type, array $patch ): void {
if ( defined( 'TMDO_IS_SQLITE' ) && TMDO_IS_SQLITE ) {
$data = self::get_blob_raw( $post_id, $post_type );
$data = array_merge( $data, $patch );
self::save_blob( $post_id, $post_type, $data );
return;
}
global $wpdb;
$table = self::table( $post_type );
$json = wp_json_encode( $patch );
$now = TMDO_DB::now();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name from TMDO_Zone_Cold::table() via TMDO_DB::table().
$wpdb->query(
$wpdb->prepare(
"INSERT INTO `{$table}` (post_id, data, updated_at) VALUES (%d, %s, %s)
ON DUPLICATE KEY UPDATE data = JSON_MERGE_PATCH(COALESCE(data, '{}'), %s), updated_at = %s",
$post_id,
$json,
$now,
$json,
$now
)
);
// phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
wp_cache_delete( "cold_{$post_id}", self::cache_group( $post_type ) );
}
}