コード例 #1
0
ファイル: LWFilesBTreeTests.cs プロジェクト: iamr8/IoTGateway
        private async Task DBFiles_BTree_Test_UpdateObjects(int c)
        {
            Simple[] Objects = new Simple[c];
            Simple   Obj;
            int      i;

            for (i = 0; i < c; i++)
            {
                Objects[i] = Obj = CreateSimple(this.MaxStringLength);
                await this.file.SaveNewObject(Obj);
            }

            //await AssertConsistent(this.file, this.provider, null, null, true);
            //Console.Out.WriteLine(await ExportXML(this.file, "Data\\BTreeBeforeUpdates.xml"));

            for (i = 0; i < c; i++)
            {
                Obj          = CreateSimple(this.MaxStringLength);
                Obj.ObjectId = Objects[i].ObjectId;

                await this.file.UpdateObject(Obj);

                Objects[i] = Obj;

                Obj = await this.file.LoadObject <Simple>(Obj.ObjectId);

                DBFilesObjectSerializationTests.AssertEqual(Objects[i], Obj);
            }

            await AssertConsistent(this.file, this.provider, null, null, true);

            //Console.Out.WriteLine(await ExportXML(this.file, "Data\\BTreeAfterUpdates.xml"));

            for (i = 0; i < c; i++)
            {
                Obj = await this.file.LoadObject <Simple>(Objects[i].ObjectId);

                DBFilesObjectSerializationTests.AssertEqual(Objects[i], Obj);
            }
        }
コード例 #2
0
        private async Task Test_Set(int MaxLen)
        {
            byte[] ByteArray = this.GetBytes(MaxLen, 0);
            Simple Obj       = DBFilesBTreeTests.CreateSimple(MaxLen);

            this.file["Key1"] = "Value1";
            this.file["Key2"] = "Value2";
            this.file["Key3"] = "Value3";
            this.file["Key4"] = null;
            this.file["Key5"] = Obj;
            this.file["Key6"] = ByteArray;
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key1"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key2"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key3"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key4"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key5"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key6"));
            AssertEx.Same(this.file["Key1"], "Value1");
            AssertEx.Same(this.file["Key2"], "Value2");
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);
        }
コード例 #3
0
        public async Task DBFiles_StringDictionary_01_Set()
        {
            byte[] ByteArray = new byte[] { 1, 2, 3, 4, 5 };
            Simple Obj       = DBFilesBTreeTests.CreateSimple(100);

            this.file["Key1"] = "Value1";
            this.file["Key2"] = "Value2";
            this.file["Key3"] = "Value3";
            this.file["Key4"] = null;
            this.file["Key5"] = Obj;
            this.file["Key6"] = ByteArray;
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key1"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key2"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key3"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key4"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key5"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key6"));
            AssertEx.Same(this.file["Key1"], "Value1");
            AssertEx.Same(this.file["Key2"], "Value2");
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);
        }
コード例 #4
0
        public async Task DBFiles_BTree_Test_22_SelectIthObject()
        {
            int c = ObjectsToEnumerate;
            SortedDictionary <Guid, Simple> Objects = await this.CreateObjects(c);

            Simple[] Ordered = new Simple[c];
            Objects.Values.CopyTo(Ordered, 0);
            Guid?  Prev = null;
            Simple Obj;
            Random gen = new Random();
            int    i, j;

            for (i = 0; i < c; i++)
            {
                j    = 0;
                Prev = null;

                if (i < 10 || (gen.Next(0, 2) == 0 && i <= c - 10))
                {
                    using (ObjectBTreeFileEnumerator <Simple> e = await this.file.GetTypedEnumeratorAsync <Simple>(true))
                    {
                        Assert.IsTrue(await e.GoToObject((uint)i));

                        do
                        {
                            Obj = e.Current;
                            if (Prev.HasValue)
                            {
                                AssertEx.Less(Prev.Value, Obj.ObjectId);
                            }

                            Prev = Obj.ObjectId;
                            DBFilesObjectSerializationTests.AssertEqual(Ordered[i + j], Obj);

                            AssertEx.Same(i + j, e.CurrentRank);
                            AssertEx.Same(Obj.ObjectId, e.CurrentObjectId);
                        }while (e.MoveNext() && j++ < 10);
                    }
                }
                else
                {
                    using (ObjectBTreeFileEnumerator <Simple> e = await this.file.GetTypedEnumeratorAsync <Simple>(true))
                    {
                        Assert.IsTrue(await e.GoToObject((uint)i));

                        do
                        {
                            Obj = e.Current;
                            if (Prev.HasValue)
                            {
                                AssertEx.Greater(Prev.Value, Obj.ObjectId);
                            }

                            Prev = Obj.ObjectId;
                            DBFilesObjectSerializationTests.AssertEqual(Ordered[i - j], Obj);

                            AssertEx.Same(i - j, e.CurrentRank);
                            AssertEx.Same(Obj.ObjectId, e.CurrentObjectId);
                        }while (e.MovePrevious() && j++ < 10);
                    }
                }
            }
        }
コード例 #5
0
        private async Task TestMultiple(int c, bool AssertIndividually, int?LogStatisticsEvery)
        {
            DateTime Start                     = DateTime.Now;
            List <FileStatistics> Stat         = null;
            List <double>         Milliseconds = null;
            int i;

            Simple[] Objects = new Simple[c];
            Simple   Obj2;

            for (i = 0; i < c; i++)
            {
                Objects[i] = CreateSimple(this.MaxStringLength);
                await this.file.SaveNewObject(Objects[i]);

                if (AssertIndividually)
                {
                    Console.Out.WriteLine();
                    Console.Out.WriteLine((i + 1).ToString() + " objects:");
                    Console.Out.WriteLine(new string('-', 80));

                    await AssertConsistent(this.file, this.provider, i + 1, Objects[i], true);
                }

                if (LogStatisticsEvery.HasValue && (i + 1) % LogStatisticsEvery.Value == 0)
                {
                    if (Stat == null)
                    {
                        Milliseconds = new List <double>();
                        Stat         = new List <FileStatistics>();
                    }

                    Milliseconds.Add((DateTime.Now - Start).TotalMilliseconds / LogStatisticsEvery.Value);
                    Stat.Add(await this.file.ComputeStatistics());

                    Start = DateTime.Now;
                }
            }

            for (i = 0; i < c; i++)
            {
                Obj2 = await this.file.LoadObject <Simple>(Objects[i].ObjectId);

                DBFilesObjectSerializationTests.AssertEqual(Objects[i], Obj2);
            }

            if (!AssertIndividually)
            {
                await AssertConsistent(this.file, this.provider, c, null, true);
            }

            if (Stat != null)
            {
                Variables v = new Variables()
                {
                    { "Stat", Stat.ToArray() },
                    { "ms", Milliseconds.ToArray() },
                    { "StepSize", LogStatisticsEvery.Value }
                };

                Expression Exp = new Expression("[ms,Stat.BlockSize,Stat.NrBlocks,Stat.NrBytesUsed,Stat.NrBytesUnused,Stat.NrBytesTotal," +
                                                "Stat.Usage,Stat.NrObjects,Stat.MinObjectSize,Stat.MaxObjectSize,Stat.AverageObjectSize,Stat.MinDepth,Stat.MaxDepth," +
                                                "Stat.NrBlockLoads,Stat.NrCacheLoads,Stat.NrBlockSaves,Stat.MinObjectsPerBlock,Stat.MaxObjectsPerBlock," +
                                                "Stat.AverageObjectsPerBlock,Stat.MinBytesUsedPerBlock,Stat.MaxBytesUsedPerBlock,Stat.AverageBytesUsedPerBlock]T");

                Console.Out.WriteLine("ms, BlockSize, NrBlocks, NrBytesUsed, NrBytesUnused, NrBytesTotal, " +
                                      "Usage, NrObjects, MinObjectSize, MaxObjectSize, AverageObjectSize, MinDepth, MaxDepth, " +
                                      "NrBlockLoads, NrCacheLoads, NrBlockSaves,Min(Obj/Block),Max(Obj/Block),Avg(Obj/Block)," +
                                      "Min(UsedBytes/Block),Max(UsedBytes/Block),Avg(UsedBytes/Block)");
                Console.Out.WriteLine(new string('-', 80));
                Console.Out.WriteLine(Exp.Evaluate(v).ToString());
            }
        }
コード例 #6
0
        private async Task DBFiles_BTree_Test_DeleteObjects(int c, bool CheckForEachObject)
        {
            Random Gen = new Random();

            Simple[] Objects = new Simple[c];
            Simple   Obj;
            int      i;

            for (i = 0; i < c; i++)
            {
                Objects[i] = Obj = CreateSimple(this.MaxStringLength);
                await this.file.SaveNewObject(Obj);
            }

            while (c > 0)
            {
                i = Gen.Next(0, c);

                Obj = Objects[i];
                c--;
                if (i < c)
                {
                    Array.Copy(Objects, i + 1, Objects, i, c - i);
                }

                if (CheckForEachObject)
                {
                    try
                    {
                        this.provider.CloseFile(this.file.CollectionName);
                        this.file = null;

                        File.Copy(FileName, FileName + ".bak", true);
                        File.Copy(BlobFileName, BlobFileName + ".bak", true);
                        File.Copy(NamesFileName, NamesFileName + ".bak", true);

                        this.file = await this.provider.GetFile(CollectionName);

                        if (File.Exists(ObjIdFileName))
                        {
                            File.Delete(ObjIdFileName);
                        }

                        File.WriteAllBytes(ObjIdFileName, Obj.ObjectId.ToByteArray());

                        if (File.Exists(BlockSizeFileName))
                        {
                            File.Delete(BlockSizeFileName);
                        }

                        File.WriteAllText(BlockSizeFileName, this.BlockSize.ToString());

                        Console.Out.WriteLine(await ExportXML(this.file, "Data\\BTreeBefore.xml"));
                        Console.Out.WriteLine(Obj.ObjectId);
                        await this.file.DeleteObject(Obj);

                        //Console.Out.WriteLine(await ExportXML(this.file, "Data\\BTreeAfter.xml"));
                        await AssertConsistent(this.file, this.provider, null, null, true);

                        for (i = 0; i < c; i++)
                        {
                            Obj = await this.file.LoadObject <Simple>(Objects[i].ObjectId);

                            DBFilesObjectSerializationTests.AssertEqual(Objects[i], Obj);
                        }
                    }
                    catch (Exception ex)
                    {
                        if (this.file != null)
                        {
                            Console.Out.WriteLine(await ExportXML(this.file, "Data\\BTreeError.xml"));
                        }

                        ExceptionDispatchInfo.Capture(ex).Throw();
                    }
                }
                else
                {
                    await this.file.DeleteObject(Obj);
                }
            }

            FileStatistics Stat = await AssertConsistent(this.file, this.provider, null, null, true);

            AssertEx.Same(0, this.file.Count);
            AssertEx.Same(1, Stat.NrBlocks);
            AssertEx.Same(0, Stat.NrBlobBlocks);
        }
コード例 #7
0
        private async Task TestMultiple(int NrObjects, int ArraySize, int BulkSize, bool AssertIndividually, int?LogStatisticsEvery)
        {
            DateTime Start = DateTime.Now;
            List <FileStatistics> Stat = null;
            List <double>         Milliseconds = null;
            int i = 0, j;

            Simple[]         Objects = new Simple[NrObjects];
            Simple[]         Block   = new Simple[ArraySize];
            Simple           Obj2;
            ObjectSerializer Serializer = this.provider.GetObjectSerializerEx(typeof(Simple));

            if (BulkSize > 1)
            {
                await this.provider.StartBulk();
            }

            while (i < NrObjects)
            {
                for (j = 0; j < ArraySize; j++)
                {
                    Block[j]     = CreateSimple(this.MaxStringLength);
                    Objects[i++] = Block[j];
                }

                if (ArraySize > 1)
                {
                    await this.file.SaveNewObjects(Block, Serializer);
                }
                else
                {
                    await this.file.SaveNewObject(Block[0]);
                }

                if (BulkSize > 1 && i % BulkSize == 0)
                {
                    await this.provider.EndBulk();

                    await this.provider.StartBulk();
                }

                if (AssertIndividually)
                {
                    Console.Out.WriteLine();
                    Console.Out.WriteLine(i.ToString() + " objects:");
                    Console.Out.WriteLine(new string('-', 80));

                    await AssertConsistent(this.file, this.provider, i, Objects[i - 1], true);
                }

                if (LogStatisticsEvery.HasValue && i % LogStatisticsEvery.Value == 0)
                {
                    if (Stat is null)
                    {
                        Milliseconds = new List <double>();
                        Stat         = new List <FileStatistics>();
                    }

                    Milliseconds.Add((DateTime.Now - Start).TotalMilliseconds / LogStatisticsEvery.Value);
                    Stat.Add((await this.file.ComputeStatistics()).Key);

                    Start = DateTime.Now;
                }
            }

            if (BulkSize > 1)
            {
                await this.provider.EndBulk();
            }

            for (i = 0; i < NrObjects; i++)
            {
                Obj2 = await this.file.LoadObject <Simple>(Objects[i].ObjectId);

                DBFilesObjectSerializationTests.AssertEqual(Objects[i], Obj2);
            }

            if (!AssertIndividually)
            {
                await AssertConsistent(this.file, this.provider, NrObjects, null, true);
            }

            if (Stat != null)
            {
                Variables v = new Variables()
                {
                    { "Stat", Stat.ToArray() },
                    { "ms", Milliseconds.ToArray() },
                    { "StepSize", LogStatisticsEvery.Value }
                };

                Expression Exp = new Expression("[ms,Stat.BlockSize,Stat.NrBlocks,Stat.NrBytesUsed,Stat.NrBytesUnused,Stat.NrBytesTotal," +
                                                "Stat.Usage,Stat.NrObjects,Stat.MinObjectSize,Stat.MaxObjectSize,Stat.AverageObjectSize,Stat.MinDepth,Stat.MaxDepth," +
                                                "Stat.NrBlockLoads,Stat.NrCacheLoads,Stat.NrBlockSaves,Stat.MinObjectsPerBlock,Stat.MaxObjectsPerBlock," +
                                                "Stat.AverageObjectsPerBlock,Stat.MinBytesUsedPerBlock,Stat.MaxBytesUsedPerBlock,Stat.AverageBytesUsedPerBlock]T");

                Console.Out.WriteLine("ms, BlockSize, NrBlocks, NrBytesUsed, NrBytesUnused, NrBytesTotal, " +
                                      "Usage, NrObjects, MinObjectSize, MaxObjectSize, AverageObjectSize, MinDepth, MaxDepth, " +
                                      "NrBlockLoads, NrCacheLoads, NrBlockSaves,Min(Obj/Block),Max(Obj/Block),Avg(Obj/Block)," +
                                      "Min(UsedBytes/Block),Max(UsedBytes/Block),Avg(UsedBytes/Block)");
                Console.Out.WriteLine(new string('-', 80));
                Console.Out.WriteLine(Exp.Evaluate(v).ToString());
            }
        }
