public XbimReadWriteTransaction BeginTransaction(string operationName) { if (InverseCache != null) { throw new XbimException("Transaction can't be open when cache is in operation."); } if (_editTransactionEntityCursor != null) { throw new XbimException("Attempt to begin another transaction whilst one is already running"); } try { //check if write permission upgrade is required _editTransactionEntityCursor = InstanceCache.GetWriteableEntityTable(); InstanceCache.BeginCaching(); var txn = new XbimReadWriteTransaction(this, _editTransactionEntityCursor.BeginLazyTransaction(), operationName); CurrentTransaction = txn; return(txn); } catch (Exception e) { throw new XbimException("Failed to create ReadWrite transaction", e); } }
protected override void BeginParse() { _binaryWriter = new BinaryWriter(new MemoryStream(0x7FFF)); _toStore = new BlockingCollection <Tuple <int, short, List <int>, byte[], bool> >(512); if (_modelCache.IsCaching) { _toProcess = new BlockingCollection <Tuple <int, Type, byte[]> >(); _cacheProcessor = Task.Factory.StartNew(() => { try { // Consume the BlockingCollection while (!_toProcess.IsCompleted) { Tuple <int, Type, byte[]> h; if (_toProcess.TryTake(out h)) { _modelCache.GetOrCreateInstanceFromCache(h.Item1, h.Item2, h.Item3); } } } catch (InvalidOperationException) { } } ); } _storeProcessor = Task.Factory.StartNew(() => { using (var transaction = _table.BeginLazyTransaction()) { while (!_toStore.IsCompleted) { try { Tuple <int, short, List <int>, byte[], bool> h; if (_toStore.TryTake(out h)) { _table.AddEntity(h.Item1, h.Item2, h.Item3, h.Item4, h.Item5, transaction); if (_toStore.IsCompleted) { _table.WriteHeader(Header); } long remainder = _entityCount % TransactionBatchSize; if (remainder == TransactionBatchSize - 1) { transaction.Commit(); transaction.Begin(); } } } catch (SystemException) { // An InvalidOperationException means that Take() was called on a completed collection //OperationCanceledException can also be called } } transaction.Commit(); } } ); }