Ejemplo n.º 1
0
        public async Task <StoreResponse> Store(string bucketName, SiaqodbDocument document)
        {
            using (var client = new MyCouchClient(DbServerUrl, bucketName))
            {
                await CheckTagsViews(client, bucketName, document.Tags);

                CouchDBDocument doc           = Mapper.ToCouchDBDoc(document);
                var             serializedObj = client.Serializer.Serialize <CouchDBDocument>(doc);

                var response = await client.Documents.PostAsync(serializedObj);

                if (response.IsSuccess)
                {
                    var cnorResponse = new StoreResponse();
                    cnorResponse.Version = response.Rev;
                    cnorResponse.Key     = response.Id;

                    await this.StartRebuildViews(client, document);

                    return(cnorResponse);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw new BucketNotFoundException(bucketName);
                }
                else if (response.StatusCode == System.Net.HttpStatusCode.Conflict)
                {
                    throw new ConflictException(response.Reason);
                }
                else
                {
                    throw new GenericCouchDBException(response.Reason, response.StatusCode);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <StoreResponse> Store(string bucketName, SiaqodbDocument document)
        {
            var db = client.GetDatabase(DbName);

            var collection = db.GetCollection <BsonDocument>(bucketName);
            var newDoc     = Mapper.ToBsonDoc(document);

            newDoc["_rev"] = this.GenerateNewVersion();
            try
            {
                var result = await collection.ReplaceOneAsync(filter : new BsonDocument {
                    { "_id", document.Key }, { "_rev", BsonTypeMapper.MapToBsonValue(document.Version) }
                },
                                                              options : new UpdateOptions {
                    IsUpsert = true
                },
                                                              replacement : newDoc);

                var cnorResponse = new StoreResponse();
                cnorResponse.Version = newDoc["_rev"].AsString;
                cnorResponse.Key     = document.Key;
                return(cnorResponse);
            }
            catch (MongoWriteException ex)
            {
                if (ex.Message.Contains("duplicate key"))//conflict
                {
                    throw new ConflictException("There is a document with the same key and another version already stored");
                }
                throw ex;
            }
        }
        public static BsonDocument ToBsonDoc(SiaqodbDocument cobj)
        {
            var doc = new BsonDocument();

            doc["_id"]  = cobj.Key;
            doc["_rev"] = BsonTypeMapper.MapToBsonValue(cobj.Version);
            doc["doc"]  = cobj.Content;
            foreach (string tagName in cobj.Tags.Keys)
            {
                doc[tagName] = BsonTypeMapper.MapToBsonValue(cobj.Tags[tagName]);
            }
            return(doc);
        }
Ejemplo n.º 4
0
 private async Task StartRebuildViews(MyCouchClient client, SiaqodbDocument crObjForUpdateViews)
 {
     //force building the index at Store time avoiding to wait on Query time
     if (crObjForUpdateViews.Tags != null)
     {
         foreach (string tagName in crObjForUpdateViews.Tags.Keys)
         {
             string           viewName = "tags_" + tagName;
             QueryViewRequest query    = new QueryViewRequest(viewName, viewName);
             query.Stale = Stale.UpdateAfter;
             query.Limit = 1;
             var res = await client.Views.QueryAsync(query);
         }
     }
 }
Ejemplo n.º 5
0
        public static CouchDBDocument ToCouchDBDoc(SiaqodbDocument cobj)
        {
            var doc = new CouchDBDocument()
            {
                _id = cobj.Key, doc = cobj.Content, tags = cobj.Tags
            };

            if (cobj.Version == string.Empty)
            {
                doc._rev = null;
            }
            else
            {
                doc._rev = cobj.Version;
            }
            return(doc);
        }
Ejemplo n.º 6
0
        private void AddChangedDoc(BatchSet bs, BsonDocument doc, SyncLogItem logItem, Filter query)
        {
            if (bs.ChangedDocuments == null)
            {
                bs.ChangedDocuments = new List <SiaqodbDocument>();
            }

            var nestedDoc = doc["o"].AsBsonDocument;

            if (logItem != null && logItem.KeyVersion != null && logItem.KeyVersion.ContainsKey(nestedDoc["_id"].AsString) && logItem.KeyVersion[nestedDoc["_id"].AsString] == nestedDoc["_rev"].AsString)
            {
                return;
            }
            if (OutOfFilter(query, nestedDoc))
            {
                return;
            }
            SiaqodbDocument siaqodbDoc = Mapper.ToSiaqodbDocument(nestedDoc);

            bs.ChangedDocuments.Add(siaqodbDoc);
        }
Ejemplo n.º 7
0
        public async Task <BatchResponse> Store(string bucketName, BatchSet batch)
        {
            using (var client = new MyCouchClient(DbServerUrl, bucketName))
            {
                var dbExists = await client.Database.HeadAsync();

                if (dbExists.IsSuccess)
                {
                    BulkRequest bulkRequest = new BulkRequest();

                    DateTime        start = DateTime.Now;
                    int             size  = 0;
                    SiaqodbDocument crObjForUpdateViews = null;
                    if (batch.ChangedDocuments != null)
                    {
                        foreach (SiaqodbDocument obj in batch.ChangedDocuments)
                        {
                            if (obj != null)
                            {
                                if (crObjForUpdateViews == null)
                                {
                                    crObjForUpdateViews = obj;
                                }

                                await CheckTagsViews(client, bucketName, obj.Tags);

                                CouchDBDocument doc = Mapper.ToCouchDBDoc(obj);
                                var             serializedObject = client.Serializer.Serialize <CouchDBDocument>(doc);
                                bulkRequest.Include(serializedObject);
                                size += serializedObject.Length;
                            }
                        }
                    }
                    if (batch.DeletedDocuments != null)
                    {
                        foreach (DeletedDocument obj in batch.DeletedDocuments)
                        {
                            if (obj != null)
                            {
                                if (obj.Version != null)//otherwise means is a non-existing object
                                {
                                    bulkRequest.Delete(obj.Key, obj.Version);
                                }
                            }
                        }
                    }
                    var response = await client.Documents.BulkAsync(bulkRequest);

                    if (response.IsSuccess)
                    {
                        var cnorResponse = new BatchResponse();
                        if (response.Rows != null)
                        {
                            cnorResponse.BatchItemResponses = new List <BatchItemResponse>();
                            SyncLogItem syncLogItem = new SyncLogItem();
                            syncLogItem.KeyVersion = new Dictionary <string, string>();
                            foreach (var row in response.Rows)
                            {
                                BatchItemResponse wresp = new BatchItemResponse();
                                if (!string.IsNullOrEmpty(row.Error))
                                {
                                    cnorResponse.ItemsWithErrors++;
                                }
                                wresp.Error     = row.Error;
                                wresp.ErrorDesc = row.Reason;
                                wresp.Key       = row.Id;
                                wresp.Version   = row.Rev;
                                cnorResponse.BatchItemResponses.Add(wresp);
                                if (string.IsNullOrEmpty(row.Error))
                                {
                                    syncLogItem.KeyVersion.Add(row.Id, row.Rev);
                                }
                            }
                            if (syncLogItem.KeyVersion.Count > 0)
                            {
                                syncLogItem.TimeInserted = DateTime.UtcNow;
                                using (var clientLog = new MyCouchClient(DbServerUrl, SyncLogBucket))
                                {
                                    string serLogItem = Newtonsoft.Json.JsonConvert.SerializeObject(syncLogItem);
                                    var    logResp    = await clientLog.Documents.PostAsync(serLogItem);

                                    cnorResponse.UploadAnchor = logResp.Id;
                                }
                            }
                        }
                        if (crObjForUpdateViews != null)
                        {
                            await this.StartRebuildViews(client, crObjForUpdateViews);
                        }

                        return(cnorResponse);
                    }
                    else
                    {
                        CheckBucketNotFound(bucketName, response);
                    }
                }
                else if (dbExists.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    throw new BucketNotFoundException(bucketName);
                }
                return(null);
            }
        }
Ejemplo n.º 8
0
        public async Task <StoreResponse> Post(string bucketName, [FromBody] SiaqodbDocument value)
        {
            var result = await repository.Store(bucketName, value);

            return(result);
        }
Ejemplo n.º 9
0
 public async Task <StoreResponse> Put(string bucketName, string key, [FromBody] SiaqodbDocument value)
 {
     value.Key = key;
     return(await repository.Store(bucketName, value));
 }