Couch base lite Index超出范围

时间:2015-07-27 06:04:06

标签: c# foreach task-parallel-library couchbase-lite

我正在尝试添加/更新沙发基础精简数据库从远程服务器获取数据 作为批处理但是当我尝试将数据添加到数据库时出现以下错误,我在第一次迭代中没有得到它。

at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.set_Item(TKey key, TValue value)
at Couchbase.Lite.Database.GetDocument(String id)
at Couchbase.Lite.Database.GetExistingDocument(String id)
at Sys.Offline.DAL.CouchInventoryRepository.

我的电话如下

public  bool FullSync()
{
    IInventoryFetcher fullfetcher = new InventoryFetcher();
    SyncData data=null;          

    do
    {
        var bucket = new ConcurrentDictionary<string, 
        ConcurrentDictionary<string, object>>();
        var skubucket = new ConcurrentDictionary<string,
        ConcurrentDictionary<string, object>>();

        // Create bucket dictionaries 
        for (int slot = 0; slot < 100; slot++)
        {
             bucket.TryAdd(slot.ToString() + _bucketDocSuffix, new 
             ConcurrentDictionary<string, object>());
             skubucket.TryAdd(slot.ToString() + _bucketDocSuffixSku, new
             ConcurrentDictionary<string, object>());
         }

         data = fullfetcher.GetInventory();

         if(data == null) break;

         // For each UPC item calculate the bucket 
         Parallel.ForEach(data.UPCSkuMap, item =>
         {                    
              // calculate the bucket slot
              var bucketSlot = (int)(Int64.Parse(item.Key.Substring(5, 6)) % 100);

              if (!bucket[bucketSlot.ToString() + 
              _bucketDocSuffix].TryAdd(item.Key, item.Value))
                {
                    //if (!bucket[bucketSlot.ToString() + 
                    _bucketDocSuffix].ContainsKey(item.Key))
                        System.Diagnostics.Debugger.Break();
                }
            });


            Parallel.ForEach(data.SkuMap, item =>
            {

                // calculate the bucket slot
                var bucketSlot = (int)(Int64.Parse(item.Value[_slotfield].ToString()) % 100);

                if (!skubucket[bucketSlot.ToString() + 
                    _bucketDocSuffixSku].TryAdd(item.Key, item.Value))
                {
                    //if (!bucket[bucketSlot.ToString() + _bucketDocSuffix].ContainsKey(item.Key))
                    System.Diagnostics.Debugger.Break();
                }
            });

          if(! _database.RunInTransaction(new RunInTransactionDelegate(() =>
          {
                 return UpdateData(bucket, skubucket);
          })))
            {
                //TODO Log
            }          
        } while (!data.IsCompleted);

        return true;
}

public bool UpdateData(IDictionary<string, ConcurrentDictionary<string, object>> itemDictionary, IDictionary<string, ConcurrentDictionary<string, object>> skuDictionary)
{
    try
    {

        Parallel.ForEach(itemDictionary, item =>
        {
             if (item.Value.Count == 0) return;
             Document document=null;
             try
             {
                  document = _database.GetExistingDocument(item.Key);
             }
             catch(IndexOutOfRangeException ex)
             {
                 System.Diagnostics.Debugger.Break();
             }

             if (document == null)
                    document = _database.GetDocument(item.Key);
             else
                 if (!item.Value.TryAdd("_rev", document.CurrentRevisionId))
                 {
                     System.Diagnostics.Debugger.Break();
                 }

             var rev = document.PutProperties(item.Value);
         });

         Parallel.ForEach(skuDictionary, item =>
         {
             if (item.Value.Count == 0) return;
             var document = _database.GetExistingDocument(item.Key);

             if (document == null)
                 document = _database.GetDocument(item.Key);
             else
                 if (!item.Value.TryAdd("_rev", document.CurrentRevisionId))
                 {
                     System.Diagnostics.Debugger.Break();
                 }

             var rev = document.PutProperties(item.Value);
         });
     }
     catch (CouchbaseLiteException chEX) 
     {
         //TODO LOG...
         return false;  
     }
     return true;       
}

SyncData如下

public class SyncData
{
    // key - sku , value - {"itemnumber":"0000000","AgeCode":null,"Hierarchy":"10925359","Id":"SK100000","Name":"CHRIS ABCD","Parent":"SKU00000","Price":"10","Taxcode":"NaN","Type":"6","upc":["924763541015","529713449709","883357620409"]}
    public IDictionary<string, JObject> SkuMap { get; private set; }

    // Key - UPC  value {skus:[{sku:xx,itemnumber:xxx}]}
    public IDictionary<string, JObject> UPCSkuMap { get; private set; }

    public bool IsCompleted { get; private set; }

    public SyncData(IDictionary<string, JObject> skuMap, IDictionary<string, JObject> upcSkuMap, bool isCompleted=false)
    {
        SkuMap = skuMap;
        UPCSkuMap = upcSkuMap;
        IsCompleted = isCompleted;

    }        
}

0 个答案:

没有答案