Exemple #1
0
        public bool SaveUserProfile(UserProfile userProfile)
        {
            try
            {
                if (userProfile != null)
                {
                    var mutableDocument = new MutableDocument(userProfile.Id);
                    mutableDocument.SetString("Name", userProfile.Name);
                    mutableDocument.SetString("Email", userProfile.Email);
                    mutableDocument.SetString("Address", userProfile.Address);

                    if (userProfile.ImageData != null)
                    {
                        mutableDocument.SetBlob("ImageData", new Blob("image/jpeg", userProfile.ImageData));
                    }

                    Database.Save(mutableDocument);

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"UserProfileRepository Exception: {ex.Message}");
            }

            return(false);
        }
        public void TestDocumentIDs()
        {
            using (var doc1 = new MutableDocument("doc1")) {
                doc1.SetString("species", "Tiger");
                doc1.SetString("name", "Hobbes");
                Db.Save(doc1).Dispose();
            }

            using (var doc2 = new MutableDocument("doc2")) {
                doc2.SetString("species", "Tiger");
                doc2.SetString("pattern", "striped");
                Db.Save(doc2).Dispose();
            }

            var config = CreateConfig(true, false, false);

            config.DocumentIDs = new[] { "doc1" };
            RunReplication(config, 0, 0);

            _otherDB.Count.Should().Be(1UL);
            using (var doc1 = _otherDB.GetDocument("doc1")) {
                doc1.Should().NotBeNull();
                doc1.GetString("species").Should().Be("Tiger");
                doc1.GetString("name").Should().Be("Hobbes");
            }
        }
        public async Task TestAddSameChangeListeners()
        {
            var doc1 = new MutableDocument("doc1");

            doc1.SetString("name", "Scott");
            var saved = Db.Save(doc1);

            doc1.Dispose();
            doc1 = saved.ToMutable();

            Db.AddDocumentChangeListener("doc1", DocumentChanged);
            Db.AddDocumentChangeListener("doc1", DocumentChanged);
            Db.AddDocumentChangeListener("doc1", DocumentChanged);
            Db.AddDocumentChangeListener("doc1", DocumentChanged);
            Db.AddDocumentChangeListener("doc1", DocumentChanged);

            _wa = new WaitAssert();
            _expectedDocumentChanges = new HashSet <string> {
                "doc1"
            };
            doc1.SetString("name", "Scott Tiger");
            Db.Save(doc1);

            await Task.Delay(500);

            _wa.CaughtExceptions.Should().BeEmpty("because otherwise too many callbacks happened");
        }
        public async Task TestRemoveDocumentChangeListener()
        {
            var doc1 = new MutableDocument("doc1");

            doc1.SetString("name", "Scott");
            Db.Save(doc1);

            var token = Db.AddDocumentChangeListener("doc1", DocumentChanged);

            _wa = new WaitAssert();
            _expectedDocumentChanges = new HashSet <string> {
                "doc1"
            };

            doc1.SetString("name", "Scott Tiger");
            Db.Save(doc1);
            _wa.WaitForResult(TimeSpan.FromSeconds(5));

            Db.RemoveChangeListener(token);

            _wa = new WaitAssert();
            _docCallbackShouldThrow = true;
            doc1.SetString("name", "Scott Pilgrim");
            Db.Save(doc1);

            await Task.Delay(500);

            _wa.CaughtExceptions.Should().BeEmpty("because otherwise too many callbacks happened");

            // Remove again
            Db.RemoveChangeListener(token);
        }
        public void TestDocumentChange()
        {
            var doc1 = new MutableDocument("doc1");

            doc1.SetString("name", "Scott");
            doc1 = Db.Save(doc1).ToMutable();

            var doc2 = new MutableDocument("doc2");

            doc2.SetString("name", "Daniel");
            doc2 = Db.Save(doc2).ToMutable();

            Db.AddDocumentChangeListener("doc1", DocumentChanged);
            Db.AddDocumentChangeListener("doc2", DocumentChanged);
            Db.AddDocumentChangeListener("doc3", DocumentChanged);

            _expectedDocumentChanges = new HashSet <string> {
                "doc1",
                "doc2",
                "doc3"
            };
            _wa = new WaitAssert();

            doc1.SetString("name", "Scott Tiger");
            Db.Save(doc1);

            Db.Delete(doc2);

            var doc3 = new MutableDocument("doc3");

            doc3.SetString("name", "Jack");
            Db.Save(doc3);

            _wa.WaitForResult(TimeSpan.FromSeconds(5));
        }
        private MutableDocument CreateDocumentWithTag(string id, string tag)
        {
            var doc = new MutableDocument(id);

            doc.SetString("tag", tag);

            doc.SetString("firstName", "Daniel");
            doc.SetString("lastName", "Tiger");

            var address = new MutableDictionary();

            address.SetString("street", "1 Main street");
            address.SetString("city", "Mountain View");
            address.SetString("state", "CA");
            doc.SetDictionary("address", address);

            var phones = new MutableArray();

            phones.AddString("650-123-0001").AddString("650-123-0002");
            doc.SetArray("phones", phones);

            doc.SetDate("updated", DateTimeOffset.UtcNow);

            return(doc);
        }
Exemple #7
0
        //Cust

        public void AddCustomerMaster(Database db)
        {
            String    straddstatus = String.Empty;
            Hashtable hstcustadd   = new Hashtable();

            Console.WriteLine();
            string[] arr1 = new string[] { "CUST_NBR", "CUST_NM", "STORE_NBR" };

            foreach (String strcol in arr1)
            {
                String mutestr   = String.Empty;
                bool   userentry = true;

                Console.Write("Please enter " + strcol + " : ");
                mutestr = Console.ReadLine();
                Console.WriteLine();

                while (userentry)
                {
                    if (!String.IsNullOrEmpty(mutestr))
                    {
                        hstcustadd.Add(strcol, mutestr);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("Please enter " + strcol);
                        mutestr   = Console.ReadLine();
                        userentry = true;
                    }
                }
            }

            using (var CustomerDoc = new MutableDocument(hstcustadd["CUST_NBR"].ToString()))
            {
                CustomerDoc.SetString("CUST_NBR", hstcustadd["CUST_NBR"].ToString());
                CustomerDoc.SetString("CUST_EFF_DT", DateTime.Now.ToString("MM/dd/yyyy"));
                CustomerDoc.SetString("CUST_END_DT", "12/25/9999");
                CustomerDoc.SetString("CUST_NM", hstcustadd["CUST_NM"].ToString());
                CustomerDoc.SetString("STORE_NBR", hstcustadd["STORE_NBR"].ToString());

                db.Save(CustomerDoc);
                Console.WriteLine();
                Console.WriteLine("Customer " + hstcustadd["CUST_NBR"].ToString() + " Added Succefully");

                synccust(db);
            }

            Console.WriteLine();
            runselect(db);
            Console.WriteLine();
            Promptuser(db, "Customer");
        }
        public virtual string Create(T obj)
        {
            var mutableDoc = new MutableDocument();

            mutableDoc.SetString("uuid", UUID);
            mutableDoc.SetString("type", typeof(T).Name);
            Database.Save(mutableDoc);

            //Update Id
            var objwithId = Helper.Function.UpdateGenericObjectProperty(obj, mutableDoc.Id);

            mutableDoc.SetValue("value", JsonConvert.SerializeObject(objwithId));
            Database.Save(mutableDoc);
            return(mutableDoc.Id);
        }
