Guest User

RavenDb OutOfMemoryException with batch export loop

a guest
Jan 9th, 2013
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. namespace ImdbLoader
  2. {
  3.     class Program
  4.     {
  5.         private static DocumentStore Store;
  6.         private static Encoding encoding = Encoding.GetEncoding(1252);
  7.  
  8.         static void Main(string[] args)
  9.         {
  10.             Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
  11.             Reinitialize();
  12.  
  13.             Test();
  14.  
  15.             Console.WriteLine("Press any key to close...");
  16.             Console.ReadLine();
  17.         }
  18.  
  19.         private static void Reinitialize()
  20.         {
  21.             if (Store != null)
  22.             {
  23.                 Store.Dispose();
  24.                 Store = null;
  25.             }
  26.  
  27.             Store = new DocumentStore { ConnectionStringName = "RavenDB" };
  28.             Store.Conventions.IdentityPartsSeparator = "-";
  29.             Store.Initialize();
  30.         }
  31.  
  32.         private static void Test()
  33.         {
  34.             Console.WriteLine(@"Exporting all MediaIds to file...");
  35.             var mediaIdsPath = Path.GetTempFileName();
  36.             var mediaIdsOrderedPath = Path.GetTempFileName();
  37.             using (var f = File.Open(mediaIdsPath, FileMode.Truncate, FileAccess.Write, FileShare.Read))
  38.             {
  39.                 using (var sw = new StreamWriter(f, encoding))
  40.                 {
  41.                     int offset = 0, num = 0, i = 0, batchSize = 1024;
  42.  
  43.                     while (true)
  44.                     {
  45.                         if ((num = WriteDocs(offset, batchSize, sw)) == 0) break;
  46.                         offset += num;
  47.  
  48.                         //uncomment to fix OutOfMemoryException
  49.                         //if (++i % 10 == 0) Reinitialize();
  50.                     }
  51.                 }
  52.             }
  53.         }
  54.  
  55.         private static int WriteDocs(int offset, int batchSize, StreamWriter sw)
  56.         {
  57.             int num = 0;
  58.  
  59.             using (var session = Store.OpenSession())
  60.             {
  61.                 using (var foo = session.Advanced.DocumentStore.DisableAggressiveCaching())
  62.                 {
  63.                     var batch = session.Query<ImdbMedia>("Raven/DocumentsByEntityName")
  64.                         .Skip(offset)
  65.                         .Take(batchSize)
  66.                         .Select(x => new { GlobalId = x.Id, ImdbFullTitle = x.ImdbFullTitle });
  67.  
  68.                     foreach (var item in batch)
  69.                     {
  70.                         sw.Write(string.Format("{0}{1}\t\t\t{2}", (offset + num++ > 0 ? "\n" : ""), item.ImdbFullTitle, item.GlobalId));
  71.                     }
  72.  
  73.                     sw.Flush();
  74.                 }
  75.             }
  76.  
  77.             return num;
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment