internal static Document CreateDocumentWithProperties(Database db, IDictionary <string, object> properties)
        {
            var doc = db.CreateDocument();

            Assert.IsNotNull(doc);
            Assert.IsNull(doc.CurrentRevisionId);
            Assert.IsNull(doc.CurrentRevision);
            Assert.IsNotNull(doc.Id, "Document has no ID");

            try
            {
                doc.PutProperties(properties);
            }
            catch (Exception e)
            {
                Log.E(TAG, "Error creating document", e);
                Assert.IsTrue(false, "can't create new document in db:" + db.Name + " with properties:" + properties.ToString());
            }

            Assert.IsNotNull(doc.Id);
            Assert.IsNotNull(doc.CurrentRevisionId);
            Assert.IsNotNull(doc.CurrentRevision);

            // should be same doc instance, since there should only ever be a single Document instance for a given document
            Assert.AreEqual(db.GetDocument(doc.Id), doc);
            Assert.AreEqual(db.GetDocument(doc.Id).Id, doc.Id);

            return(doc);
        }
예제 #2
0
        public static Document CreateDocumentWithProperties(Database db, IDictionary <string
                                                                                      , object> properties)
        {
            Document doc = db.CreateDocument();

            NUnit.Framework.Assert.IsNotNull(doc);
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevision());
            NUnit.Framework.Assert.IsNotNull("Document has no ID", doc.GetId());
            // 'untitled' docs are no longer untitled (8/10/12)
            try
            {
                doc.PutProperties(properties);
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error creating document", e);
                NUnit.Framework.Assert.IsTrue("can't create new document in db:" + db.GetName() +
                                              " with properties:" + properties.ToString(), false);
            }
            NUnit.Framework.Assert.IsNotNull(doc.GetId());
            NUnit.Framework.Assert.IsNotNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNotNull(doc.GetUserProperties());
            // should be same doc instance, since there should only ever be a single Document instance for a given document
            NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()), doc);
            NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()).GetId(), doc.GetId());
            return(doc);
        }
예제 #3
0
        public virtual Document GetDocument()
        {
            if (GetDocumentId() == null)
            {
                return(null);
            }
            Document document = database.GetDocument(GetDocumentId());

            document.LoadCurrentRevisionFrom(this);
            return(document);
        }
예제 #4
0
        public static Document CreateDocumentWithProperties(Database db, IDictionary <String, Object> properties)
        {
            var doc = db.CreateDocument();

            Assert.IsNotNull(doc);
            Assert.IsNull(doc.CurrentRevisionId);
            Assert.IsNull(doc.CurrentRevision);
            Assert.IsNotNull("Document has no ID", doc.Id);

            // 'untitled' docs are no longer untitled (8/10/12)
            try
            {
                doc.PutProperties(properties);
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error creating document", e);
                Assert.IsTrue(false, "can't create new document in db:" + db.Name +
                              " with properties:" + properties.Aggregate(new StringBuilder(" >>> "), (str, kvp) => { str.AppendFormat("'{0}:{1}' ", kvp.Key, kvp.Value); return(str); }, str => str.ToString()));
            }

            Assert.IsNotNull(doc.Id);
            Assert.IsNotNull(doc.CurrentRevisionId);
            Assert.IsNotNull(doc.UserProperties);
            Assert.AreEqual(db.GetDocument(doc.Id), doc);

            return(doc);
        }
예제 #5
0
        public static Document CreateDocumentWithProperties(Database db, IDictionary <string
                                                                                      , object> properties)
        {
            Document doc = db.CreateDocument();

            NUnit.Framework.Assert.IsNotNull(doc);
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNull(doc.GetCurrentRevision());
            NUnit.Framework.Assert.IsNotNull("Document has no ID", doc.GetId());
            // 'untitled' docs are no longer untitled (8/10/12)
            try
            {
                doc.PutProperties(properties);
            }
            catch (Exception e)
            {
                Log.E(Tag, "Error creating document", e);
                NUnit.Framework.Assert.IsTrue("can't create new document in db:" + db.GetName() +
                                              " with properties:" + properties.ToString(), false);
            }
            NUnit.Framework.Assert.IsNotNull(doc.GetId());
            NUnit.Framework.Assert.IsNotNull(doc.GetCurrentRevisionId());
            NUnit.Framework.Assert.IsNotNull(doc.GetUserProperties());
            // this won't work until the weakref hashmap is implemented which stores all docs
            // Assert.assertEquals(db.getDocument(doc.getId()), doc);
            NUnit.Framework.Assert.AreEqual(db.GetDocument(doc.GetId()).GetId(), doc.GetId());
            return(doc);
        }
예제 #6
0
	private IEnumerator Start () {
		Debug.LogFormat ("Data path = {0}", Application.persistentDataPath);

#if UNITY_EDITOR_WIN
		Log.SetLogger(new UnityLogger());
#endif
		_db = Manager.SharedInstance.GetDatabase ("spaceshooter");
		_pull = _db.CreatePullReplication (GameController.SYNC_URL);
		_pull.Continuous = true;
		_pull.Start ();
		while (_pull != null && _pull.Status == ReplicationStatus.Active) {
			yield return new WaitForSeconds(0.5f);
		}

		var doc = _db.GetExistingDocument ("player_data");
		if (doc != null) {
			//We have a record!  Get the ship data, if possible.
			string assetName = String.Empty;
			if(doc.UserProperties.ContainsKey("ship_data")) {
				assetName = doc.UserProperties ["ship_data"] as String;
			}
			StartCoroutine(LoadAsset (assetName));
		} else {
			//Create a new record
			doc = _db.GetDocument("player_data");
			doc.PutProperties(new Dictionary<string, object> { { "ship_data", String.Empty } });
		}

		doc.Change += DocumentChanged;
		_push = _db.CreatePushReplication (new Uri ("http://127.0.0.1:4984/spaceshooter"));
		_push.Start();
	}
        private void CreateDocs(Database db, bool withAttachments)
        {
            WriteDebug("Creating {0} documents in {1}", DOCUMENT_COUNT, db.Name);
            db.RunInTransaction(() =>
            {
                for (int i = 1; i <= DOCUMENT_COUNT; i++)
                {
                    var doc = db.GetDocument(String.Format("doc-{0}", i));
                    var rev = doc.CreateRevision();
                    rev.SetUserProperties(new Dictionary <string, object> {
                        { "index", i },
                        { "bar", false }
                    });

                    if (withAttachments)
                    {
                        int length = (int)(MIN_ATTACHMENT_LENGTH + _rng.Next() /
                                           (double)Int32.MaxValue * (MAX_ATTACHMENT_LENGTH - MIN_ATTACHMENT_LENGTH));
                        var data = new byte[length];
                        _rng.NextBytes(data);
                        rev.SetAttachment("README", "application/octet-stream", data);
                    }

                    Assert.DoesNotThrow(() => rev.Save());
                }

                return(true);
            });
        }
예제 #8
0
        private SavedRevision GetRevisionWithId(String revId)
        {
            if (!StringEx.IsNullOrWhiteSpace(revId) && revId.Equals(currentRevision.Id))
            {
                return(currentRevision);
            }

            return(GetRevisionFromRev(Database.GetDocument(Id, revId, true)));
        }
예제 #9
0
        internal static Document CreateDocumentWithProperties(Database db, IDictionary <string, object> properties)
        {
            var doc = db.CreateDocument();

            Assert.IsNotNull(doc);
            Assert.IsNull(doc.CurrentRevisionId);
            Assert.IsNull(doc.CurrentRevision);
            Assert.IsNotNull(doc.Id, "Document has no ID");

            doc.PutProperties(properties);

            Assert.IsNotNull(doc.Id);
            Assert.IsNotNull(doc.CurrentRevisionId);
            Assert.IsNotNull(doc.CurrentRevision);

            // should be same doc instance, since there should only ever be a single Document instance for a given document
            Assert.AreEqual(db.GetDocument(doc.Id), doc);
            Assert.AreEqual(db.GetDocument(doc.Id).Id, doc.Id);

            return(doc);
        }