Exemple #9
0
        public void TestConflictHandlerWithDeletedOldDoc()
        {
            using (var doc1 = new MutableDocument("doc1")){
                doc1.SetString("name", "Jim");
                Db.Save(doc1);

                var doc1a = Db.GetDocument("doc1").ToMutable();
                doc1a.SetString("name", "Kim");

                Document currDoc        = null;
                var      updatedDocName = "";

                //delete old doc
                Db.Delete(doc1);
                Db.Save(doc1a, (updated, current) =>
                {
                    currDoc        = current;
                    updatedDocName = updated.GetString("name");
                    return(true);
                });

                currDoc.Should().BeNull();
                updatedDocName.Should().Be("Kim");
                Db.GetDocument("doc1").GetString("name").Should().Be("Kim");
            }
        }
        public void TestMultipleListenersOnSameDatabase()
        {
            _listener = CreateListener();
            var _listener2 = CreateNewListener();

            using (var doc1 = new MutableDocument("doc1"))
                using (var doc2 = new MutableDocument("doc2")) {
                    doc1.SetString("name", "Sam");
                    Db.Save(doc1);
                    doc2.SetString("name", "Mary");
                    OtherDb.Save(doc2);
                }

            RunReplication(
                _listener.LocalEndpoint(),
                ReplicatorType.PushAndPull,
                false,
                null,
                false, //accept only self signed server cert
                _listener.TlsIdentity.Certs[0],
                0,
                0
                );

            _listener.Stop();
            _listener2.Stop();

            OtherDb.Count.Should().Be(2);
        }
        public void TestDatabaseChange()
        {
            var wa = new WaitAssert();

            Db.AddChangeListener(null, (sender, args) =>
            {
                var docIDs = args.DocumentIDs;
                wa.RunAssert(() =>
                {
                    args.Database.Should().Be(Db);
                    docIDs.Should().HaveCount(10, "because that is the number of expected rows");
                });
            });

            Db.InBatch(() =>
            {
                for (uint i = 0; i < 10; i++)
                {
                    var doc = new MutableDocument($"doc-{i}");
                    doc.SetString("type", "demo");
                    Db.Save(doc);
                }
            });

            wa.WaitForResult(TimeSpan.FromSeconds(5));
        }
Exemple #12
0
        public void TestConflictHandlerSaveMergedDocument()
        {
            using (var doc1 = new MutableDocument("doc1")){
                doc1.SetString("name", "Jim");

                Db.Save(doc1);

                // Get two doc1 document objects (doc1a and doc1b):
                var doc1a = Db.GetDocument(doc1.Id).ToMutable();
                var doc1b = Db.GetDocument(doc1.Id).ToMutable();

                // Modify doc1a:
                doc1a.SetString("name", "Jim");
                doc1a.SetString("language", "English");

                Db.Save(doc1a);

                // Modify doc1b:
                doc1b.SetString("name", "Jim");
                doc1b.SetString("language", "C#");
                doc1b.SetString("location", "Japan");
                Db.Save(doc1b, ResolveConflict);
            }

            using (var doc1 = Db.GetDocument("doc1")) {
                doc1.GetString("name").Should().Be("Jim");
                var lanStr = doc1.GetString("language");
                lanStr.Should().Contain("English");
                lanStr.Should().Contain("C#");
                doc1.GetString("location").Should().Be("Japan");
            }
        }
Exemple #13
0
        public void TestConflictHandlerWhenDocumentIsPurged()
        {
            using (var doc = new MutableDocument("doc1")) {
                doc.SetString("firstName", "Tiger");
                Db.Save(doc);
            }

            using (var doc = Db.GetDocument("doc1")) {
                doc.Generation.Should().Be(1);
            }

            using (var doc1 = Db.GetDocument("doc1"))
                using (var doc1b = doc1.ToMutable()) {
                    Db.Purge("doc1");
                    doc1b.SetString("nickName", "Scott");
                    Db.Invoking(d => Db.Save(doc1b, (updated, current) =>
                    {
                        return(true);
                    })).Should().Throw <CouchbaseLiteException>()
                    .Where(
                        e => e.Error == CouchbaseLiteError.NotFound &&
                        e.Domain == CouchbaseLiteErrorType.CouchbaseLite,
                        "because the document is purged");
                }
        }
