Advertisement
Guest User

Sample Upsert Program

a guest
Jun 10th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.87 KB | None | 0 0
  1. using Couchbase;
  2. using Couchbase.Configuration.Client;
  3. using Couchbase.Core;
  4. using CouchBomb.CouchModel;
  5. using CouchBombBetter.APSS;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11.  
  12. namespace CouchBombBetter
  13. {
  14.     class Program
  15.     {
  16.         static void Main(string[] args)
  17.         {
  18.  
  19.             seedItem("receiptItem");
  20.         }
  21.  
  22.         private static void seedItem(string itemType)
  23.         {
  24.             switch (itemType.ToLower())
  25.             {
  26.                 case "receiptitem":
  27.                     seedReceiptItem();
  28.                     break;
  29.             }
  30.         }
  31.  
  32.         private static void seedReceiptItem()
  33.         {
  34.             APSSEntities1 sqlDB = new APSSEntities1();
  35.             var config = setupCouchBaseConfig();
  36.  
  37.             Console.Write("What is the batch size?\n");
  38.             int batchSize = Convert.ToInt16(Console.ReadLine());
  39.             int tableCount = sqlDB.ReceiptItems.Count();
  40.             int batchLoopSize = (int)Math.Ceiling(tableCount/(double)batchSize);
  41.  
  42.             Console.Write("What is starting site\n");
  43.             int minSite = Convert.ToInt16(Console.ReadLine());
  44.  
  45.             Console.Write("What is ending site\n");
  46.             int maxSite = Convert.ToInt16(Console.ReadLine());
  47.             try {
  48.                 for (int j = 0; j < batchLoopSize; j++)
  49.                 {
  50.                     sqlDB = new APSSEntities1();
  51.                     var results = sqlDB.ReceiptItems.OrderBy(x => x.ID).Skip(j * batchSize).Take(batchSize).ToList();
  52.  
  53.                     for (int i = minSite; i <= maxSite; i++)
  54.                     {
  55.                         var dict = new Dictionary<string, dynamic>();
  56.                         foreach (var result in results)
  57.                         {
  58.                             ReceiptItemCB r = new ReceiptItemCB(result, i, null);
  59.                             string key = "receipt_item:" + i.ToString() + ":" + result.ID.ToString();
  60.                             dict.Add(key, r);
  61.                         }
  62.                         sqlDB.Dispose();
  63.  
  64.                         using (var cluster = new Cluster(config))
  65.                         {
  66.                             IBucket bucket = null;
  67.                             try
  68.                             {
  69.                                 bucket = cluster.OpenBucket("myhoney");
  70.                                 var multiUpsert = bucket.Upsert(dict);
  71.                                 foreach (var item in multiUpsert)
  72.                                 {
  73.                                     if (!item.Value.Success)
  74.                                     {
  75.                                         Console.WriteLine("FAILED");
  76.                                     }
  77.                                 }
  78.                                 //Console.Clear();
  79.                             } catch (Exception ex)
  80.                             {
  81.                                 Console.WriteLine(ex.Message);
  82.                                 Console.WriteLine(ex.InnerException.Message);
  83.                             }
  84.                             finally
  85.                             {
  86.                                 if (bucket != null)
  87.                                 {
  88.                                     cluster.CloseBucket(bucket);
  89.                                 }
  90.                             }
  91.                         }
  92.                     }
  93.                     double percentComplete = Math.Round(((double)j / (double)batchLoopSize), 6) * 100;
  94.                     Console.WriteLine(percentComplete + "%");
  95.  
  96.                 }
  97.             }catch(Exception ex) {
  98.                 Console.WriteLine(ex.Message);
  99.                 Console.WriteLine(ex.InnerException.Message);
  100.             }          
  101.             // Finished
  102.             Console.WriteLine("Seeding is done");
  103.             Console.ReadLine();
  104.         }
  105.  
  106.         private static ClientConfiguration setupCouchBaseConfig()
  107.         {
  108.             var config = new ClientConfiguration
  109.             {
  110.                 Servers = new List<Uri>
  111.                 {
  112.                      new Uri("##########################################"),
  113.                      new Uri("##########################################")
  114.                 },
  115.                 UseSsl = false,
  116.                 DefaultOperationLifespan = 2000,
  117.                 BucketConfigs = new Dictionary<string, BucketConfiguration>
  118.                 {
  119.                     {"myhoney", new BucketConfiguration
  120.                     {
  121.                         PoolConfiguration = new PoolConfiguration
  122.                         {
  123.                             MaxSize = 10,
  124.                             MinSize = 5,
  125.                             SendTimeout = 120000
  126.  
  127.                         }
  128.                     }}
  129.                 }
  130.             };
  131.             return config;
  132.         }
  133.  
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement