Esempio n. 1
0
 public static SiaqodbDocument ToSiaqodbDocument(CouchDBDocument cobj)
 {
     return(new SiaqodbDocument()
     {
         Key = cobj._id, Version = cobj._rev, Content = cobj.doc, Tags = cobj.tags
     });
 }
Esempio n. 2
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);
                }
            }
        }
Esempio n. 3
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;
 }
Esempio n. 4
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);
        }
Esempio n. 5
0
        private bool OutOfFilter(Filter query, CouchDBDocument co)
        {
            if (query == null)
            {
                return(false);
            }
            string tagName = query.TagName;

            if (query.TagName == "key")
            {
                tagName = "_id";
            }
            else if (!co.tags.Keys.Contains(tagName))
            {
                return(true);
            }
            IComparable docValue = null;

            if (query.TagName == "key")
            {
                docValue = co._id;
            }
            else
            {
                docValue = co.tags[tagName] as IComparable;
            }

            if (query.Value != null && docValue.CompareTo((IComparable)query.Value) != 0)
            {
                return(true);
            }
            if (query.Start != null && docValue.CompareTo((IComparable)query.Start) < 0)
            {
                return(true);
            }
            if (query.End != null && docValue.CompareTo((IComparable)query.End) > 0)
            {
                return(true);
            }


            return(false);
        }
Esempio n. 6
0
        private void AddChangedDoc(BatchSet changeSet, ChangesResponse <string> .Row row, SyncLogItem logItem, Filter query, MyCouch.Serialization.ISerializer serializer)
        {
            if (changeSet.ChangedDocuments == null)
            {
                changeSet.ChangedDocuments = new List <SiaqodbDocument>();
            }
            CouchDBDocument co = serializer.Deserialize <CouchDBDocument>(row.IncludedDoc);

            if (co._id.StartsWith("_design/"))
            {
                return;
            }
            //check uploaded anchor- means cliet just uploaded this record and we should not return back
            if (logItem != null && logItem.KeyVersion != null && logItem.KeyVersion.ContainsKey(co._id) && logItem.KeyVersion[co._id] == co._rev)
            {
                return;
            }
            if (OutOfFilter(query, co))
            {
                return;
            }
            changeSet.ChangedDocuments.Add(Mapper.ToSiaqodbDocument(co));
        }
Esempio 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);
            }
        }
Esempio n. 8
0
        public async Task <StoreResponse> Delete(string bucketName, string key, string version)
        {
            using (var client = new MyCouchClient(DbServerUrl, bucketName))
            {
                if (version == null)
                {
                    var response = await client.Documents.GetAsync(key);

                    if (response.IsSuccess)
                    {
                        if (response.Content != null)
                        {
                            CouchDBDocument doc = client.Serializer.Deserialize <CouchDBDocument>(response.Content);
                            version = doc._rev;
                        }
                    }
                    else if (response.StatusCode == System.Net.HttpStatusCode.NotFound)
                    {
                        if (response.Reason == "no_db_file")
                        {
                            throw new BucketNotFoundException(bucketName);
                        }
                        else
                        {
                            throw new DocumentNotFoundException(key, version);
                        }
                    }
                    else
                    {
                        throw new GenericCouchDBException(response.Reason, response.StatusCode);
                    }
                }
                var deletedResponse = await client.Documents.DeleteAsync(key, version);

                if (deletedResponse.IsSuccess)
                {
                    return(new StoreResponse()
                    {
                        Key = deletedResponse.Id, Version = deletedResponse.Rev
                    });
                }
                else if (deletedResponse.StatusCode == System.Net.HttpStatusCode.NotFound)
                {
                    if (deletedResponse.Reason == "no_db_file")
                    {
                        throw new BucketNotFoundException(bucketName);
                    }
                    else
                    {
                        throw new DocumentNotFoundException(key, version);
                    }
                }
                else if (deletedResponse.StatusCode == System.Net.HttpStatusCode.BadRequest)
                {
                    throw new InvalidVersionFormatException();
                }
                else
                {
                    throw new GenericCouchDBException(deletedResponse.Reason, deletedResponse.StatusCode);
                }
            }
        }
Esempio n. 9
0
 public static SiaqodbDocument ToSiaqodbDocument(CouchDBDocument cobj)
 {
     return new SiaqodbDocument() { Key = cobj._id, Version = cobj._rev, Content = cobj.doc, Tags = cobj.tags };
 }
Esempio n. 10
0
        private bool OutOfFilter(Filter query, CouchDBDocument co)
        {
            if (query == null)
                return false;
            string tagName = query.TagName;
            if (query.TagName == "key")
            {
                tagName = "_id";
            }
            else if (!co.tags.Keys.Contains(tagName))
            {
                return true;
            }
            IComparable docValue = null;
            if (query.TagName == "key")
            {
                docValue = co._id;
            }
            else
            {
                docValue = co.tags[tagName] as IComparable;
            }
            
            if (query.Value != null && docValue.CompareTo((IComparable)query.Value) != 0)
            {
                return true;
            }
            if (query.Start != null && docValue.CompareTo((IComparable)query.Start) < 0)
                return true;
            if (query.End != null && docValue.CompareTo((IComparable)query.End) > 0)
                return true;


            return false;
        }