Exemple #14
0
 public void TestConflictHandlerWithMultipleIncomingConflicts()
 {
     using (var doc1 = new MutableDocument("doc1"))
         using (var doc1a = new MutableDocument("doc1"))
             using (var doc1b = new MutableDocument("doc1")) {
                 var waitObj = new AutoResetEvent(true);
                 doc1.SetString("name", "Jim");
                 Db.Save(doc1);
                 Task task2 = Task.Factory.StartNew(() => {
                     doc1a.SetString("name", "Kim");
                     Db.Save(doc1a, (updated, current) => {
                         waitObj.Set();
                         Thread.Sleep(250);
                         waitObj.WaitOne(TimeSpan.FromMilliseconds(250));
                         return(false);
                     });
                     waitObj.Set();
                     Thread.Sleep(250);
                 });
                 waitObj.WaitOne(TimeSpan.FromMilliseconds(250));
                 doc1b.SetString("name", "Tim");
                 Db.Save(doc1b);
                 Db.GetDocument("doc1").GetString("name").Should().Be("Tim");
                 waitObj.Set();
                 Thread.Sleep(250);
                 waitObj.WaitOne(TimeSpan.FromMilliseconds(250));
                 Db.GetDocument("doc1").GetString("name").Should().Be("Tim");
             }
 }
        public void TestDocIDFilter()
        {
            var doc1 = new MutableDocument("doc1");

            doc1.SetString("species", "Tiger");
            var saved = Db.Save(doc1);

            Misc.SafeSwap(ref doc1, saved.ToMutable());
            doc1.SetString("name", "Hobbes");
            Db.Save(doc1);

            var doc2 = new MutableDocument("doc2");

            doc2.SetString("species", "Tiger");
            saved = Db.Save(doc2);
            Misc.SafeSwap(ref doc2, saved.ToMutable());
            doc2.SetString("pattern", "striped");
            Db.Save(doc2);

            var doc3 = new MutableDocument("doc3");

            doc3.SetString("species", "Tiger");
            saved = _otherDB.Save(doc3);
            Misc.SafeSwap(ref doc3, saved.ToMutable());
            doc3.SetString("name", "Hobbes");
            _otherDB.Save(doc3);

            var doc4 = new MutableDocument("doc4");

            doc4.SetString("species", "Tiger");
            saved = _otherDB.Save(doc4);
            Misc.SafeSwap(ref doc4, saved.ToMutable());
            doc4.SetString("pattern", "striped");
            _otherDB.Save(doc4);

            var config = CreateConfig(true, true, false);

            config.DocumentIDs = new[] { "doc1", "doc3" };
            RunReplication(config, 0, 0);
            Db.Count.Should().Be(3, "because only one document should have been pulled");
            Db.GetDocument("doc3").Should().NotBeNull();
            _otherDB.Count.Should().Be(3, "because only one document should have been pushed");
            _otherDB.GetDocument("doc1").Should().NotBeNull();
        }
        private MutableDocument SetupConflict()
        {
            var doc = new MutableDocument("doc1");

            doc.SetString("type", "profile");
            doc.SetString("name", "Scott");
            var savedDoc = Db.Save(doc);

            // Force a conflict
            var properties = doc.ToDictionary();

            properties["name"] = "Scotty";
            SaveProperties(properties, doc.Id);

            doc.Dispose();
            doc = savedDoc.ToMutable();
            doc.SetString("name", "Scott Pilgrim");
            return(doc);
        }
        public void TestConflict()
        {
            ConflictResolver = new TheirsWins();
            ReopenDB();
            var doc1      = SetupConflict();
            var savedDoc1 = Db.Save(doc1);

            savedDoc1["name"].ToString().Should().Be("Scotty", "because the 'theirs' version should win");
            doc1.Dispose();
            savedDoc1.Dispose();

            ConflictResolver = new MergeThenTheirsWins();
            ReopenDB();

            var doc2 = new MutableDocument("doc2");

            doc2.SetString("type", "profile");
            doc2.SetString("name", "Scott");
            var savedDoc2 = Db.Save(doc2);

            // Force a conflict again
            var properties = doc2.ToDictionary();

            properties["type"]   = "bio";
            properties["gender"] = "male";
            SaveProperties(properties, doc2.Id);
            doc2.Dispose();

            // Save and make sure that the correct conflict resolver won
            doc2 = savedDoc2.ToMutable();
            doc2.SetString("type", "bio");
            doc2.SetInt("age", 31);

            savedDoc2.Dispose();
            savedDoc2 = Db.Save(doc2);
            doc2.Dispose();

            savedDoc2["age"].Long.Should().Be(31L, "because 'age' was changed by 'mine' and not 'theirs'");
            savedDoc2["type"].String.Should().Be("bio", "because 'type' was changed by 'mine' and 'theirs' so 'theirs' should win");
            savedDoc2["gender"].String.Should().Be("male", "because 'gender' was changed by 'theirs' but not 'mine'");
            savedDoc2["name"].String.Should().Be("Scott", "because 'name' was unchanged");
            savedDoc2.Dispose();
        }
        public virtual string Create(T obj, params KeyValuePair <string, string>[] pairs)
        {
            var mutableDoc = new MutableDocument();

            mutableDoc.SetString("uuid", UUID);
            mutableDoc.SetString("type", typeof(T).Name);
            foreach (var pair in pairs)
            {
                mutableDoc.SetString(pair.Key, pair.Value);
            }
            Database.Save(mutableDoc);

            //Update Id
            var objwithId = Helper.Function.UpdateGenericObjectProperty(obj, mutableDoc.Id);

            mutableDoc.SetValue("value", JsonConvert.SerializeObject(objwithId));
            Database.Save(mutableDoc);
            return(mutableDoc.Id);
        }
        public void AddItem(string name, String value)
        {
            using (var mutableDoc = new MutableDocument())
            {
                System.Diagnostics.Debug.WriteLine("ID from new doc " + mutableDoc.Id);
                mutableDoc.SetString("name", name).SetString("value", value).SetString("ID", mutableDoc.Id);

                // Save it to the database
                this.database.Save(mutableDoc);
            }
        }
