/// <summary> /// Every table inside of the transaction before calling Transaction Commit, goes to this in-memory dictionary /// </summary> /// <param name="tranNumber"></param> /// <param name="table"></param> public void AddTableForTransaction(ulong tranNumber, ITransactable table) { _sync_transactionsTables.EnterWriteLock(); try { Dictionary<string, ITransactable> tbls = null; _transactionsTables.TryGetValue(tranNumber, out tbls); if (tbls == null) { tbls = new Dictionary<string, ITransactable>(); tbls.Add(table.TableName, table); _transactionsTables.Add(tranNumber, tbls); } else { if (!tbls.ContainsKey(table.TableName)) tbls.Add(table.TableName, table); } } catch (System.Exception ex) { //Called from TransactionCoordinator.Commit throw ex; } finally { _sync_transactionsTables.ExitWriteLock(); } }
/// <summary> /// Adds a table which will take place in transaction operations. /// Reserved has value null, Real (which are acquired by Transaction for Write) has ITransactable filled. /// ITransactable = null, gives to differ from toched and reserved. /// </summary> /// <param name="tableName"></param> /// <param name="table">if null - will be added to Reservation table</param> public void AddTransactionWriteTable(string tableName, ITransactable table) { _sync_transactionWriteTables.EnterWriteLock(); try { if (!_transactionWriteTables.ContainsKey(tableName)) { this._transactionWriteTables.Add(tableName, table); transactionWriteTablesCount++; } else { if (_transactionWriteTables[tableName] == null) { _transactionWriteTables[tableName] = table; } } } catch (System.Exception ex) { //CIRCULAR throw ex; } finally { _sync_transactionWriteTables.ExitWriteLock(); } }
public void TestTransactBlock() { SharedMemoryStream shared = new SharedMemoryStream(); FragmentedFile.CreateNew(shared, 512, 100, 2).Dispose(); using (FragmentedFile ff = new FragmentedFile(shared, 512, 100, 2)) { long id; byte[] orig = MakeBytes(255); using (Stream write = ff.Create(out id)) write.Write(orig, 0, orig.Length); Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); byte[] change = MakeBytes(800); using (Stream write = ff.Open(id, FileAccess.Write)) using (ITransactable trans = (ITransactable)write) //the Fragmented File Streams are ITransactable { write.Write(change, 0, change.Length); Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); trans.Commit(); //commit changes so that readers can read Assert.AreEqual(change, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); trans.Rollback(); //rollback even after commit to 'undo' the changes Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); } //once disposed you can no longer rollback, if rollback has not been called commit is implied. Assert.AreEqual(orig, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); } }
/// <summary> /// Every table inside of the transaction before calling Transaction Commit, goes to this in-memory dictionary /// </summary> /// <param name="tranNumber"></param> /// <param name="table"></param> public void AddTableForTransaction(ulong tranNumber, ITransactable table) { _sync_transactionsTables.EnterWriteLock(); try { Dictionary <string, ITransactable> tbls = null; _transactionsTables.TryGetValue(tranNumber, out tbls); if (tbls == null) { tbls = new Dictionary <string, ITransactable>(); tbls.Add(table.TableName, table); _transactionsTables.Add(tranNumber, tbls); } else { if (!tbls.ContainsKey(table.TableName)) { tbls.Add(table.TableName, table); } } } catch (System.Exception ex) { //Called from TransactionCoordinator.Commit throw ex; } finally { _sync_transactionsTables.ExitWriteLock(); } }
public void SetContents(string path, HttpStatusCode status, string contentType, string etag, DateTime?modified, byte[] contents) { if (contents.Length == 0) { Console.Error.WriteLine("{0} - {1} (Content is empty)", (int)status, path); } ContentRecord rec = _data[path]; ITransactable pendingUpdate = null; try { ContentRecord.Builder builder = rec.ToBuilder() .SetContentUri(path) .SetLastCrawled(CrawlTime) .SetLastValid(CrawlTime) .SetHttpStatus((uint)status) ; builder.ClearContentRedirect(); builder.SetContentType(contentType); builder.SetContentLength((uint)contents.Length); if (!String.IsNullOrEmpty(etag)) { builder.SetETag(etag); } string hash = Hash.SHA256(contents).ToString(); if (hash != builder.HashOriginal) { Modified = true; builder.SetHashOriginal(hash); builder.SetDateModified(CrawlTime); pendingUpdate = _data.WriteContent(builder, contents); } if (_data.AddOrUpdate(path, rec = builder.Build())) { if (pendingUpdate != null) { pendingUpdate.Commit(); pendingUpdate.Dispose(); pendingUpdate = null; } } } finally { if (pendingUpdate != null) { pendingUpdate.Rollback(); pendingUpdate.Dispose(); } } ProcessFileContent(rec, contents); }
public void AddTransactionMember(ITransactable member) { lock (this) { if (!m_transactionMembers.Contains(member)) { m_transactionMembers.Add(member); } } }
public void Commit() { Flush(); ITransactable tstore = _store as ITransactable; if (tstore != null) { tstore.Commit(); } }
public void Rollback() { ITransactable tstore = _store as ITransactable; if (tstore != null) { _serializer = null; ClearCache(); tstore.Rollback(); } }
public void Commit() { lock (_flushSync) // disallow concurrent async flush { Flush(); ITransactable tstore = _store as ITransactable; if (tstore != null) { tstore.Commit(); } } }
public void Rollback() { ITransactable tstore = _store as ITransactable; if (tstore != null) { lock (_flushSync) // disallow concurrent async flush { _serializer = null; ClearCache(); tstore.Rollback(); } } }
public void Rollback() { ITransactable tstore = _store as ITransactable; if (tstore != null) { using (_lock.Write()) { _ordered.Clear(); _cache.Clear(); tstore.Rollback(); } } }
public void Commit() { lock (_writeBehindFunc) { CompleteAsync(); Flush(); ITransactable tstore = _store as ITransactable; if (tstore != null) { tstore.Commit(); } } }
/// <summary> /// Списать со счета /// </summary> /// <param name="amount"></param> public void Withdraw(decimal amount, ITransactable reciever = null) { Amount -= amount; if (reciever != null) { amountTransact?.Invoke(this, reciever, amount); } else { amountWithdrawed?.Invoke(this, amount); } NotifyPropertyChanged(nameof(Amount)); NotifyPropertyChanged(nameof(Income)); }
public void TestTransactWriteAfterCommit() { SharedMemoryStream shared = new SharedMemoryStream(); FragmentedFile.CreateNew(shared, 512, 100, 2).Dispose(); using (FragmentedFile ff = new FragmentedFile(shared, 512, 100, 2)) { long id; byte[] bytes = MakeBytes(255); using (Stream write = ff.Create(out id)) using (ITransactable trans = (ITransactable)write) { write.Write(bytes, 0, bytes.Length); trans.Commit(); write.Write(bytes, 0, bytes.Length); } } }
public void WriteContent(ContentRecord rec, byte[] bytes) { bool[] modified = new bool[1]; modified[0] = false; bool success = Update(rec.ContentUri, r => { ContentRecord.Builder b = r.ToBuilder(); using (ITransactable t = WriteContent(b, bytes)) t.Commit(); ContentRecord newRec = b.Build(); modified[0] = !newRec.Equals(r); return(newRec); } ); if (!success && modified[0]) { throw new ApplicationException("Record not found."); } }
public void CopyTo(ContentStorage writer, Func <ContentRecord, byte[], byte[]> fnprocess) { bool success; foreach (KeyValuePair <string, ContentRecord> item in _content) { ContentRecord.Builder builder = item.Value.ToBuilder(); if (item.Value.HasContentStoreId) { byte[] data = _content.ReadContent(item.Value, true); if (fnprocess != null) { data = fnprocess(item.Value, data); } using (ITransactable trans = writer.WriteContent(builder, data)) { success = Overwrite ? writer.AddOrUpdate(item.Key, builder.Build()) : writer.Add(item.Key, builder.Build()); if (success) { trans.Commit(); } } } else { success = Overwrite ? writer.AddOrUpdate(item.Key, builder.Build()) : writer.Add(item.Key, builder.Build()); } if (!success) { Console.Error.WriteLine("Path already exists " + item.Key); } } }
public void TestRollbackCreate() { SharedMemoryStream shared = new SharedMemoryStream(); FragmentedFile.CreateNew(shared, 512, 100, 2).Dispose(); using (FragmentedFile ff = new FragmentedFile(shared, 512, 100, 2)) { long id; byte[] bytes = MakeBytes(255); using (Stream write = ff.Create(out id)) using (ITransactable trans = (ITransactable)write) { write.Write(bytes, 0, bytes.Length); trans.Commit(); Assert.AreEqual(bytes, IOStream.ReadAllBytes(ff.Open(id, FileAccess.Read))); trans.Rollback(); } AssertThrows <InvalidDataException>(delegate() { ff.Open(id, FileAccess.Read); }); } }
public Database(ITransactable connection) { DatabaseConnection = $@"{new AppSettings().DatabaseLocation}\{connection.DatabaseConnection()}"; }
internal Transaction(ITransactable <TKey, TValue> owner) { this.owner = owner ?? throw new ArgumentNullException(nameof(owner)); }
public AccountToClientMediator(ITransactable client, ITransactable account, bool isWithdraw = false) { IsWithdraw = isWithdraw; Sender = client; Reciever = account; }
private void UpdateTemplate(bool forced) { string tempPath = new Uri(_baseUri, _config.Searching.TemplateUri).NormalizedPathAndQuery(); ContentRecord record; ContentRecord.Builder update; if (_data.TryGetValue(TemplatePath, out record)) { update = record.ToBuilder(); } else { update = _data.New(TemplatePath, DateTime.Now); } ContentRecord template; if (_data.TryGetValue(tempPath, out template)) { if (template.HasContentStoreId && (forced || template.HashOriginal != update.HashOriginal)) { update.SetContentType(template.ContentType); update.SetHashOriginal(template.HashOriginal); update.SetLastCrawled(template.LastCrawled); update.SetLastValid(template.LastValid); update.SetDateModified(DateTime.Now); update.SetHttpStatus(template.HttpStatus); update.ClearContentRedirect(); if (template.HasContentRedirect) { update.SetContentRedirect(update.ContentRedirect); } ContentParser parser = new ContentParser(_data, _baseUri); parser.RelativeUri = true; parser.RewriteUri += uri => new Uri(uri.OriginalString); Uri templateUri = new Uri(_baseUri, SearchTemplate.SearchPath); parser.MakeRelativeUri = (s, d) => templateUri.MakeRelativeUri(d); byte[] mapped = parser.ProcessFile(template, _data.ReadContent(template, true)); string templateHtml = CreateTemplate(Encoding.UTF8.GetString(mapped)); using (ITransactable trans = _data.WriteContent(update, Encoding.UTF8.GetBytes(templateHtml))) { _data.AddOrUpdate(TemplatePath, update.Build()); trans.Commit(); } } } if (!_data.TryGetValue(SearchCssPath, out record)) { ContentRecord cssRecord = _data.New(SearchCssPath, DateTime.Now) .SetContentType("text/css") .SetHttpStatus(200) .Build(); _data.Add(cssRecord.ContentUri, cssRecord); _data.WriteContent(cssRecord, Encoding.UTF8.GetBytes(Properties.Resources.search_css)); } }
/// <summary> /// Adds a table which will take place in transaction operations. /// Reserved has value null, Real (which are acquired by Transaction for Write) has ITransactable filled. /// ITransactable = null, gives to differ from toched and reserved. /// </summary> /// <param name="tableName"></param> /// <param name="table">if null - will be added to Reservation table</param> public void AddTransactionWriteTable(string tableName, ITransactable table) { _sync_transactionWriteTables.EnterWriteLock(); try { if (!_transactionWriteTables.ContainsKey(tableName)) { this._transactionWriteTables.Add(tableName, table); transactionWriteTablesCount++; } else { if (_transactionWriteTables[tableName] == null) _transactionWriteTables[tableName] = table; } } catch (System.Exception ex) { //CIRCULAR throw ex; } finally { _sync_transactionWriteTables.ExitWriteLock(); } }
public AccountToAccountMediator(List <BankAccount> accounts, ITransactable sender) { Accounts = accounts; Sender = sender; }
/// <summary> /// Достать сумму из кармана /// </summary> /// <param name="amount"></param> /// <param name="reciever"></param> public void Withdraw(decimal amount, ITransactable reciever) { Amount -= amount; NotifyPropertyChanged(nameof(Amount)); }
protected virtual void LogTransact(object sender, ITransactable accountReciever, decimal amount) { var accountSender = sender as ITransactable; Log.Add(new LogMessage($"{accountSender.Name} перевод на счет {accountReciever.Name} на сумму: {amount}")); }
public TisTransactionManager(ITransactable member) { m_transactionMembers.Add(member); }
public async void DeleteEntry(ITransactable transactable) { DatabaseConnection = $@"{new AppSettings().DatabaseLocation}\{transactable.DatabaseConnection()}"; await UpdateDatabase(transactable.Delete()); }
public static Transaction CreateTransaction(TransactionType type, ITransactable source, ITransactable target, DateTime date) { Transaction transaction = new Transaction(type); transaction.Source = source; transaction.Target = target; transaction.Date = date; return transaction; }