예제 #10
0
        /// <summary>
        /// Returns the <see cref="Couchbase.Lite.Revision"/> with the specified id if it exists, otherwise null.
        /// </summary>
        /// <param name="id">The <see cref="Couchbase.Lite.Revision"/> id.</param>
        /// <returns>The <see cref="Couchbase.Lite.Revision"/> with the specified id if it exists, otherwise null</returns>
        public SavedRevision GetRevision(String id)
        {
            if (CurrentRevision != null && id.Equals(CurrentRevision.Id))
            {
                return(CurrentRevision);
            }

            var revisionInternal = Database.GetDocument(Id, id, true);

            var revision = GetRevisionFromRev(revisionInternal);

            return(revision);
        }
예제 #11
0
        internal SavedRevision GetRevisionWithId(RevisionID revId, bool withBody)
        {
            if (revId == null)
            {
                return(null);
            }

            if (revId.Equals(currentRevision.Id))
            {
                return(currentRevision);
            }

            return(GetRevisionFromRev(Database.GetDocument(Id, revId, withBody)));
        }
예제 #12
0
        /// <summary>
        /// Gets the conflicting <see cref="Couchbase.Lite.Revision"/>s of the associated <see cref="Couchbase.Lite.Document"/>.
        /// </summary>
        /// <remarks>
        /// Gets the conflicting <see cref="Couchbase.Lite.Revision"/>s of the associated <see cref="Couchbase.Lite.Document"/>.
        /// The first <see cref="Couchbase.Lite.Revision"/> in the array will be the default 'winning' <see cref="Couchbase.Lite.Revision"/>
        /// that shadows the <see cref="Couchbase.Lite.Revision"/>s. This is only valid in an all-documents <see cref="Couchbase.Lite.Query"/>
        /// whose allDocsMode is set to ShowConflicts or OnlyConflicts, otherwise it returns null.
        /// </remarks>
        /// <returns>The conflicting <see cref="Couchbase.Lite.Revision"/>s of the associated <see cref="Couchbase.Lite.Document"/></returns>
        public IEnumerable <SavedRevision> GetConflictingRevisions()
        {
            var doc   = Database.GetDocument(SourceDocumentId);
            var value = Value as IDictionary <string, object>;

            if (value == null)
            {
                return(null);
            }

            var conflicts = value.GetCast <IList <string> >("_conflicts", new List <string>());

            return(from revID in conflicts
                   select doc.GetRevision(revID));
        }
예제 #13
0
 public static void SaveObject(IStorable obj, Database db, IDReferenceResolver resolver = null)
 {
     Document doc = db.GetDocument (obj.ID.ToString ());
     doc.Update ((UnsavedRevision rev) => {
         JObject jo = SerializeObject (obj, rev, db, resolver);
         IDictionary<string, object> props = jo.ToObject<IDictionary<string, object>> ();
         /* SetProperties sets a new properties dictionary, removing the attachments we
              * added in the serialization */
         if (rev.Properties.ContainsKey ("_attachments")) {
             props ["_attachments"] = rev.Properties ["_attachments"];
         }
         rev.SetProperties (props);
         return true;
     });
 }
예제 #14
0
        private void VerifyHistory(Database db, RevisionInternal rev, IList <string> history)
        {
            var gotRev = db.GetDocument(rev.DocID, null,
                                        true);

            Assert.AreEqual(rev, gotRev);
            AssertPropertiesAreEqual(rev.GetProperties(), gotRev.GetProperties());

            var revHistory = db.Storage.GetRevisionHistory(gotRev, null);

            Assert.AreEqual(history.Count, revHistory.Count);

            for (int i = 0; i < history.Count; i++)
            {
                RevisionInternal hrev = revHistory[i];
                Assert.AreEqual(rev.DocID, hrev.DocID);
                Assert.AreEqual(history[i], hrev.RevID);
                Assert.IsFalse(rev.Deleted);
            }
        }
예제 #15
0
        /// <summary>
        /// Gets the conflicting <see cref="Couchbase.Lite.Revision"/>s of the associated <see cref="Couchbase.Lite.Document"/>.
        /// </summary>
        /// <remarks>
        /// Gets the conflicting <see cref="Couchbase.Lite.Revision"/>s of the associated <see cref="Couchbase.Lite.Document"/>.
        /// The first <see cref="Couchbase.Lite.Revision"/> in the array will be the default 'winning' <see cref="Couchbase.Lite.Revision"/>
        /// that shadows the <see cref="Couchbase.Lite.Revision"/>s. This is only valid in an all-documents <see cref="Couchbase.Lite.Query"/>
        /// whose allDocsMode is set to ShowConflicts or OnlyConflicts, otherwise it returns null.
        /// </remarks>
        /// <returns>The conflicting <see cref="Couchbase.Lite.Revision"/>s of the associated <see cref="Couchbase.Lite.Document"/></returns>
        public IEnumerable <SavedRevision> GetConflictingRevisions()
        {
            var doc      = Database.GetDocument(SourceDocumentId);
            var valueTmp = (IDictionary <string, object>)Value;

            var conflicts = (IList <string>)valueTmp["_conflicts"];

            if (conflicts == null)
            {
                conflicts = new AList <string>();
            }

            var conflictingRevisions = new AList <SavedRevision>();

            foreach (var conflictRevisionId in conflicts)
            {
                var revision = doc.GetRevision(conflictRevisionId);
                conflictingRevisions.AddItem(revision);
            }
            return(conflictingRevisions);
        }
        internal static Document CreateDocumentWithProperties(Database db, IDictionary<string, object> properties) 
        {
            var doc = db.CreateDocument();

            Assert.IsNotNull(doc);
            Assert.IsNull(doc.CurrentRevisionId);
            Assert.IsNull(doc.CurrentRevision);
            Assert.IsNotNull(doc.Id, "Document has no ID");

            doc.PutProperties(properties);

            Assert.IsNotNull(doc.Id);
            Assert.IsNotNull(doc.CurrentRevisionId);
            Assert.IsNotNull(doc.CurrentRevision);

            // should be same doc instance, since there should only ever be a single Document instance for a given document
            Assert.AreEqual(db.GetDocument(doc.Id), doc);
            Assert.AreEqual(db.GetDocument(doc.Id).Id, doc.Id);

            return doc;
        }
예제 #17
0
        private void VerifyHistory(Database db, RevisionInternal rev, IList<string> history)
        {
            var gotRev = db.GetDocument(rev.GetDocId(), null, 
                true);
            Assert.AreEqual(rev, gotRev);
            AssertPropertiesAreEqual(rev.GetProperties(), gotRev.GetProperties());

            var revHistory = db.Storage.GetRevisionHistory(gotRev, null);
            Assert.AreEqual(history.Count, revHistory.Count);
            
            for (int i = 0; i < history.Count; i++)
            {
                RevisionInternal hrev = revHistory[i];
                Assert.AreEqual(rev.GetDocId(), hrev.GetDocId());
                Assert.AreEqual(history[i], hrev.GetRevId());
                Assert.IsFalse(rev.IsDeleted());
            }
        }
 /// <summary>Constructor</summary>
 internal SavedRevision(Database database, RevisionInternal revision)
     : this(database.GetDocument(revision == null ? null : revision.DocID), revision) { }
