public async Task <TValue> GetOrAddAsync(ITransaction tx, TKey key, Func <TKey, TValue> valueFactory) { if (reliableDictionary == null) { await InitializeReliableDictionary(); } return(await reliableDictionary.GetOrAddAsync(tx, key, valueFactory)); }
private async Task ProcessOrdersAsync( Order order, IReliableDictionary <OrderQueueKey, OrderQueue> storage, OrderQueueKey orderQueueKey, OrderQueueKey oppositeOrderQueueKey) { using (var tx = _stateManager.CreateTransaction()) { var orderQueue = await storage.GetOrAddAsync( tx, orderQueueKey, new OrderQueue(order.CurrencyPair, order.Side)); var oppositeOrderQueue = await storage.GetOrAddAsync( tx, oppositeOrderQueueKey, new OrderQueue(order.CurrencyPair, order.Side == Side.Buy ? Side.Sell : Side.Buy)); while (true) { var oppositeOrder = oppositeOrderQueue.Peek(); if (oppositeOrder == null) { break; } var matched = _ordersMatchingRule.IsMatched(order, oppositeOrder); if (matched) { matched = _ordersMatchingRule.IsMatched(order, oppositeOrder); if (matched) { ProcessOrders(order, orderQueue, oppositeOrder, oppositeOrderQueue); } else { // process further if some thread is here continue; } } if (order.IsClosed || !matched || oppositeOrderQueue.IsEmpty) { break; } } await storage.AddOrUpdateAsync(tx, orderQueueKey, orderQueue, (key, old) => orderQueue); await storage.AddOrUpdateAsync(tx, oppositeOrderQueueKey, oppositeOrderQueue, (key, old) => orderQueue); await tx.CommitAsync(); } }
public async Task ProcessDependencyUpdateErrorsAsync() { if (_options.IsEnabled) { IReliableDictionary <string, DateTimeOffset> checkpointEvaluator = await _stateManager.GetOrAddAsync <IReliableDictionary <string, DateTimeOffset> >("checkpointEvaluator"); try { DateTimeOffset checkpoint; using (ITransaction tx = _stateManager.CreateTransaction()) { checkpoint = await checkpointEvaluator.GetOrAddAsync( tx, "checkpointEvaluator", DateTimeOffset.UtcNow ); await tx.CommitAsync(); } await CheckForErrorsInUpdateHistoryTablesAsync( checkpoint, _options.GithubUrl, checkpointEvaluator); } catch (TimeoutException exe) { _logger.LogError(exe, "Unable to connect to reliable services."); } catch (Exception ex) { _logger.LogError(ex, "Unable to create a github issue."); } } }
/// <summary> /// Write all messages contained in a single transaction to the underlying reliable dictionary /// </summary> /// <param name="messages">list of messages</param> /// <returns></returns> protected async override Task <IImmutableList <Exception> > WriteMessagesAsync(IEnumerable <AtomicWrite> messages) { IReliableDictionary <string, LinkedList <IPersistentRepresentation> > messagelist = null; using (var tx = this.StateManager.CreateTransaction()) { foreach (var message in messages) { foreach (var payload in (IEnumerable <IPersistentRepresentation>)message.Payload) { if (messagelist == null) { messagelist = await this.StateManager.GetOrAddAsync <IReliableDictionary <string, LinkedList <IPersistentRepresentation> > >($"Messages_{payload.PersistenceId}"); } var list = await messagelist.GetOrAddAsync(tx, payload.PersistenceId, pid => new LinkedList <IPersistentRepresentation>()); list.AddLast(payload); } } await tx.CommitAsync(); } return((IImmutableList <Exception>)null); // all good }
public async Task AddProduct(Product product) { IReliableDictionary <Guid, Product> products = await _stateManager .GetOrAddAsync <IReliableDictionary <Guid, Product> >("products"); using (ITransaction tx = _stateManager.CreateTransaction()) { await products.GetOrAddAsync(tx, product.Id, product); await tx.CommitAsync(); } }
private async Task AddOrderAsync(Order order, IReliableDictionary <OrderQueueKey, OrderQueue> storage, OrderQueueKey orderQueueKey) { using (var tx = _stateManager.CreateTransaction()) { var orderQueue = await storage.GetOrAddAsync( tx, orderQueueKey, new OrderQueue(order.CurrencyPair, order.Side)); orderQueue.Add(order); await storage.AddOrUpdateAsync(tx, orderQueueKey, orderQueue, (key, old) => orderQueue); await tx.CommitAsync(); } }
public Task <TValue> GetOrAdd(Core.ITransaction tx, TKey key, Func <TKey, TValue> addValueFactory) { return(wrappedReliableDictionary.GetOrAddAsync(((ServiceFabTransaction)tx).WrappedTransaction, key, key1 => addValueFactory.Invoke(key))); }
public async Task AddMessageAsync(Message message) { DateTime time = DateTime.Now.ToLocalTime(); IReliableDictionary <DateTime, Message> messagesDictionary = await this.StateManager.GetOrAddAsync <IReliableDictionary <DateTime, Message> >("messages"); //dictionary for the scores IReliableDictionary <string, int> scoresDictionary = await StateManager.GetOrAddAsync <IReliableDictionary <string, int> >("scores"); var currentQuestion = await StateManager.GetOrAddAsync <IReliableQueue <KeyValuePair <string, string> > >("currentQuestion"); using (ITransaction tx = StateManager.CreateTransaction()) { var currentScoreConditional = await scoresDictionary.TryGetValueAsync(tx, message.Name); if (!currentScoreConditional.HasValue) { await scoresDictionary.GetOrAddAsync(tx, message.Name, 0); } await tx.CommitAsync(); } //checking for the right answer using (ITransaction tx = this.StateManager.CreateTransaction()) { if (message.MessageText.ToLowerInvariant().Trim() == "try me") { var q = TriviaDatabase.GetRandomQuestion(); await currentQuestion.TryDequeueAsync(tx); await currentQuestion.EnqueueAsync(tx, q); await tx.CommitAsync(); } else { var question = await currentQuestion.TryPeekAsync(tx); if (question.Value.Value.ToLowerInvariant().Trim() == message.MessageText.ToLowerInvariant().Trim()) { await messagesDictionary.AddAsync(tx, time, new Message { Name = message.Name, MessageText = message.MessageText }); await messagesDictionary.AddAsync(tx, time.AddTicks(1), new Message { Name = "Admin", MessageText = "You are correct " + message.Name + "! You get one point for this" }); //get and update the score var currentScoreConditional = await scoresDictionary.TryGetValueAsync(tx, message.Name); var currentScore = currentScoreConditional.HasValue ? currentScoreConditional.Value + 1 : 0; await scoresDictionary.AddOrUpdateAsync(tx, message.Name, currentScore, (x, y) => ++ y); await currentQuestion.TryDequeueAsync(tx); await currentQuestion.EnqueueAsync(tx, TriviaDatabase.GetRandomQuestion()); await tx.CommitAsync(); } else { await messagesDictionary.AddAsync(tx, time, message); await tx.CommitAsync(); } } } }