コード例 #8
0
        private async Task Test_Remove(int MaxLen)
        {
            byte[] ByteArray = this.GetBytes(MaxLen, 0);
            Simple Obj       = DBFilesBTreeTests.CreateSimple(MaxLen);

            await this.file.AddAsync("Key1", "Value1");

            await this.file.AddAsync("Key2", "Value2");

            await this.file.AddAsync("Key3", "Value3");

            await this.file.AddAsync("Key4", null);

            await this.file.AddAsync("Key5", Obj);

            await this.file.AddAsync("Key6", ByteArray);

            Assert.IsTrue(await this.file.ContainsKeyAsync("Key1"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key2"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key3"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key4"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key5"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key6"));
            AssertEx.Same(this.file["Key1"], "Value1");
            AssertEx.Same(this.file["Key2"], "Value2");
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key2"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key2"));
            Assert.IsFalse(await this.file.RemoveAsync("Key2"));
            AssertEx.Same(this.file["Key1"], "Value1");
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key1"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key1"));
            Assert.IsFalse(await this.file.RemoveAsync("Key1"));
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key3"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key3"));
            Assert.IsFalse(await this.file.RemoveAsync("Key3"));
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key4"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key4"));
            Assert.IsFalse(await this.file.RemoveAsync("Key4"));
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key5"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key5"));
            Assert.IsFalse(await this.file.RemoveAsync("Key5"));
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key6"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key6"));
            Assert.IsFalse(await this.file.RemoveAsync("Key6"));
        }
コード例 #9
0
        public async Task DBFiles_StringDictionary_05_Remove()
        {
            byte[] ByteArray = new byte[] { 1, 2, 3, 4, 5 };
            Simple Obj       = DBFilesBTreeTests.CreateSimple(100);

            await this.file.AddAsync("Key1", "Value1");

            await this.file.AddAsync("Key2", "Value2");

            await this.file.AddAsync("Key3", "Value3");

            await this.file.AddAsync("Key4", null);

            await this.file.AddAsync("Key5", Obj);

            await this.file.AddAsync("Key6", ByteArray);

            Assert.IsTrue(await this.file.ContainsKeyAsync("Key1"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key2"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key3"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key4"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key5"));
            Assert.IsTrue(await this.file.ContainsKeyAsync("Key6"));
            AssertEx.Same(this.file["Key1"], "Value1");
            AssertEx.Same(this.file["Key2"], "Value2");
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key2"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key2"));
            Assert.IsFalse(await this.file.RemoveAsync("Key2"));
            AssertEx.Same(this.file["Key1"], "Value1");
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key1"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key1"));
            Assert.IsFalse(await this.file.RemoveAsync("Key1"));
            AssertEx.Same(this.file["Key3"], "Value3");
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key3"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key3"));
            Assert.IsFalse(await this.file.RemoveAsync("Key3"));
            Assert.IsNull(this.file["Key4"]);
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key4"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key4"));
            Assert.IsFalse(await this.file.RemoveAsync("Key4"));
            DBFilesObjectSerializationTests.AssertEqual(this.file["Key5"] as Simple, Obj);
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key5"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key5"));
            Assert.IsFalse(await this.file.RemoveAsync("Key5"));
            AssertEx.Same(this.file["Key6"], ByteArray);

            Assert.IsTrue(await this.file.RemoveAsync("Key6"));
            Assert.IsFalse(await this.file.ContainsKeyAsync("Key6"));
            Assert.IsFalse(await this.file.RemoveAsync("Key6"));
        }