Exemple #1
0
        bool InsertInTransaction(Db db, int i)
        {
            using (var transaction = new LinqdbTransaction())
            {
                var d = new SomeData()
                {
                    Id         = i,
                    Normalized = 1.2,
                    PeriodId   = 5,
                    Value      = i
                };
                db.Table <SomeData>(transaction).Save(d);

                var d2 = new BinaryData()
                {
                    Id   = i,
                    Data = new List <byte>()
                    {
                        1, 2, 3
                    }.ToArray()
                };
                db.Table <BinaryData>(transaction).Save(d2);

                if (i % 2 == 0)
                {
                    transaction.Commit();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Exemple #2
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif


            using (var transaction = new LinqdbTransaction())
            {
                db.Table <SomeData>(transaction).Delete(new HashSet <int>());
                transaction.Commit();
            }

            using (var transaction = new LinqdbTransaction())
            {
                db.Table <SomeData>(transaction).SaveBatch(new List <SomeData>());
                transaction.Commit();
            }

            using (var transaction = new LinqdbTransaction())
            {
                db.Table <SomeData>(transaction).Update(f => f.Normalized, new Dictionary <int, double?>());
                transaction.Commit();
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #3
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

            using (var tr = new LinqdbTransaction())
            {
                var tasks = new List <Task <bool> >();
                var t1    = Task.Run <bool>(() =>
                {
                    return(InsertInTransaction <SomeData>(tr, db));
                });
                tasks.Add(t1);
                var t2 = Task.Run <bool>(() =>
                {
                    return(InsertInTransaction <BinaryData>(tr, db));
                });
                tasks.Add(t2);
                var t3 = Task.Run <bool>(() =>
                {
                    return(InsertInTransaction <SomeType>(tr, db));
                });
                tasks.Add(t3);
                var t4 = Task.Run <bool>(() =>
                {
                    return(InsertInTransaction <Answer>(tr, db));
                });
                tasks.Add(t4);

                tasks.ForEach(f => f.Wait());

                tr.Commit();
                if (db.Table <SomeData>().SelectEntity().Count() != 1 || db.Table <BinaryData>().SelectEntity().Count() != 1 ||
                    db.Table <SomeType>().SelectEntity().Count() != 1 || db.Table <Answer>().SelectEntity().Count() != 1)
                {
                    throw new Exception("Assert failure");
                }
            }
            if (dispose)
            {
                db.Dispose();
            }
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
        }
Exemple #4
0
        public static void Do(Db db, List <int> a_ids)
        {
            try
            {
                using (var batch = new LinqdbTransaction())
                {
                    var k = new KaggleClass()
                    {
                        Q1 = "1 2 3",
                        Q2 = "4 5 6",
                        CommonPercentage = 0.5
                    };
                    db.Table <KaggleClass>(batch).Save(k);

                    var rg = new Random();
                    int id = 0;
                    lock (_lock)
                    {
                        if (a_ids.Any())
                        {
                            int which = rg.Next(0, Int32.MaxValue) % a_ids.Count();
                            id = a_ids[which];
                            a_ids.RemoveAt(which);
                        }
                    }
                    if (id > 0)
                    {
                        db.Table <Answer>(batch).Delete(id);
                    }
                    batch.Commit();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ParallelBatch: " + ex.Message);
                lock (_lock)
                {
                    errors++;
                }
            }
        }
Exemple #5
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif
            using (var transaction = new LinqdbTransaction())
            {
                var d = new SomeData()
                {
                    NameSearch = "test"
                };
                db.Table <SomeData>(transaction).Save(d);

                d = new SomeData()
                {
                    NameSearch = "test"
                };
                db.Table <SomeData>(transaction).Save(d);
                transaction.Commit();
            }
            var res = db.Table <SomeData>().Search(f => f.NameSearch, "test").SelectEntity();
            if (res.Count() != 2)
            {
                throw new Exception("Assert failure");
            }
            if (db.Table <SomeData>().Count() != 2)
            {
                throw new Exception("Assert failure");
            }

            var dic = new Dictionary <int, string>()
            {
                { res[0].Id, "updated" }, { res[1].Id, "updated" }
            };
            using (var transaction = new LinqdbTransaction())
            {
                db.Table <SomeData>(transaction).Update(f => f.NameSearch, dic);
                transaction.Commit();
            }
            res = db.Table <SomeData>().Search(f => f.NameSearch, "test").SelectEntity();
            if (res.Count() != 0)
            {
                throw new Exception("Assert failure");
            }
            res = db.Table <SomeData>().Search(f => f.NameSearch, "updated").SelectEntity();
            if (res.Count() != 2)
            {
                throw new Exception("Assert failure");
            }
            if (db.Table <SomeData>().Count() != 2)
            {
                throw new Exception("Assert failure");
            }

            using (var transaction = new LinqdbTransaction())
            {
                db.Table <SomeData>(transaction).Delete(new HashSet <int>(new int[2] {
                    res[0].Id, res[1].Id
                }));
                transaction.Commit();
            }
            res = db.Table <SomeData>().Search(f => f.NameSearch, "test").SelectEntity();
            if (res.Count() != 0)
            {
                throw new Exception("Assert failure");
            }
            res = db.Table <SomeData>().Search(f => f.NameSearch, "updated").SelectEntity();
            if (res.Count() != 0)
            {
                throw new Exception("Assert failure");
            }
            res = db.Table <SomeData>().SelectEntity();
            if (res.Count() != 0)
            {
                throw new Exception("Assert failure");
            }
            if (db.Table <SomeData>().Count() != 0)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #6
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif

            var a = new SomeData()
            {
                Id         = 1,
                Normalized = 1.2,
                PeriodId   = 5,
                NameSearch = "test"
            };
            db.Table <SomeData>().Save(a);

            using (var tran = new LinqdbTransaction())
            {
                var d = new SomeData()
                {
                    Id         = 2,
                    Normalized = 1.2,
                    PeriodId   = 5,
                    NameSearch = "test"
                };
                db.Table <SomeData>(tran).Save(d);

                d = new SomeData()
                {
                    Id         = 3,
                    Normalized = 1.2,
                    PeriodId   = 5,
                    NameSearch = "test"
                };
                db.Table <SomeData>(tran).Save(d);

                db.Table <SomeData>(tran).Update(f => f.NameSearch, 1, "hohoho");

                tran.Commit();
            }

            var count = db.Table <SomeData>().Count();

            if (count != 3)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #7
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
            db.Table <BinaryData>().Delete(new HashSet <int>(db.Table <BinaryData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
            db.Table <Counter>().Delete(new HashSet <int>(db.Table <Counter>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif
            using (var transaction = new LinqdbTransaction())
            {
                var d = new SomeData()
                {
                    Id         = 1,
                    Normalized = 1.2,
                    PeriodId   = 5
                };
                db.Table <SomeData>(transaction).Save(d);

                var d2 = new BinaryData()
                {
                    Id   = 1,
                    Data = new List <byte>()
                    {
                        1, 2, 3
                    }.ToArray()
                };
                db.Table <BinaryData>(transaction).Save(d2);
            }

            if (db.Table <SomeData>().SelectEntity().Any() || db.Table <BinaryData>().SelectEntity().Any())
            {
                throw new Exception("Assert failure");
            }

            using (var transaction = new LinqdbTransaction())
            {
                var d = new SomeData()
                {
                    Normalized = 1.2,
                    PeriodId   = 5
                };
                db.Table <SomeData>(transaction).Save(d);

                d = new SomeData()
                {
                    Normalized = 1.2,
                    PeriodId   = 5
                };
                db.Table <SomeData>(transaction).Save(d);

                var d2 = new BinaryData()
                {
                    Data = new List <byte>()
                    {
                        1, 2, 3
                    }.ToArray()
                };
                db.Table <BinaryData>(transaction).Save(d2);


                transaction.Commit();
            }

            if (db.Table <SomeData>().SelectEntity().Count() != 2 || db.Table <BinaryData>().SelectEntity().Count() != 1)
            {
                throw new Exception("Assert failure");
            }

            if (db.Table <SomeData>().Count() != 2)
            {
                throw new Exception("Assert failure");
            }


#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #8
0
        public void Do2(Db db)
        {
            bool dispose = false;

            if (db == null)
            {
                db      = new Db("DATA");
                dispose = true;
#if (SERVER)
                db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
                db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
                using (var trans = new LinqdbTransaction())
                {
                    db.Table <SomeData>(trans).Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
                    trans.Commit();
                }
#endif
                DoInit = true;
            }
            this.db = db;

            var start_list = new List <SomeData>(JobTransManySelects.start);
            for (int i = 1; i < JobTransManySelects.start; i++)
            {
                start_list.Add(new SomeData()
                {
                    Id         = i,
                    GroupBy    = i % 10,
                    Normalized = Convert.ToDouble("0," + i)
                });
            }
            if (start_list.Any())
            {
                for (int i = 0; ; i++)
                {
                    var list = start_list.Skip(i * 50000).Take(50000).ToList();
                    if (!list.Any())
                    {
                        break;
                    }
                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <SomeData>(trans).SaveBatch(list);
                        trans.Commit();
                    }
                }
            }


            JobTransManySelects.errors  = 0;
            JobTransManySelects.numbers = new List <int>();
            JobTransManySelects.rg      = new Random();

            for (int j = 0; j < 5; j++)
            {
                var jobs = new List <JobTransManySelects>();
                for (int i = 0; i < 10000; i++)
                {
                    jobs.Add(new JobTransManySelects());
                }

                Parallel.ForEach(jobs, /*new ParallelOptions { MaxDegreeOfParallelism = 500 },*/ f =>
                {
                    JobTransManySelects.Do(db);
                });
            }

            if (JobTransManySelects.errors > 0)
            {
                throw new Exception("Assert failure, errors " + JobTransManySelects.errors);
            }
            var ids = db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList();
            for (int i = 0; ; i++)
            {
                var ids_delete = ids.Skip(i * 50000).Take(50000).ToList();
                if (!ids_delete.Any())
                {
                    break;
                }
                using (var trans = new LinqdbTransaction())
                {
                    db.Table <SomeData>().Delete(new HashSet <int>(ids_delete));
                    trans.Commit();
                }
            }
            if (DoInit)
            {
#if (SERVER || SOCKETS)
                if (dispose)
                {
                    Logic.Dispose();
                }
#else
                if (dispose)
                {
                    db.Dispose();
                }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
                if (dispose)
                {
                    ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
                }
#endif
            }
        }
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <LogEntry>().Delete(new HashSet <int>(db.Table <LogEntry>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
            db.Table <LangCounter>().Delete(new HashSet <int>(db.Table <LangCounter>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
            db.Table <Question>().Delete(new HashSet <int>(db.Table <Question>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif
            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    var n = new LangCounter()
                    {
                        Count        = 1,
                        Date         = DateTime.Now,
                        Lang         = 1,
                        SuccessCount = 1,
                        Is_api       = 1
                    };
                    db.Table <LangCounter>(tran)
                    .Where(f => f.Lang == 1)
                    .AtomicIncrement2Props(f => f.Count, z => z.SuccessCount, 1, 1, n);

                    throw new Exception("Assert failure");
                }
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("transactions are not supported with AtomicIncrement"))
                {
                    throw new Exception("Assert failure");
                }
            }

            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    var n = new LangCounter()
                    {
                        Count        = 1,
                        Date         = DateTime.Now,
                        Lang         = 1,
                        SuccessCount = 1,
                        Is_api       = 1
                    };
                    db.Table <LangCounter>(tran)
                    .Where(f => f.Lang == 1)
                    .AtomicIncrement(f => f.Count, 1, n, null);

                    throw new Exception("Assert failure");
                }
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("transactions are not supported with AtomicIncrement"))
                {
                    throw new Exception("Assert failure");
                }
            }

            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    var n = new LangCounter()
                    {
                        Count        = 1,
                        Date         = DateTime.Now,
                        Lang         = 1,
                        SuccessCount = 1,
                        Is_api       = 1
                    };
                    db.Table <LangCounter>(tran)
                    .Where(f => f.Lang == 1)
                    .AtomicIncrement(f => f.Count, 1, n, 0);

                    throw new Exception("Assert failure");
                }
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("transactions are not supported with AtomicIncrement"))
                {
                    throw new Exception("Assert failure");
                }
            }

            //transaction testing
            int total = 10000;
            using (var trans = new LinqdbTransaction())
            {
                var list = new List <SomeData>();
                for (int i = 1; i <= total; i++)
                {
                    list.Add(new SomeData()
                    {
                        Id         = i,
                        NameSearch = i + "",
                        Normalized = i,
                        ObjectId   = i,
                        PeriodId   = i,
                        Date       = DateTime.Now.AddDays(-i),
                        PersonId   = i,
                        Value      = i
                    });
                }
                db.Table <SomeData>(trans).SaveBatch(list);

                var list2 = new List <Question>();
                for (int i = 1; i <= total; i++)
                {
                    list2.Add(new Question()
                    {
                        Id               = i,
                        Body             = BitConverter.GetBytes(i),
                        AcceptedAnswerId = i,
                        AnswerCount      = i,
                        CommentCount     = i,
                        OwnerUserId      = i,
                        CreationDate     = DateTime.Now.AddDays(-i)
                    });
                }
                db.Table <Question>(trans).SaveBatch(list2);

                trans.Commit();
            }
            var res1 = db.Table <SomeData>().Count();
            if (res1 != total)
            {
                throw new Exception("Assert failure");
            }
            res1 = db.Table <Question>().Count();
            if (res1 != total)
            {
                throw new Exception("Assert failure");
            }

            int left = total;
            using (var trans = new LinqdbTransaction())
            {
                for (int i = 1; i <= total; i++)
                {
                    if (i % 2 == 0)
                    {
                        db.Table <SomeData>(trans).Delete(i);
                        db.Table <Question>(trans).Delete(i);
                        left--;
                    }
                }
                trans.Commit();
            }
            res1 = db.Table <SomeData>().Count();
            if (res1 != left)
            {
                throw new Exception("Assert failure");
            }
            res1 = db.Table <Question>().Count();
            if (res1 != left)
            {
                throw new Exception("Assert failure");
            }


#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #10
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif
            int id1 = 0;
            int id2 = 0;
            using (var transaction = new LinqdbTransaction())
            {
                var d = new SomeData()
                {
                    NameSearch = "test",
                    Normalized = 1
                };
                db.Table <SomeData>(transaction).Save(d);
                if (d.Id == 0)
                {
                    throw new Exception("Assert failure");
                }
                id1 = d.Id;

                d = new SomeData()
                {
                    NameSearch = "test",
                    Normalized = 1
                };
                db.Table <SomeData>(transaction).Save(d);
                if (d.Id == 0)
                {
                    throw new Exception("Assert failure");
                }
                id2 = d.Id;
                if (id1 == id2)
                {
                    throw new Exception("Assert failure");
                }
                transaction.Commit();
            }

            var res = db.Table <SomeData>().Where(f => f.Id == id1).SelectEntity();
            if (res.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            res = db.Table <SomeData>().Where(f => f.Id == id2).SelectEntity();
            if (res.Count() != 1)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif

            var d = new SomeData()
            {
                Id         = 1,
                Normalized = 1.2,
                PeriodId   = 5,
                NameSearch = "test"
            };
            db.Table <SomeData>().Save(d);

            d = new SomeData()
            {
                Id         = 2,
                Normalized = 1.2,
                PeriodId   = 5,
                NameSearch = "test"
            };
            db.Table <SomeData>().Save(d);

            string ex_msg = "";
            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    db.Table <SomeData>(tran).Update(f => f.NameSearch, 1, "hohoho");
                    db.Table <SomeData>(tran).Update(f => f.NameSearch, 2, "hohoho");
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                ex_msg = ex.Message;
            }

            if (!string.IsNullOrEmpty(ex_msg))
            {
                throw new Exception("Assert failure");
            }

            ex_msg = "";
            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    d = new SomeData()
                    {
                        Id         = 1,
                        Normalized = 1.2,
                        PeriodId   = 5,
                        NameSearch = "test"
                    };
                    db.Table <SomeData>(tran).Save(d);
                    db.Table <SomeData>(tran).Update(f => f.NameSearch, 1, "hohoho");
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                ex_msg = ex.Message;
            }
            if (!ex_msg.Contains("Linqdb: same entity cannot be modified twice in a transaction."))
            {
                throw new Exception("Assert failure");
            }

            ex_msg = "";
            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    db.Table <SomeData>(tran).Update(f => f.NameSearch, 1, "hohoho");
                    db.Table <SomeData>(tran).Update(f => f.NameSearch, 1, "hohoha");
                    tran.Commit();
                }
            }
            catch (Exception ex)
            {
                ex_msg = ex.Message;
            }

            if (!ex_msg.Contains("Linqdb: same entity's field cannot be updated twice in a transaction."))
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
        public static void Do(Db db)
        {
            try
            {
                int next = TransJob2.NextNumber();
                if (next % 4 == 0)
                {
                    var d = new BinaryData()
                    {
                        Id    = next,
                        Value = -next,
                        Data  = BitConverter.GetBytes(next)
                    };

                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <BinaryData>(trans).Save(d);
                        trans.Commit();
                    }

                    lock (_lock)
                    {
                        numbers.Add(next);
                    }
                }
                else if (next % 4 == 1)
                {
                    int to_read = 0;
                    lock (_lock)
                    {
                        if (!numbers.Any())
                        {
                            return;
                        }
                        to_read = numbers[rg.Next() % numbers.Count];
                    }
                    var entity = db.Table <BinaryData>().Where(f => f.Id == to_read).SelectEntity().FirstOrDefault();
                    if (entity == null)
                    {
                        lock (_lock)
                        {
                            if (numbers.Contains(to_read))
                            {
                                throw new Exception("Assert failure 2");
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    if (entity.Id != to_read || BitConverter.ToInt32(entity.Data, 0) != to_read || !(entity.Value == -to_read || entity.Value == null))
                    {
                        throw new Exception("Assert failure 3");
                    }
                }
                else if (next % 4 == 2)
                {
                    int to_delete = 0;
                    lock (_lock)
                    {
                        if (!numbers.Any())
                        {
                            return;
                        }

                        to_delete = numbers[rg.Next() % numbers.Count];
                        while (numbers.Contains(to_delete))
                        {
                            numbers.Remove(to_delete);
                        }
                    }
                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <BinaryData>(trans).Delete(to_delete);
                        trans.Commit();
                    }
                }
                else
                {
                    int to_update = 0;
                    lock (_lock)
                    {
                        if (!numbers.Any())
                        {
                            return;
                        }

                        to_update = numbers[rg.Next() % numbers.Count];
                    }
                    var val = db.Table <BinaryData>().Where(f => f.Id == to_update).Select(f => new { Value = f.Value }).FirstOrDefault();
                    if (val == null)
                    {
                        lock (_lock)
                        {
                            if (numbers.Contains(to_update))
                            {
                                throw new Exception("Assert failure 4");
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <BinaryData>(trans).Update(f => f.Value, to_update, (int?)null);
                        trans.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("TransParallel2: " + ex.Message);
                lock (_lock)
                {
                    errors++;
                }
            }
        }
Exemple #13
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif

            var count = db.Table <SomeData>()
                        .Search(f => f.NameSearch, "test")
                        .Count();

            if (count != 0)
            {
                throw new Exception("Assert failure");
            }

            for (int i = 1; i < 1000; i++)
            {
                var d = new SomeData()
                {
                    Id         = i,
                    Normalized = 1.2,
                    PeriodId   = 5,
                    NameSearch = "test"
                };
                db.Table <SomeData>().Save(d);
            }

            using (var tran = new LinqdbTransaction())
            {
                db.Table <SomeData>(tran).Delete(1);
                var d = new SomeData()
                {
                    Id         = 1000,
                    Normalized = 1.2,
                    PeriodId   = 5,
                    NameSearch = "test"
                };
                db.Table <SomeData>(tran).Save(d);
                tran.Commit();
            }

            count = db.Table <SomeData>()
                    .Where(f => f.Id > 999)
                    .Search(f => f.NameSearch, "test")
                    .Count();

            if (count != 1)
            {
                throw new Exception("Assert failure");
            }


            using (var tran = new LinqdbTransaction())
            {
                db.Table <SomeData>(tran).Delete(2);
                var d = new SomeData()
                {
                    Id         = 1001,
                    Normalized = 1.2,
                    PeriodId   = 5,
                    NameSearch = "test"
                };
                db.Table <SomeData>(tran).Save(d);

                tran.Commit();
            }

            count = db.Table <SomeData>()
                    .Where(f => f.Id > 999)
                    .Search(f => f.NameSearch, "test")
                    .Count();

            if (count != 2)
            {
                throw new Exception("Assert failure");
            }

#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            int last_phase = db.Table <SomeData>().LastStep();
            if (last_phase != 1)
            {
                throw new Exception("Assert failure");
            }
#endif



#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #14
0
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            db.Table <SomeData>().Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
#endif
            var d = new SomeData()
            {
                Id         = 1,
                Normalized = 1.1
            };
            db.Table <SomeData>().Save(d);

            var dic = new Dictionary <int, int>()
            {
                { 1, 2 }
            };

            try
            {
                db.Table <SomeData>().Update(f => f.Normalized, dic);
                throw new Exception("Assert failure!");
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("wrong data type for given column"))
                {
                    throw new Exception("Assert failure!");
                }
            }

            try
            {
                using (var tran = new LinqdbTransaction())
                {
                    db.Table <SomeData>(tran).Update(f => f.Normalized, dic);
                    tran.Commit();
                }
                throw new Exception("Assert failure!");
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("wrong data type for given column"))
                {
                    throw new Exception("Assert failure!");
                }
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #15
0
        public static void Do(Db db)
        {
            try
            {
                int next = JobTransManySelects.NextNumber();
                if (next % 10 == 0)
                {
                    var d = new SomeData()
                    {
                        Id         = next,
                        NameSearch = next + " " + next,
                        Normalized = Convert.ToDouble("0," + next),
                        ObjectId   = next,
                        PeriodId   = next,
                        PersonId   = next,
                        Value      = next
                    };

                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <SomeData>(trans).Save(d);
                        trans.Commit();
                    }

                    lock (_lock)
                    {
                        numbers.Add(next);
                    }
                }
                else if (next % 10 > 2)
                {
                    int to_read = 0;
                    lock (_lock)
                    {
                        if (!numbers.Any())
                        {
                            return;
                        }
                        to_read = numbers[rg.Next() % numbers.Count];
                    }
                    var entity = db.Table <SomeData>().Where(f => f.Id == to_read).SelectEntity().FirstOrDefault();
                    if (entity == null)
                    {
                        lock (_lock)
                        {
                            if (numbers.Contains(to_read))
                            {
                                throw new Exception("Assert failure 1");
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    if (entity.Id != to_read || entity.NameSearch != to_read + " " + to_read || entity.Normalized != Convert.ToDouble("0," + to_read) || entity.ObjectId != to_read ||
                        entity.PersonId != to_read || entity.PersonId != to_read || entity.Value != to_read || !(entity.PeriodId == to_read || entity.PeriodId == -1 * to_read))
                    {
                        throw new Exception("Assert failure 2");
                    }

                    var search_res = db.Table <SomeData>().Search(f => f.NameSearch, to_read + "").Select(f => new { Id = f.Id }).Select(f => f.Id).ToList();
                    if (!search_res.Contains(to_read))
                    {
                        lock (_lock)
                        {
                            if (numbers.Contains(to_read))
                            {
                                throw new Exception("Assert failure 3");
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                }
                else if (next % 10 == 2)
                {
                    int to_delete = 0;
                    lock (_lock)
                    {
                        if (!numbers.Any())
                        {
                            return;
                        }

                        to_delete = numbers[rg.Next() % numbers.Count];
                        while (numbers.Contains(to_delete))
                        {
                            numbers.Remove(to_delete);
                        }
                    }
                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <SomeData>(trans).Delete(to_delete);
                        trans.Commit();
                    }
                }
                else
                {
                    int to_update = 0;
                    lock (_lock)
                    {
                        if (!numbers.Any())
                        {
                            return;
                        }

                        to_update = numbers[rg.Next() % numbers.Count];
                    }
                    var val = db.Table <SomeData>().Where(f => f.Id == to_update).Select(f => new { PeriodId = f.PeriodId }).FirstOrDefault();
                    if (val == null)
                    {
                        lock (_lock)
                        {
                            if (numbers.Contains(to_update))
                            {
                                throw new Exception("Assert failure 4");
                            }
                            else
                            {
                                return;
                            }
                        }
                    }
                    using (var trans = new LinqdbTransaction())
                    {
                        db.Table <SomeData>(trans).Update(f => f.PeriodId, to_update, -1 * val.PeriodId);
                        trans.Commit();
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Trans Parallel: " + ex.Message);
                lock (_lock)
                {
                    errors++;
                }
            }
        }
        public void Do(Db db)
        {
            bool dispose = false; if (db == null)

            {
                db = new Db("DATA"); dispose = true;
            }

#if (SERVER)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f)); };
#endif
#if (SOCKETS)
            db._db_internal.CallServer = (byte[] f) => { return(SocketTesting.CallServer(f, db)); };
#endif
#if (SOCKETS || SAMEDB || INDEXES || SERVER)
            using (var trans = new LinqdbTransaction())
            {
                db.Table <BinaryData>(trans).Delete(new HashSet <int>(db.Table <BinaryData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
                db.Table <SomeData>(trans).Delete(new HashSet <int>(db.Table <SomeData>().Select(f => new { f.Id }).Select(f => f.Id).ToList()));
                trans.Commit();
            }
#endif
            var job1 = new TransParallelWorkload();
            var job2 = new TransParallelWorkload2();



            var t1 = Task.Run(() =>
            {
                try
                {
                    job1.Do2(db);
                }
                catch (Exception e)
                {
                    TransParallelWorkload3.errors++;
                }
            });

            var t2 = Task.Run(() =>
            {
                try
                {
                    job2.Do2(db);
                }
                catch (Exception e)
                {
                    TransParallelWorkload3.errors++;
                }
            });


            t1.Wait();
            t2.Wait();


            if (TransParallelWorkload3.errors > 0)
            {
                throw new Exception("Assert failure");
            }

#if (SERVER || SOCKETS)
            if (dispose)
            {
                Logic.Dispose();
            }
#else
            if (dispose)
            {
                db.Dispose();
            }
#endif
#if (!SOCKETS && !SAMEDB && !INDEXES && !SERVER)
            if (dispose)
            {
                ServerSharedData.SharedUtils.DeleteFilesAndFoldersRecursively("DATA");
            }
#endif
        }
Exemple #17
0
        void TestGenerics <T>(Db db, T item) where T : ISomeData, new()
        {
            db.Table <T>().Save(item);
            var some = db.Table <T>().Where(f => f.Id > 0).SelectEntity();

            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            db.Table <T>().Update(f => f.ObjectId, some.Single().Id, 5);
            some = db.Table <T>().Where(f => f.Id > 0).SelectEntity();
            if (some.Single().ObjectId != 5)
            {
                throw new Exception("Assert failure");
            }
            int id = some.Single().Id;

            db.Table <T>().Where(f => f.Id == id).AtomicIncrement(f => f.ObjectId, 1, new T(), null);
            some = db.Table <T>().Where(f => f.Id > 0).SelectEntity();
            if (some.Single().ObjectId != 6)
            {
                throw new Exception("Assert failure");
            }
            db.Table <T>().Delete(some.Single().Id);
            some = db.Table <T>().Where(f => f.Id > 0).SelectEntity();
            if (some.Count() != 0)
            {
                throw new Exception("Assert failure");
            }
            using (var tran = new LinqdbTransaction())
            {
                item.NameSearch = "test";
                item.ObjectId   = 5;
                item.PeriodId   = 5;
                item.GroupBy    = 5;
                db.Table <T>(tran).Save(item);
                tran.Commit();
            }
            some = db.Table <T>().Where(f => f.Id > 0).SelectEntity();
            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            some = db.Table <T>().Search(f => f.NameSearch, "test").SelectEntity();
            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            some = db.Table <T>().Between(f => f.ObjectId, 5, 5, BetweenBoundaries.BothInclusive).SelectEntity();
            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            some = db.Table <T>().Intersect(f => f.ObjectId, new HashSet <int?>()
            {
                5
            }).SelectEntity();
            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            some = db.Table <T>().Intersect(f => f.ObjectId, new HashSet <int?>()
            {
                5
            }).OrderBy(f => f.Id).Skip(0).Take(1).SelectEntity();
            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
            some = db.Table <T>().Intersect(f => f.ObjectId, new HashSet <int?>()
            {
                5
            }).OrderByDescending(f => f.Id).Skip(0).Take(1).SelectEntity();
            if (some.Count() != 1)
            {
                throw new Exception("Assert failure");
            }
#if (!INDEXES)
            db.Table <T>().CreatePropertyMemoryIndex(f => f.PeriodId);
            db.Table <T>().CreateGroupByMemoryIndex(f => f.GroupBy, f => f.PeriodId);
            var res = db.Table <T>().GroupBy(f => f.GroupBy).Select(f => new { Sum = f.Sum(z => z.PeriodId) }).Select(f => f.Sum).Single();
            if (res != 5)
            {
                throw new Exception("Assert failure");
            }
            db.Table <T>().RemoveGroupByMemoryIndex(f => f.GroupBy, f => f.PeriodId);
            db.Table <T>().RemovePropertyMemoryIndex(f => f.PeriodId);
            var indexes = db.GetExistingIndexes();
            if (indexes.Count() != 0)
            {
                throw new Exception("Assert failure");
            }
#endif
        }