public Task <AppendResult> Append(UnversionedCommit commit) { lock (_sync) { var all = _commits.Where(d => d.TenantId == commit.TenantId && d.EntityId == commit.EntityId); var dup = all.FirstOrDefault(d => d.CommitId == commit.CommitId); if (dup != null) { return(Task.FromResult(new AppendResult(dup))); } var max = !all.Any()?0:all.Max(d => d.Version); var c = new Commit(max + 1, commit); _commits.Add(c); } return(Task.FromResult(AppendResult.Ok)); }
public async Task <AppendResult> Append(UnversionedCommit commit) { using (var db = await _db.CreateAsync(CancellationToken.None)) { using (var t = db.BeginTransaction()) { var max = await db.QueryValueAsync <int?>(q => q.From <Commit>().Where(d => d.EntityId == commit.EntityId && d.TenantId == commit.TenantId) .Select(d => d.Max(d.Version)).MapTo <int?>(), CancellationToken.None).ConfigureFalse() ?? 0; var com = new Commit(max + 1, commit); try { await db.InsertAsync(com, CancellationToken.None).ConfigureFalse(); t.Commit(); return(AppendResult.Ok); } catch (DbException ex) { if (ex.Message.Contains(DuplicateCommmitMessage)) { var existing = await db .QueryRowAsync <Commit>( q => q.From <Commit>() .Where(d => d.CommitId == commit.CommitId && d.EntityId == commit.EntityId && d.TenantId == commit.TenantId) .Limit(1) .SelectAll(useAsterisk: true), CancellationToken.None).ConfigureFalse(); return(new AppendResult(existing)); } if (ex.Message.Contains(DuplicateVersion)) { throw new ConcurrencyException(); } throw; } } } }