public void Update(TMessage curr) { if (curr is null) { throw new ArgumentNullException(nameof(curr)); } var key = _serializer.GetKey(curr); lock (_snapshots.SyncRoot) { var prev = _snapshots.TryGetValue(key); if (prev is null) { if (curr.SecurityId == default) { throw new ArgumentException(curr.ToString()); } _snapshots.Add(key, curr.TypedClone()); } else { _serializer.Update(prev, curr); } _dirtyKeys.Add(key); } }
public void Update(TMessage curr) { if (curr == null) { throw new ArgumentNullException(nameof(curr)); } var key = _serializer.GetKey(curr); lock (_snapshots.SyncRoot) { var prev = _snapshots.TryGetValue(key); if (prev == null) { _snapshots.Add(key, _serializer.CreateCopy(curr)); } else { _serializer.Update(prev, curr); } _dirtyKeys.Add(key); } }
// version has 2 bytes //private const int _versionLen = 2; // buffer length 4 bytes //private const int _bufSizeLen = 4; public SnapshotStorageDate(string fileName, ISnapshotSerializer <TKey, TMessage> serializer) { if (fileName.IsEmpty()) { throw new ArgumentNullException(nameof(fileName)); } _fileName = fileName; _serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); if (File.Exists(_fileName)) { Debug.WriteLine($"Snapshot (Load): {_fileName}"); using (var stream = File.OpenRead(_fileName)) { _version = new Version(stream.ReadByte(), stream.ReadByte()); while (stream.Position < stream.Length) { var size = stream.Read <int>(); var buffer = new byte[size]; stream.ReadBytes(buffer, buffer.Length); //var offset = stream.Position; TMessage message; try { message = _serializer.Deserialize(_version, buffer); } catch (Exception ex) { ex.LogError(); continue; } var key = _serializer.GetKey(message); _snapshots.Add(key, message); _buffers.Add(key, buffer); } //_currOffset = stream.Length; } } else { _version = _serializer.Version; //_currOffset = _versionLen; } }