XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEServerManager

【Java】我的世界XFE服务器管理器

公开
关注 0 Fork 0 Star 0
UTF-8
package com.xfestudio.xfeservermanager.core.governance.claim;

import java.util.Map;
import java.util.Optional;
import java.util.UUID;

/** Immutable hot-path index, replaced atomically after persistent updates. */
public record ClaimSnapshot(long revision, Map<UUID, Claim> claims, Map<ChunkKey, UUID> chunkIndex) {
    public ClaimSnapshot {
        if (revision < 0) throw new IllegalArgumentException("revision must be non-negative");
        claims = Map.copyOf(claims);
        chunkIndex = Map.copyOf(chunkIndex);
        for (var entry : chunkIndex.entrySet()) {
            Claim claim = claims.get(entry.getValue());
            if (claim == null || !claim.chunks().contains(entry.getKey())) {
                throw new IllegalArgumentException("chunk index is inconsistent");
            }
        }
        var expected = new java.util.HashMap<ChunkKey, UUID>();
        claims.forEach((claimId, claim) -> {
            if (!claimId.equals(claim.claimId())) {
                throw new IllegalArgumentException("claim map key does not match claim id");
            }
            claim.chunks().forEach(chunk -> {
            if (expected.put(chunk, claim.claimId()) != null) {
                throw new IllegalArgumentException("claims overlap at " + chunk);
            }
            });
        });
        if (!expected.equals(chunkIndex)) throw new IllegalArgumentException("chunk index is incomplete");
    }

    public static ClaimSnapshot empty() {
        return new ClaimSnapshot(0, Map.of(), Map.of());
    }

    public Optional<Claim> at(ChunkKey chunk) {
        return Optional.ofNullable(chunkIndex.get(chunk)).map(claims::get);
    }
}