Пример #1
0
        ///<summary>
        /// Translate a collection of RavenJObject to JsonDocuments
        ///</summary>
        public static IEnumerable <JsonDocument> RavenJObjectsToJsonDocuments(IEnumerable <RavenJObject> responses)
        {
            var list = new List <JsonDocument>();

            foreach (var doc in responses)
            {
                if (doc == null)
                {
                    list.Add(null);
                    continue;
                }
                var metadata = (RavenJObject)doc["@metadata"];
                doc.Remove("@metadata");
                var key = Extract(metadata, "@id", string.Empty);

                var lastModified = GetLastModified(metadata);

                var etag = Extract(metadata, "@etag", Guid.Empty, (string g) => HttpExtensions.EtagHeaderToGuid(g));
                var nai  = Extract(metadata, "Non-Authoritative-Information", false, (string b) => Convert.ToBoolean(b));
                list.Add(new JsonDocument
                {
                    Key            = key,
                    LastModified   = lastModified,
                    Etag           = etag,
                    TempIndexScore = metadata == null ? null : metadata.Value <float?>(Constants.TemporaryScoreValue),
                    NonAuthoritativeInformation = nai,
                    Metadata   = metadata.FilterHeaders(),
                    DataAsJson = doc,
                });
            }
            return(list);
        }
Пример #2
0
        /// <summary>
        /// Deserialize a request to a JsonDocument
        /// </summary>
        public static JsonDocumentMetadata DeserializeJsonDocumentMetadata(string key,
                                                                           NameValueCollection headers,
                                                                           HttpStatusCode statusCode)
        {
            RavenJObject meta = null;

            try
            {
                meta = headers.FilterHeadersToObject();
            }
            catch (JsonReaderException jre)
            {
                throw new JsonReaderException("Invalid Json Response", jre);
            }
            var    etag             = headers[Constants.MetadataEtagField];
            string lastModified     = headers[Constants.RavenLastModified] ?? headers[Constants.LastModified];
            var    dateTime         = DateTime.ParseExact(lastModified, new[] { "o", "r" }, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
            var    lastModifiedDate = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);

            return(new JsonDocumentMetadata
            {
                NonAuthoritativeInformation = statusCode == HttpStatusCode.NonAuthoritativeInformation,
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = lastModifiedDate,
                Metadata = meta
            });
        }
Пример #3
0
        public static JsonDocument RavenJObjectToJsonDocument(RavenJObject doc)
        {
            var metadata = (RavenJObject)doc["@metadata"];

            doc.Remove("@metadata");
            var key = Extract(metadata, "@id", string.Empty);

            if (string.IsNullOrEmpty(key))
            {
                // projection, it seems.
                return(new JsonDocument
                {
                    Key = string.Empty,
                    DataAsJson = doc,
                    LastModified = SystemTime.UtcNow,
                });
            }

            var lastModified = GetLastModified(metadata);

            metadata[Constants.Headers.LastModified] = lastModified;
            var etag = Extract(metadata, "@etag", 0, (string g) => HttpExtensions.EtagHeaderToEtag(g));

            var jsonDocument = new JsonDocument
            {
                Key            = key,
                LastModified   = lastModified,
                Etag           = etag,
                TempIndexScore = metadata == null ? null : metadata.Value <float?>(Constants.Indexing.Fields.IndexFieldScoreName),
                Metadata       = metadata.FilterHeadersToObject(),
                DataAsJson     = doc
            };

            return(jsonDocument);
        }
Пример #4
0
        /// <summary>
        /// Deserialize a request to a JsonDocument
        /// </summary>
        public static JsonDocumentMetadata DeserializeJsonDocumentMetadata(string key,
                                                                           HttpResponseHeaders headers,
                                                                           HttpStatusCode statusCode)
        {
            RavenJObject metadata = null;

            try
            {
                metadata = headers.FilterHeadersToObject();
            }
            catch (JsonReaderException jre)
            {
                throw new JsonReaderException("Invalid Json Response", jre);
            }
            var    etag             = headers.ETag.Tag;
            string lastModified     = headers.GetFirstValue(Constants.Headers.RavenLastModified) ?? headers.GetFirstValue(Constants.Headers.LastModified);
            var    dateTime         = DateTime.ParseExact(lastModified, new[] { "o", "r" }, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);
            var    lastModifiedDate = DateTime.SpecifyKind(dateTime, DateTimeKind.Utc);

            metadata[Constants.Headers.LastModified] = lastModified;

            return(new JsonDocumentMetadata
            {
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = lastModifiedDate,
                Metadata = metadata
            });
        }
Пример #5
0
        public static JsonDocument RavenJObjectToJsonDocument(RavenJObject doc)
        {
            var metadata = (RavenJObject)doc["@metadata"];

            doc.Remove("@metadata");
            var key = Extract(metadata, "@id", string.Empty);

            if (string.IsNullOrEmpty(key))
            {
                // projection, it seems.
                return(new JsonDocument
                {
                    DataAsJson = doc,
                    LastModified = SystemTime.UtcNow,
                });
            }

            var lastModified = GetLastModified(metadata);

            var etag = Extract(metadata, "@etag", Etag.Empty, (string g) => HttpExtensions.EtagHeaderToEtag(g));
            var nai  = Extract(metadata, "Non-Authoritative-Information", false, (string b) => Convert.ToBoolean(b));

            var jsonDocument = new JsonDocument
            {
                Key            = key,
                LastModified   = lastModified,
                Etag           = etag,
                TempIndexScore = metadata == null ? null : metadata.Value <float?>(Constants.TemporaryScoreValue),
                NonAuthoritativeInformation = nai,
                Metadata   = metadata.FilterHeaders(),
                DataAsJson = doc,
            };

            return(jsonDocument);
        }
