Task IGridGrain.Init() { long key = this.GetPrimaryKeyLong(); uint grid_id = (uint)key; uint x_grid_index = (grid_id - 1) / Common.y_grid_num; uint y_grid_index = (grid_id - 1) % Common.y_grid_num; uint grid_px = x_grid_index * Common.grid_len; uint grid_py = y_grid_index * Common.grid_wid; this.attr.Id = grid_id; this.attr.Px = grid_px; this.attr.Py = grid_py; for (long i = 0; i < Common.per_ball_count; i++) { ball_arr.PerformUpdate(x => x.Add(10000 * grid_id + i)); } var tasks = new List <Task>(); List <long> ball_list = InnerGetBallList(); foreach (var ball_id in ball_list) { tasks.Add(GrainFactory.GetGrain <IBallGrain>(ball_id).Init(GetBallInitPx(), GetBallInitPy())); } return(Task.WhenAll(tasks)); }
public Task Withdraw(ulong amount) { return(_balance.PerformUpdate(m => { if (m.Value < amount) { throw new Exception("余额不足"); } m.Value -= amount; })); }
private Task <Observation[]> Write() { var txid = TransactionContext.CurrentTransactionId; return(data.PerformUpdate((state) => { var observe = new Observation[2]; observe[0] = new Observation() { ExecutingTx = txid, WriterTx = state.WriterTx, Grain = MyNumber, SeqNo = state.SeqNo }; state.WriterTx = txid; state.SeqNo++; observe[1] = new Observation() { ExecutingTx = txid, WriterTx = state.WriterTx, Grain = MyNumber, SeqNo = state.SeqNo }; return observe; })); }
Task IAccountGrain.Deposit(uint amount) { //this.balance.State.Value += amount; var random = new Random(); if (random.Next(1, 10) % 2 == 0) { throw new Exception("11111"); } else { balance.PerformUpdate(b => b.Value += amount); //this.balance.Save(); return(Task.CompletedTask); } }
Task IBallGrain.Move() { uint ball_id = attr.PerformRead(x => x.Id).Result; attr.PerformUpdate(x => { x.Px += x.Vx; x.Py += x.Vy; }); double px = attr.PerformRead(x => x.Px).Result; if (px < 0) { attr.PerformUpdate(x => { x.Px = 0; x.Vx = -x.Vx; }); } if (px > Common.x_max) { attr.PerformUpdate(x => { x.Px = Common.x_max; x.Vx = -x.Vx; }); } double py = attr.PerformRead(x => x.Py).Result; if (py < 0) { attr.PerformUpdate(x => { x.Py = 0; x.Vy = -x.Vy; }); } if (py > Common.y_max) { attr.PerformUpdate(x => { x.Py = Common.y_max; x.Vy = -x.Vy; }); } Console.WriteLine($"\n\nball {ball_id} move px = {px} py = {py}\n\n"); return(Task.CompletedTask); }
private async Task UpdateBalance(decimal amount, string desciption, TransactionType type = TransactionType.Default) { await _transactionalState.PerformUpdate(b => { b.Balance += amount; b.Transactions.Add(CreateTransaction(amount, desciption, type)); }); }
async Task IAccountGrain.Deposit(uint amount) { await _balance.PerformUpdate(x => x.Value += amount); }
/// <summary> /// Performs an update operation, without returning any result. /// </summary> /// <param name="transactionalState">Transactional state to perform update upon.</param> /// <param name="updateAction">An action that updates the state.</param> public static Task PerformUpdate <TState>(this ITransactionalState <TState> transactionalState, Action <TState> updateAction) where TState : class, new() { return(transactionalState.PerformUpdate <bool>(state => { updateAction(state); return true; })); }
public Task Deposit(uint amount) => _balance.PerformUpdate(x => x.Value += amount);
Task IAccountGrain.Deposit(uint amount) { return(_balanceState.PerformUpdate(x => x.Balance += amount)); }
public Task Execute() { return(_state.PerformUpdate(s => s.Counter++)); }