예제 #19
0
        internal static Document CreateDocumentWithProperties(Database db, IDictionary<string, object> properties) 
        {
            var doc = db.CreateDocument();

            Assert.IsNotNull(doc);
            Assert.IsNull(doc.CurrentRevisionId);
            Assert.IsNull(doc.CurrentRevision);
            Assert.IsNotNull(doc.Id, "Document has no ID");

            try
            {
                doc.PutProperties(properties);
            } 
            catch (Exception e)
            {
                Log.E(Tag, "Error creating document", e);
                Assert.IsTrue(false, "can't create new document in db:" + db.Name + " with properties:" + properties.ToString());
            }

            Assert.IsNotNull(doc.Id);
            Assert.IsNotNull(doc.CurrentRevisionId);
            Assert.IsNotNull(doc.CurrentRevision);

            // should be same doc instance, since there should only ever be a single Document instance for a given document
            Assert.AreEqual(db.GetDocument(doc.Id), doc);
            Assert.AreEqual(db.GetDocument(doc.Id).Id, doc.Id);

            return doc;
        }
예제 #20
0
        public static Document CreateDocumentWithProperties(Database db, IDictionary<String, Object> properties)
		{
            var doc = db.CreateDocument();

			Assert.IsNotNull(doc);
			Assert.IsNull(doc.CurrentRevisionId);
			Assert.IsNull(doc.CurrentRevision);
			Assert.IsNotNull("Document has no ID", doc.Id);

			// 'untitled' docs are no longer untitled (8/10/12)
			try
			{
				doc.PutProperties(properties);
			}
			catch (Exception e)
			{
				Log.E(Tag, "Error creating document", e);
                Assert.IsTrue( false, "can't create new document in db:" + db.Name +
                    " with properties:" + properties.Aggregate(new StringBuilder(" >>> "), (str, kvp)=> { str.AppendFormat("'{0}:{1}' ", kvp.Key, kvp.Value); return str; }, str=>str.ToString()));
			}

			Assert.IsNotNull(doc.Id);
			Assert.IsNotNull(doc.CurrentRevisionId);
			Assert.IsNotNull(doc.UserProperties);
			Assert.AreEqual(db.GetDocument(doc.Id), doc);

			return doc;
		}
예제 #21
0
 /// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception>
 public static Couchbase.Lite.Document CreateProfile(Database database, string userId
     , string name)
 {
     SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
         );
     Calendar calendar = GregorianCalendar.GetInstance();
     string currentTimeString = dateFormatter.Format(calendar.GetTime());
     IDictionary<string, object> properties = new Dictionary<string, object>();
     properties.Put("type", DocType);
     properties.Put("user_id", userId);
     properties.Put("name", name);
     Couchbase.Lite.Document document = database.GetDocument("profile:" + userId);
     document.PutProperties(properties);
     return document;
 }
예제 #22
0
        private void CreateDocs(Database db, bool withAttachments)
        {
            Log.D(TAG, "Creating {0} documents in {1}", DOCUMENT_COUNT, db.Name);
            db.RunInTransaction(() =>
            {
                for(int i = 1; i <= DOCUMENT_COUNT; i++) {
                    var doc = db.GetDocument(String.Format("doc-{0}", i));
                    var rev = doc.CreateRevision();
                    rev.SetUserProperties(new Dictionary<string, object> {
                        { "index", i },
                        { "bar", false }
                    });

                    if(withAttachments) {
                        int length = (int)(MIN_ATTACHMENT_LENGTH + _rng.Next() / 
                            (double)Int32.MaxValue * (MAX_ATTACHMENT_LENGTH - MIN_ATTACHMENT_LENGTH));
                        var data = new byte[length];
                        _rng.NextBytes(data);
                        rev.SetAttachment("README", "application/octet-stream", data);
                    }

                    Assert.DoesNotThrow(() => rev.Save());
                }

                return true;
            });
        }
예제 #23
0
 /// <summary>Constructor</summary>
 internal SavedRevision(Database database, RevisionInternal revision)
     : this(database.GetDocument(revision.GetDocId()), revision)
 {
 }
예제 #24
0
 /// <summary>Constructor</summary>
 internal SavedRevision(Database database, RevisionInternal revision)
     : this(database.GetDocument(revision == null ? null : revision.DocID), revision)
 {
 }
예제 #25
0
 /// <summary>Constructor</summary>
 internal SavedRevision(Database database, RevisionInternal revision)
     : this(database.GetDocument(revision.GetDocId()), revision) { }