Advertisement
Guest User

Untitled

a guest
May 2nd, 2021
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. public class BJsonEnumerable<T> : IEnumerator<T>, IEnumerable<T> where T : class
  2. {
  3. public IEnumerator<T> GetEnumerator()
  4. {
  5. if (check) new BJsonEnumerable<T>(this.lite, this.dbFIle, this.maxOffset);
  6. return this;
  7. }
  8.  
  9.  
  10. ILiteQueryable<T> bsonData;
  11. IBsonDataReader bsonDataReader;
  12. LiteDatabase lite;
  13. bool check;
  14. string dbFIle;
  15. public BJsonEnumerable(LiteDatabase bsonData, string dbFIle, int maxOffset = 10000)
  16. {
  17. this.lite = bsonData;
  18. check = false;
  19. this.maxOffset = maxOffset;
  20. this.dbFIle = dbFIle;
  21. }
  22. public void Begin()
  23. {
  24. Dispose();
  25. this.lite = new LiteDatabase(this.dbFIle);
  26. var lite = this.lite.GetCollection<T>();
  27. this.bsonData = lite.Query();
  28. this.bsonDataReader = bsonData.ExecuteReader();
  29. bsonDataReader = bsonData.Offset(1000000).ExecuteReader();
  30.  
  31. }
  32.  
  33. int offset = 0;
  34. int abstract_offset = 0;
  35. int maxOffset;
  36. public T Current { get { return current; } set { current = value; } }
  37. private T current;
  38.  
  39. object IEnumerator.Current { get { return current; } }
  40.  
  41.  
  42. public void Dispose()
  43. {
  44. this.current = null;
  45. this.bsonDataReader = null;
  46. this.lite.Dispose();
  47. // GC.ReRegisterForFinalize(this.lite);
  48. this.lite = null;
  49.  
  50. GC.Collect(GC.MaxGeneration);
  51.  
  52. }
  53.  
  54. public bool MoveNext()
  55. {
  56.  
  57. this.check = true;
  58. if (bsonDataReader == null) Reset();
  59. bool read = bsonDataReader.Read();
  60. if (read == false)
  61. {
  62. this.current = default;
  63. return false;
  64. }
  65. var bJson = bsonDataReader.Current;
  66.  
  67. string json = bJson.ToString();
  68. this.current = JsonConvert.DeserializeObject<T>(json);
  69. offset++;
  70. abstract_offset++;
  71. if (abstract_offset > maxOffset)
  72. {
  73. abstract_offset = 0;
  74. Begin();
  75. }
  76. return read;
  77. }
  78.  
  79. public void Reset()
  80. {
  81. this.current = null;
  82. if (this.bsonDataReader == null) Begin();
  83.  
  84. }
  85.  
  86. IEnumerator IEnumerable.GetEnumerator()
  87. {
  88. if (check) new BJsonEnumerable<T>(this.lite, this.dbFIle, this.maxOffset);
  89. return this;
  90. }
  91. }
  92. static class ExtMethod
  93. {
  94. public static IEnumerable<T> AsEnumerable<T>(this LiteDatabase lite, string fileDB) where T : class
  95. {
  96. return new BJsonEnumerable<T>(lite, fileDB, 1000);
  97. }
  98.  
  99.  
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement