public static extern bool c4doc_selectNextLeafRevision(C4Document *doc, [MarshalAs(UnmanagedType.U1)]bool includeDeleted, [MarshalAs(UnmanagedType.U1)]bool withBody, C4Error *outError);
public static extern int c4doc_insertRevision(C4Document *doc, C4Slice revID, C4Slice body, [MarshalAs(UnmanagedType.U1)]bool deleted, [MarshalAs(UnmanagedType.U1)]bool hasAttachments, [MarshalAs(UnmanagedType.U1)]bool allowConflict, C4Error *outError);
private bool SaveDocument(C4Document *doc, string revId, IDictionary<string, object> properties) { // Is the new revision the winner? Native.c4doc_selectCurrentRevision(doc); bool isWinner = (string)doc->selectedRev.revID == revId; // Update the documentType: if (isWinner) { var type = properties == null ? null : properties.GetCast<string>("type"); ForestDBBridge.Check(err => Native.c4doc_setType(doc, type, err)); } // Save: ForestDBBridge.Check(err => Native.c4doc_save(doc, (uint)MaxRevTreeDepth, err)); return isWinner; }
public static extern bool c4indexer_emit(C4Indexer *indexer, C4Document *document, uint viewNumber, uint emitCount, C4Key** emittedKeys, C4Slice* emittedValues, C4Error *outError);
public static extern int c4doc_purgeRevision(C4Document *doc, C4Slice revId, C4Error *outError);
public static int c4doc_purgeRevision(C4Document *doc, string revId, C4Error *outError) { using(var revId_ = new C4String(revId)) { return c4doc_purgeRevision(doc, revId_.AsC4Slice(), outError); } }
/// <summary> /// Returns the document type (as set by setDocType.) This value is ignored by CBForest itself; by convention /// Couchbase Lite sets it to the value of the current revision's "type" property, and uses it as an optimization /// when indexing a view. /// </summary> /// <param name="doc">The document to operate on</param> /// <returns>The document type of the document in question</returns> public static string c4doc_getType(C4Document *doc) { return BridgeSlice(() => _c4doc_getType(doc)); }
/// <summary> /// Adds a revision to a document, plus its ancestors (given in reverse chronological order.) /// On success, the new revision will be selected. /// Must be called within a transaction. /// <param name="doc">The document to operate on</param> /// <param name="body">The (JSON) body of the revision</param> /// <param name="deleted">True if this revision is a deletion (tombstone)</param> /// <param name="hasAttachments">True if this revision contains an _attachments dictionary</param> /// <param name="history">The ancestors' revision IDs, starting with the parent, in reverse order</param> /// <param name="outError">The error that occurred if the operation doesn't succeed</param> /// <returns>The number of revisions added to the document, or -1 on error.</returns> public static int c4doc_insertRevisionWithHistory(C4Document *doc, string body, bool deleted, bool hasAttachments, string[] history, C4Error *outError) { var flattenedStringArray = new C4String[history.Length + 1]; flattenedStringArray[0] = new C4String(body); for(int i = 0; i < history.Length; i++) { flattenedStringArray[i + 1] = new C4String(history[i]); } var sliceArray = flattenedStringArray.Skip(1).Select<C4String, C4Slice>(x => x.AsC4Slice()).ToArray(); var retVal = default(int); fixed(C4Slice *a = sliceArray) { retVal = c4doc_insertRevisionWithHistory(doc, flattenedStringArray[0].AsC4Slice(), deleted, hasAttachments, a, (uint)history.Length, outError); } foreach(var s in flattenedStringArray) { s.Dispose(); } return retVal; }
public static void c4doc_free(C4Document *doc) { #if DEBUG var ptr = (IntPtr)doc; #if ENABLE_LOGGING if(ptr != IntPtr.Zero && !_AllocatedObjects.ContainsKey(ptr)) { Console.WriteLine("WARNING: [c4doc_free] freeing object 0x{0} that was not found in allocated list", ptr.ToString("X")); } else { #endif _AllocatedObjects.Remove(ptr); #if ENABLE_LOGGING } #endif #endif _c4doc_free(doc); }
public static extern C4Slice _c4doc_getType(C4Document *doc);
private static extern void _c4doc_free(C4Document *doc);
public static bool c4indexer_emit(C4Indexer *indexer, C4Document *document, uint viewNumber, C4Key*[] emittedKeys, string[] emittedValues, C4Error *outError) { var c4StringArr = emittedValues.Select(x => new C4String(x)).ToArray(); var sliceArr = c4StringArr.Select(x => x.AsC4Slice()).ToArray(); var retVal = c4indexer_emit(indexer, document, viewNumber, emittedKeys, sliceArr, outError); foreach(var c4str in c4StringArr) { c4str.Dispose(); } return retVal; }
/// <summary> /// Emits new keys/values derived from one document, for one view. /// This function needs to be called once for each(document, view) pair.Even if the view's map /// function didn't emit anything, the old keys/values need to be cleaned up. /// /// Values are uninterpreted by CBForest, but by convention are JSON. A special value "*" /// (a single asterisk) is used as a placeholder for the entire document. /// /// </summary> /// <param name="indexer">The indexer to operate on</param> /// <param name="document">The document being indexed</param> /// <param name="viewNumber">The position of the view in the indexer's views[] array</param> /// <param name="emittedKeys">Array of keys being emitted</param> /// <param name="emittedValues">Array of values being emitted. (JSON by convention.)</param> /// <param name="outError">The error that occurred if the operation doesn't succeed</param> /// <returns>true on success, false otherwise</returns> public static bool c4indexer_emit(C4Indexer *indexer, C4Document *document, uint viewNumber, C4Key*[] emittedKeys, C4Slice[] emittedValues, C4Error *outError) { fixed(C4Key** keysPtr = emittedKeys) fixed(C4Slice* valuesPtr = emittedValues) { return c4indexer_emit(indexer, document, viewNumber, (uint)emittedKeys.Length, keysPtr, valuesPtr, outError); } }
/// <summary> /// Adds a revision to a document, as a child of the currently selected revision /// (or as a root revision if there is no selected revision.) /// On success, the new revision will be selected. /// Must be called within a transaction. /// </summary> /// <param name="doc">The document to operate on</param> /// <param name="revID">The ID of the revision being inserted</param> /// <param name="body">The (JSON) body of the revision</param> /// <param name="deleted">True if this revision is a deletion (tombstone)</param> /// <param name="hasAttachments">True if this revision contains an _attachments dictionary</param> /// <param name="allowConflict">If false, and the parent is not a leaf, a 409 error is returned</param> /// <param name="outError">The error that occurred if the operation doesn't succeed</param> /// <returns>The number of revisions inserted (0, 1, or -1 on error)</returns> public static int c4doc_insertRevision(C4Document *doc, string revID, string body, bool deleted, bool hasAttachments, bool allowConflict, C4Error *outError) { using(var revID_ = new C4String(revID)) using(var body_ = new C4String(body)) { return c4doc_insertRevision(doc, revID_.AsC4Slice(), body_.AsC4Slice(), deleted, hasAttachments, allowConflict, outError); } }
public static extern bool c4doc_selectRevision(C4Document *doc, C4Slice revID, [MarshalAs(UnmanagedType.U1)]bool withBody, C4Error *outError);
public static extern int c4doc_insertRevisionWithHistory(C4Document *doc, C4Slice body, [MarshalAs(UnmanagedType.U1)]bool deleted, [MarshalAs(UnmanagedType.U1)]bool hasAttachments, C4Slice* history, uint historyCount, C4Error *outError);
/// <summary> /// Selects a specific revision of a document (or no revision, if revID is NULL.) /// </summary> /// <param name="doc">The document to operate on</param> /// <param name="revID">The revID of the revision to select</param> /// <param name="withBody">Whether or not to load the body of the revision</param> /// <param name="outError">The error that occurred if the operation doesn't succeed</param> /// <returns>true on success, false otherwise</returns> public static bool c4doc_selectRevision(C4Document *doc, string revID, bool withBody, C4Error *outError) { using(var revID_ = new C4String(revID)) { return c4doc_selectRevision(doc, revID_.AsC4Slice(), withBody, outError); } }
public static extern bool c4doc_setType(C4Document *doc, C4Slice docType, C4Error *outError);
public static extern bool c4doc_loadRevisionBody(C4Document *doc, C4Error *outError);
/// <summary> /// Sets a document's docType. (By convention this is the value of the "type" property of the /// current revision's JSON; this value can be used as optimization when indexing a view.) /// The change will not be persisted until the document is saved. /// </summary> /// <param name="doc">The document to operate on</param> /// <param name="docType">The document type to set</param> /// <param name="outError">The error that occurred if the operation doesn't succeed</param> /// <returns>true on success, false otherwise</returns> public static bool c4doc_setType(C4Document *doc, string docType, C4Error *outError) { using(var docType_ = new C4String(docType)) { return c4doc_setType(doc, docType_.AsC4Slice(), outError); } }
public static extern bool c4doc_hasRevisionBody(C4Document *doc);
public static extern bool c4doc_save(C4Document *doc, uint maxRevTreeDepth, C4Error *outError);
public static extern bool c4doc_selectNextRevision(C4Document *doc);
private DocumentChange ChangeWithNewRevision(RevisionInternal inRev, bool isWinningRev, C4Document *doc, Uri source) { var winningRevId = default(string); if(isWinningRev) { winningRevId = inRev.GetRevId(); } else { winningRevId = (string)doc->revID; } return new DocumentChange(inRev, winningRevId, doc->IsConflicted, source); }
private bool SaveDocument(C4Document* doc, RevisionID revId, IDictionary<string, object> properties) { // Is the new revision the winner? var winningRevID = doc->revID.AsRevID(); bool isWinner = winningRevID.Equals(revId); // Update the documentType: if(!isWinner) { Native.c4doc_selectCurrentRevision(doc); properties = Manager.GetObjectMapper().ReadValue<IDictionary<string, object>>(doc->selectedRev.body); } Native.c4doc_setType(doc, properties?.GetCast<string>("type")); // Save: ForestDBBridge.Check(err => Native.c4doc_save(doc, (uint)MaxRevTreeDepth, err)); return isWinner; }