Ejemplo n.º 1
0
        /// <summary>
        /// Delete documents in bulk fashion.
        /// </summary>
        /// <param name="bulk">The bulk.</param>
        public void DeleteDocuments(ICanJson bulk)
        {
            try
            {
                var json = BigDDocument.WriteJson(bulk, false);

                var result = Request("_bulk_docs")
                             .Data(json)
                             .PostJson()
                             .Parse <JArray>();
                for (var i = 0; i < result.Count(); i++)
                {
                    //documents[i]._id= (result[i])["id"].Value<string>();
                    //documents[i]._rev = (result[i])["rev"].Value<string>();
                    if ((result[i])["error"] == null)
                    {
                        continue;
                    }
                    throw BigDException.Create(string.Format(CultureInfo.InvariantCulture,
                                                             "Document with id {0} was not deleted: {1}: {2}",
                                                             (result[i])["id"].Value <string>(), (result[i])["error"], (result[i])["reason"]));
                }
            }
            catch (WebException e)
            {
                throw BigDException.Create("Failed to bulk delete documents", e);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create or update a list of ICouchDocuments in CouchDB. Uses POST and CouchDB will
        /// allocate new ids if the documents lack them.
        /// </summary>
        /// <param name="documents">List of documents to store.</param>
        /// <param name="allOrNothing">if set to <c>true</c> [all or nothing].</param>
        /// <remarks>POST may be problematic in some environments.</remarks>
        public void SaveDocuments(IEnumerable <IBigDbDocument> documents, bool allOrNothing)
        {
            var bulk = new BigDBulkDocuments(documents);

            try
            {
                var json = BigDDocument.WriteJson(bulk, false);

                var result = Request("_bulk_docs")
                             .Data(json)
                             .Query("?all_or_nothing=" + allOrNothing.ToString().ToLower())
                             .PostJson()
                             .Parse <JArray>();

                int index = 0;
                foreach (var document in documents)
                {
                    document.Id  = (result[index])["id"].Value <string>();
                    document.Rev = (result[index])["rev"].Value <string>();
                    ++index;
                }
            }
            catch (WebException e)
            {
                throw BigDException.Create("Failed to create bulk documents", e);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the arbitrary documents.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="documentIds">The document ids.</param>
        /// <param name="ctor">The ctor.</param>
        /// <returns>IEnumerable{``0}.</returns>
        public IEnumerable <T> GetArbitraryDocuments <T>(IEnumerable <string> documentIds, Func <T> ctor)
        {
            var bulk = new BigDBulkKeys(documentIds.Cast <object>());
            var json = BigDDocument.WriteJson(bulk, false);

            return(QueryAllDocuments()
                   .Data(json)
                   .IncludeDocuments()
                   .GetResult()
                   .ArbitraryDocuments(ctor));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Gets the documents.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="documentIds">The document ids.</param>
        /// <returns>IEnumerable{``0}.</returns>
        public IEnumerable <T> GetDocuments <T>(IEnumerable <string> documentIds) where T : IBigDbDocument, new()
        {
            var bulk = new BigDBulkKeys(documentIds.Cast <object>());
            var json = BigDDocument.WriteJson(bulk, false);

            return(QueryAllDocuments()
                   .Data(json)
                   .IncludeDocuments()
                   .GetResult()
                   .Documents <T>());
        }
Ejemplo n.º 5
0
 public override void WriteJson(JsonWriter writer)
 {
     writer.WritePropertyName("docs");
     writer.WriteStartArray();
     foreach (IBigDbDocument doc in Docs)
     {
         writer.WriteStartObject();
         BigDDocument.WriteIdAndRev(doc, writer);
         writer.WritePropertyName("_deleted");
         writer.WriteValue(true);
         writer.WriteEndObject();
     }
     writer.WriteEndArray();
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Create a given ICouchDocument in CouchDB. Uses POST and CouchDB will allocate a new id and overwrite any existing id.
        /// </summary>
        /// <param name="document">Document to store.</param>
        /// <returns>Document with Id and Rev set.</returns>
        /// <remarks>POST which may be problematic in some environments.</remarks>
        public IBigDbDocument CreateDocument(IBigDbDocument document)
        {
            try
            {
                // Wulka mods
                var json = BigDDocument.WriteJson(document, false);
                // end Wulka mods

                JObject result = Request().Data(json).PostJson().Check("Failed to create document").Result();
                document.Id  = result["id"].Value <string>();
                document.Rev = result["rev"].Value <string>();
                return(document);
            }
            catch (WebException e)
            {
                throw BigDException.Create("Failed to create document", e);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Write a CouchDocument or ICouchDocument, it may already exist in db and will then be overwritten.
        /// </summary>
        /// <param name="document">Couch document</param>
        /// <param name="batch">True if we don't want to wait for flush (commit).</param>
        /// <returns>Couch Document with new Rev set.</returns>
        /// <remarks>This relies on the document to already have an id.</remarks>
        public IBigDbDocument WriteDocument(IBigDbDocument document, bool batch)
        {
            if (document.Id == null)
            {
                throw BigDException.Create(
                          "Failed to write document using PUT because it lacks an id, use CreateDocument instead to let CouchDB generate an id");
            }

            // Wulka additions

            var json = BigDDocument.WriteJson(document, false);

            // End Wulka additions


            JObject result =
                Request(document.Id).Query(batch ? "?batch=ok" : null).Data(json).Put().Check("Failed to write document").Result();

            document.Id  = result["id"].Value <string>();           // Not really needed
            document.Rev = result["rev"].Value <string>();

            return(document);
        }