refactor: centralize raw frame identity derivation
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.lingniu.ingest.facts;
|
||||
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.HexFormat;
|
||||
|
||||
public record RawFrameIdentity(
|
||||
String checksum,
|
||||
String vehicleKey,
|
||||
String frameId
|
||||
) {
|
||||
public RawFrameIdentity {
|
||||
checksum = checksum == null ? "" : checksum;
|
||||
vehicleKey = vehicleKey == null ? "" : vehicleKey;
|
||||
frameId = frameId == null ? "" : frameId;
|
||||
}
|
||||
|
||||
public static RawFrameIdentity derive(ProtocolId protocol,
|
||||
String vin,
|
||||
String phone,
|
||||
String rawIdentitySeed,
|
||||
int messageId,
|
||||
int subType,
|
||||
Instant receivedAt,
|
||||
byte[] rawBytes) {
|
||||
String checksum = "sha256:" + sha256(rawBytes);
|
||||
String vehicleKey = VehicleKey.derive(protocol, vin, phone, rawIdentitySeed);
|
||||
String frameId = FactIds.rawFrameId(protocol, vehicleKey, messageId, subType, receivedAt, checksum);
|
||||
return new RawFrameIdentity(checksum, vehicleKey, frameId);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package com.lingniu.ingest.facts;
|
||||
|
||||
import com.lingniu.ingest.api.ProtocolId;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class RawFrameIdentityTest {
|
||||
|
||||
@Test
|
||||
void derivesChecksumVehicleKeyAndFrameIdTogether() {
|
||||
RawFrameIdentity identity = RawFrameIdentity.derive(
|
||||
ProtocolId.JT808,
|
||||
"",
|
||||
"13307795425",
|
||||
"1782900000000000",
|
||||
0x0200,
|
||||
0,
|
||||
Instant.parse("2026-07-01T10:00:00Z"),
|
||||
new byte[]{0x7e, 0x02, 0x00, 0x7e});
|
||||
|
||||
assertThat(identity.checksum()).startsWith("sha256:");
|
||||
assertThat(identity.vehicleKey()).isEqualTo("jt808:13307795425");
|
||||
assertThat(identity.frameId())
|
||||
.isEqualTo(FactIds.rawFrameId(
|
||||
ProtocolId.JT808,
|
||||
"jt808:13307795425",
|
||||
0x0200,
|
||||
0,
|
||||
Instant.parse("2026-07-01T10:00:00Z"),
|
||||
identity.checksum()))
|
||||
.startsWith("rf_");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user