Ejemplo n.º 1
0
        /// <summary>
        /// Sets the view's entity mapping function.
        /// </summary>
        /// <typeparam name="TEntity">The entity type.</typeparam>
        /// <param name="view">The view.</param>
        /// <param name="mapDelegate">The document mapping function.</param>
        /// <param name="version">The view's version.</param>
        /// <remarks>
        /// You may pass the <b>document</b> parameter as a value to the emit
        /// delegate, just as you would for a non-entity mapping function.
        /// In this case, Emit will to the <i>right thing</i> and add the document's
        /// low-level properties, as expected.
        /// </remarks>
        public static void SetMap <TEntity>(this View view, EntityMapDelegate <TEntity> mapDelegate, string version)
            where TEntity : class, IDynamicEntity, new()
        {
            Covenant.Requires <ArgumentNullException>(view != null);
            Covenant.Requires <ArgumentNullException>(mapDelegate != null);
            Covenant.Requires <ArgumentNullException>(!string.IsNullOrEmpty(version));

            view.SetMap(
                (doc, emit) =>
            {
                mapDelegate(new EntityDocument <TEntity>(Stub.Param, doc, EntityDatabase.From(view.Database)),
                            (key, value) =>
                {
                    var entityDocument = value as EntityDocument <TEntity>;

                    if (entityDocument != null)
                    {
                        // For entity document values, we want to add its properties,
                        // not the document itself.

                        emit(key, entityDocument.Properties);
                    }
                    else
                    {
                        emit(key, value);
                    }
                });
            },
                version);
        }
Ejemplo n.º 2
0
 static Conversation()
 {
     database = Manager.SharedInstance.GetDatabase ("conversations");
     defaultView = database.GetView ("conversation-messages");
     defaultView.SetMap ((document, emit) => {
         //
     }, "1");
 }
Ejemplo n.º 3
0
 public ObservableCollection <Dictionary <string, string> > recuperarDocs(string esport, string user)
 {
     Couchbase.Lite.View a = db.GetView("running2");
     a.SetMap((document, emit) =>
     {
         if ((string)document ["esportista"] == user && (string)document ["tipus"] == esport)
         {
             emit((string)document ["tipus"], document);
         }
     }, "1");
     LogQueryResultsAsync(a);
     return(LogQueryResultsAsync(a));
 }
Ejemplo n.º 4
0
        static ContactDatabase()
        {
            database = Manager.SharedInstance.GetDatabase ("contacts");
            emailLookup = new Dictionary<string, string> ();

            lookupView = database.GetView ("addresses");
            lookupView.SetMap ((document, emit) => {
                var val = document["contact.address.address"];
                emit(document["_id"], val);
            }, "1");

            Query lookupQuery = lookupView.CreateQuery ();

            BuildLookup(lookupQuery.Run ());

            lookupViewLiveQuery = lookupQuery.ToLiveQuery();
            lookupViewLiveQuery.Changed += (object sender, QueryChangeEventArgs e) => { BuildLookup(e.Rows); };
            lookupViewLiveQuery.Start ();
        }
Ejemplo n.º 5
0
        public void TestAsyncViewQuery()
        {
            var doneSignal = new CountDownLatch(1);
            var db         = StartDatabase();

            View view = db.GetView("vu");

            view.SetMap((document, emitter) => emitter(document ["sequence"], null), "1");

            const int kNDocs = 50;

            CreateDocuments(db, kNDocs);

            var query = view.CreateQuery();

            query.StartKey = 23;
            query.EndKey   = 33;

            var task = query.RunAsync().ContinueWith((resultTask) =>
            {
                Log.I(LiteTestCase.Tag, "Async query finished!");
                var rows = resultTask.Result;

                Assert.IsNotNull(rows);
                Assert.AreEqual(rows.Count, 11);

                var expectedKey = 23;
                for (IEnumerator <QueryRow> it = rows; it.MoveNext();)
                {
                    var row = it.Current;
                    Assert.AreEqual(row.Document.Database, db);
                    Assert.AreEqual(row.Key, expectedKey);
                    ++expectedKey;
                }
                doneSignal.CountDown();
            });

            Log.I(Tag, "Waiting for async query to finish...");

            var success = task.Wait(TimeSpan.FromSeconds(10));

            Assert.IsTrue(success, "Done signal timed out..StartKey=ry never ran");
        }
Ejemplo n.º 6
0
        public void TestAsyncViewQuery()
        {
            var doneSignal = new CountdownEvent(1);
            var db         = database;

            View view = db.GetView("vu");

            view.SetMap((document, emitter) => emitter(document ["sequence"], null), "1");

            const int kNDocs = 50;

            CreateDocuments(db, kNDocs);

            var query = view.CreateQuery();

            query.StartKey = 23;
            query.EndKey   = 33;

            var task = query.RunAsync().ContinueWith((resultTask) =>
            {
                Log.I(TAG, "Async query finished!");
                var rows = resultTask.Result;

                Assert.IsNotNull(rows);
                Assert.AreEqual(11, rows.Count);

                var expectedKey = 23;
                foreach (var row in rows)
                {
                    Assert.AreEqual(row.Document.Database, db);
                    Assert.AreEqual(row.Key, expectedKey);
                    ++expectedKey;
                }

                doneSignal.Signal();
            }, manager.CapturedContext.Scheduler);

            Console.WriteLine("Waiting for async query to finish...");
            var success = task.Wait(TimeSpan.FromSeconds(130));

            Assert.IsTrue(success, "Done signal timed out. Query.RunAsync() has never run or returned the result.");
        }
