Beispiel #1
1
        public BsonValue Execute(LiteDatabase db, StringScanner s)
        {
            var col = this.ReadCollection(db, s);
            var query = s.Match("{") ? Query.Query.All() : this.ReadQuery(s);
            var code = DynamicCode.GetCode(s);

            var docs = col.Find(query).ToArray();

            try
            {
                db.BeginTrans();

                foreach (var doc in docs)
                {
                    code(doc["_id"], doc, col, db);
                }

                db.Commit();

                return docs.Length;
            }
            catch (Exception ex)
            {
                db.Rollback();
                throw ex;
            }
        }
Beispiel #2
0
        public void TransactionNestedException_Test()
        {
            using (var f = new TempFile())
            using (var db = new LiteDatabase(f.Filename))
            {
                var col = db.GetCollection<Person>("Person");

                try
                {
                    using (var transaction1 = db.BeginTrans())
                    {
                        col.Insert(new Person { Id = 1, Fullname = "John" });

                        using (var transaction2 = db.BeginTrans())
                        {
                            col.Insert(new Person { Id = 2, Fullname = "Joana" });
                        }

                        col.Insert(new Person { Id = 1, Fullname = "Foo Bar" }); // throws duplicate key exception
                    }

                }
                catch (LiteException) { }

                Assert.AreEqual(0, col.Count());
            }
        }
Beispiel #3
0
        public void TransactionCommit_Test()
        {
            using (var f = new TempFile())
            using (var db = new LiteDatabase(f.Filename))
            {
                var col = db.GetCollection<Person>("Person");

                col.Insert(new Person { Fullname = "John" });
                col.Insert(new Person { Fullname = "Doe" });
                using (var transaction = db.BeginTrans())
                {
                    col.Insert(new Person { Fullname = "Joana" });
                    col.Insert(new Person { Fullname = "Marcus" });
                }

                Assert.AreEqual(4, col.Count());
            }
        }
Beispiel #4
0
        public void TransactionException_Test()
        {
            using (var f = new TempFile())
            using (var db = new LiteDatabase(f.Filename))
            {
                var col = db.GetCollection<Person>("Person");

                col.Insert(new Person { Fullname = "John" });
                col.Insert(new Person { Fullname = "Doe" });

                try
                {
                    using (var transaction = db.BeginTrans())
                    {
                        col.Insert(new Person { Fullname = "Joana" });
                        col.Insert(new Person { Fullname = "Marcus" });
                        throw new IOException();
                    }
                }
                catch (IOException) { }

                Assert.AreEqual(2, col.Count());
            }
        }
Beispiel #5
0
        /// <summary>
        /// Добавить информацию о матче в БД. В случае неверных данных кидает exception
        /// </summary>
        /// <param name="matchInfo">Информация о матче в JSON</param>
        public void PutMatch(string endPoint, string timeStamp, string matchInfo)
        {
            var endTime = DateTime.Parse(timeStamp);

            if (endTime > LastMatchTime)
            {
                LastMatchTime = endTime;
            }

            Match match = DeserializeMatchInfo(matchInfo);

            match.EndPoint  = endPoint;
            match.TimeStamp = endTime;

            using (var db = new LiteDatabase(statsDBConn)) {
                var playersCol = db.GetCollection <Player> ("players");
                var serversCol = db.GetCollection <Server> ("servers");
                var matchesCol = db.GetCollection <Match> ("matches");

                Server server = serversCol.FindOne(x => x.EndPoint == endPoint);

                if (server == null)
                {
                    throw new RequestException("Server not found");
                }

                //TODO speed up
                using (var trans = db.BeginTrans()) {
                    matchesCol.Insert(match);
                    playersCol.Upsert(UpdatePlayers(playersCol, endPoint, match, endTime));
                    UpdateServer(server, match, endTime);
                    serversCol.Update(server);
                    trans.Commit();
                }
            }
        }
Beispiel #6
0
        public BsonValue Execute(LiteDatabase db, StringScanner s)
        {
            db.BeginTrans();

            return BsonValue.Null;
        }