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;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading.Tasks;
11
13{
18 {
19 private readonly Dictionary<string, bool> isLabel = new Dictionary<string, bool>();
20 private readonly string collectionName;
21 private readonly string alias;
22
28 public CollectionSource(string CollectionName, string Alias)
29 {
30 this.collectionName = CollectionName;
31 this.alias = Alias;
32 }
33
45 public async Task<IResultSetEnumerator> Find(int Offset, int Top, bool Generic, ScriptNode Where, Variables Variables,
46 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
47 {
48 if (Generic)
49 {
50 IEnumerable<GenericObject> Objects = await Database.Find<GenericObject>(this.collectionName, Offset, Top,
51 await TypeSource.ConvertAsync(Where, Variables, this.Name), TypeSource.Convert(Order));
52
53 return new SynchEnumerator(Objects.GetEnumerator());
54 }
55 else
56 {
57 IEnumerable<object> Objects = await Database.Find(this.collectionName, Offset, Top,
58 await TypeSource.ConvertAsync(Where, Variables, this.Name), TypeSource.Convert(Order));
59
60 return new SynchEnumerator(Objects.GetEnumerator());
61 }
62 }
63
75 public async Task<int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables,
76 KeyValuePair<VariableReference, bool>[] Order, ScriptNode Node)
77 {
78 Filter Filter = await TypeSource.ConvertAsync(Where, Variables, this.Name);
79
80 if (Filter is null && Offset == 0 && Top == int.MaxValue)
81 {
82 await Database.Clear(this.Name);
83 return null; // TODO: Get number of objects in collection before clearing it.
84 }
85 else if (Lazy)
86 {
87 await Database.DeleteLazy(this.collectionName, Offset, Top, Filter, TypeSource.Convert(Order));
88 return null;
89 }
90 else
91 {
92 IEnumerable<object> Objects = await Database.FindDelete(this.collectionName, Offset, Top, Filter, TypeSource.Convert(Order));
93 int Count = 0;
94
95 foreach (object Obj in Objects)
96 Count++;
97
98 return Count;
99 }
100 }
101
107 public Task Update(bool Lazy, IEnumerable<object> Objects)
108 {
109 return Lazy ? Database.UpdateLazy(Objects) : Database.Update(Objects);
110 }
111
117 public Task Insert(bool Lazy, object Object)
118 {
119 return Lazy ? Database.InsertLazy(Object) : Database.Insert(Object);
120 }
121
125 public string CollectionName => this.collectionName;
126
130 public string TypeName
131 {
132 get { return string.Empty; }
133 }
134
138 public string Name
139 {
140 get => string.IsNullOrEmpty(this.alias) ? this.collectionName : this.alias;
141 }
142
148 public bool IsSource(string Name)
149 {
150 return
151 string.Compare(this.collectionName, Name, true) == 0 ||
152 string.Compare(this.alias, Name, true) == 0;
153 }
154
160 public async Task<bool> IsLabel(string Label)
161 {
162 bool Result;
163
164 lock (this.isLabel)
165 {
166 if (this.isLabel.TryGetValue(Label, out Result))
167 return Result;
168 }
169
170 Result = await Database.IsLabel(this.collectionName, Label);
171
172 lock (this.isLabel)
173 {
174 this.isLabel[Label] = Result;
175 }
176
177 return Result;
178 }
179
185 public async Task CreateIndex(string Name, string[] Fields)
186 {
187 await Database.AddIndex(this.collectionName, Fields);
188
189 StringBuilder sb = new StringBuilder();
190 foreach (string Field in Fields)
191 sb.AppendLine(Field);
192
193 await RuntimeSettings.SetAsync("SQL.INDEX." + this.collectionName + "." + Name, sb.ToString());
194 }
195
201 public async Task<bool> DropIndex(string Name)
202 {
203 string Key = "SQL.INDEX." + this.collectionName + "." + Name;
204 string s = await RuntimeSettings.GetAsync(Key, string.Empty);
205 string[] Fields = s.Split(crlf, StringSplitOptions.RemoveEmptyEntries);
206 if (Fields.Length == 0)
207 return false;
208
209 await Database.RemoveIndex(this.collectionName, Fields);
210 await RuntimeSettings.SetAsync(Key, string.Empty);
211
212 return true;
213 }
214
215 private static readonly char[] crlf = new char[] { '\r', '\n' };
216
220 public Task DropCollection()
221 {
222 return Database.DropCollection(this.collectionName);
223 }
224
225 }
226}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:19
static Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
Definition: Database.cs:879
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:756
static Task RemoveIndex(string CollectionName, string[] FieldNames)
Removes an index from a collection, if one exist.
Definition: Database.cs:1478
static Task< bool > IsLabel(string Collection, string Label)
Checks if a string is a label in a given collection.
Definition: Database.cs:1592
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:156
static Task AddIndex(string CollectionName, string[] FieldNames)
Adds an index to a collection, if one does not already exist.
Definition: Database.cs:1467
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:687
static async Task Update(object Object)
Updates an object in the database.
Definition: Database.cs:626
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:247
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:95
static Task DropCollection(string CollectionName)
Drops a collection, if it exist.
Definition: Database.cs:1620
static async Task Clear(string CollectionName)
Clears a collection of all objects.
Definition: Database.cs:1206
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
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.
async Task< int?> FindDelete(bool Lazy, int Offset, int Top, ScriptNode Where, Variables Variables, KeyValuePair< VariableReference, bool >[] Order, ScriptNode Node)
Finds and Deletes a set of objects.
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.
Task DropCollection()
Drops the collection from 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:21
Collection of variables.
Definition: Variables.cs:25
Interface for data sources that can be used in SQL statements.
Definition: IDataSource.cs:13