internal static IDictionary<string, object> InstallAttachmentBodies(IDictionary<string , object> attachments, Database database) { IDictionary<string, object> updatedAttachments = new Dictionary<string, object>(); foreach (string name in attachments.Keys) { object value = attachments.Get(name); if (value is Couchbase.Lite.Attachment) { Couchbase.Lite.Attachment attachment = (Couchbase.Lite.Attachment)value; IDictionary<string, object> metadataMutable = new Dictionary<string, object>(); metadataMutable.PutAll(attachment.GetMetadata()); InputStream body = attachment.GetBodyIfNew(); if (body != null) { // Copy attachment body into the database's blob store: BlobStoreWriter writer = BlobStoreWriterForBody(body, database); metadataMutable.Put("length", (long)writer.GetLength()); metadataMutable.Put("digest", writer.MD5DigestString()); metadataMutable.Put("follows", true); database.RememberAttachmentWriter(writer); } updatedAttachments.Put(name, metadataMutable); } else { if (value is AttachmentInternal) { throw new ArgumentException("AttachmentInternal objects not expected here. Could indicate a bug" ); } else { if (value != null) { updatedAttachments.Put(name, value); } } } } return updatedAttachments; }
private static void SetDiagnostics(SegmentInfo info, string source, IDictionary<string, string> details) { IDictionary<string, string> diagnostics = new Dictionary<string, string>(); diagnostics["source"] = source; diagnostics["lucene.version"] = Constants.LUCENE_VERSION; diagnostics["os"] = Constants.OS_NAME; diagnostics["os.arch"] = Constants.OS_ARCH; diagnostics["os.version"] = Constants.OS_VERSION; diagnostics["java.version"] = Constants.JAVA_VERSION; diagnostics["java.vendor"] = Constants.JAVA_VENDOR; diagnostics["timestamp"] = Convert.ToString((DateTime.Now)); if (details != null) { diagnostics.PutAll(details); } info.Diagnostics = diagnostics; }
internal IDictionary<string, object> GetChangesFeedParams() { if (docIDs != null && docIDs.Count > 0) { filterName = "_doc_ids"; filterParams = new Dictionary<string, object>(); filterParams.Put("doc_ids", docIDs); } var bodyParams = new Dictionary<string, object>(); bodyParams["feed"] = GetFeed(); bodyParams["heartbeat"] = _heartbeatMilliseconds; if (includeConflicts) { bodyParams["style"] = "all_docs"; } else { bodyParams["style"] = null; } if (lastSequenceID != null) { Int64 sequenceAsLong; var success = Int64.TryParse(lastSequenceID.ToString(), out sequenceAsLong); bodyParams["since"] = success ? sequenceAsLong : lastSequenceID; } if (mode == ChangeTrackerMode.LongPoll) { bodyParams["limit"] = LongPollModeLimit; } if (filterName != null) { bodyParams["filter"] = filterName; bodyParams.PutAll(filterParams); } return bodyParams; }
private void SaveLastSequence(SaveLastSequenceCompletionBlock completionHandler) { if (!lastSequenceChanged) { if (completionHandler != null) { completionHandler(); } return; } if (_savingCheckpoint) { // If a save is already in progress, don't do anything. (The completion block will trigger // another save after the first one finishes.) Task.Delay(500).ContinueWith(t => SaveLastSequence(completionHandler)); } lastSequenceChanged = false; Log.D(TAG, "saveLastSequence() called. lastSequence: " + LastSequence); var body = new Dictionary<String, Object>(); if (_remoteCheckpoint != null) { body.PutAll(_remoteCheckpoint); } body["lastSequence"] = LastSequence; var remoteCheckpointDocID = RemoteCheckpointDocID(); if (String.IsNullOrEmpty(remoteCheckpointDocID)) { Log.W(TAG, "remoteCheckpointDocID is null, aborting saveLastSequence()"); return; } _savingCheckpoint = true; var message = SendAsyncRequest(HttpMethod.Put, "/_local/" + remoteCheckpointDocID, body, (result, e) => { _savingCheckpoint = false; if (e != null) { Log.V(TAG, "Unable to save remote checkpoint", e); } if (e != null) { switch (GetStatusFromError(e)) { case StatusCode.NotFound: _remoteCheckpoint = null; break; case StatusCode.Conflict: RefreshRemoteCheckpointDoc(); break; default: // TODO: On 401 or 403, and this is a pull, remember that remote // TODO: is read-only & don't attempt to read its checkpoint next time. break; } } else { Log.D(TAG, "Save checkpoint response: " + result); var response = result.AsDictionary<string, object>(); body.Put ("_rev", response.Get ("rev")); _remoteCheckpoint = body; var localDb = LocalDatabase; if(localDb == null || localDb.Storage == null) { Log.W(TAG, "Database is null, ignoring remote checkpoint response"); if(completionHandler != null) { completionHandler(); } return; } localDb.SetLastSequence(LastSequence, remoteCheckpointDocID); } if (completionHandler != null) { completionHandler (); } }); // This request should not be canceled when the replication is told to stop: Task dummy; _requests.TryRemove(message, out dummy); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void UpdateCheckedStatus(Couchbase.Lite.Document task, bool @checked ) { IDictionary<string, object> properties = new Dictionary<string, object>(); properties.PutAll(task.GetProperties()); properties.Put("checked", @checked); task.PutProperties(properties); }
public virtual Couchbase.Lite.Internal.RevisionInternal CopyWithDocID(string docId , string revId) { //assert((docId != null) && (revId != null)); System.Diagnostics.Debug.Assert((docId != null)); System.Diagnostics.Debug.Assert(((this.docId == null) || (this.docId.Equals(docId )))); Couchbase.Lite.Internal.RevisionInternal result = new Couchbase.Lite.Internal.RevisionInternal (docId, revId, deleted, database); IDictionary<string, object> unmodifiableProperties = GetProperties(); IDictionary<string, object> properties = new Dictionary<string, object>(); if (unmodifiableProperties != null) { properties.PutAll(unmodifiableProperties); } properties.Put("_id", docId); properties.Put("_rev", revId); result.SetProperties(properties); return result; }
public RevisionInternal CopyWithDocID(String docId, String revId) { System.Diagnostics.Debug.Assert((docId != null)); System.Diagnostics.Debug.Assert(((this.docId == null) || (this.docId.Equals(docId)))); var result = new RevisionInternal(docId, revId, deleted, database); var unmodifiableProperties = GetProperties(); var properties = new Dictionary<string, object>(); if (unmodifiableProperties != null) { properties.PutAll(unmodifiableProperties); } properties["_id"] = docId; properties["_rev"] = revId; result.SetProperties(properties); return result; }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void AddMemberToList(Couchbase.Lite.Document list, Couchbase.Lite.Document user) { IDictionary<string, object> newProperties = new Dictionary<string, object>(); newProperties.PutAll(list.Properties); IList<string> members = (IList<string>)newProperties.Get("members"); if (members == null) { members = new AList<string>(); } members.AddItem(user.Id); newProperties.Put("members", members); try { list.PutProperties(newProperties); } catch (CouchbaseLiteException e) { Log.E(Application.Tag, "Cannot add member to the list", e); } }
//TODO issue: deleteLocalDocument should return error.code( see ios) // HISTORY /// <exception cref="System.Exception"></exception> public virtual void TestHistory() { IDictionary<string, object> properties = new Dictionary<string, object>(); properties.Put("testName", "test06_History"); properties.Put("tag", 1); Database db = StartDatabase(); Document doc = CreateDocumentWithProperties(db, properties); string rev1ID = doc.GetCurrentRevisionId(); Log.I(Tag, "1st revision: " + rev1ID); NUnit.Framework.Assert.IsNotNull("1st revision looks wrong: " + rev1ID, rev1ID.StartsWith ("1-")); NUnit.Framework.Assert.AreEqual(doc.GetUserProperties(), properties); properties = new Dictionary<string, object>(); properties.PutAll(doc.GetProperties()); properties.Put("tag", 2); NUnit.Framework.Assert.IsNotNull(!properties.Equals(doc.GetProperties())); NUnit.Framework.Assert.IsNotNull(doc.PutProperties(properties)); string rev2ID = doc.GetCurrentRevisionId(); Log.I(Tag, "rev2ID" + rev2ID); NUnit.Framework.Assert.IsNotNull("2nd revision looks wrong:" + rev2ID, rev2ID.StartsWith ("2-")); IList<SavedRevision> revisions = doc.GetRevisionHistory(); Log.I(Tag, "Revisions = " + revisions); NUnit.Framework.Assert.AreEqual(revisions.Count, 2); SavedRevision rev1 = revisions[0]; NUnit.Framework.Assert.AreEqual(rev1.GetId(), rev1ID); IDictionary<string, object> gotProperties = rev1.GetProperties(); NUnit.Framework.Assert.AreEqual(1, gotProperties.Get("tag")); SavedRevision rev2 = revisions[1]; NUnit.Framework.Assert.AreEqual(rev2.GetId(), rev2ID); NUnit.Framework.Assert.AreEqual(rev2, doc.GetCurrentRevision()); gotProperties = rev2.GetProperties(); NUnit.Framework.Assert.AreEqual(2, gotProperties.Get("tag")); IList<SavedRevision> tmp = new AList<SavedRevision>(); tmp.AddItem(rev2); NUnit.Framework.Assert.AreEqual(doc.GetConflictingRevisions(), tmp); NUnit.Framework.Assert.AreEqual(doc.GetLeafRevisions(), tmp); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void RemoveMemberFromList(Couchbase.Lite.Document list, Couchbase.Lite.Document user) { IDictionary<string, object> newProperties = new Dictionary<string, object>(); newProperties.PutAll(list.Properties); IList<string> members = (IList<string>)newProperties.Get("members"); if (members != null) { members.Remove(user.Id); } newProperties.Put("members", members); list.PutProperties(newProperties); }
/// <exception cref="Couchbase.Lite.CouchbaseLiteException"></exception> public static void AssignOwnerToListsIfNeeded(Database database, Couchbase.Lite.Document user) { QueryEnumerator enumerator = GetQuery(database).Run(); if (enumerator == null) { return; } foreach (var row in enumerator) { Couchbase.Lite.Document document = row.Document; string owner = (string)document.GetProperty("owner"); if (owner != null) { continue; } IDictionary<string, object> properties = new Dictionary<string, object>(); properties.PutAll(document.Properties); properties.Put("owner", user.Id); document.PutProperties(properties); } }
internal void SaveLastSequence() { if (!lastSequenceChanged) { return; } if (savingCheckpoint) { // If a save is already in progress, don't do anything. (The completion block will trigger // another save after the first one finishes.) overdueForSave = true; return; } lastSequenceChanged = false; overdueForSave = false; Log.D(Tag, this + " saveLastSequence() called. lastSequence: " + LastSequence); var body = new Dictionary<String, Object>(); if (remoteCheckpoint != null) { body.PutAll(remoteCheckpoint); } body["lastSequence"] = LastSequence; var remoteCheckpointDocID = RemoteCheckpointDocID(); if (String.IsNullOrEmpty(remoteCheckpointDocID)) { Log.W(Tag, this + ": remoteCheckpointDocID is null, aborting saveLastSequence()"); return; } savingCheckpoint = true; Log.D(Tag, this + " put remote _local document. checkpointID: " + remoteCheckpointDocID); SendAsyncRequest(HttpMethod.Put, "/_local/" + remoteCheckpointDocID, body, (result, e) => { savingCheckpoint = false; if (e != null) { Log.V (Tag, this + ": Unable to save remote checkpoint", e); } if (LocalDatabase == null) { Log.W(Tag, this + ": Database is null, ignoring remote checkpoint response"); return; } if (e != null) { switch (GetStatusFromError(e)) { case StatusCode.NotFound: { remoteCheckpoint = null; overdueForSave = true; break; } case StatusCode.Conflict: { RefreshRemoteCheckpointDoc(); break; } default: { // TODO: On 401 or 403, and this is a pull, remember that remote // TODO: is read-only & don't attempt to read its checkpoint next time. break; } } } else { var response = (IDictionary<String, Object>)result; body.Put ("_rev", response.Get ("rev")); remoteCheckpoint = body; LocalDatabase.SetLastSequence(LastSequence, RemoteCheckpointDocID(), IsPull); } if (overdueForSave) { SaveLastSequence (); } }); }
public RevisionInternal Copy(string docId, RevisionID revId) { System.Diagnostics.Debug.Assert((docId != null)); System.Diagnostics.Debug.Assert(((_docId == null) || (_docId.Equals(docId)))); var result = new RevisionInternal(docId, revId, Deleted); var unmodifiableProperties = GetProperties(); var properties = new Dictionary<string, object>(); if(unmodifiableProperties != null) { properties.PutAll(unmodifiableProperties); } properties.SetDocRevID(docId, revId); result.SetProperties(properties); return result; }
internal RevisionInternal(RevisionInternal other) : this(other.DocID, other.RevID, other.Deleted) { var unmodifiableProperties = other.GetProperties(); var properties = new Dictionary<string, object>(); if(unmodifiableProperties != null) { properties.PutAll(unmodifiableProperties); } SetProperties(properties); }
public void SetUserProperties(IDictionary<string, object> userProperties) { IDictionary<string, object> newProps = new Dictionary<string, object>(); newProps.PutAll(userProperties); foreach (string key in properties.Keys) { if (key.StartsWith("_")) { newProps.Put(key, properties.Get(key)); } } // Preserve metadata properties properties = newProps; }
/// <exception cref="System.Exception"></exception> public virtual void TestConflict() { IDictionary<string, object> prop = new Dictionary<string, object>(); prop.Put("foo", "bar"); Database db = StartDatabase(); Document doc = CreateDocumentWithProperties(db, prop); SavedRevision rev1 = doc.GetCurrentRevision(); IDictionary<string, object> properties = new Dictionary<string, object>(); properties.PutAll(doc.GetProperties()); properties.Put("tag", 2); SavedRevision rev2a = doc.PutProperties(properties); properties = new Dictionary<string, object>(); properties.PutAll(rev1.GetProperties()); properties.Put("tag", 3); UnsavedRevision newRev = rev1.CreateRevision(); newRev.SetProperties(properties); bool allowConflict = true; SavedRevision rev2b = newRev.Save(allowConflict); NUnit.Framework.Assert.IsNotNull("Failed to create a a conflict", rev2b); IList<SavedRevision> confRevs = new AList<SavedRevision>(); confRevs.AddItem(rev2b); confRevs.AddItem(rev2a); NUnit.Framework.Assert.AreEqual(doc.GetConflictingRevisions(), confRevs); NUnit.Framework.Assert.AreEqual(doc.GetLeafRevisions(), confRevs); SavedRevision defaultRev; SavedRevision otherRev; if (Sharpen.Runtime.CompareOrdinal(rev2a.GetId(), rev2b.GetId()) > 0) { defaultRev = rev2a; otherRev = rev2b; } else { defaultRev = rev2b; otherRev = rev2a; } NUnit.Framework.Assert.AreEqual(doc.GetCurrentRevision(), defaultRev); Query query = db.CreateAllDocumentsQuery(); query.SetAllDocsMode(Query.AllDocsMode.ShowConflicts); QueryEnumerator rows = query.Run(); NUnit.Framework.Assert.AreEqual(rows.GetCount(), 1); QueryRow row = rows.GetRow(0); IList<SavedRevision> revs = row.GetConflictingRevisions(); NUnit.Framework.Assert.AreEqual(revs.Count, 2); NUnit.Framework.Assert.AreEqual(revs[0], defaultRev); NUnit.Framework.Assert.AreEqual(revs[1], otherRev); }
private void SaveLastSequence(SaveLastSequenceCompletionBlock completionHandler) { if (!lastSequenceChanged) { if (completionHandler != null) { completionHandler(); } return; } if (_savingCheckpoint) { // If a save is already in progress, don't do anything. (The completion block will trigger // another save after the first one finishes.) _overdueForSave = true; return; } lastSequenceChanged = false; _overdueForSave = false; Log.D(TAG, "saveLastSequence() called. lastSequence: " + LastSequence); var body = new Dictionary<String, Object>(); if (_remoteCheckpoint != null) { body.PutAll(_remoteCheckpoint); } body["lastSequence"] = LastSequence; var remoteCheckpointDocID = RemoteCheckpointDocID(); if (String.IsNullOrEmpty(remoteCheckpointDocID)) { Log.W(TAG, "remoteCheckpointDocID is null, aborting saveLastSequence()"); return; } _savingCheckpoint = true; SendAsyncRequest(HttpMethod.Put, "/_local/" + remoteCheckpointDocID, body, (result, e) => { _savingCheckpoint = false; if (e != null) { Log.V (TAG, "Unable to save remote checkpoint", e); } if (LocalDatabase == null) { Log.W(TAG, "Database is null, ignoring remote checkpoint response"); if (completionHandler != null) { completionHandler (); } return; } if (!LocalDatabase.Open()) { Log.W(TAG, "Database is closed, ignoring remote checkpoint response"); if (completionHandler != null) { completionHandler (); } return; } if (e != null) { switch (GetStatusFromError(e)) { case StatusCode.NotFound: _remoteCheckpoint = null; _overdueForSave = true; break; case StatusCode.Conflict: RefreshRemoteCheckpointDoc(); break; default: // TODO: On 401 or 403, and this is a pull, remember that remote // TODO: is read-only & don't attempt to read its checkpoint next time. break; } } else { Log.D(TAG, "Save checkpoint response: " + result.ToString()); var response = result.AsDictionary<string, object>(); body.Put ("_rev", response.Get ("rev")); _remoteCheckpoint = body; LocalDatabase.SetLastSequence(LastSequence, RemoteCheckpointDocID(), !IsPull); } if (_overdueForSave) { SaveLastSequence (completionHandler); } else if (completionHandler != null) { completionHandler (); } }); }
/// <summary> /// Gets or sets the userProperties of the <see cref="Couchbase.Lite.Revision"/>. /// </summary> /// <remarks> /// Gets or sets the userProperties of the <see cref="Couchbase.Lite.Revision"/>. /// Get, returns the properties of the <see cref="Couchbase.Lite.Revision"/> /// without any properties with keys prefixed with '_' (which contain Couchbase Lite data). /// Set, replaces all properties except for those with keys prefixed with '_'. /// </remarks> /// <value>The userProperties of the <see cref="Couchbase.Lite.Revision"/>.</value> public void SetUserProperties(IDictionary<String, Object> userProperties) { var newProps = new Dictionary<String, Object>(); newProps.PutAll(userProperties); foreach(string key in Properties.Keys) { if(key.StartsWith("_", StringComparison.InvariantCultureIgnoreCase)) { newProps[key] = properties.Get(key); } } // Preserve metadata properties properties = newProps; }