Beispiel #1
0
        //public Func<SubjectAlterations, uint, Reply> PostCreateValidator { get; set; }


        public SubjectAlterationsRepoBase(IFileSystemAccesor fileSystemAccesor)
        {
            _fs    = fileSystemAccesor;
            _bMapr = new BsonMapper();

            _bMapr.RegisterAutoId <ulong>(v => v == 0,
                                          (db, col) => (ulong)db.Count(col) + 1);
        }
Beispiel #2
0
        private LiteRepository ConnectToRepo()
        {
            var mapr = new BsonMapper();

            mapr.RegisterAutoId <ulong>(v => v == 0,
                                        (db, col) => (ulong)db.Count(col) + 1);

            return(new LiteRepository(CONN_STR, mapr));
        }
Beispiel #3
0
        private static LiteRepository ConnectToRepo(string filepath)
        {
            var mapr = new BsonMapper();
            var conn = LiteDbConn.Str(filepath, LiteDbMode.Shared);

            mapr.RegisterAutoId <ulong>(v => v == 0,
                                        (db, col) => (ulong)db.Count(col) + 1);

            return(new LiteRepository(conn, mapr));
        }
        private LiteRepository ConnectToRepo()
        {
            var mapr = new BsonMapper();
            var conn = $"Filename={_cfg.DbFilePath}";

            mapr.RegisterAutoId <ulong>(v => v == 0,
                                        (db, col) => (ulong)db.Count(col) + 1);

            return(new LiteRepository(conn, mapr));
        }
Beispiel #5
0
        public R2LiteRepoBase(IFileSystemAccesor fileSystemAccessor)
        {
            _fs    = fileSystemAccessor;
            _bMapr = new BsonMapper();

            if (_fs == null)
            {
                _memStream = new MemoryStream();
            }

            _bMapr.RegisterAutoId <uint>(v => v == 0,
                                         (db, col) => (uint)db.Count(col) + 1);
        }
Beispiel #6
0
        public void AutoId_Test()
        {
            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (c) => "doc-" + c.Count()
                );

            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename, mapper))
                {
                    var cs_int  = db.GetCollection <EntityInt>("int");
                    var cs_guid = db.GetCollection <EntityGuid>("guid");
                    var cs_oid  = db.GetCollection <EntityOid>("oid");
                    var cs_str  = db.GetCollection <EntityString>("str");

                    // int32
                    var cint_1 = new EntityInt {
                    };
                    var cint_2 = new EntityInt {
                    };
                    var cint_5 = new EntityInt {
                        Id = 5
                    };                                 // set Id, do not generate (jump 3 and 4)!
                    var cint_6 = new EntityInt {
                        Id = 0
                    };                                 // for int, 0 is empty

                    // guid
                    var guid = Guid.NewGuid();

                    var cguid_1 = new EntityGuid {
                        Id = guid
                    };
                    var cguid_2 = new EntityGuid {
                    };

                    // oid
                    var oid = ObjectId.NewObjectId();

                    var coid_1 = new EntityOid {
                    };
                    var coid_2 = new EntityOid {
                        Id = oid
                    };

                    // string - there is no AutoId for string
                    var cstr_1 = new EntityString {
                    };
                    var cstr_2 = new EntityString {
                        Id = "mydoc2"
                    };

                    cs_int.Insert(cint_1);
                    cs_int.Insert(cint_2);
                    cs_int.Insert(cint_5);
                    cs_int.Insert(cint_6);

                    cs_guid.Insert(cguid_1);
                    cs_guid.Insert(cguid_2);

                    cs_oid.Insert(coid_1);
                    cs_oid.Insert(coid_2);

                    cs_str.Insert(cstr_1);

                    // test for int
                    Assert.AreEqual(1, cint_1.Id);
                    Assert.AreEqual(2, cint_2.Id);
                    Assert.AreEqual(5, cint_5.Id);
                    Assert.AreEqual(6, cint_6.Id);

                    // test for guid
                    Assert.AreEqual(guid, cguid_1.Id);
                    Assert.AreNotEqual(Guid.Empty, cguid_2.Id);
                    Assert.AreNotEqual(cguid_2.Id, cguid_1.Id);

                    // test for oid
                    Assert.AreNotEqual(ObjectId.Empty, coid_1.Id);
                    Assert.AreEqual(oid, coid_2.Id);

                    // test for string
                    Assert.AreEqual("doc-0", cstr_1.Id);
                    Assert.AreEqual("mydoc2", cstr_2.Id);
                }
        }
