Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BJsonEnumerable<T> : IEnumerator<T>, IEnumerable<T> where T : class
- {
- public IEnumerator<T> GetEnumerator()
- {
- if (check) new BJsonEnumerable<T>(this.lite, this.dbFIle, this.maxOffset);
- return this;
- }
- ILiteQueryable<T> bsonData;
- IBsonDataReader bsonDataReader;
- LiteDatabase lite;
- bool check;
- string dbFIle;
- public BJsonEnumerable(LiteDatabase bsonData, string dbFIle, int maxOffset = 10000)
- {
- this.lite = bsonData;
- check = false;
- this.maxOffset = maxOffset;
- this.dbFIle = dbFIle;
- }
- public void Begin()
- {
- Dispose();
- this.lite = new LiteDatabase(this.dbFIle);
- var lite = this.lite.GetCollection<T>();
- this.bsonData = lite.Query();
- this.bsonDataReader = bsonData.ExecuteReader();
- bsonDataReader = bsonData.Offset(1000000).ExecuteReader();
- }
- int offset = 0;
- int abstract_offset = 0;
- int maxOffset;
- public T Current { get { return current; } set { current = value; } }
- private T current;
- object IEnumerator.Current { get { return current; } }
- public void Dispose()
- {
- this.current = null;
- this.bsonDataReader = null;
- this.lite.Dispose();
- // GC.ReRegisterForFinalize(this.lite);
- this.lite = null;
- GC.Collect(GC.MaxGeneration);
- }
- public bool MoveNext()
- {
- this.check = true;
- if (bsonDataReader == null) Reset();
- bool read = bsonDataReader.Read();
- if (read == false)
- {
- this.current = default;
- return false;
- }
- var bJson = bsonDataReader.Current;
- string json = bJson.ToString();
- this.current = JsonConvert.DeserializeObject<T>(json);
- offset++;
- abstract_offset++;
- if (abstract_offset > maxOffset)
- {
- abstract_offset = 0;
- Begin();
- }
- return read;
- }
- public void Reset()
- {
- this.current = null;
- if (this.bsonDataReader == null) Begin();
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- if (check) new BJsonEnumerable<T>(this.lite, this.dbFIle, this.maxOffset);
- return this;
- }
- }
- static class ExtMethod
- {
- public static IEnumerable<T> AsEnumerable<T>(this LiteDatabase lite, string fileDB) where T : class
- {
- return new BJsonEnumerable<T>(lite, fileDB, 1000);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement