Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObjectEnumerator.cs
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using System.Text;
6using System.Threading.Tasks;
7using Waher.Events;
10
12{
18 {
19 private readonly NeuroLedgerProvider provider = null;
20 private ObjectSerializer serializer = null;
21 private IAsyncEnumerator<BlockReference> blockEnumerator;
22 private BlockReader reader = null;
23 private Entry currentEntry = null;
24 private T currentObject = default;
25 private LedgerEntry<T> currentDecodedEntry = null;
26 private bool endOfBlock;
27
34 NeuroLedgerProvider Provider)
35 {
36 this.blockEnumerator = BlockEnumerator;
37 this.endOfBlock = true;
38 this.provider = Provider;
39 }
40
47 public static async Task<ObjectEnumerator<T>> Create(IAsyncEnumerator<BlockReference> BlockEnumerator,
48 NeuroLedgerProvider Provider)
49 {
50 return new ObjectEnumerator<T>(BlockEnumerator, Provider)
51 {
52 serializer = await Provider.GetObjectSerializerEx(typeof(T))
53 };
54 }
55
59 public T Current => this.currentObject;
60
64 object IEnumerator.Current => this.currentObject;
65
69 public Entry CurrentEntry => this.currentEntry;
70
74 ILedgerEntry<T> IEnumerator<ILedgerEntry<T>>.Current
75 {
76 get
77 {
78 if (this.currentDecodedEntry is null)
79 this.currentDecodedEntry = new LedgerEntry<T>(this.currentEntry, this.currentObject);
80
81 return this.currentDecodedEntry;
82 }
83 }
84
88 public void Dispose()
89 {
90 this.reader?.Dispose();
91 this.reader = null;
92
93 this.blockEnumerator?.Dispose();
94 this.blockEnumerator = null;
95 }
96
102 public bool MoveNext()
103 {
104 return this.MoveNextAsync().Result;
105 }
106
111 public void Reset()
112 {
113 this.blockEnumerator.Reset();
114 this.endOfBlock = true;
115 }
116
127 public async Task<bool> MoveNextAsync()
128 {
129 while (true)
130 {
131 while (this.endOfBlock)
132 {
133 if (!await this.blockEnumerator.MoveNextAsync())
134 return false;
135
136 BlockReference BlockRef = this.blockEnumerator.Current;
137 string FileName = this.provider.GetFullFileName(BlockRef.FileName);
138
139 try
140 {
141 if (File.Exists(FileName))
142 {
143 this.reader?.Dispose();
144 this.reader = null;
145
146 this.reader = await BlockReader.CreateAsync(FileName, this.provider);
147 this.endOfBlock = this.reader.EOF;
148 }
149 }
150 catch (Exception ex)
151 {
152 Log.Exception(ex);
153
154 this.reader?.Dispose();
155 this.reader = null;
156
157 this.endOfBlock = true;
158 }
159 }
160
161 this.currentDecodedEntry = null;
162 this.currentEntry = await this.reader.ReadEntryAsync();
163 this.endOfBlock = this.reader.EOF;
164
165 BinaryDeserializer Reader = new BinaryDeserializer(this.reader.CollectionName, Encoding.UTF8, this.currentEntry.Data, 0);
166 try
167 {
168 object Obj = await this.serializer.Deserialize(Reader, ObjectSerializer.TYPE_OBJECT, false);
169 if (Obj is T Typed)
170 {
171 this.currentObject = Typed;
172 return true;
173 }
174 }
175 catch (Exception)
176 {
177 // Ignore. Not compatible.
178 }
179 }
180 }
181 }
182}
Static class managing the application event log. Applications and services log events on this static ...
Definition: Log.cs:13
static void Exception(Exception Exception, string Object, string Actor, string EventId, EventLevel Level, string Facility, string Module, params KeyValuePair< string, object >[] Tags)
Logs an exception. Event type will be determined by the severity of the exception.
Definition: Log.cs:1647
static async Task< BlockReader > CreateAsync(string FileName, NeuroLedgerProvider Provider)
Creates a block reader.
Definition: BlockReader.cs:79
async Task< Entry > ReadEntryAsync()
Reads an entry from the block.
Definition: BlockReader.cs:272
bool EOF
If the current position is at the end of the file.
Definition: BlockReader.cs:218
void Dispose()
IDisposable.Dispose
Definition: BlockReader.cs:253
Represents an entry in a block or bucket file.
Definition: Entry.cs:11
Represents a decoded ledger entry.
Definition: LedgerEntry.cs:12
Task< ObjectSerializer > GetObjectSerializerEx(object Object)
Gets the object serializer corresponding to a specific object.
Enumeratres through objects available in a series of blocks.
T Current
Gets the element in the collection at the current position of the enumerator.
async Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
bool MoveNext()
Advances the enumerator to the next element of the collection.
void Reset()
Sets the enumerator to its initial position, which is before the first element in the collection.
static async Task< ObjectEnumerator< T > > Create(IAsyncEnumerator< BlockReference > BlockEnumerator, NeuroLedgerProvider Provider)
Creates an object enumerator from a block enumerator.
Contains a reference to a block in the ledger.
Manages binary deserialization of data.
Serializes a class, taking into account attributes defined in Attributes.
virtual async Task< object > Deserialize(IDeserializer Reader, uint? DataType, bool Embedded)
Deserializes a value.
Interface for asynchronous enumerators.
Task< bool > MoveNextAsync()
Advances the enumerator to the next element of the collection.
Interface for ledger entries.
Definition: ILedgerEntry.cs:36
Enumerator of ledger entries