Beispiel #7
0
        public void AutoId_Test()
        {
            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (db, col) => "doc-" + db.Count(col, null)
                );

            // register for "long" - use an collection to manage
            mapper.RegisterAutoId <long>(
                v => v == 0,
                (db, col) =>
            {
                var seq = "_sequence";
                db.BeginTrans();
                var next = 0L;
                try
                {
                    var current = db.Find(seq, Query.EQ("_id", col)).FirstOrDefault();

                    if (current == null)
                    {
                        db.Insert(seq, new BsonDocument {
                            { "_id", col }, { "value", 1L }
                        });
                        next = 1;
                    }
                    else
                    {
                        next = current["value"] = current["value"].AsInt64 + 1;
                        db.Update(seq, current);
                    }

                    db.Commit();
                }
                catch (Exception)
                {
                    db.Rollback();
                    throw;
                }

                return(next);
            }
                );

            using (var file = new TempFile())
                using (var db = new LiteDatabase(file.Filename, mapper))
                {
                    var cs_int  = db.GetCollection <EntityInt>("int");
                    var cs_long = db.GetCollection <EntityLong>("long");
                    var cs_guid = db.GetCollection <EntityGuid>("guid");
                    var cs_oid  = db.GetCollection <EntityOid>("oid");
                    var cs_str  = db.GetCollection <EntityString>("str");

                    // int32
                    var cint_1 = new EntityInt {
                    };
                    var cint_2 = new EntityInt {
                    };
                    var cint_5 = new EntityInt {
                        Id = 5
                    };                                 // set Id, do not generate (jump 3 and 4)!
                    var cint_6 = new EntityInt {
                        Id = 0
                    };                                 // for int, 0 is empty

                    // long
                    var clong_1 = new EntityLong {
                    };
                    var clong_2 = new EntityLong {
                    };
                    var clong_3 = new EntityLong {
                    };
                    var clong_4 = new EntityLong {
                    };

                    // guid
                    var guid = Guid.NewGuid();

                    var cguid_1 = new EntityGuid {
                        Id = guid
                    };
                    var cguid_2 = new EntityGuid {
                    };

                    // oid
                    var oid = ObjectId.NewObjectId();

                    var coid_1 = new EntityOid {
                    };
                    var coid_2 = new EntityOid {
                        Id = oid
                    };

                    // string - there is no AutoId for string
                    var cstr_1 = new EntityString {
                    };
                    var cstr_2 = new EntityString {
                        Id = "mydoc2"
                    };

                    cs_int.Insert(cint_1);
                    cs_int.Insert(cint_2);
                    cs_int.Insert(cint_5);
                    cs_int.Insert(cint_6);

                    cs_long.Insert(clong_1);
                    cs_long.Insert(clong_2);
                    // delete 1 and 2 and will not re-used
                    cs_long.Delete(Query.In("_id", 1, 2));
                    cs_long.Insert(clong_3);
                    cs_long.Insert(clong_4);

                    cs_guid.Insert(cguid_1);
                    cs_guid.Insert(cguid_2);

                    cs_oid.Insert(coid_1);
                    cs_oid.Insert(coid_2);

                    cs_str.Insert(cstr_1);

                    // test for int
                    Assert.AreEqual(1, cint_1.Id);
                    Assert.AreEqual(2, cint_2.Id);
                    Assert.AreEqual(5, cint_5.Id);
                    Assert.AreEqual(6, cint_6.Id);

                    // test for long
                    Assert.AreEqual(3, clong_3.Id);
                    Assert.AreEqual(4, clong_4.Id);

                    // test for guid
                    Assert.AreEqual(guid, cguid_1.Id);
                    Assert.AreNotEqual(Guid.Empty, cguid_2.Id);
                    Assert.AreNotEqual(cguid_2.Id, cguid_1.Id);

                    // test for oid
                    Assert.AreNotEqual(ObjectId.Empty, coid_1.Id);
                    Assert.AreEqual(oid, coid_2.Id);

                    // test for string
                    Assert.AreEqual("doc-0", cstr_1.Id);
                    Assert.AreEqual("mydoc2", cstr_2.Id);
                }
        }
Beispiel #8
0
        public void AutoId_Test()
        {
            string test_name = "AutoId_Test";

            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (c) => "doc-" + c.Count()
                );

            using (var db = new LiteDatabase(new MemoryStream(), mapper))
            {
                var cs_int  = db.GetCollection <EntityInt>("int");
                var cs_guid = db.GetCollection <EntityGuid>("guid");
                var cs_oid  = db.GetCollection <EntityOid>("oid");
                var cs_str  = db.GetCollection <EntityString>("str");

                // int32
                var cint_1 = new EntityInt {
                };
                var cint_2 = new EntityInt {
                };
                var cint_5 = new EntityInt {
                    Id = 5
                };                                     // set Id, do not generate (jump 3 and 4)!
                var cint_6 = new EntityInt {
                    Id = 0
                };                                     // for int, 0 is empty

                // guid
                var guid = Guid.NewGuid();

                var cguid_1 = new EntityGuid {
                    Id = guid
                };
                var cguid_2 = new EntityGuid {
                };

                // oid
                var oid = ObjectId.NewObjectId();

                var coid_1 = new EntityOid {
                };
                var coid_2 = new EntityOid {
                    Id = oid
                };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString {
                };
                var cstr_2 = new EntityString {
                    Id = "mydoc2"
                };

                cs_int.Insert(cint_1);
                cs_int.Insert(cint_2);
                cs_int.Insert(cint_5);
                cs_int.Insert(cint_6);

                cs_guid.Insert(cguid_1);
                cs_guid.Insert(cguid_2);

                cs_oid.Insert(coid_1);
                cs_oid.Insert(coid_2);

                cs_str.Insert(cstr_1);

                // test for int
                Helper.AssertIsTrue(test_name, 0, 1 == cint_1.Id);
                Helper.AssertIsTrue(test_name, 1, 2 == cint_2.Id);
                Helper.AssertIsTrue(test_name, 2, 5 == cint_5.Id);
                Helper.AssertIsTrue(test_name, 3, 6 == cint_6.Id);

                // test for guid
                Helper.AssertIsTrue(test_name, 4, guid == cguid_1.Id);
                Helper.AssertIsTrue(test_name, 5, Guid.Empty != cguid_2.Id);
                Helper.AssertIsTrue(test_name, 6, cguid_2.Id != cguid_1.Id);

                // test for oid
                Helper.AssertIsTrue(test_name, 7, ObjectId.Empty != coid_1.Id);
                Helper.AssertIsTrue(test_name, 8, oid == coid_2.Id);

                // test for string
                Helper.AssertIsTrue(test_name, 9, "doc-0" == cstr_1.Id);
                Helper.AssertIsTrue(test_name, 10, "mydoc2" == cstr_2.Id);
            }
        }
Beispiel #9
0
        public void AutoId_Test()
        {
            var mapper = new BsonMapper();

            mapper.RegisterAutoId(
                (s) => s == null,
                (c) => "doc-" + c.Count()
            );

            using (var file = new TempFile())
            using (var db = new LiteDatabase(file.Filename, mapper))
            {
                var cs_int = db.GetCollection<EntityInt>("int");
                var cs_guid = db.GetCollection<EntityGuid>("guid");
                var cs_oid = db.GetCollection<EntityOid>("oid");
                var cs_str = db.GetCollection<EntityString>("str");

                // int32
                var cint_1 = new EntityInt { };
                var cint_2 = new EntityInt { };
                var cint_5 = new EntityInt { Id = 5 }; // set Id, do not generate (jump 3 and 4)!
                var cint_6 = new EntityInt { Id = 0 }; // for int, 0 is empty

                // guid
                var guid = Guid.NewGuid();

                var cguid_1 = new EntityGuid { Id = guid };
                var cguid_2 = new EntityGuid { };

                // oid
                var oid = ObjectId.NewObjectId();

                var coid_1 = new EntityOid { };
                var coid_2 = new EntityOid { Id = oid };

                // string - there is no AutoId for string
                var cstr_1 = new EntityString { };
                var cstr_2 = new EntityString { Id = "mydoc2" };

                cs_int.Insert(cint_1);
                cs_int.Insert(cint_2);
                cs_int.Insert(cint_5);
                cs_int.Insert(cint_6);

                cs_guid.Insert(cguid_1);
                cs_guid.Insert(cguid_2);

                cs_oid.Insert(coid_1);
                cs_oid.Insert(coid_2);

                cs_str.Insert(cstr_1);

                // test for int
                Assert.AreEqual(1, cint_1.Id);
                Assert.AreEqual(2, cint_2.Id);
                Assert.AreEqual(5, cint_5.Id);
                Assert.AreEqual(6, cint_6.Id);

                // test for guid
                Assert.AreEqual(guid, cguid_1.Id);
                Assert.AreNotEqual(Guid.Empty, cguid_2.Id);
                Assert.AreNotEqual(cguid_2.Id, cguid_1.Id);

                // test for oid
                Assert.AreNotEqual(ObjectId.Empty, coid_1.Id);
                Assert.AreEqual(oid, coid_2.Id);

                // test for string
                Assert.AreEqual("doc-0", cstr_1.Id);
                Assert.AreEqual("mydoc2", cstr_2.Id);
            }
        }