Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Couchbase;
- using Couchbase.Configuration.Client;
- using Couchbase.Core;
- using CouchBomb.CouchModel;
- using CouchBombBetter.APSS;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace CouchBombBetter
- {
- class Program
- {
- static void Main(string[] args)
- {
- seedItem("receiptItem");
- }
- private static void seedItem(string itemType)
- {
- switch (itemType.ToLower())
- {
- case "receiptitem":
- seedReceiptItem();
- break;
- }
- }
- private static void seedReceiptItem()
- {
- APSSEntities1 sqlDB = new APSSEntities1();
- var config = setupCouchBaseConfig();
- Console.Write("What is the batch size?\n");
- int batchSize = Convert.ToInt16(Console.ReadLine());
- int tableCount = sqlDB.ReceiptItems.Count();
- int batchLoopSize = (int)Math.Ceiling(tableCount/(double)batchSize);
- Console.Write("What is starting site\n");
- int minSite = Convert.ToInt16(Console.ReadLine());
- Console.Write("What is ending site\n");
- int maxSite = Convert.ToInt16(Console.ReadLine());
- try {
- for (int j = 0; j < batchLoopSize; j++)
- {
- sqlDB = new APSSEntities1();
- var results = sqlDB.ReceiptItems.OrderBy(x => x.ID).Skip(j * batchSize).Take(batchSize).ToList();
- for (int i = minSite; i <= maxSite; i++)
- {
- var dict = new Dictionary<string, dynamic>();
- foreach (var result in results)
- {
- ReceiptItemCB r = new ReceiptItemCB(result, i, null);
- string key = "receipt_item:" + i.ToString() + ":" + result.ID.ToString();
- dict.Add(key, r);
- }
- sqlDB.Dispose();
- using (var cluster = new Cluster(config))
- {
- IBucket bucket = null;
- try
- {
- bucket = cluster.OpenBucket("myhoney");
- var multiUpsert = bucket.Upsert(dict);
- foreach (var item in multiUpsert)
- {
- if (!item.Value.Success)
- {
- Console.WriteLine("FAILED");
- }
- }
- //Console.Clear();
- } catch (Exception ex)
- {
- Console.WriteLine(ex.Message);
- Console.WriteLine(ex.InnerException.Message);
- }
- finally
- {
- if (bucket != null)
- {
- cluster.CloseBucket(bucket);
- }
- }
- }
- }
- double percentComplete = Math.Round(((double)j / (double)batchLoopSize), 6) * 100;
- Console.WriteLine(percentComplete + "%");
- }
- }catch(Exception ex) {
- Console.WriteLine(ex.Message);
- Console.WriteLine(ex.InnerException.Message);
- }
- // Finished
- Console.WriteLine("Seeding is done");
- Console.ReadLine();
- }
- private static ClientConfiguration setupCouchBaseConfig()
- {
- var config = new ClientConfiguration
- {
- Servers = new List<Uri>
- {
- new Uri("##########################################"),
- new Uri("##########################################")
- },
- UseSsl = false,
- DefaultOperationLifespan = 2000,
- BucketConfigs = new Dictionary<string, BucketConfiguration>
- {
- {"myhoney", new BucketConfiguration
- {
- PoolConfiguration = new PoolConfiguration
- {
- MaxSize = 10,
- MinSize = 5,
- SendTimeout = 120000
- }
- }}
- }
- };
- return config;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement