Class BulkOperations<K,V>

java.lang.Object
cloud.opencode.base.cache.bulk.BulkOperations<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型

Security | 安全性:

  • Thread-safe: Yes (delegates to thread-safe cache) - 线程安全: 是(委托给线程安全的缓存)
  • Null-safe: Yes - 空值安全: 是

public class BulkOperations<K,V> extends Object
Bulk Operations - Enhanced bulk operations for cache 批量操作 - 缓存的增强批量操作

Provides advanced bulk operations with atomic semantics, conditional updates, and batch processing.

提供具有原子语义、条件更新和批处理的高级批量操作。

Features | 主要功能:

  • Atomic compare-and-swap-all - 原子比较并交换全部
  • Conditional bulk put - 条件批量放置
  • Batch processing with callback - 带回调的批处理
  • Parallel batch operations - 并行批量操作
  • Bulk compute operations - 批量计算操作

Usage Examples | 使用示例:

BulkOperations<String, User> bulk = BulkOperations.on(cache);

// Bulk put all - best-effort rollback on failure
boolean success = bulk.bulkPutAll(users);

// Conditional put - only if all keys present
boolean success = bulk.putAllIfAllPresent(updates);

// Batch process with callback
bulk.batchProcess(keys, 100, batch -> {
    // Process each batch of 100
});

// Compute all values
bulk.computeAll(keys, (key, oldValue) -> newValue);
Since:
JDK 25, opencode-base-cache V2.0.5
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • on

      public static <K,V> BulkOperations<K,V> on(Cache<K,V> cache)
      Create bulk operations for a cache 为缓存创建批量操作
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Parameters:
      cache - the cache | 缓存
      Returns:
      bulk operations | 批量操作
    • bulkPutAll

      public boolean bulkPutAll(Map<? extends K, ? extends V> entries)
      Bulk put all entries with best-effort rollback on failure. 批量放置所有条目,失败时尽最大努力回滚。

      Note: This is NOT truly atomic. Entries are put one by one and concurrent readers may see partial updates. If an exception occurs mid-way, previously inserted entries are rolled back on a best-effort basis.

      Parameters:
      entries - entries to put | 要放置的条目
      Returns:
      true if all succeeded | 如果全部成功返回 true
    • atomicInvalidateAll

      public Map<K,V> atomicInvalidateAll(Iterable<? extends K> keys)
      Atomic invalidate all - either all succeed or none 原子使全部无效 - 要么全部成功,要么都不
      Parameters:
      keys - keys to invalidate | 要使无效的键
      Returns:
      map of invalidated entries for potential restore | 无效条目的映射以便可能恢复
    • putAllIfAllPresent

      public boolean putAllIfAllPresent(Map<? extends K, ? extends V> entries)
      Put all only if all keys are already present 仅当所有键都已存在时才放置全部
      Parameters:
      entries - entries to put | 要放置的条目
      Returns:
      true if all keys present and updated | 如果所有键都存在并已更新返回 true
    • putAllIfAllAbsent

      public boolean putAllIfAllAbsent(Map<? extends K, ? extends V> entries)
      Put all only if all keys are absent 仅当所有键都不存在时才放置全部
      Parameters:
      entries - entries to put | 要放置的条目
      Returns:
      true if all keys absent and inserted | 如果所有键都不存在并已插入返回 true
    • putAllIfAbsent

      public BulkOperations.BulkPutResult<K> putAllIfAbsent(Map<? extends K, ? extends V> entries)
      Put entries that are absent, skip existing 放置不存在的条目,跳过已存在的
      Parameters:
      entries - entries to put | 要放置的条目
      Returns:
      result with counts of inserted and skipped | 带有插入和跳过计数的结果
    • invalidateAllIfMatch

      public boolean invalidateAllIfMatch(Map<? extends K, ? extends V> expected)
      Invalidate all only if all values match expected 仅当所有值与预期匹配时才使全部无效
      Parameters:
      expected - expected key-value pairs | 预期的键值对
      Returns:
      true if all matched and invalidated | 如果全部匹配并已使无效返回 true
    • batchProcess

      public int batchProcess(Iterable<? extends K> keys, int batchSize, BulkOperations.BatchProcessor<K,V> processor)
      Process keys in batches 分批处理键
      Parameters:
      keys - keys to process | 要处理的键
      batchSize - batch size | 批次大小
      processor - batch processor | 批处理器
      Returns:
      total processed count | 处理的总数
    • batchProcessParallel

      public CompletableFuture<BulkOperations.BatchResult> batchProcessParallel(List<? extends K> keys, int batchSize, int parallelism, BulkOperations.BatchProcessor<K,V> processor)
      Process keys in parallel batches 并行分批处理键
      Parameters:
      keys - keys to process | 要处理的键
      batchSize - batch size | 批次大小
      parallelism - number of parallel threads | 并行线程数
      processor - batch processor | 批处理器
      Returns:
      future completing when all batches done | 所有批次完成时完成的 Future
    • computeAll

      public Map<K,V> computeAll(Iterable<? extends K> keys, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Compute values for all specified keys 为所有指定的键计算值
      Parameters:
      keys - keys to compute | 要计算的键
      remappingFunction - function to compute new value | 计算新值的函数
      Returns:
      map of new values | 新值的映射
    • computeAllIfAbsent

      public Map<K,V> computeAllIfAbsent(Iterable<? extends K> keys, Function<? super K, ? extends V> mappingFunction)
      Compute values for all absent keys 为所有不存在的键计算值
      Parameters:
      keys - keys to compute | 要计算的键
      mappingFunction - function to compute value | 计算值的函数
      Returns:
      map of computed values | 计算值的映射
    • replaceAllMatching

      public int replaceAllMatching(BiPredicate<K,V> predicate, BiFunction<? super K, ? super V, ? extends V> remappingFunction)
      Replace all values matching predicate 替换所有匹配谓词的值
      Parameters:
      predicate - filter for entries to replace | 要替换的条目过滤器
      remappingFunction - function to compute new value | 计算新值的函数
      Returns:
      count of replaced entries | 替换的条目数
    • putAllWithTtl

      public void putAllWithTtl(Map<? extends K, ? extends BulkOperations.TtlValue<V>> entries)
      Put all with individual TTLs 放置全部并带有各自的 TTL
      Parameters:
      entries - entries with their TTLs | 带有 TTL 的条目