public override async Task ProcessStreamItem(StreamEntityBase item) { if (_persister != null && item != null) { if (_numItemsPerBatch > 0 && _persister.SupportsBatches && _batch == null) { _batch = await _persister.CreateBatch(_entityType); _currentNumItemsInBatch = 0; } item.PartitionKey = _partitionKey; var result = (item.Operation == StreamOperation.Insert || item.Operation == StreamOperation.Update ? _persister.Upsert(item, _batch) : _persister.Delete(item, _batch)); if (_batch != null) { _currentNumItemsInBatch++; if (_currentNumItemsInBatch >= _numItemsPerBatch) { var storedLatency = await _batch.Commit(); ReportLatency(storedLatency); _batch = null; } } else { var storedLatency = await result; ReportLatency(storedLatency); } } DisplayLatency(); }
public override async Task ProcessPendingItems(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { bool anyDataProcessed = false; SwitchDictionaries(); Interlocked.Increment(ref _numSwitchDirectories); var processItems = ProcessItems; while (processItems.Any()) { int storedItems = 0; IStreamPersisterBatch batch = null; if (_persister != null) { foreach (var item in processItems) { if (_persister.SupportsBatches && batch == null) { batch = await _persister.CreateBatch(_entityType); } StoredLatency storedLatency; if (item.Operation == StreamOperation.Insert || item.Operation == StreamOperation.Update) { storedLatency = await _persister.Upsert(item, batch); } else { storedLatency = await _persister.Delete(item, batch); } ReportLatency(storedLatency); storedItems++; anyDataProcessed = true; Interlocked.Increment(ref _numProcessedItems); } } else { Interlocked.Add(ref _numProcessedItems, processItems.Count); } if (batch != null) { var storedLatency = await batch.Commit(); ReportLatency(storedLatency); } processItems.Clear(); } DisplayLatency(); await Task.Delay(anyDataProcessed?_holdOffBusy : _holdOffIdle); } }
public override async Task ProcessPendingItems(CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) { bool anyDataProcessed = false; SwitchDictionaries(); Interlocked.Increment(ref _numSwitchDirectories); var processItems = ProcessItems; while (processItems.Values.Any()) { int storedItems = 0; IStreamPersisterBatch batch = null; while (processItems.Values.Any() && !cancellationToken.IsCancellationRequested) { if (_persister != null) { if (_persister.SupportsBatches && batch == null) { batch = await _persister.CreateBatch(_entityType); } var processItemPair = processItems.First(); if (processItems.Remove(processItemPair.Key)) { StoredLatency storedLatency; if (processItemPair.Value.DeleteItem != null) { storedLatency = await _persister.Delete(processItemPair.Value.DeleteItem, batch); } else if (processItemPair.Value.UpsertItem != null) { storedLatency = await _persister.Upsert(processItemPair.Value.UpsertItem, batch); } else { storedLatency = new StoredLatency() { NumItems = 0, Time = 0.0 } }; ReportLatency(storedLatency); storedItems++; anyDataProcessed = true; Interlocked.Increment(ref _numProcessedItems); } } else { Interlocked.Add(ref _numProcessedItems, processItems.Count); processItems.Clear(); } } if (batch != null) { var storedLatency = await batch.Commit(); ReportLatency(storedLatency); } } DisplayLatency(); await Task.Delay(anyDataProcessed?_holdOffBusy : _holdOffIdle); } }