Exemple #20
0
        private static void DoBatchOperation()
        {
            var db = _Database;

            // # tag::batch[]
            db.InBatch(() =>
            {
                for (var i = 0; i < 10; i++)
                {
                    using (var doc = new MutableDocument()) {
                        doc.SetString("type", "user");
                        doc.SetString("name", $"user {i}");
                        doc.SetBoolean("admin", false);
                        db.Save(doc);
                        Console.WriteLine($"Saved user document {doc.GetString("name")}");
                    }
                }
            });
            // # end::batch[]
        }
Exemple #21
0
        public void Add(Book book)
        {
            try
            {
                if (book != null)
                {
                    string bookId = Guid.NewGuid().ToString();
                    book.CreatedAt = DateTime.Now;

                    MutableDocument mutableDocument = new MutableDocument(bookId);

                    mutableDocument.SetString("Id", book.Id);
                    mutableDocument.SetString("Title", book.Title);
                    mutableDocument.SetString("ShortDescription", book.ShortDescription);
                    mutableDocument.SetString("LongDescription", book.LongDescription);
                    mutableDocument.SetString("CreatedAt", book.CreatedAt.ToString("g"));
                    mutableDocument.SetString("type", "book");

                    _database.Save(mutableDocument);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }
        }
        private void WithActiveReplicatorAndURLEndpointListeners(bool isCloseNotDelete)
        {
            WaitAssert waitIdleAssert1    = new WaitAssert();
            WaitAssert waitStoppedAssert1 = new WaitAssert();

            _listener = CreateListener();
            var _listener2 = CreateNewListener();

            _listener.Config.Database.ActiveStoppables.Count.Should().Be(2);
            _listener2.Config.Database.ActiveStoppables.Count.Should().Be(2);

            using (var doc1 = new MutableDocument("doc1"))
                using (var doc2 = new MutableDocument("doc2")) {
                    doc1.SetString("name", "Sam");
                    Db.Save(doc1);
                    doc2.SetString("name", "Mary");
                    OtherDb.Save(doc2);
                }

            var target  = new DatabaseEndpoint(Db);
            var config1 = CreateConfig(target, ReplicatorType.PushAndPull, true, sourceDb: OtherDb);
            var repl1   = new Replicator(config1);

            repl1.AddChangeListener((sender, args) => {
                waitIdleAssert1.RunConditionalAssert(() => {
                    return(args.Status.Activity == ReplicatorActivityLevel.Idle);
                });

                waitStoppedAssert1.RunConditionalAssert(() => {
                    return(args.Status.Activity == ReplicatorActivityLevel.Stopped);
                });
            });

            repl1.Start();

            waitIdleAssert1.WaitForResult(TimeSpan.FromSeconds(10));
            OtherDb.ActiveStoppables.Count.Should().Be(3);

            if (isCloseNotDelete)
            {
                OtherDb.Close();
            }
            else
            {
                OtherDb.Delete();
            }

            OtherDb.ActiveStoppables.Count.Should().Be(0);
            OtherDb.IsClosedLocked.Should().Be(true);

            waitStoppedAssert1.WaitForResult(TimeSpan.FromSeconds(30));
        }
        public void TestFallbackType()
        {
            SimpleObject obj = new SimpleObject()
            {
                StringValue = "Hey"
            };

            MutableDocument doc = obj.ToMutableDocument();

            doc.SetString("$type", "not a type");

            object newObject = doc.ToObject(typeof(SimpleObject));
        }
        public void TestPullConflictNoBaseRevision()
        {
            // Create the conflicting docs separately in each database.  They have the same base revID
            // because the contents are identical, but because the DB never pushed revision 1, it doesn't
            // think it needs to preserve the body; so when it pulls a conflict, there won't be a base
            // revision for the resolver.

            var doc1 = new MutableDocument("doc");

            doc1.SetString("species", "tiger");
            var saved = Db.Save(doc1);

            Misc.SafeSwap(ref doc1, saved.ToMutable());
            doc1.SetString("name", "Hobbes");
            Db.Save(doc1);

            var doc2 = new MutableDocument("doc");

            doc2.SetString("species", "Tiger");
            saved = _otherDB.Save(doc2);
            Misc.SafeSwap(ref doc2, saved.ToMutable());
            doc2.SetString("pattern", "striped");
            _otherDB.Save(doc2);

            var config = CreateConfig(false, true, false);

            config.ConflictResolver = new MergeThenTheirsWins();
            RunReplication(config, 0, 0);

            Db.Count.Should().Be(1, "because the document in otherDB has the same ID");
            var gotDoc1 = Db.GetDocument("doc");

            gotDoc1.ToDictionary().ShouldBeEquivalentTo(new Dictionary <string, object> {
                ["species"] = "Tiger",
                ["name"]    = "Hobbes",
                ["pattern"] = "striped"
            });
        }
        public void TestModelReturningNull()
        {
            CreateDocument(1, 2, 3, 4, 5);

            using (var doc = new MutableDocument()) {
                doc.SetString("text", "Knox on fox in socks in box.  Socks on Knox and Knox in box.");
                Db.Save(doc);
            }

            var aggregateModel = new AggregateModel();

            aggregateModel.RegisterModel();

            var model      = nameof(AggregateModel);
            var input      = AggregateModel.CreateInput("numbers");
            var prediction = Function.Prediction(model, input);

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction),
                                               SelectResult.Expression(prediction.Property("sum")))
                           .From(DataSource.Database(Db))) {
                var rows = VerifyQuery(q, (n, result) =>
                {
                    if (n == 1)
                    {
                        result.GetDictionary(0).Should().NotBeNull();
                        result.GetInt(1).Should().Be(15);
                    }
                    else
                    {
                        result.GetDictionary(0).Should().BeNull();
                        result.GetValue(1).Should().BeNull();
                    }
                });
                rows.Should().Be(2);
            }

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction),
                                               SelectResult.Expression(prediction.Property("sum")))
                           .From(DataSource.Database(Db))
                           //.Where(prediction.NotNullOrMissing())) { //deprecated
                           .Where(prediction.IsValued())) {
                var explain = q.Explain();
                var rows    = VerifyQuery(q, (n, result) =>
                {
                    result.GetDictionary(0).Should().NotBeNull();
                    result.GetInt(1).Should().Be(15);
                });
                rows.Should().Be(1);
            }
        }
        public void TestCopy()
        {
            for (int i = 0; i < 10; i++)
            {
                var docID = $"doc{i}";
                using (var doc = new MutableDocument(docID)) {
                    doc.SetString("name", docID);

                    var data = Encoding.UTF8.GetBytes(docID);
                    var blob = new Blob("text/plain", data);
                    doc.SetBlob("data", blob);

                    Db.Save(doc);
                }
            }

            var dbName = "nudb";
            var config = Db.Config;
            var dir    = config.Directory;

            Database.Delete(dbName, dir);
            Database.Copy(Db.Path, dbName, config);

            Database.Exists(dbName, dir).Should().BeTrue();
            using (var nudb = new Database(dbName, config)) {
                nudb.Count.Should().Be(10, "because it is a copy of another database with 10 items");
                var DOCID   = Meta.ID;
                var S_DOCID = SelectResult.Expression(DOCID);
                using (var q = QueryBuilder.Select(S_DOCID).From(DataSource.Database(nudb))) {
                    var rs = q.Execute();
                    foreach (var r in rs)
                    {
                        var docID = r.GetString(0);
                        docID.Should().NotBeNull();

                        var doc = nudb.GetDocument(docID);
                        doc.Should().NotBeNull();
                        doc.GetString("name").Should().Be(docID);

                        var blob = doc.GetBlob("data");
                        blob.Should().NotBeNull();

                        var data = Encoding.UTF8.GetString(blob.Content);
                        data.Should().Be(docID);
                    }
                }
            }

            Database.Delete(dbName, dir);
        }
