Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
CollectionSource.cs
1using System;
3using System.Text;
4using System.Threading.Tasks;
12
14{
19 {
20 private readonly Dictionary<string, bool> isLabel = new Dictionary<string, bool>();
21 private readonly string collectionName;
22 private readonly string alias;
23
29 public CollectionSource(string CollectionName, string Alias)
30 {
31 this.collectionName = CollectionName;
32 this.alias = Alias;
33 }
34
46 public async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
47 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
48 {
49 if (Generic)
50 {
51 IEnumerable<GenericObject> Objects = await Database.Find<GenericObject>(this.collectionName, Offset, Top,
52 await TypeSource.ConvertAsync(Where, Variables, this.Name), TypeSource.Convert(Order));
53
54 return new SynchEnumerator(Objects.GetEnumerator());
55 }
56 else
57 {
58 IEnumerable<object> Objects = await Database.Find(this.collectionName, Offset, Top,
59 await TypeSource.ConvertAsync(Where, Variables, this.Name), TypeSource.Convert(Order));
60
61 return new SynchEnumerator(Objects.GetEnumerator());
62 }
63 }
64
78 public async Task<bool> Process(IProcessor<object> Processor, int Offset, int Top, bool Generic,
79 ScriptNode Where, Variables Variables, KeyValuePair<VariableReference, bool>[] Order,
80 ScriptNode Node)
81 {
82 if (Generic)
83 {
84 return await Database.Process(new TypedObjectProcessor<GenericObject>(Processor), this.collectionName, Offset, Top,
85 await TypeSource.ConvertAsync(Where, Variables, this.Name), TypeSource.Convert(Order));
86 }
87 else
88 {
89 return await Database.Process(Processor, this.collectionName, Offset, Top,
90 await TypeSource.ConvertAsync(Where, Variables, this.Name), TypeSource.Convert(Order));
91 }
92 }
93
105 public async Task<int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
106 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
107 {
108 Filter Filter = await TypeSource.ConvertAsync(Where, Variables, this.Name);
109
110 if (Filter is null && Offset == 0 && Top == int.MaxValue)
111 {
112 await Database.Clear(this.Name);
113 return null; // TODO: Get number of objects in collection before clearing it.
114 }
115 else if (Lazy)
116 {
117 await Database.DeleteLazy(this.collectionName, Offset, Top, Filter, TypeSource.Convert(Order));
118 return null;
119 }
120 else
121 return await Database.Delete(this.collectionName, Offset, Top, Filter, TypeSource.Convert(Order));
122 }
123
129 public Task Update(bool Lazy, IEnumerable<object> Objects)
130 {
131 return Lazy ? Database.UpdateLazy(Objects) : Database.Update(Objects);
132 }
133
139 public Task Insert(bool Lazy, object Object)
140 {
141 return Lazy ? Database.InsertLazy(Object) : Database.Insert(Object);
142 }
143
147 public string CollectionName => this.collectionName;
148
152 public string TypeName
153 {
154 get { return string.Empty; }
155 }
156
160 public string Name
161 {
162 get => string.IsNullOrEmpty(this.alias) ? this.collectionName : this.alias;
163 }
164
170 public bool IsSource(string Name)
171 {
172 return
173 string.Compare(this.collectionName, Name, true) == 0 ||
174 string.Compare(this.alias, Name, true) == 0;
175 }
176
182 public async Task<bool> IsLabel(string Label)
183 {
184 bool Result;
185
186 lock (this.isLabel)
187 {
188 if (this.isLabel.TryGetValue(Label, out Result))
189 return Result;
190 }
191
192 Result = await Database.IsLabel(this.collectionName, Label);
193
194 lock (this.isLabel)
195 {
196 this.isLabel[Label] = Result;
197 }
198
199 return Result;
200 }
201
207 public async Task CreateIndex(string Name, string[] Fields)
208 {
209 await Database.AddIndex(this.collectionName, Fields);
210
211 StringBuilder sb = new StringBuilder();
212 foreach (string Field in Fields)
213 sb.AppendLine(Field);
214
215 await RuntimeSettings.SetAsync("SQL.INDEX." + this.collectionName + "." + Name, sb.ToString());
216 }
217
223 public async Task<bool> DropIndex(string Name)
224 {
225 string Key = "SQL.INDEX." + this.collectionName + "." + Name;
226 string s = await RuntimeSettings.GetAsync(Key, string.Empty);
227 string[] Fields = s.Split(crlf, StringSplitOptions.RemoveEmptyEntries);
228 if (Fields.Length == 0)
229 return false;
230
231 await Database.RemoveIndex(this.collectionName, Fields);
232 await RuntimeSettings.SetAsync(Key, string.Empty);
233
234 return true;
235 }
236
237 private static readonly char[] crlf = new char[] { '\r', '\n' };
238
242 public Task DropCollection()
243 {
244 return Database.DropCollection(this.collectionName);
245 }
246
247 }
248}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static async Task DeleteLazy(object Object)
Deletes an object in the database, if unlocked. If locked, object will be deleted at next opportunity...
Definition: Database.cs:1330
static Task RemoveIndex(string CollectionName, string[] FieldNames)
Removes an index from a collection, if one exist.
Definition: Database.cs:2231
static Task< bool > Process(IProcessor< object > Processor, string Collection, params string[] SortOrder)
Processes objects in a given collection.
Definition: Database.cs:688
static Task< bool > IsLabel(string Collection, string Label)
Checks if a string is a label in a given collection.
Definition: Database.cs:2387
static async Task InsertLazy(object Object)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
Definition: Database.cs:147
static Task AddIndex(string CollectionName, string[] FieldNames)
Adds an index to a collection, if one does not already exist.
Definition: Database.cs:2220
static async Task UpdateLazy(object Object)
Updates an object in the database, if unlocked. If locked, object will be updated at next opportunity...
Definition: Database.cs:1261
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:1211
static async Task Delete(object Object)
Deletes an object in the database.
Definition: Database.cs:1291
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
static Task DropCollection(string CollectionName)
Drops a collection, if it exist.
Definition: Database.cs:2415
static async Task Clear(string CollectionName)
Clears a collection of all objects.
Definition: Database.cs:1965
Base class for all filter classes.
Definition: Filter.cs:15
Generic object. Contains a sequence of properties.
Static class managing persistent settings.
static async Task< string > GetAsync(string Key, string DefaultValue)
Gets a string-valued setting.
static async Task< bool > SetAsync(string Key, string Value)
Sets a string-valued setting.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
Processor of generic objects of type GenericObject.
Data Source defined by a collection name
async Task< bool > DropIndex(string Name)
Drops an index from the source.
CollectionSource(string CollectionName, string Alias)
Data Source defined by a collection name
Task Update(bool Lazy, IEnumerable< object > Objects)
Updates a set of objects.
async Task< bool > IsLabel(string Label)
Checks if the label is a label in the source.
Task Insert(bool Lazy, object Object)
Inserts an object.
async Task< IResultSetEnumerator > Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds objects matching filter conditions in Where .
async Task CreateIndex(string Name, string[] Fields)
Creates an index in the source.
async Task< int?> Delete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Deletes a set of objects.
async Task< bool > Process(IProcessor< object > Processor, int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Processes objects matching filter conditions in Where .
Task DropCollection()
Drops the collection specified by the source.
bool IsSource(string Name)
Checks if the name refers to the source.
string CollectionName
Name of corresponding collection.
Data Source defined by a type definition
Definition: TypeSource.cs:22
Collection of variables.
Definition: Variables.cs:25
Interface for processors of objects.
Definition: IProcessor.cs:9
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:14