public Task<int> ReadHighestIndexedProjectionStreamSequenceAsync(Stream stream) { lock (_projectionIndexes) { List<long> projection; if (_projectionIndexes.TryGetValue(stream.ToHexString(), out projection)) return Task.FromResult(projection.Count); } return Task.FromResult(0); }
public Task ReadIndexedProjectionStreamAsync(Stream stream, int initialSequence, int count, Action<IPersistedRawEvent> readCallback, CancellationToken ct) { lock (_projectionIndexes) { List<long> projection; if (_projectionIndexes.TryGetValue(stream.ToHexString(), out projection)) { IEnumerable<PersistedRawEvent> events; lock (_events) { events = from s in projection join e in _events on s equals e.GlobalSequence orderby e.GlobalSequence select e; if (initialSequence > 0) events = events.Skip(initialSequence - 1); if (count >= 0) events = events.Take(count); events = events.ToList(); } foreach (var e in events) { if (ct.IsCancellationRequested) break; readCallback(e); } } } return Unit.GetCompletedTask(); }
public Task<long> ReadHighestIndexedProjectionGlobalSequenceAsync(Stream stream) { lock (_projectionIndexes) { List<long> projection; if (_projectionIndexes.TryGetValue(stream.ToHexString(), out projection)) return Task.FromResult(projection.LastOrDefault()); } return Task.FromResult(0L); }
public Task<long> ReadProjectionCheckpointAsync(Stream stream) { lock (_projectionCheckpoints) { long checkpoint; if (_projectionCheckpoints.TryGetValue(stream.ToHexString(), out checkpoint)) return Task.FromResult(checkpoint); } return Task.FromResult(0L); }
public Task WriteProjectionCheckpointAsync(Stream stream, long globalSequence) { lock (_projectionCheckpoints) { _projectionCheckpoints[stream.ToHexString()] = globalSequence; } return Unit.GetCompletedTask(); }
public Task WriteProjectionIndexAsync(Stream stream, int expectedSequence, IReadOnlyCollection<long> globalSequences) { lock (_projectionIndexes) { List<long> projection; var key = stream.ToHexString(); if (!_projectionIndexes.TryGetValue(key, out projection)) _projectionIndexes.Add(key, projection = new List<long>()); if (projection.Count != expectedSequence) throw new UnexpectedStreamSequenceException(); if (globalSequences.Intersect(projection).Any()) throw new DuplicatedEntryException(); lock (_events) { projection.AddRange(globalSequences); } } return Unit.GetCompletedTask(); }
public Task ClearProjectionIndexAsync(Stream stream) { var key = stream.ToHexString(); lock (_projectionCheckpoints) { if (_projectionCheckpoints.ContainsKey(key)) _projectionCheckpoints.Remove(key); } lock (_projectionIndexes) { if (_projectionIndexes.ContainsKey(key)) _projectionIndexes.Remove(key); } return Unit.GetCompletedTask(); }