refactor: centralize raw frame identity derivation

This commit is contained in:
lingniu
2026-07-01 18:12:04 +08:00
parent c8abc721e1
commit f89ac1c23f
4 changed files with 92 additions and 42 deletions

View File

@@ -10,17 +10,13 @@ import com.lingniu.ingest.api.pipeline.RawFrame;
import com.lingniu.ingest.core.concurrency.AsyncBatchExecutor;
import com.lingniu.ingest.core.concurrency.DisruptorEventBus;
import com.lingniu.ingest.core.pipeline.InterceptorChain;
import com.lingniu.ingest.facts.FactIds;
import com.lingniu.ingest.facts.VehicleKey;
import com.lingniu.ingest.facts.RawFrameIdentity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HexFormat;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -187,11 +183,9 @@ public final class Dispatcher {
Instant ingestTime = frame.receivedAt() != null ? frame.receivedAt() : Instant.now();
String eventId = nextRawArchiveEventId(ingestTime);
String key = RawArchiveKeys.key(ingestTime, frame.protocolId(), vin, eventId);
String checksum = "sha256:" + sha256(bytes);
String vehicleKey = VehicleKey.derive(frame.protocolId(), vin, phone, eventId);
String frameId = FactIds.rawFrameId(
frame.protocolId(), vehicleKey, frame.command(), frame.infoType(), ingestTime, checksum);
Map<String, String> archiveMeta = addRawArchiveMetadata(meta, eventId, key, frameId);
RawFrameIdentity identity = RawFrameIdentity.derive(
frame.protocolId(), vin, phone, eventId, frame.command(), frame.infoType(), ingestTime, bytes);
Map<String, String> archiveMeta = addRawArchiveMetadata(meta, eventId, key, identity.frameId());
VehicleEvent.RawArchive raw = new VehicleEvent.RawArchive(
eventId,
@@ -206,7 +200,7 @@ public final class Dispatcher {
bytes,
parsedJson);
out.add(raw);
return new RawArchiveLookup(eventId, key, RawArchiveKeys.logicalUri(key), frameId);
return new RawArchiveLookup(eventId, key, RawArchiveKeys.logicalUri(key), identity.frameId());
}
private static String parsedJson(Object payload, String explicitParsedJson) {
@@ -231,15 +225,6 @@ public final class Dispatcher {
return value.replace("\\", "\\\\").replace("\"", "\\\"");
}
private static String sha256(byte[] value) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
return HexFormat.of().formatHex(digest.digest(value == null ? new byte[0] : value));
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException("SHA-256 is unavailable", e);
}
}
private static String nextRawArchiveEventId(Instant ingestTime) {
// 文件名需要可排序且单进程内唯一:毫秒时间放大到微秒量级,再用 AtomicLong 递增兜底。
long base = (ingestTime == null ? Instant.now() : ingestTime).toEpochMilli() * 1000L;