lum_ccc_rust/flutter_src/ccc/ccc_iso_operation_id.dart

97 lines
2.4 KiB
Dart

///
///
///
/// Fast sequence-based ID generator for crypto operations
/// Optimized for high-frequency crypto tasks with minimal overhead
class CryptoOperationId {
static int _counter = 0;
static late final String _sessionId;
static bool _initialized = false;
/// Initialize the ID system with a new session
static void initialize() {
if (_initialized) return; // Don't reinitialize
_sessionId = DateTime.now().millisecondsSinceEpoch.toString();
_counter = 0;
_initialized = true;
}
/// Generate next operation ID
/// Format: "timestamp_sequence"
/// Example: "1692123456789_1"
static String generate() {
if (!_initialized) {
initialize();
}
return '${_sessionId}_${++_counter}';
}
/// Get current session ID (for debugging)
static String get currentSession {
if (!_initialized) {
initialize();
}
return _sessionId;
}
/// Get total operations generated in this session
static int get operationCount => _counter;
/// Reset counter (useful for testing)
static void reset() {
_counter = 0;
}
/// Parse operation ID to extract session and sequence
static OperationIdInfo? parse(String operationId) {
final parts = operationId.split('_');
if (parts.length != 2) return null;
final sessionId = parts[0];
final sequence = int.tryParse(parts[1]);
if (sequence == null) return null;
return OperationIdInfo(
sessionId: sessionId,
sequence: sequence,
operationId: operationId,
);
}
}
/// Information extracted from an operation ID
class OperationIdInfo {
final String sessionId;
final int sequence;
final String operationId;
const OperationIdInfo({
required this.sessionId,
required this.sequence,
required this.operationId,
});
/// Get session timestamp
DateTime? get sessionTimestamp {
final timestamp = int.tryParse(sessionId);
if (timestamp == null) return null;
return DateTime.fromMillisecondsSinceEpoch(timestamp);
}
@override
String toString() => 'OperationIdInfo(session: $sessionId, sequence: $sequence)';
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is OperationIdInfo &&
runtimeType == other.runtimeType &&
sessionId == other.sessionId &&
sequence == other.sequence;
@override
int get hashCode => sessionId.hashCode ^ sequence.hashCode;
}