3using MongoDB.Bson.Serialization;
6using System.Collections.Generic;
8using System.Reflection;
9using System.Runtime.ExceptionServices;
10using System.Threading;
11using System.Threading.Tasks;
30 private readonly Dictionary<string, IMongoCollection<BsonDocument>> collections =
new Dictionary<string, IMongoCollection<BsonDocument>>();
31 private readonly Dictionary<Type, IObjectSerializer> serializers =
new Dictionary<Type, IObjectSerializer>();
32 private readonly AutoResetEvent serializerAdded =
new AutoResetEvent(
false);
33 private MongoClient client;
34 private IMongoDatabase database;
36 private string defaultCollectionName;
37 private string lastCollectionName =
null;
38 private IMongoCollection<BsonDocument> lastCollection =
null;
39 private IMongoCollection<BsonDocument> defaultCollection;
48 MongoClientSettings Settings =
new MongoClientSettings();
60 MongoClientSettings Settings =
new MongoClientSettings()
62 Server =
new MongoServerAddress(HostName)
77 MongoClientSettings Settings =
new MongoClientSettings()
79 Server =
new MongoServerAddress(HostName, Port)
98 this.id = Guid.NewGuid().ToString().Replace(
"-",
string.Empty);
99 this.client =
new MongoClient(Settings);
100 this.database = this.client.GetDatabase(DatabaseName);
103 this.defaultCollection = this.
GetCollection(this.defaultCollectionName);
105 ConstructorInfo DefaultConstructor;
113 if (DefaultConstructor is
null)
135 public string Id => this.id;
149 IMongoCollection<BsonDocument> Result;
151 lock (this.collections)
153 if (CollectionName == this.lastCollectionName)
154 Result = this.lastCollection;
157 if (!this.collections.TryGetValue(CollectionName, out Result))
159 Result = this.database.GetCollection<BsonDocument>(CollectionName);
160 this.collections[CollectionName] = Result;
163 this.lastCollection = Result;
164 this.lastCollectionName = CollectionName;
174 public MongoClient
Client => this.client;
194 TypeInfo TI = Type.GetTypeInfo();
196 lock (this.collections)
198 if (this.serializers.TryGetValue(Type, out Result))
203 else if (Type.IsArray)
205 Type ElementType = Type.GetElementType();
207 Type SerializerType = T.MakeGenericType(
new Type[] { ElementType });
210 else if (TI.IsGenericType)
212 Type GT = Type.GetGenericTypeDefinition();
213 if (GT == typeof(Nullable<>))
215 Type NullableType = Type.GenericTypeArguments[0];
217 if (NullableType.GetTypeInfo().IsEnum)
218 Result =
new Serialization.NullableTypes.NullableEnumSerializer(NullableType);
228 if (!(Result is
null))
230 this.serializers[Type] = Result;
231 this.serializerAdded.Set();
241 lock (this.collections)
243 this.serializers[Type] = Result;
244 this.serializerAdded.Set();
247 catch (FileLoadException ex)
253 if (!this.serializerAdded.WaitOne(1000))
254 ExceptionDispatchInfo.Capture(ex).Throw();
256 lock (this.collections)
258 if (this.serializers.TryGetValue(Type, out Result))
285 throw new Exception(
"Objects of type " + Type.FullName +
" must be embedded.");
298 IMongoCollection<BsonDocument> Collection;
300 if (
string.IsNullOrEmpty(CollectionName))
301 Collection = this.defaultCollection;
308 throw new Exception(
"Object already has an Object ID. If updating an object, use the Update method.");
314 BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
315 await Collection.InsertOneAsync(Doc);
323 public Task
Insert(params
object[] Objects)
325 return this.
Insert((IEnumerable<object>)Objects);
332 public async Task
Insert(IEnumerable<object> Objects)
334 Dictionary<string, KeyValuePair<IMongoCollection<BsonDocument>, LinkedList<BsonDocument>>> DocumentsPerCollection =
335 new Dictionary<string, KeyValuePair<IMongoCollection<BsonDocument>, LinkedList<BsonDocument>>>();
337 Type LastType =
null;
339 string CollectionName;
340 string LastCollectionName =
null;
341 IMongoCollection<BsonDocument> Collection;
342 IMongoCollection<BsonDocument> LastCollection =
null;
343 LinkedList<BsonDocument> Documents =
null;
344 BsonDocument Document;
346 foreach (
object Object
in Objects)
348 Type = Object.GetType();
350 if (Type != LastType)
353 CollectionName = Serializer.CollectionName(Object);
356 if (CollectionName == LastCollectionName)
357 Collection = LastCollection;
360 LastCollectionName = CollectionName;
362 if (
string.IsNullOrEmpty(CollectionName))
363 CollectionName = this.defaultCollectionName;
365 if (DocumentsPerCollection.TryGetValue(CollectionName, out KeyValuePair<IMongoCollection<BsonDocument>, LinkedList<BsonDocument>> P))
370 P =
new KeyValuePair<IMongoCollection<BsonDocument>, LinkedList<BsonDocument>>(Collection,
new LinkedList<BsonDocument>());
371 DocumentsPerCollection[CollectionName] = P;
375 LastCollection = Collection;
379 Document = Object.ToBsonDocument(Type, Serializer);
380 Documents.AddLast(Document);
383 foreach (KeyValuePair<IMongoCollection<BsonDocument>, LinkedList<BsonDocument>> P2
in DocumentsPerCollection.Values)
384 await P2.Key.InsertManyAsync(P2.Value);
393 => this.Process(Object, this.
Insert(Object), Callback);
401 => this.Process(Objects, this.
Insert(Objects), Callback);
409 => this.Process(Objects, this.
Insert(Objects), Callback);
420 public Task<IEnumerable<T>>
Find<T>(
int Offset,
int MaxCount, params
string[] SortOrder)
423 return this.
Find<T>(Offset, MaxCount, (
Filter)
null, SortOrder);
441 IMongoCollection<BsonDocument> Collection;
442 FilterDefinition<BsonDocument> BsonFilter;
444 if (
string.IsNullOrEmpty(CollectionName))
445 Collection = this.defaultCollection;
450 BsonFilter =
new BsonDocument();
452 BsonFilter = this.Convert(
Filter, Serializer);
454 return this.
Find<T>(Serializer, Collection, Offset, MaxCount, BsonFilter,
null, SortOrder);
469 T ContinueAfter, params
string[] SortOrder)
474 IMongoCollection<BsonDocument> Collection;
475 FilterDefinition<BsonDocument> BsonFilter;
477 if (
string.IsNullOrEmpty(CollectionName))
478 Collection = this.defaultCollection;
483 BsonFilter =
new BsonDocument();
485 BsonFilter = this.Convert(
Filter, Serializer);
487 return this.
Find(Serializer, Collection, Offset, MaxCount, BsonFilter, ContinueAfter, SortOrder);
500 public Task<IEnumerable<T>>
Find<T>(
int Offset,
int MaxCount, FilterDefinition<BsonDocument> BsonFilter,
501 params
string[] SortOrder)
506 IMongoCollection<BsonDocument> Collection;
508 if (
string.IsNullOrEmpty(CollectionName))
509 Collection = this.defaultCollection;
513 return this.
Find<T>(Serializer, Collection, Offset, MaxCount, BsonFilter,
null, SortOrder);
526 public Task<IEnumerable<T>>
Find<T>(
string CollectionName,
int Offset,
int MaxCount,
Filter Filter, params
string[] SortOrder)
530 IMongoCollection<BsonDocument> Collection;
531 FilterDefinition<BsonDocument> BsonFilter;
533 if (
string.IsNullOrEmpty(CollectionName))
534 Collection = this.defaultCollection;
539 BsonFilter =
new BsonDocument();
541 BsonFilter = this.Convert(
Filter, Serializer);
543 return this.
Find<T>(Serializer, Collection, Offset, MaxCount, BsonFilter,
null, SortOrder);
558 T ContinueAfter, params
string[] SortOrder)
562 IMongoCollection<BsonDocument> Collection;
563 FilterDefinition<BsonDocument> BsonFilter;
565 if (
string.IsNullOrEmpty(CollectionName))
566 Collection = this.defaultCollection;
571 BsonFilter =
new BsonDocument();
573 BsonFilter = this.Convert(
Filter, Serializer);
575 return this.
Find(Serializer, Collection, Offset, MaxCount, BsonFilter, ContinueAfter, SortOrder);
587 public Task<IEnumerable<object>>
Find(
string CollectionName,
int Offset,
int MaxCount, params
string[] SortOrder)
589 return this.
Find(CollectionName, Offset, MaxCount,
null, SortOrder);
602 public Task<IEnumerable<object>>
Find(
string CollectionName,
int Offset,
int MaxCount,
Filter Filter, params
string[] SortOrder)
605 IMongoCollection<BsonDocument> Collection;
606 FilterDefinition<BsonDocument> BsonFilter;
608 if (
string.IsNullOrEmpty(CollectionName))
609 Collection = this.defaultCollection;
614 BsonFilter =
new BsonDocument();
616 BsonFilter = this.Convert(
Filter, Serializer);
618 return this.Find<object>(Serializer, Collection, Offset, MaxCount, BsonFilter, SortOrder);
621 private async Task<IEnumerable<T>>
Find<T>(
ObjectSerializer Serializer, IMongoCollection<BsonDocument> Collection,
622 int Offset,
int MaxCount, FilterDefinition<BsonDocument> BsonFilter, T ContinueAfter, params
string[] SortOrder)
625 if (!(ContinueAfter is
null))
626 throw new NotImplementedException(
"Paginated searches not implemented in MongoDB provider.");
628 IFindFluent<BsonDocument, BsonDocument> ResultSet = Collection.Find(BsonFilter);
630 if (SortOrder.Length > 0)
632 SortDefinition<BsonDocument> SortDefinition =
null;
634 foreach (
string SortBy
in SortOrder)
636 if (SortDefinition is
null)
638 if (SortBy.StartsWith(
"-"))
639 SortDefinition = Builders<BsonDocument>.Sort.Descending(Serializer.ToShortName(SortBy.Substring(1)));
641 SortDefinition = Builders<BsonDocument>.Sort.Ascending(Serializer.ToShortName(SortBy));
645 if (SortBy.StartsWith(
"-"))
646 SortDefinition = SortDefinition.Descending(Serializer.ToShortName(SortBy.Substring(1)));
648 SortDefinition = SortDefinition.Ascending(Serializer.ToShortName(SortBy));
652 ResultSet = ResultSet.Sort(SortDefinition);
656 ResultSet = ResultSet.Skip(Offset);
658 if (MaxCount <
int.MaxValue)
659 ResultSet = ResultSet.Limit(MaxCount);
661 IAsyncCursor<BsonDocument> Cursor = await ResultSet.ToCursorAsync();
662 LinkedList<T> Result =
new LinkedList<T>();
663 BsonDeserializationArgs Args =
new BsonDeserializationArgs()
665 NominalType = typeof(T)
668 while (await Cursor.MoveNextAsync())
670 foreach (BsonDocument Document
in Cursor.Current)
672 BsonDocumentReader Reader =
new BsonDocumentReader(Document);
673 BsonDeserializationContext Context = BsonDeserializationContext.CreateRoot(Reader);
675 if (Serializer.Deserialize(Context, Args) is T Obj)
688 int i, c = ChildFilters.Length;
689 FilterDefinition<BsonDocument>[] Children =
new FilterDefinition<BsonDocument>[c];
691 for (i = 0; i < c; i++)
692 Children[i] = this.Convert(ChildFilters[i], Serializer);
695 return Builders<BsonDocument>.Filter.And(Children);
697 return Builders<BsonDocument>.Filter.Or(Children);
699 throw this.UnknownFilterType(
Filter);
706 return Builders<BsonDocument>.Filter.Not(Child);
708 throw this.UnknownFilterType(
Filter);
720 return Builders<BsonDocument>.Filter.Eq<
string>(FieldName,
null);
721 else if (Value is
string s)
724 return Builders<BsonDocument>.
Filter.Eq<
string>(FieldName +
"_L", s);
726 return Builders<BsonDocument>.
Filter.Eq<
string>(FieldName, s);
729 return Builders<BsonDocument>.Filter.Eq<
string>(FieldName +
"_L", cis.LowerCase);
730 else if (Value is sbyte i8)
731 return Builders<BsonDocument>.Filter.Eq<
int>(FieldName, i8);
732 else if (Value is
short i16)
733 return Builders<BsonDocument>.Filter.Eq<
int>(FieldName, i16);
734 else if (Value is
int i32)
735 return Builders<BsonDocument>.Filter.Eq<
int>(FieldName, i32);
736 else if (Value is
long i64)
737 return Builders<BsonDocument>.Filter.Eq<
long>(FieldName, i64);
738 else if (Value is
byte ui8)
739 return Builders<BsonDocument>.Filter.Eq<
int>(FieldName, ui8);
740 else if (Value is ushort ui16)
741 return Builders<BsonDocument>.Filter.Eq<
int>(FieldName, ui16);
742 else if (Value is uint ui32)
743 return Builders<BsonDocument>.Filter.Eq<
long>(FieldName, ui32);
744 else if (Value is ulong ui64)
745 return Builders<BsonDocument>.Filter.Eq<Decimal128>(FieldName, ui64);
746 else if (Value is
double d)
747 return Builders<BsonDocument>.Filter.Eq<
double>(FieldName, d);
748 else if (Value is
float f)
749 return Builders<BsonDocument>.Filter.Eq<
double>(FieldName, f);
750 else if (Value is decimal d2)
751 return Builders<BsonDocument>.Filter.Eq<Decimal128>(FieldName, d2);
752 else if (Value is
bool b)
753 return Builders<BsonDocument>.Filter.Eq<
bool>(FieldName, b);
754 else if (Value is DateTime DT)
756 else if (Value is DateTimeOffset DTO)
758 return Builders<BsonDocument>.Filter.And(
760 Builders<BsonDocument>.
Filter.Eq<
string>(FieldName +
".tz", DTO.Offset.ToString()));
762 else if (Value is TimeSpan TS)
763 return Builders<BsonDocument>.Filter.Eq<
string>(FieldName, TS.ToString());
764 else if (Value is Guid Guid)
765 return Builders<BsonDocument>.Filter.Eq<
string>(FieldName, Guid.ToString());
766 else if (Value is ObjectId ObjectId)
767 return Builders<BsonDocument>.Filter.Eq<ObjectId>(FieldName, ObjectId);
769 throw this.UnhandledFilterValueDataType(Serializer.
ValueType.FullName, FieldName, Value);
774 return Builders<BsonDocument>.Filter.Ne<
string>(FieldName,
null);
775 else if (Value is
string s)
778 return Builders<BsonDocument>.
Filter.Ne<
string>(FieldName +
"_L", s);
780 return Builders<BsonDocument>.
Filter.Ne<
string>(FieldName, s);
783 return Builders<BsonDocument>.Filter.Ne<
string>(FieldName +
"_L", cis.LowerCase);
784 else if (Value is sbyte i8)
785 return Builders<BsonDocument>.Filter.Ne<
int>(FieldName, i8);
786 else if (Value is
short i16)
787 return Builders<BsonDocument>.Filter.Ne<
int>(FieldName, i16);
788 else if (Value is
int i32)
789 return Builders<BsonDocument>.Filter.Ne<
int>(FieldName, i32);
790 else if (Value is
long i64)
791 return Builders<BsonDocument>.Filter.Ne<
long>(FieldName, i64);
792 else if (Value is
byte ui8)
793 return Builders<BsonDocument>.Filter.Ne<
int>(FieldName, ui8);
794 else if (Value is ushort ui16)
795 return Builders<BsonDocument>.Filter.Ne<
int>(FieldName, ui16);
796 else if (Value is uint ui32)
797 return Builders<BsonDocument>.Filter.Ne<
long>(FieldName, ui32);
798 else if (Value is ulong ui64)
799 return Builders<BsonDocument>.Filter.Ne<Decimal128>(FieldName, ui64);
800 else if (Value is
double d)
801 return Builders<BsonDocument>.Filter.Ne<
double>(FieldName, d);
802 else if (Value is
float f)
803 return Builders<BsonDocument>.Filter.Ne<
double>(FieldName, f);
804 else if (Value is decimal d2)
805 return Builders<BsonDocument>.Filter.Ne<Decimal128>(FieldName, d2);
806 else if (Value is
bool b)
807 return Builders<BsonDocument>.Filter.Ne<
bool>(FieldName, b);
808 else if (Value is DateTime DT)
810 else if (Value is DateTimeOffset DTO)
812 return Builders<BsonDocument>.Filter.Or(
814 Builders<BsonDocument>.
Filter.Ne<
string>(FieldName +
".tz", DTO.Offset.ToString()));
816 else if (Value is TimeSpan TS)
817 return Builders<BsonDocument>.Filter.Ne<
string>(FieldName, TS.ToString());
818 else if (Value is Guid Guid)
819 return Builders<BsonDocument>.Filter.Ne<
string>(FieldName, Guid.ToString());
820 else if (Value is ObjectId ObjectId)
821 return Builders<BsonDocument>.Filter.Ne<ObjectId>(FieldName, ObjectId);
823 throw this.UnhandledFilterValueDataType(Serializer.
ValueType.FullName, FieldName, Value);
827 if (Value is
string s)
830 return Builders<BsonDocument>.Filter.Gt<
string>(FieldName +
"_L", s);
832 return Builders<BsonDocument>.Filter.Gt<
string>(FieldName, s);
835 return Builders<BsonDocument>.Filter.Gt<
string>(FieldName +
"_L", cis.LowerCase);
836 else if (Value is sbyte i8)
837 return Builders<BsonDocument>.Filter.Gt<
int>(FieldName, i8);
838 else if (Value is
short i16)
839 return Builders<BsonDocument>.Filter.Gt<
int>(FieldName, i16);
840 else if (Value is
int i32)
841 return Builders<BsonDocument>.Filter.Gt<
int>(FieldName, i32);
842 else if (Value is
long i64)
843 return Builders<BsonDocument>.Filter.Gt<
long>(FieldName, i64);
844 else if (Value is
byte ui8)
845 return Builders<BsonDocument>.Filter.Gt<
int>(FieldName, ui8);
846 else if (Value is ushort ui16)
847 return Builders<BsonDocument>.Filter.Gt<
int>(FieldName, ui16);
848 else if (Value is uint ui32)
849 return Builders<BsonDocument>.Filter.Gt<
long>(FieldName, ui32);
850 else if (Value is ulong ui64)
851 return Builders<BsonDocument>.Filter.Gt<Decimal128>(FieldName, ui64);
852 else if (Value is
double d)
853 return Builders<BsonDocument>.Filter.Gt<
double>(FieldName, d);
854 else if (Value is
float f)
855 return Builders<BsonDocument>.Filter.Gt<
double>(FieldName, f);
856 else if (Value is decimal d2)
857 return Builders<BsonDocument>.Filter.Gt<Decimal128>(FieldName, d2);
858 else if (Value is
bool b)
859 return Builders<BsonDocument>.Filter.Gt<
bool>(FieldName, b);
860 else if (Value is DateTime DT)
862 else if (Value is TimeSpan TS)
863 return Builders<BsonDocument>.Filter.Gt<
string>(FieldName, TS.ToString());
864 else if (Value is Guid Guid)
865 return Builders<BsonDocument>.Filter.Gt<
string>(FieldName, Guid.ToString());
866 else if (Value is ObjectId ObjectId)
867 return Builders<BsonDocument>.Filter.Gt<ObjectId>(FieldName, ObjectId);
869 throw this.UnhandledFilterValueDataType(Serializer.
ValueType.FullName, FieldName, Value);
878 else if (Value is
string s)
881 return Builders<BsonDocument>.Filter.Gte<
string>(FieldName +
"_L", s);
883 return Builders<BsonDocument>.Filter.Gte<
string>(FieldName, s);
886 return Builders<BsonDocument>.Filter.Gte<
string>(FieldName +
"_L", cis.LowerCase);
887 else if (Value is sbyte i8)
888 return Builders<BsonDocument>.Filter.Gte<
int>(FieldName, i8);
889 else if (Value is
short i16)
890 return Builders<BsonDocument>.Filter.Gte<
int>(FieldName, i16);
891 else if (Value is
int i32)
892 return Builders<BsonDocument>.Filter.Gte<
int>(FieldName, i32);
893 else if (Value is
long i64)
894 return Builders<BsonDocument>.Filter.Gte<
long>(FieldName, i64);
895 else if (Value is
byte ui8)
896 return Builders<BsonDocument>.Filter.Gte<
int>(FieldName, ui8);
897 else if (Value is ushort ui16)
898 return Builders<BsonDocument>.Filter.Gte<
int>(FieldName, ui16);
899 else if (Value is uint ui32)
900 return Builders<BsonDocument>.Filter.Gte<
long>(FieldName, ui32);
901 else if (Value is ulong ui64)
902 return Builders<BsonDocument>.Filter.Gte<Decimal128>(FieldName, ui64);
903 else if (Value is
double d)
904 return Builders<BsonDocument>.Filter.Gte<
double>(FieldName, d);
905 else if (Value is
float f)
906 return Builders<BsonDocument>.Filter.Gte<
double>(FieldName, f);
907 else if (Value is decimal d2)
908 return Builders<BsonDocument>.Filter.Gte<Decimal128>(FieldName, d2);
909 else if (Value is
bool b)
910 return Builders<BsonDocument>.Filter.Gte<
bool>(FieldName, b);
911 else if (Value is DateTime DT)
913 else if (Value is TimeSpan TS)
914 return Builders<BsonDocument>.Filter.Gte<
string>(FieldName, TS.ToString());
915 else if (Value is Guid Guid)
916 return Builders<BsonDocument>.Filter.Gte<
string>(FieldName, Guid.ToString());
917 else if (Value is ObjectId ObjectId)
918 return Builders<BsonDocument>.Filter.Gte<ObjectId>(FieldName, ObjectId);
920 throw this.UnhandledFilterValueDataType(Serializer.
ValueType.FullName, FieldName, Value);
924 if (Value is
string s)
927 return Builders<BsonDocument>.Filter.Lt<
string>(FieldName +
"_L", s);
929 return Builders<BsonDocument>.Filter.Lt<
string>(FieldName, s);
932 return Builders<BsonDocument>.Filter.Lt<
string>(FieldName +
"_L", cis.LowerCase);
933 else if (Value is sbyte i8)
934 return Builders<BsonDocument>.Filter.Lt<
int>(FieldName, i8);
935 else if (Value is
short i16)
936 return Builders<BsonDocument>.Filter.Lt<
int>(FieldName, i16);
937 else if (Value is
int i32)
938 return Builders<BsonDocument>.Filter.Lt<
int>(FieldName, i32);
939 else if (Value is
long i64)
940 return Builders<BsonDocument>.Filter.Lt<
long>(FieldName, i64);
941 else if (Value is
byte ui8)
942 return Builders<BsonDocument>.Filter.Lt<
int>(FieldName, ui8);
943 else if (Value is ushort ui16)
944 return Builders<BsonDocument>.Filter.Lt<
int>(FieldName, ui16);
945 else if (Value is uint ui32)
946 return Builders<BsonDocument>.Filter.Lt<
long>(FieldName, ui32);
947 else if (Value is ulong ui64)
948 return Builders<BsonDocument>.Filter.Lt<Decimal128>(FieldName, ui64);
949 else if (Value is
double d)
950 return Builders<BsonDocument>.Filter.Lt<
double>(FieldName, d);
951 else if (Value is
float f)
952 return Builders<BsonDocument>.Filter.Lt<
double>(FieldName, f);
953 else if (Value is decimal d2)
954 return Builders<BsonDocument>.Filter.Lt<Decimal128>(FieldName, d2);
955 else if (Value is
bool b)
956 return Builders<BsonDocument>.Filter.Lt<
bool>(FieldName, b);
957 else if (Value is DateTime DT)
959 else if (Value is TimeSpan TS)
960 return Builders<BsonDocument>.Filter.Lt<
string>(FieldName, TS.ToString());
961 else if (Value is Guid Guid)
962 return Builders<BsonDocument>.Filter.Lt<
string>(FieldName, Guid.ToString());
963 else if (Value is ObjectId ObjectId)
964 return Builders<BsonDocument>.Filter.Lt<ObjectId>(FieldName, ObjectId);
966 throw this.UnhandledFilterValueDataType(Serializer.
ValueType.FullName, FieldName, Value);
975 else if (Value is
string s)
978 return Builders<BsonDocument>.Filter.Lte<
string>(FieldName +
"_L", s);
980 return Builders<BsonDocument>.Filter.Lte<
string>(FieldName, s);
983 return Builders<BsonDocument>.Filter.Lte<
string>(FieldName +
"_L", cis.LowerCase);
984 else if (Value is sbyte i8)
985 return Builders<BsonDocument>.Filter.Lte<
int>(FieldName, i8);
986 else if (Value is
short i16)
987 return Builders<BsonDocument>.Filter.Lte<
int>(FieldName, i16);
988 else if (Value is
int i32)
989 return Builders<BsonDocument>.Filter.Lte<
int>(FieldName, i32);
990 else if (Value is
long i64)
991 return Builders<BsonDocument>.Filter.Lte<
long>(FieldName, i64);
992 else if (Value is
byte ui8)
993 return Builders<BsonDocument>.Filter.Lte<
int>(FieldName, ui8);
994 else if (Value is ushort ui16)
995 return Builders<BsonDocument>.Filter.Lte<
int>(FieldName, ui16);
996 else if (Value is uint ui32)
997 return Builders<BsonDocument>.Filter.Lte<
long>(FieldName, ui32);
998 else if (Value is ulong ui64)
999 return Builders<BsonDocument>.Filter.Lte<Decimal128>(FieldName, ui64);
1000 else if (Value is
double d)
1001 return Builders<BsonDocument>.Filter.Lte<
double>(FieldName, d);
1002 else if (Value is
float f)
1003 return Builders<BsonDocument>.Filter.Lte<
double>(FieldName, f);
1004 else if (Value is decimal d2)
1005 return Builders<BsonDocument>.Filter.Lte<Decimal128>(FieldName, d2);
1006 else if (Value is
bool b)
1007 return Builders<BsonDocument>.Filter.Lte<
bool>(FieldName, b);
1008 else if (Value is DateTime DT)
1010 else if (Value is TimeSpan TS)
1011 return Builders<BsonDocument>.Filter.Lte<
string>(FieldName, TS.ToString());
1012 else if (Value is Guid Guid)
1013 return Builders<BsonDocument>.Filter.Lte<
string>(FieldName, Guid.ToString());
1014 else if (Value is ObjectId ObjectId)
1015 return Builders<BsonDocument>.Filter.Lte<ObjectId>(FieldName, ObjectId);
1017 throw this.UnhandledFilterValueDataType(Serializer.
ValueType.FullName, FieldName, Value);
1020 throw this.UnknownFilterType(
Filter);
1030 throw this.UnknownFilterType(
Filter);
1036 return new NotSupportedException(
"Filters of type " +
Filter.GetType().FullName +
" not supported.");
1039 private Exception UnhandledFilterValueDataType(
string TypeName,
string FieldName,
object Value)
1043 return new NotSupportedException(
"Null filter values for field " + TypeName +
"." + FieldName +
1048 return new NotSupportedException(
"Filter values of type " + Value.GetType().FullName +
1049 " for field " + TypeName +
"." + FieldName +
" not supported.");
1061 public async Task<IPage<T>>
FindFirst<T>(
int PageSize, params
string[] SortOrder)
1065 IEnumerable<T> Items = await this.
Find<T>(0, PageSize, SortOrder);
1066 return new Page<T>(PageSize,
null,
null, SortOrder, Items, Serializer,
this);
1082 IEnumerable<T> Items = await this.
Find<T>(0, PageSize,
Filter, SortOrder);
1083 return new Page<T>(PageSize,
null,
Filter, SortOrder, Items, Serializer,
this);
1094 public async Task<IPage<object>>
FindFirst(
string Collection,
int PageSize, params
string[] SortOrder)
1097 IEnumerable<object> Items = await this.
Find(Collection, 0, PageSize, SortOrder);
1098 return new Page<object>(PageSize, Collection,
null, SortOrder, Items, Serializer,
this);
1113 IEnumerable<object> Items = await this.
Find(Collection, 0, PageSize,
Filter, SortOrder);
1114 return new Page<object>(PageSize, Collection,
Filter, SortOrder, Items, Serializer,
this);
1131 IEnumerable<T> Items = await this.
Find<T>(Collection, 0, PageSize,
Filter, SortOrder);
1132 return new Page<T>(PageSize, Collection,
Filter, SortOrder, Items, Serializer,
this);
1145 return CurrentPage.FindNext();
1147 throw new IOException(
"Incompatible page.");
1158 return CurrentPage.FindNext();
1160 throw new IOException(
"Incompatible page.");
1174 if (ObjectId is ObjectId ObjId)
1176 else if (ObjectId is
string s)
1177 OID =
new ObjectId(s);
1178 else if (ObjectId is
byte[] A)
1179 OID =
new ObjectId(A);
1180 else if (ObjectId is Guid Guid)
1183 throw new NotSupportedException(
"Unsupported type for Object ID: " + ObjectId.GetType().FullName);
1197 string Key = typeof(T).FullName +
" " + ObjectId.ToString();
1199 if (this.loadCache.
TryGetValue(Key, out
object Obj) && Obj is T Result)
1206 foreach (T Item
in ReferencedObjects)
1211 throw new Exception(
"Multiple objects of type T found with object ID " + ObjectId.ToString());
1214 if (!(First is
null))
1215 this.loadCache.
Add(Key, First);
1232 if (ObjectId is ObjectId ObjId)
1234 else if (ObjectId is
string s)
1235 OID =
new ObjectId(s);
1236 else if (ObjectId is
byte[] A)
1237 OID =
new ObjectId(A);
1238 else if (ObjectId is Guid Guid)
1241 throw new NotSupportedException(
"Unsupported type for Object ID: " + ObjectId.GetType().FullName);
1256 string Key = typeof(T).FullName +
" " + ObjectId.ToString();
1258 if (this.loadCache.
TryGetValue(Key, out
object Obj) && Obj is T Result)
1265 foreach (T Item
in ReferencedObjects)
1270 throw new Exception(
"Multiple objects of type T found with object ID " + ObjectId.ToString());
1273 if (!(First is
null))
1274 this.loadCache.
Add(Key, First);
1289 if (ObjectId is ObjectId ObjId)
1291 else if (ObjectId is
string s)
1292 OID =
new ObjectId(s);
1293 else if (ObjectId is
byte[] A)
1294 OID =
new ObjectId(A);
1295 else if (ObjectId is Guid Guid)
1298 throw new NotSupportedException(
"Unsupported type for Object ID: " + ObjectId.GetType().FullName);
1302 object First =
null;
1304 foreach (
object Item
in ReferencedObjects)
1309 throw new Exception(
"Multiple objects of type T found with object ID " + ObjectId.ToString());
1324 ObjectId ObjectId = await Serializer.
GetObjectId(Object,
false);
1326 IMongoCollection<BsonDocument> Collection;
1328 if (
string.IsNullOrEmpty(CollectionName))
1329 Collection = this.defaultCollection;
1333 BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
1334 await Collection.ReplaceOneAsync(Builders<BsonDocument>.
Filter.Eq<ObjectId>(
"_id", ObjectId), Doc);
1343 return this.
Update((IEnumerable<object>)Objects);
1350 public async Task
Update(IEnumerable<object> Objects)
1352 foreach (
object Obj
in Objects)
1362 => this.Process(Object, this.
Update(Object), Callback);
1370 => this.Process(Objects, this.
Update(Objects), Callback);
1378 => this.Process(Objects, this.
Update(Objects), Callback);
1387 ObjectId ObjectId = await Serializer.
GetObjectId(Object,
false);
1389 IMongoCollection<BsonDocument> Collection;
1391 if (
string.IsNullOrEmpty(CollectionName))
1392 Collection = this.defaultCollection;
1396 await Collection.DeleteOneAsync(Builders<BsonDocument>.
Filter.Eq<ObjectId>(
"_id", ObjectId));
1405 return this.
Delete((IEnumerable<object>)Objects);
1412 public async Task
Delete(IEnumerable<object> Objects)
1414 foreach (
object Obj
in Objects)
1418 private async Task Process(
object Object, Task Op,
ObjectCallback Callback)
1421 if (!(Callback is
null))
1425 private async Task Process(IEnumerable<object> Objects, Task Op,
ObjectsCallback Callback)
1429 if (!(Callback is
null))
1439 => this.Process(Object, this.
Delete(Object), Callback);
1447 => this.Process(Objects, this.
Delete(Objects), Callback);
1455 => this.Process(Objects, this.
Delete(Objects), Callback);
1466 public async Task<IEnumerable<T>>
FindDelete<T>(
int Offset,
int MaxCount, params
string[] SortOrder)
1469 IEnumerable<T> Result = await this.
Find<T>(Offset, MaxCount, SortOrder);
1470 await this.
Delete(Result);
1487 IEnumerable<T> Result = await this.
Find<T>(Offset, MaxCount,
Filter, SortOrder);
1488 await this.
Delete(Result);
1501 public async Task<IEnumerable<object>>
FindDelete(
string Collection,
int Offset,
int MaxCount, params
string[] SortOrder)
1503 IEnumerable<object> Result = await this.
Find(Collection, Offset, MaxCount, SortOrder);
1504 await this.
Delete(Result);
1518 public async Task<IEnumerable<object>>
FindDelete(
string Collection,
int Offset,
int MaxCount,
Filter Filter, params
string[] SortOrder)
1520 IEnumerable<object> Result = await this.
Find(Collection, Offset, MaxCount,
Filter, SortOrder);
1521 await this.
Delete(Result);
1537 IEnumerable<T> Objects = await this.
FindDelete<T>(Offset, MaxCount, SortOrder);
1538 if (!(Callback is
null))
1556 if (!(Callback is
null))
1571 IEnumerable<object> Objects = await this.
FindDelete(Collection, Offset, MaxCount, SortOrder);
1572 if (!(Callback is
null))
1588 IEnumerable<object> Objects = await this.
FindDelete(Collection, Offset, MaxCount,
Filter, SortOrder);
1589 if (!(Callback is
null))
1598 public Task
Clear(
string CollectionName)
1600 IMongoCollection<BsonDocument> Collection = this.
GetCollection(CollectionName);
1601 return Collection.DeleteManyAsync(FilterDefinition<BsonDocument>.Empty);
1610 public Task
AddIndex(
string CollectionName,
string[] FieldNames)
1612 IMongoCollection<BsonDocument> Collection;
1613 List<BsonDocument> Indices;
1615 if (
string.IsNullOrEmpty(CollectionName))
1620 IAsyncCursor<BsonDocument> Cursor = Collection.Indexes.List();
1621 Indices = Cursor.ToList<BsonDocument>();
1623 return ObjectSerializer.CheckIndexExists(Collection, Indices, FieldNames,
null);
1634 IMongoCollection<BsonDocument> Collection;
1635 List<BsonDocument> Indices;
1637 if (
string.IsNullOrEmpty(CollectionName))
1642 IAsyncCursor<BsonDocument> Cursor = Collection.Indexes.List();
1643 Indices = Cursor.ToList<BsonDocument>();
1655 public Task<string[]>
Analyze(XmlWriter Output,
string XsltPath,
string ProgramDataFolder,
bool ExportData)
1657 return this.
Analyze(Output, XsltPath, ProgramDataFolder, ExportData,
false);
1668 public Task<string[]>
Analyze(XmlWriter Output,
string XsltPath,
string ProgramDataFolder,
bool ExportData,
ProfilerThread Thread)
1670 return this.
Analyze(Output, XsltPath, ProgramDataFolder, ExportData,
false, Thread);
1680 public Task<string[]>
Repair(XmlWriter Output,
string XsltPath,
string ProgramDataFolder,
bool ExportData)
1682 return this.
Analyze(Output, XsltPath, ProgramDataFolder, ExportData,
true);
1693 public Task<string[]>
Repair(XmlWriter Output,
string XsltPath,
string ProgramDataFolder,
bool ExportData,
ProfilerThread Thread)
1695 return this.
Analyze(Output, XsltPath, ProgramDataFolder, ExportData,
true, Thread);
1706 public Task<string[]>
Analyze(XmlWriter Output,
string XsltPath,
string ProgramDataFolder,
bool ExportData,
bool Repair)
1708 return this.
Analyze(
null, XsltPath, ProgramDataFolder, ExportData,
Repair,
null);
1720 public async Task<string[]>
Analyze(XmlWriter Output,
string XsltPath,
string ProgramDataFolder,
bool ExportData,
bool Repair,
1724 Output.WriteStartDocument();
1726 if (!
string.IsNullOrEmpty(XsltPath))
1728 if (File.Exists(XsltPath))
1732 byte[] XsltBin = File.ReadAllBytes(XsltPath);
1734 Output.WriteProcessingInstruction(
"xml-stylesheet",
"type=\"text/xsl\" href=\"data:text/xsl;base64," +
1735 System.Convert.ToBase64String(XsltBin) +
"\"");
1739 Output.WriteProcessingInstruction(
"xml-stylesheet",
"type=\"text/xsl\" href=\"" + Encode(XsltPath) +
"\"");
1743 Output.WriteProcessingInstruction(
"xml-stylesheet",
"type=\"text/xsl\" href=\"" + Encode(XsltPath) +
"\"");
1746 Output.WriteStartElement(
"DatabaseStatistics",
"http://waher.se/Schema/Persistence/Statistics.xsd");
1748 foreach (
string CollectionName
in (await this.database.ListCollectionNamesAsync()).ToEnumerable())
1750 Thread?.NewState(CollectionName);
1752 IMongoCollection<BsonDocument> Collection = this.database.GetCollection<BsonDocument>(CollectionName);
1754 Output.WriteStartElement(
"File");
1755 Output.WriteAttributeString(
"id", Collection.CollectionNamespace.FullName);
1756 Output.WriteAttributeString(
"collectionName", CollectionName);
1757 Output.WriteAttributeString(
"count", (await Collection.CountDocumentsAsync(Builders<BsonDocument>.Filter.Empty)).ToString());
1759 if (!(Collection.Settings.WriteEncoding is
null))
1760 Output.WriteAttributeString(
"encoding", Collection.Settings.WriteEncoding.WebName);
1762 if (Collection.Settings.WriteConcern.WTimeout.HasValue)
1763 Output.WriteAttributeString(
"timeoutMs", ((
int)Collection.Settings.WriteConcern.WTimeout.Value.TotalMilliseconds).ToString());
1765 foreach (BsonDocument Index
in (await Collection.Indexes.ListAsync()).ToEnumerable())
1767 List<string> FieldNames =
new List<string>();
1769 Output.WriteStartElement(
"Index");
1771 foreach (BsonElement E
in Index.Elements)
1776 foreach (BsonElement E2
in E.Value.AsBsonDocument.Elements)
1778 if (E2.Value.AsInt32 < 0)
1779 FieldNames.Add(
"-" + E2.Name);
1781 FieldNames.Add(E2.Name);
1786 Output.WriteAttributeString(
"id", E.Value.AsString);
1791 foreach (
string Field
in FieldNames)
1792 Output.WriteElementString(
"Field", Field);
1794 Output.WriteEndElement();
1797 Output.WriteEndElement();
1800 Output.WriteEndElement();
1801 Output.WriteEndDocument();
1806 return new string[0];
1814 public Task<string[]>
Repair(params
string[] CollectionNames)
1816 return Task.FromResult<
string[]>(
new string[0]);
1827 return Task.FromResult<
string[]>(
new string[0]);
1830 private static string Encode(
string s)
1833 Replace(
"&",
"&").
1834 Replace(
"<",
"<").
1835 Replace(
">",
">").
1836 Replace(
"\"",
""").
1837 Replace(
"'",
"'");
1848 return this.
Export(Output, CollectionNames,
null);
1869 BsonDeserializationArgs Args =
new BsonDeserializationArgs()
1874 foreach (
string CollectionName
in (await this.database.ListCollectionNamesAsync()).ToEnumerable())
1876 if (!(CollectionNames is
null) && Array.IndexOf(CollectionNames, CollectionName) < 0)
1879 if (!(
Filter is
null) && !
Filter.CanExportCollection(CollectionName))
1884 IMongoCollection<BsonDocument> Collection = this.database.GetCollection<BsonDocument>(CollectionName);
1890 foreach (BsonDocument Index
in (await Collection.Indexes.ListAsync()).ToEnumerable())
1895 foreach (BsonElement E
in Index.Elements)
1897 if (E.Name ==
"key")
1899 foreach (BsonElement E2
in E.Value.AsBsonDocument.Elements)
1913 foreach (BsonDocument Doc
in (await Collection.FindAsync<BsonDocument>(Builders<BsonDocument>.Filter.Empty)).ToEnumerable())
1915 BsonDocumentReader Reader =
new BsonDocumentReader(Doc);
1916 BsonDeserializationContext Context = BsonDeserializationContext.CreateRoot(Reader);
1918 object Object = Serializer.
Deserialize(Context, Args);
1925 if (await Output.
StartObject(Obj.ObjectId.ToString(), Obj.TypeName) is
null)
1929 foreach (KeyValuePair<string, object> P
in Obj)
1931 if (P.Value is ObjectId ObjectId)
1943 catch (Exception ex)
1946 if (!await this.ReportException(ex, Output))
1957 else if (!(Object is
null))
1959 if (!await Output.
ReportError(
"Unable to load object " + Doc[
"_id"].AsString +
"."))
1964 catch (Exception ex)
1967 if (!await this.ReportException(ex, Output))
1979 catch (Exception ex)
1982 if (!await this.ReportException(ex, Output))
1995 private async Task<bool> ReportException(Exception ex,
IDatabaseExport Output)
1999 if (ex is AggregateException ex2)
2001 foreach (Exception ex3
in ex2.InnerExceptions)
2023 return this.Iterate(Recipient, CollectionNames,
null);
2038 await Recipient.StartDatabase();
2042 BsonDeserializationArgs Args =
new BsonDeserializationArgs()
2047 foreach (
string CollectionName
in (await this.database.ListCollectionNamesAsync()).ToEnumerable())
2049 if (!(CollectionNames is
null) && Array.IndexOf(CollectionNames, CollectionName) < 0)
2052 Thread?.NewState(CollectionName);
2054 IMongoCollection<BsonDocument> Collection = this.database.GetCollection<BsonDocument>(CollectionName);
2056 await Recipient.StartCollection(CollectionName);
2059 foreach (BsonDocument Doc
in (await Collection.FindAsync<BsonDocument>(Builders<BsonDocument>.Filter.Empty)).ToEnumerable())
2061 BsonDocumentReader Reader =
new BsonDocumentReader(Doc);
2062 BsonDeserializationContext Context = BsonDeserializationContext.CreateRoot(Reader);
2064 object Object = Serializer.
Deserialize(Context, Args);
2066 if (Object is T Obj)
2067 await Recipient.ProcessObject(Obj);
2068 else if (!(Object is
null))
2070 ObjectId ObjectId = await Serializer.
GetObjectId(Object,
false);
2071 if (ObjectId != ObjectId.Empty)
2072 await Recipient.IncompatibleObject(ObjectId);
2076 catch (Exception ex)
2078 Thread?.Exception(ex);
2079 this.ReportException(ex, Recipient);
2083 await Recipient.EndCollection();
2087 catch (Exception ex)
2089 Thread?.Exception(ex);
2090 this.ReportException(ex, Recipient);
2094 await Recipient.EndDatabase();
2103 ex = Events.Log.UnnestException(ex);
2105 if (ex is AggregateException ex2)
2107 foreach (Exception ex3
in ex2.InnerExceptions)
2108 Recipient.ReportException(ex3);
2111 Recipient.ReportException(ex);
2119 return Task.CompletedTask;
2127 return Task.CompletedTask;
2135 return Task.CompletedTask;
2143 return Task.CompletedTask;
2151 return Task.CompletedTask;
2170 List<string> Collections =
new List<string>();
2172 foreach (
string CollectionName
in (await this.database.ListCollectionNamesAsync()).ToEnumerable())
2174 if (CollectionName.StartsWith(
"DICT_"))
2175 Collections.Add(CollectionName);
2178 return Collections.ToArray();
2187 List<string> Collections =
new List<string>();
2189 foreach (
string CollectionName
in (await this.database.ListCollectionNamesAsync()).ToEnumerable())
2191 if (!CollectionName.StartsWith(
"DICT_"))
2192 Collections.Add(CollectionName);
2195 return Collections.ToArray();
2217 return Task.FromResult<
string>(Serializer.
CollectionName(Object));
2227 public async Task<bool>
IsLabel(
string CollectionName,
string Label)
2229 IMongoCollection<BsonDocument> Collection = this.
GetCollection(CollectionName);
2230 FilterDefinition<BsonDocument> BsonFilter = Builders<BsonDocument>.Filter.Ne<
string>(Label,
null);
2231 IFindFluent<BsonDocument, BsonDocument> ResultSet = Collection.Find<BsonDocument>(BsonFilter);
2233 return !(await ResultSet.SingleAsync<BsonDocument>() is
null);
2242 throw new NotImplementedException();
2259 return await SerializerEx.GetObjectId(Object,
false);
2271 lock (this.collections)
2273 this.collections.Remove(CollectionName);
2275 if (CollectionName == this.lastCollectionName)
2277 this.lastCollection =
null;
2278 this.lastCollectionName =
string.Empty;
2282 return this.database.DropCollectionAsync(CollectionName);
2296 string CollectionName = Serializer.CollectionName(Object);
2298 BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
2302 BsonDocumentReader Reader =
new BsonDocumentReader(Doc);
2303 BsonDeserializationContext Context = BsonDeserializationContext.CreateRoot(Reader);
2304 BsonDeserializationArgs Args =
new BsonDeserializationArgs()
2311 Obj.ArchivingTime = Serializer.GetArchivingTimeDays(Object);
2312 return Task.FromResult(Obj);
2315 throw new InvalidOperationException(
"Unable to generalize object.");
2326 return Task.FromResult<
object>(
null);
2330 return Task.FromResult<
object>(Object);
2333 string CollectionName = Serializer.CollectionName(Object);
2335 BsonDocument Doc = Object.ToBsonDocument(Object.GetType(), Serializer);
2339 BsonDocumentReader Reader =
new BsonDocumentReader(Doc);
2340 BsonDeserializationContext Context = BsonDeserializationContext.CreateRoot(Reader);
2341 BsonDeserializationArgs Args =
new BsonDeserializationArgs()
2346 return Task.FromResult<
object>(Serializer.Deserialize(Context, Args));
2355 SortedDictionary<string, bool> Sorted =
new SortedDictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
2357 lock (this.collections)
2366 string[] Result =
new string[Sorted.Count];
2367 Sorted.Keys.CopyTo(Result, 0);
Static class managing the application event log. Applications and services log events on this static ...
static Exception UnnestException(Exception Exception)
Unnests an exception, to extract the relevant inner exception.
Represents a case-insensitive string.
This filter selects objects that conform to all child-filters provided.
Abstract base class for filters having a single child-filters.
Filter ChildFilter
Child filter.
Abstract base class for filters having a variable number of child-filters.
Filter[] ChildFilters
Child filters.
This filter selects objects that have a named field equal to a given value.
This filter selects objects that have a named field greater or equal to a given value.
This filter selects objects that have a named field greater than a given value.
string FieldName
FIeld Name.
This filter selects objects that have a named field lesser or equal to a given value.
This filter selects objects that have a named field lesser than a given value.
This filter selects objects that have a named field matching a given regular expression.
string RegularExpression
Regular expression.
This filter selects objects that have a named field not equal to a given value.
Abstract base class for all field filters operating on a constant value.
Base class for all filter classes.
Filter()
Base class for all filter classes.
This filter selects objects that does not conform to the child-filter provided.
This filter selects objects that conform to any of the child-filters provided.
MongoDB database provider.
Task Delete(params object[] Objects)
Deletes a collection of objects in the database.
Task Clear(string CollectionName)
Clears a collection of all objects.
Task UpdateLazy(object Object, ObjectCallback Callback)
Updates an object in the database, if unlocked. If locked, object will be updated at next opportunity...
Task< string[]> Analyze(XmlWriter Output, string XsltPath, string ProgramDataFolder, bool ExportData, ProfilerThread Thread)
Analyzes the database and exports findings to XML.
MongoDBProvider(string DatabaseName, string DefaultCollectionName)
MongoDB database provider, for a local MongoDB database.
Task< string[]> Repair(XmlWriter Output, string XsltPath, string ProgramDataFolder, bool ExportData, ProfilerThread Thread)
Analyzes the database and repairs it if necessary. Results are exported to XML.
async Task< object > TryLoadObject(string CollectionName, object ObjectId)
Tries to load an object given its Object ID ObjectId and its collection name CollectionName .
Task< IPage< T > > FindNext< T >(IPage< T > Page)
Finds the next page of objects of a given class T .
Task< T > TryLoadObject< T >(object ObjectId)
Tries to load an object given its Object ID ObjectId and its base type T .
async Task< bool > IsLabel(string CollectionName, string Label)
Checks if a string is a label in a given collection.
Task< string[]> Repair(params string[] CollectionNames)
Repairs a set of collections.
async Task DeleteLazy< T >(int Offset, int MaxCount, string[] SortOrder, ObjectsCallback Callback)
Finds objects of a given class T and deletes them in the same atomic operation.
Task DropCollection(string CollectionName)
Drops a collection, if it exist.
Task< string[]> Analyze(XmlWriter Output, string XsltPath, string ProgramDataFolder, bool ExportData)
Analyzes the database and exports findings to XML.
int ObjectIdByteCount
Number of bytes used by an Object ID.
Task StartBulk()
Starts bulk-proccessing of data. Must be followed by a call to EndBulk.
Task< bool > Export(IDatabaseExport Output, string[] CollectionNames)
Performs an export of the database.
Task< string > GetCollection(Object Object)
Gets the collection corresponding to a given object.
Task Iterate< T >(IDatabaseIteration< T > Recipient, string[] CollectionNames)
Performs an iteration of contents of the entire database.
string DefaultCollectionName
Default collection name.
Task RemoveIndex(string CollectionName, string[] FieldNames)
Removes an index from a collection, if one exist.
async Task< string[]> Analyze(XmlWriter Output, string XsltPath, string ProgramDataFolder, bool ExportData, bool Repair, ProfilerThread Thread)
Analyzes the database and exports findings to XML.
Task DeleteLazy(object Object, ObjectCallback Callback)
Deletes an object in the database, if unlocked. If locked, object will be deleted at next opportunity...
Task Update(params object[] Objects)
Updates a collection of objects in the database.
async Task Update(object Object)
Updates an object in the database.
IMongoCollection< BsonDocument > DefaultCollection
Default collection.
Task DeleteLazy(IEnumerable< object > Objects, ObjectsCallback Callback)
Deletes a collection of objects in the database, if unlocked. If locked, objects will be deleted at n...
async Task< IPage< object > > FindFirst(string Collection, int PageSize, params string[] SortOrder)
Finds the first page of objects in a given collection.
async Task Insert(IEnumerable< object > Objects)
Inserts a collection of objects into the database.
Task Insert(params object[] Objects)
Inserts a collection of objects into the database.
async Task< string[]> GetDictionaries()
Gets an array of available dictionary collections.
Task< IPersistentDictionary > GetDictionary(string Collection)
Gets a persistent dictionary containing objects in a collection.
string Id
An ID of the files provider. It's unique, and constant during the life-time of the MongoDBProvider cl...
async Task< IEnumerable< object > > FindDelete(string Collection, int Offset, int MaxCount, Filter Filter, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
Task AddIndex(string CollectionName, string[] FieldNames)
Adds an index to a collection, if one does not already exist.
Task UpdateLazy(IEnumerable< object > Objects, ObjectsCallback Callback)
Updates a collection of objects in the database, if unlocked. If locked, objects will be updated at n...
async Task Delete(object Object)
Deletes an object in the database.
MongoDBProvider(string HostName, int Port, string DatabaseName, string DefaultCollectionName)
MongoDB database provider.
Task< string > GetCollection(Type Type)
Gets the collection corresponding to a given type.
async Task< IEnumerable< T > > FindDelete< T >(int Offset, int MaxCount, params string[] SortOrder)
Finds objects of a given class T and deletes them in the same atomic operation.
Task DeleteLazy(object[] Objects, ObjectsCallback Callback)
Deletes a collection of objects in the database, if unlocked. If locked, objects will be deleted at n...
MongoDBProvider(MongoClientSettings Settings, string DatabaseName, string DefaultCollectionName)
MongoDB database provider.
async Task DeleteLazy(string Collection, int Offset, int MaxCount, Filter Filter, string[] SortOrder, ObjectsCallback Callback)
Finds objects in a given collection and deletes them in the same atomic operation.
IObjectSerializer GetObjectSerializer(Type Type)
Returns a serializer for a given type.
Task< string[]> GetLabels(string Collection)
Gets an array of available labels for a collection.
Task Stop()
Called when processing ends.
async Task< IPage< T > > FindFirst< T >(int PageSize, params string[] SortOrder)
Finds the first page of objects of a given class T .
async Task< object > TryGetObjectId(object Object)
Tries to get the Object ID of an object, if it exists.
Task< IPage< object > > FindNext(IPage< object > Page)
Finds the next page of objects in a given collection.
ObjectSerializer GetObjectSerializerEx(object Object)
Gets the object serializer corresponding to a specific object.
Task< IEnumerable< T > > Find< T >(int Offset, int MaxCount, params string[] SortOrder)
Finds objects of a given class T .
Task< object > Specialize(GenericObject Object)
Creates a specialized representation of a generic object.
Task Start()
Called when processing starts.
async Task< IPage< object > > FindFirst(string Collection, int PageSize, Filter Filter, params string[] SortOrder)
Finds the first page of objects in a given collection.
MongoDBProvider(string HostName, string DatabaseName, string DefaultCollectionName)
MongoDB database provider.
string[] GetExcludedCollections()
Gets an array of collections that should be excluded from backups.
async Task< IEnumerable< object > > FindDelete(string Collection, int Offset, int MaxCount, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
Task UpdateLazy(object[] Objects, ObjectsCallback Callback)
Updates a collection of objects in the database, if unlocked. If locked, objects will be updated at n...
Task< string[]> Analyze(XmlWriter Output, string XsltPath, string ProgramDataFolder, bool ExportData, bool Repair)
Analyzes the database and exports findings to XML.
Task InsertLazy(IEnumerable< object > Objects, ObjectsCallback Callback)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
MongoClient Client
Underlying MongoDB client.
Task Flush()
Persists any pending changes.
Task< IEnumerable< object > > Find(string CollectionName, int Offset, int MaxCount, Filter Filter, params string[] SortOrder)
Finds objects in a given collection.
async Task Insert(object Object)
Inserts an object into the database.
Task EndBulk()
Ends bulk-processing of data. Must be called once for every call to StartBulk.
Task InsertLazy(object Object, ObjectCallback Callback)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
Task< string[]> Repair(ProfilerThread Thread, params string[] CollectionNames)
Repairs a set of collections.
Task< string[]> Repair(XmlWriter Output, string XsltPath, string ProgramDataFolder, bool ExportData)
Analyzes the database and repairs it if necessary. Results are exported to XML.
Task< GenericObject > Generalize(object Object)
Creates a generalized representation of an object.
async Task Update(IEnumerable< object > Objects)
Updates a collection of objects in the database.
async Task Delete(IEnumerable< object > Objects)
Deletes a collection of objects in the database.
Task< IEnumerable< object > > Find(string CollectionName, int Offset, int MaxCount, params string[] SortOrder)
Finds objects in a given collection.
async Task< string[]> GetCollections()
Gets an array of available collections.
async Task< bool > Export(IDatabaseExport Output, string[] CollectionNames, ProfilerThread Thread)
Performs an export of the database.
Task InsertLazy(object[] Objects, ObjectsCallback Callback)
Inserts an object into the database, if unlocked. If locked, object will be inserted at next opportun...
IMongoCollection< BsonDocument > GetCollection(string CollectionName)
Gets a collection.
ObjectSerializer GetObjectSerializerEx(Type Type)
Gets the object serializer corresponding to a specific object.
async Task DeleteLazy(string Collection, int Offset, int MaxCount, string[] SortOrder, ObjectsCallback Callback)
Finds objects in a given collection and deletes them in the same atomic operation.
Contains a page of items.
Abstract base class for generated object serializers.
static Guid ObjectIdToGuid(ObjectId ObjectId)
Converts a MongoDB Object ID to a GUID
static ObjectId GuidToObjectId(Guid Guid)
Converts a GUID to a MongoDB Object ID
Provides a generic object serializer.
Serializes a type to BSON, taking into account attributes defined in Waher.Persistence....
virtual string CollectionName(object Object)
Name of collection objects of this type is to be stored in, if available. If not available,...
virtual bool IsDefaultValue(string FieldName, object Value)
Checks if a given field value corresponds to the default value for the corresponding field.
virtual bool TryGetFieldType(string FieldName, object Object, out Type FieldType)
Gets the type of a field or property of an object, given its name.
Type ValueType
Gets the type of the value.
virtual async Task< ObjectId > GetObjectId(object Value, bool InsertIfNotFound)
Gets the Object ID for a given object.
string ToShortName(string FieldName)
Converts a field name to its corresponding short name. If no explicit short name is available,...
virtual string ObjectIdMemberName
Mamber name of the field or property holding the Object ID, if any. If there are no such member,...
virtual bool HasObjectId(object Value)
If the class has an Object ID.
virtual bool HasObjectIdField
If the class has an Object ID field.
object Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
Deserializes a value.
bool BackupCollection
If the corresponding collection should be backed up or not.
static readonly DateTime UnixEpoch
UNIX Epoch, started at 1970-01-01, 00:00:00 (GMT)
Serializes an enumerated value value.
This class manages a string dictionary in a persisted storage.
Generic object. Contains a sequence of properties.
string TypeName
Type name.
Implements an in-memory cache.
bool TryGetValue(KeyType Key, out ValueType Value)
Tries to get a value from the cache.
void Add(KeyType Key, ValueType Value)
Adds an item to the cache.
Static class that dynamically manages types and interfaces available in the runtime environment.
static Type GetType(string FullName)
Gets a type, given its full name.
static object[] NoParameters
Contains an empty array of parameter values.
static Type[] GetTypesImplementingInterface(string InterfaceFullName)
Gets all types implementing a given interface.
static ConstructorInfo GetDefaultConstructor(Type Type)
Gets the default constructor of a type, if one exists.
Class that keeps track of events and timing for one thread.
void Start()
Processing starts.
void Exception(System.Exception Exception)
Exception occurred
void Stop()
Processing starts.
void Idle()
Thread goes idle.
void NewState(string State)
Thread changes state.
Interface for database providers that can be plugged into the static Database class.
Interface for paginated results.
Persistent dictionary that can contain more entries than possible in the internal memory.
Interface for object serializers.
Type ValueType
What type of object is being serialized.
Interface for database exports that filter objects.
Interface for database exports.
Task< bool > EndCollection()
Is called when a collection is finished.
Task< bool > ReportException(Exception Exception)
Is called when an exception has occurred.
Task< bool > ReportProperty(string PropertyName, object PropertyValue)
Is called when a property is reported.
Task< bool > EndObject()
Is called when an object is finished.
Task< bool > EndIndex()
Is called when an index in a collection is finished.
Task< bool > ReportError(string Message)
Is called when an error is reported.
Task< bool > ReportIndexField(string FieldName, bool Ascending)
Is called when a field in an index is reported.
Task< bool > StartDatabase()
Is called when export of database is started.
Task< bool > StartIndex()
Is called when an index in a collection is started.
Task< bool > EndDatabase()
Is called when export of database is finished.
Task< string > StartObject(string ObjectId, string TypeName)
Is called when an object is started.
Task< bool > StartCollection(string CollectionName)
Is called when a collection is started.
Interface for iterations of database contents.
delegate void ObjectCallback(object Object)
Method called when a process has completed for an object.
delegate void ObjectsCallback(IEnumerable< object > Objects)
Method called when a process has completed for a set of objects.