Exemple #1
0
        /// <summary>
        /// Using the bulk API for the loading of documents.
        /// </summary>
        /// <param name="docs"></param>
        /// <remarks>Here we assume you have either added the correct rev, id, or _deleted attribute to each document.  The response will indicate if there were any errors.
        /// Please note that the max_document_size configuration variable in CouchDB limits the maximum request size to CouchDB.</remarks>
        /// <returns>JSON of updated documents in the BulkDocumentResponse class.  </returns>
        public BulkDocumentResponses SaveDocuments(Documents docs, bool all_or_nothing)
        {
            string uri = databaseBaseUri + "/_bulk_docs";

            string data = Newtonsoft.Json.JsonConvert.SerializeObject(docs);

            if (all_or_nothing == true)
            {
                uri = uri + "?all_or_nothing=true";
            }

            HttpWebResponse resp = GetRequest(uri).Post().Json().Data(data).GetResponse();

            if (resp == null)
            {
                throw new System.Exception("Response returned null.");
            }

            if (resp.StatusCode != HttpStatusCode.Created)
            {
                throw new System.Exception("Response returned with a HTTP status code of " + resp.StatusCode + " - " + resp.StatusDescription);
            }

            // Get response
            string x = resp.GetResponseString();

            // Convert to Bulk response
            BulkDocumentResponses bulk = Newtonsoft.Json.JsonConvert.DeserializeObject <BulkDocumentResponses>(x);

            return(bulk);
        }
        public BulkResponse Execute()
        {
            // This method divid the update to chunks because services such as Cloudant
            // recommend to limit the number of bulk documents to around 500 docs.

            List <BulkResponseRow> responses = new List <BulkResponseRow>(docsToUpdate.Count);

            foreach (JObject[] updateChunk in docsToUpdate.Chunks(BulkChunkSize))
            {
                Documents docs = new Documents();
                docs.Values.AddRange(updateChunk.Select(x => new Document(x)));

                BulkDocumentResponses         bulkResponse         = couchDB.SaveDocuments(docs, allOrNothing);
                IEnumerable <BulkResponseRow> abstractResponseRows =
                    bulkResponse.Select(x => new BulkResponseRow(x.Id, x.Rev, x.Error, x.Reason));

                responses.AddRange(abstractResponseRows);
            }


            return(new BulkResponse(responses.ToArray()));
        }