示例#1
0
        public static void TestiBoxDB()
        {
            DB.Root(root);
            long idbFile = 1L;

            BoxSystem.DBDebug.DeleteDBFiles(idbFile);

            using var server = new AppServer();

            using var db = server.GetInstance(idbFile);
            Console.Write("Database Transaction Test: ");
            using var box1 = db.Cube();
            box1["T1"].Insert(new T1 {
                Id = -1, Value = (-1).ToString()
            });

            using var box2 = db.Cube();
            box2["T1"].Insert(new T1 {
                Id = -2, Value = (-2).ToString()
            });

            var transaction1 = box1.Select <T1>("from T1").ToArray();
            var transaction2 = box2.Select <T1>("from T1").ToArray();

            if (transaction1.Length == 1 && transaction1[0].Id == -1 &&
                transaction2.Length == 1 && transaction2[0].Id == -2)
            {
                Console.WriteLine("Succeeded");
            }
            else
            {
                Console.WriteLine("Failed");
            }
            box1.Commit();
            box2.Commit();


            BoxSystem.DBDebug.StartWatch();
            int count = 0;

            Parallel.For(0, threadCount, (p) =>
            {
                using var box = db.Cube();
                for (int i = 0; i < batchCount; i++)
                {
                    var id = (p * batchCount) + i;
                    box["T1"].Insert(new T1 {
                        Id = id, Value = id.ToString()
                    });
                    Interlocked.Add(ref count, 1);
                }
                CommitResult cr = box.Commit();

                var minId = p * batchCount + 0;
                var maxId = p * batchCount + batchCount;
                for (int r = 0; r < reinterationSelect; r++)
                {
                    using (var boxt = db.Cube())
                    {
                        var reader = boxt.Select <T1>("from T1 where Id>=? & Id<? order by Id", minId, maxId).GetEnumerator();
                        var ti     = minId;
                        while (reader.MoveNext())
                        {
                            var iv = reader.Current.Id;
                            if (ti != iv)
                            {
                                throw new Exception(ti + "  " + iv);
                            }
                            ti++;
                        }
                        if (ti != maxId)
                        {
                            throw new Exception();
                        }
                    }
                }
            }
                         );

            if (count != (batchCount * threadCount))
            {
                throw new Exception(count + "  " + (batchCount * threadCount));
            }
            var avg = (int)(count / BoxSystem.DBDebug.StopWatch().TotalSeconds);

            Console.WriteLine("iBoxDB Insert:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

            //---------Update-----------------//
            BoxSystem.DBDebug.StartWatch();
            count = 0;
            Parallel.For(0, threadCount, (p) =>
            {
                using var box = db.Cube();
                for (int i = 0; i < batchCount; i++)
                {
                    var id  = (p * batchCount) + i;
                    var t   = box["T1", id].Update <T1>();
                    t.Value = "A" + t.Value;
                    Interlocked.Add(ref count, 1);
                }
                CommitResult cr = box.Commit();



                var minId = p * batchCount + 0;
                var maxId = p * batchCount + batchCount;

                for (int r = 0; r < reinterationSelect; r++)
                {
                    using var boxt = db.Cube();
                    var reader     = boxt.Select <T1>("from T1 where Id>=? & Id<? order by Id", minId, maxId).GetEnumerator();
                    var ti         = minId;
                    while (reader.MoveNext())
                    {
                        var iv = reader.Current.Id;
                        if (ti != iv)
                        {
                            throw new Exception(ti + "  " + iv);
                        }
                        if (reader.Current.Value != ("A" + iv))
                        {
                            throw new Exception();
                        }
                        ti++;
                    }
                    if (ti != maxId)
                    {
                        throw new Exception();
                    }
                }
            }
                         );

            if (count != (batchCount * threadCount))
            {
                throw new Exception(count + "  " + (batchCount * threadCount));
            }
            avg = (int)(count / BoxSystem.DBDebug.StopWatch().TotalSeconds);
            Console.WriteLine("iBoxDB Update:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

            //-------Delete---------------//
            BoxSystem.DBDebug.StartWatch();
            count = 0;
            Parallel.For(0, threadCount, (p) =>
            {
                var minId = p * batchCount + 0;
                var maxId = p * batchCount + batchCount;
                for (int r = 0; r < reinterationSelect; r++)
                {
                    using var boxt = db.Cube();
                    var reader     = boxt.Select <T1>("from T1 where Id>=? & Id<? order by Id", minId, maxId).GetEnumerator();
                    var ti         = minId;
                    while (reader.MoveNext())
                    {
                        var iv = reader.Current.Id;
                        if (ti != iv)
                        {
                            throw new Exception(ti + "  " + iv);
                        }
                        if (reader.Current.Value != ("A" + iv))
                        {
                            throw new Exception();
                        }
                        ti++;
                    }
                    if (ti != maxId)
                    {
                        throw new Exception();
                    }
                }


                using var box = db.Cube();
                for (int i = 0; i < batchCount; i++)
                {
                    var id = (p * batchCount) + i;
                    box["T1", id].Delete();
                    Interlocked.Add(ref count, 1);
                }
                CommitResult cr = box.Commit();
            }
                         );

            if (count != (batchCount * threadCount))
            {
                throw new Exception(count + "  " + (batchCount * threadCount));
            }
            avg = (int)(count / BoxSystem.DBDebug.StopWatch().TotalSeconds);
            Console.WriteLine("iBoxDB Delete:" + count.ToString("N0") + "  AVG: " + avg.ToString("N0") + " objects/s");

            if (db.Get().SelectCount("from T1") != 2) //transaction insert 2 more objects.
            {
                throw new Exception();
            }
        }