Exemple #27
0
        private static void CreateFullTextIndex()
        {
            var db = _Database;

            if (_NeedsExtraDocs)
            {
                var tasks = new[] { "buy groceries", "play chess", "book travels", "buy museum tickets" };
                foreach (var task in tasks)
                {
                    using (var doc = new MutableDocument()) {
                        doc.SetString("type", "task");
                        doc.SetString("name", task);
                        db.Save(doc);
                    }
                }
            }

            // # tag::fts-index[]
            var index = IndexBuilder.FullTextIndex(FullTextIndexItem.Property("name")).IgnoreAccents(false);

            db.CreateIndex("nameFTSIndex", index);
            // # end::fts-index[]
        }
Exemple #28
0
        private static void CreateDocument()
        {
            var db = _Database;

            // # tag::initializer[]
            using (var newTask = new MutableDocument("xyz")) {
                newTask.SetString("type", "task")
                .SetString("owner", "todo")
                .SetDate("createdAt", DateTimeOffset.UtcNow);

                db.Save(newTask);
            }
            // # end::initializer[]
        }
Exemple #29
0
        public void TestResetCheckpoint()
        {
            using (var doc1 = new MutableDocument("doc1")) {
                doc1.SetString("species", "Tiger");
                doc1.SetString("name", "Hobbes");
                Db.Save(doc1);
            }

            using (var doc2 = new MutableDocument("doc2")) {
                doc2.SetString("species", "Tiger");
                doc2.SetString("pattern", "striped");
                Db.Save(doc2);
            }

            var config = CreateConfig(true, false, false);

            RunReplication(config, 0, 0);
            config = CreateConfig(false, true, false);
            RunReplication(config, 0, 0);

            _otherDB.Count.Should().Be(2UL);
            using (var doc = Db.GetDocument("doc1")) {
                Db.Purge(doc);
            }

            using (var doc = Db.GetDocument("doc2")) {
                Db.Purge(doc);
            }

            Db.Count.Should().Be(0UL, "because the documents were purged");
            RunReplication(config, 0, 0);

            Db.Count.Should().Be(0UL, "because the documents were purged and the replicator is already past them");
            RunReplication(config, 0, 0, true);

            Db.Count.Should().Be(2UL, "because the replicator was reset");
        }
Exemple #30
0
        public void TestP2PFailureDuringClose()
        {
            using (var mdoc = new MutableDocument("livesindb")) {
                mdoc.SetString("name", "db");
                Db.Save(mdoc);
            }

            var config = CreateFailureP2PConfiguration(ProtocolType.ByteStream, MockConnectionLifecycleLocation.Close,
                                                       false);

            RunReplication(config, (int)CouchbaseLiteError.WebSocketAbnormalClose, CouchbaseLiteErrorType.CouchbaseLite);
            config = CreateFailureP2PConfiguration(ProtocolType.MessageStream, MockConnectionLifecycleLocation.Close,
                                                   false);
            RunReplication(config, (int)CouchbaseLiteError.WebSocketAbnormalClose, CouchbaseLiteErrorType.CouchbaseLite, true);
        }