Пример #6
0
        public static JsonDocument DeserializeJsonDocument(string key, RavenJToken requestJson,
                                                           NameValueCollection headers,
                                                           HttpStatusCode statusCode)
        {
            var jsonData = (RavenJObject)requestJson;
            var meta     = headers.FilterHeadersToObject();

            var etag = headers[Constants.MetadataEtagField];

            return(new JsonDocument
            {
                DataAsJson = jsonData,
                NonAuthoritativeInformation = statusCode == HttpStatusCode.NonAuthoritativeInformation,
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = GetLastModifiedDate(headers),
                Metadata = meta
            });
        }
Пример #7
0
        /// <summary>
        /// Deserialize a request to a JsonDocument
        /// </summary>
        public static JsonDocument DeserializeJsonDocument(string key, RavenJToken requestJson,
                                                           HttpResponseHeaders headers,
                                                           HttpStatusCode statusCode)
        {
            var jsonData = (RavenJObject)requestJson;
            var metadata = headers.FilterHeadersToObject();

            var etag         = headers.ETag.Tag;
            var lastModified = GetLastModifiedDate(headers);

            metadata[Constants.Headers.LastModified] = lastModified;

            return(new JsonDocument
            {
                DataAsJson = jsonData,
                Key = key,
                Etag = HttpExtensions.EtagHeaderToEtag(etag),
                LastModified = lastModified,
                Metadata = metadata
            });
        }
Пример #8
0
        public async Task TryResolveConflictOrCreateConcurrencyException(GetResponse[] responses, Func <string, RavenJObject, Etag, Task <ConflictException> > tryResolveConflictOrCreateConcurrencyException)
        {
            // ReSharper disable once ForCanBeConvertedToForeach
            // see: http://ayende.com/blog/169377/the-bug-that-ruined-my-weekend
            for (int index = 0; index < responses.Length; index++)
            {
                var response = responses[index];
                if (response == null)
                {
                    continue;
                }
                if (response.RequestHasErrors() && response.Status != 409)
                {
                    continue;
                }

                var result = response.Result as RavenJObject;
                if (result == null)
                {
                    continue;
                }

                if (result.ContainsKey("Results"))
                {
                    var results = result["Results"] as RavenJArray;
                    if (results == null)
                    {
                        continue;
                    }

                    foreach (RavenJToken value in results)
                    {
                        var docResult = value as RavenJObject;
                        if (docResult == null)
                        {
                            return;
                        }

                        var metadata = docResult[Constants.Metadata];
                        if (metadata == null)
                        {
                            return;
                        }

                        if (metadata.Value <int>("@Http-Status-Code") != 409)
                        {
                            return;
                        }

                        var id   = metadata.Value <string>("@id");
                        var etag = HttpExtensions.EtagHeaderToEtag(metadata.Value <string>("@etag"));

                        await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                            tryResolveConflictOrCreateConcurrencyException,
                            id,
                            etag,
                            docResult,
                            response);
                    }

                    continue;
                }
                if (result.ContainsKey("Conflicts"))
                {
                    var id   = response.Headers[Constants.DocumentIdFieldName];
                    var etag = response.GetEtagHeader();

                    await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                        tryResolveConflictOrCreateConcurrencyException,
                        id,
                        etag,
                        result,
                        response);
                }
            }
        }
Пример #9
0
        public async Task TryResolveConflictOrCreateConcurrencyException(GetResponse[] responses, Func <string, RavenJObject, Etag, Task <ConflictException> > tryResolveConflictOrCreateConcurrencyException)
        {
            foreach (var response in responses)
            {
                if (response == null)
                {
                    continue;
                }
                if (response.RequestHasErrors() && response.Status != 409)
                {
                    continue;
                }

                var result = response.Result as RavenJObject;
                if (result == null)
                {
                    continue;
                }

                if (result.ContainsKey("Results"))
                {
                    var results = result["Results"] as RavenJArray;
                    if (results == null)
                    {
                        continue;
                    }

                    foreach (RavenJToken value in results)
                    {
                        var docResult = value as RavenJObject;
                        if (docResult == null)
                        {
                            return;
                        }

                        var metadata = docResult[Constants.Metadata];
                        if (metadata == null)
                        {
                            return;
                        }

                        if (metadata.Value <int>("@Http-Status-Code") != 409)
                        {
                            return;
                        }

                        var id   = metadata.Value <string>("@id");
                        var etag = HttpExtensions.EtagHeaderToEtag(metadata.Value <string>("@etag"));

                        await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                            tryResolveConflictOrCreateConcurrencyException,
                            id,
                            etag,
                            docResult,
                            response);
                    }

                    continue;
                }
                if (result.ContainsKey("Conflicts"))
                {
                    var id   = response.Headers[Constants.DocumentIdFieldName];
                    var etag = response.GetEtagHeader();

                    await TryResolveConflictOrCreateConcurrencyExceptionForSingleDocument(
                        tryResolveConflictOrCreateConcurrencyException,
                        id,
                        etag,
                        result,
                        response);
                }
            }
        }