Ejemplo n.º 7
0
 /// <summary>Constructor</summary>
 internal Query(Database database, MapDelegate mapFunction)
     : this(database, database.MakeAnonymousView())
 {
     TemporaryView = true;
     View.SetMap(mapFunction, string.Empty);
 }
Ejemplo n.º 8
0
        /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
        public virtual void TestViewIndex()
        {
            int numTimesMapFunctionInvoked     = 0;
            IDictionary <string, object> dict1 = new Dictionary <string, object>();

            dict1["key"] = "one";
            IDictionary <string, object> dict2 = new Dictionary <string, object>();

            dict2["key"] = "two";
            IDictionary <string, object> dict3 = new Dictionary <string, object>();

            dict3["key"] = "three";
            IDictionary <string, object> dictX = new Dictionary <string, object>();

            dictX["clef"] = "quatre";
            RevisionInternal rev1 = PutDoc(database, dict1);
            RevisionInternal rev2 = PutDoc(database, dict2);
            RevisionInternal rev3 = PutDoc(database, dict3);

            PutDoc(database, dictX);
            View view            = database.GetView("aview");
            var  numTimesInvoked = 0;

            MapDelegate mapBlock = (IDictionary <string, object> document, EmitDelegate emitter) =>
            {
                numTimesInvoked += 1;
                NUnit.Framework.Assert.IsNotNull(document["_id"]);
                NUnit.Framework.Assert.IsNotNull(document["_rev"]);
                if (document["key"] != null)
                {
                    emitter(document["key"], null);
                }
            };

            view.SetMap(mapBlock, "1");
            NUnit.Framework.Assert.AreEqual(1, view.GetViewId());
            NUnit.Framework.Assert.IsTrue(view.IsStale());
            view.UpdateIndex();
            IList <IDictionary <string, object> > dumpResult = view.Dump();

            Log.V(Tag, "View dump: " + dumpResult);
            NUnit.Framework.Assert.AreEqual(3, dumpResult.Count);
            NUnit.Framework.Assert.AreEqual("\"one\"", dumpResult[0]["key"]);
            NUnit.Framework.Assert.AreEqual(1, dumpResult[0]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"two\"", dumpResult[2]["key"]);
            NUnit.Framework.Assert.AreEqual(2, dumpResult[2]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"three\"", dumpResult[1]["key"]);
            NUnit.Framework.Assert.AreEqual(3, dumpResult[1]["seq"]);
            //no-op reindex
            NUnit.Framework.Assert.IsFalse(view.IsStale());
            view.UpdateIndex();
            // Now add a doc and update a doc:
            RevisionInternal threeUpdated = new RevisionInternal(rev3.GetDocId(), rev3.GetRevId
                                                                     (), false, database);

            numTimesMapFunctionInvoked = mapBlock.GetNumTimesInvoked();
            IDictionary <string, object> newdict3 = new Dictionary <string, object>();

            newdict3["key"] = "3hree";
            threeUpdated.SetProperties(newdict3);
            Status status = new Status();

            rev3 = database.PutRevision(threeUpdated, rev3.GetRevId(), false, status);
            NUnit.Framework.Assert.IsTrue(status.IsSuccessful());
            // Reindex again:
            NUnit.Framework.Assert.IsTrue(view.IsStale());
            view.UpdateIndex();
            // Make sure the map function was only invoked one more time (for the document that was added)
            NUnit.Framework.Assert.AreEqual(mapBlock.GetNumTimesInvoked(), numTimesMapFunctionInvoked
                                            + 1);
            IDictionary <string, object> dict4 = new Dictionary <string, object>();

            dict4["key"] = "four";
            RevisionInternal rev4       = PutDoc(database, dict4);
            RevisionInternal twoDeleted = new RevisionInternal(rev2.GetDocId(), rev2.GetRevId
                                                                   (), true, database);

            database.PutRevision(twoDeleted, rev2.GetRevId(), false, status);
            NUnit.Framework.Assert.IsTrue(status.IsSuccessful());
            // Reindex again:
            NUnit.Framework.Assert.IsTrue(view.IsStale());
            view.UpdateIndex();
            dumpResult = view.Dump();
            Log.V(Tag, "View dump: " + dumpResult);
            NUnit.Framework.Assert.AreEqual(3, dumpResult.Count);
            NUnit.Framework.Assert.AreEqual("\"one\"", dumpResult[2]["key"]);
            NUnit.Framework.Assert.AreEqual(1, dumpResult[2]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"3hree\"", dumpResult[0]["key"]);
            NUnit.Framework.Assert.AreEqual(5, dumpResult[0]["seq"]);
            NUnit.Framework.Assert.AreEqual("\"four\"", dumpResult[1]["key"]);
            NUnit.Framework.Assert.AreEqual(6, dumpResult[1]["seq"]);
            // Now do a real query:
            IList <QueryRow> rows = view.QueryWithOptions(null);

            NUnit.Framework.Assert.AreEqual(3, rows.Count);
            NUnit.Framework.Assert.AreEqual("one", rows[2].Key);
            NUnit.Framework.Assert.AreEqual(rev1.GetDocId(), rows[2].DocumentId);
            NUnit.Framework.Assert.AreEqual("3hree", rows[0].Key);
            NUnit.Framework.Assert.AreEqual(rev3.GetDocId(), rows[0].DocumentId);
            NUnit.Framework.Assert.AreEqual("four", rows[1].Key);
            NUnit.Framework.Assert.AreEqual(rev4.GetDocId(), rows[1].DocumentId);
            view.DeleteIndex();
        }