Example #1
0
 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();
         }
     }
                                             );
 }