示例#1
0
        private IndexDefinition DirectGetIndex(string indexName, string operationUrl)
        {
            var httpJsonRequest = HttpJsonRequest.CreateHttpJsonRequest(this, operationUrl + "/indexes/" + indexName + "?definition=yes", "GET", credentials);

            httpJsonRequest.AddOperationHeaders(OperationsHeaders);
            string indexDefAsString;

            try
            {
                indexDefAsString = httpJsonRequest.ReadResponseString();
            }
            catch (WebException e)
            {
                var httpWebResponse = e.Response as HttpWebResponse;
                if (httpWebResponse != null &&
                    httpWebResponse.StatusCode == HttpStatusCode.NotFound)
                {
                    return(null);
                }
                throw;
            }
            var indexDefResultAsJson = JObject.Load(new JsonTextReader(new StringReader(indexDefAsString)));

            return(convention.CreateSerializer().Deserialize <IndexDefinition>(
                       new JTokenReader(indexDefResultAsJson["Index"])
                       ));
        }
示例#2
0
        /// <summary>
        /// Begins the async query.
        /// </summary>
        /// <param name="index">The index.</param>
        /// <param name="query">The query.</param>
        /// <param name="includes">The include paths</param>
        public Task <QueryResult> QueryAsync(string index, IndexQuery query, string[] includes)
        {
            EnsureIsNotNullOrEmpty(index, "index");
            var path = query.GetIndexQueryUrl(url, index, "indexes");

            if (includes != null && includes.Length > 0)
            {
                path += "&" + string.Join("&", includes.Select(x => "include=" + x).ToArray());
            }
            var request = HttpJsonRequest.CreateHttpJsonRequest(this, path, "GET", credentials, convention);

            return(Task.Factory.FromAsync <string>(request.BeginReadResponseString, request.EndReadResponseString, null)
                   .ContinueWith(task =>
            {
                JToken json;
                using (var reader = new JsonTextReader(new StringReader(task.Result)))
                    json = (JToken)convention.CreateSerializer().Deserialize(reader);

                return new QueryResult
                {
                    IsStale = Convert.ToBoolean(json["IsStale"].ToString()),
                    IndexTimestamp = json.Value <DateTime>("IndexTimestamp"),
                    IndexEtag = new Guid(request.ResponseHeaders["ETag"]),
                    Results = json["Results"].Children().Cast <JObject>().ToList(),
                    TotalResults = Convert.ToInt32(json["TotalResults"].ToString()),
                    IndexName = json.Value <string>("IndexName"),
                    SkippedResults = Convert.ToInt32(json["SkippedResults"].ToString())
                };
            }));
        }
示例#3
0
        /// <summary>
        /// Puts the document with the specified key in the database
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="etag">The etag.</param>
        /// <param name="document">The document.</param>
        /// <param name="metadata">The metadata.</param>
        public Task <PutResult> PutAsync(string key, Guid?etag, RavenJObject document, RavenJObject metadata)
        {
            if (metadata == null)
            {
                metadata = new RavenJObject();
            }
            var method = String.IsNullOrEmpty(key) ? "POST" : "PUT";

            if (etag != null)
            {
                metadata["ETag"] = new RavenJValue(etag.Value.ToString());
            }
            var request = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/docs/" + key, method, metadata, credentials, convention);

            request.AddOperationHeaders(OperationsHeaders);


            return(Task.Factory.FromAsync(request.BeginWrite, request.EndWrite, document.ToString(), null)
                   .ContinueWith(task =>
            {
                if (task.Exception != null)
                {
                    throw new InvalidOperationException("Unable to write to server");
                }

                return request.ReadResponseJsonAsync()
                .ContinueWith(task1 =>
                {
                    try
                    {
                        return convention.CreateSerializer().Deserialize <PutResult>(new RavenJTokenReader(task1.Result));
                    }
                    catch (AggregateException e)
                    {
                        var we = e.ExtractSingleInnerException() as WebException;
                        if (we == null)
                        {
                            throw;
                        }
                        var httpWebResponse = we.Response as HttpWebResponse;
                        if (httpWebResponse == null ||
                            httpWebResponse.StatusCode != HttpStatusCode.Conflict)
                        {
                            throw;
                        }
                        throw ThrowConcurrencyException(we);
                    }
                });
            })
                   .Unwrap());
        }
示例#4
0
        private void UpdateIndexInReplication(IDatabaseCommands databaseCommands, DocumentConvention documentConvention,
                                              IndexDefinition indexDefinition)
        {
            var serverClient = databaseCommands as ServerClient;

            if (serverClient == null)
            {
                return;
            }
            var doc = serverClient.Get("Raven/Replication/Destinations");

            if (doc == null)
            {
                return;
            }
            var replicationDocument =
                documentConvention.CreateSerializer().Deserialize <ReplicationDocument>(new RavenJTokenReader(doc.DataAsJson));

            if (replicationDocument == null)
            {
                return;
            }

            foreach (var replicationDestination in replicationDocument.Destinations)
            {
                try
                {
                    serverClient.DirectPutIndex(IndexName, replicationDestination.Url, true, indexDefinition);
                }
                catch (Exception e)
                {
                    Logger.WarnException("Could not put index in replication server", e);
                }
            }
        }
示例#5
0
        private List <Alert> DeserializeAlerts(MemoryStream ms)
        {
            var conventions = new DocumentConvention();
            var alertList   = RavenJToken.TryLoad(ms) as RavenJArray;

            return((List <Alert>)conventions.CreateSerializer().Deserialize(new RavenJTokenReader(alertList), typeof(List <Alert>)));
        }
示例#6
0
        public Task Migrate <TMigration, TEntity>(TMigration migration) where TMigration : IMigration <TEntity> where TEntity : IEntity
        {
            int count;

            using (var session = _store.OpenSession(_databaseName))
            {
                count = session.Query <TEntity>().
                        Customize(query => query.WaitForNonStaleResultsAsOfLastWrite()). //This is here because the streaming API will only use existing indexes, not create its own dynamically
                        Count();
            }

            QueryHeaderInformation info;
            var documents = _store.DatabaseCommands.ForDatabase(_databaseName).StreamQuery("Raven/DocumentsByEntityName", new IndexQuery {
                Query = "Tag: SillyEntities"
            }, out info);

            while (documents.MoveNext())
            {
                var documentConvention = new DocumentConvention();
                var entity             = (TEntity)(documentConvention.CreateSerializer().Deserialize(new RavenJTokenReader(documents.Current), typeof(TEntity)));
                migration.Migrate(entity);
            }

            return(Task.FromResult(true));
        }
示例#7
0
        public void LinqQueryWithStaticCallOnEnumerableIsCanBeCompiledAndRun()
        {
            var indexDefinition = new IndexDefinition <Page>
            {
                Map = pages => from p in pages
                      from coAuthor in p.CoAuthors.DefaultIfEmpty()
                      select new
                {
                    p.Id,
                    CoAuthorUserID = coAuthor != null ? coAuthor.UserId : -1
                }
            }.ToIndexDefinition(new DocumentConvention());

            var mapInstance = new DynamicViewCompiler("testView",
                                                      indexDefinition, new AbstractDynamicCompilationExtension[] { }).
                              GenerateInstance();

            var conventions = new DocumentConvention();
            var o           = JObject.FromObject(page, conventions.CreateSerializer());

            o["@metadata"] = new JObject(
                new JProperty("Raven-Entity-Name", "Pages")
                );
            dynamic dynamicObject = new DynamicJsonObject(o);

            var result = mapInstance.MapDefinition(new[] { dynamicObject }).ToList <object>();

            Assert.Equal("{ Id = 0, CoAuthorUserID = 1, __document_id =  }", result[0].ToString());
            Assert.Equal("{ Id = 0, CoAuthorUserID = 2, __document_id =  }", result[1].ToString());
        }
示例#8
0
        /// <summary>
        /// Returns a list of suggestions based on the specified suggestion query.
        /// </summary>
        /// <param name="index">The index to query for suggestions</param>
        /// <param name="suggestionQuery">The suggestion query.</param>
        public Task <SuggestionQueryResult> SuggestAsync(string index, SuggestionQuery suggestionQuery)
        {
            if (suggestionQuery == null)
            {
                throw new ArgumentNullException("suggestionQuery");
            }

            var requestUri = url + string.Format("/suggest/{0}?term={1}&field={2}&max={3}&distance={4}&accuracy={5}",
                                                 Uri.EscapeUriString(index),
                                                 Uri.EscapeDataString(suggestionQuery.Term),
                                                 Uri.EscapeDataString(suggestionQuery.Field),
                                                 Uri.EscapeDataString(suggestionQuery.MaxSuggestions.ToString()),
                                                 Uri.EscapeDataString(suggestionQuery.Distance.ToString()),
                                                 Uri.EscapeDataString(suggestionQuery.Accuracy.ToString()));

            var request = jsonRequestFactory.CreateHttpJsonRequest(this, requestUri, "GET", credentials, convention);

            request.AddOperationHeaders(OperationsHeaders);
            var serializer = convention.CreateSerializer();

            return(Task.Factory.FromAsync <string>(request.BeginReadResponseString, request.EndReadResponseString, null)
                   .ContinueWith(task =>
            {
                using (var reader = new JsonTextReader(new StringReader(task.Result)))
                {
                    var json = (RavenJObject)serializer.Deserialize(reader);
                    return new SuggestionQueryResult
                    {
                        Suggestions = ((RavenJArray)json["Suggestions"]).Select(x => x.Value <string>()).ToArray(),
                    };
                }
            }));
        }
示例#9
0
        public BlittableJsonReaderObject ConvertEntityToBlittable(object entity, DocumentConvention documentConvention, JsonOperationContext jsonOperationContext, DocumentInfo documentInfo = null)
        {
            var writer     = new BlittableJsonWriter(jsonOperationContext, documentInfo);
            var serializer = documentConvention.CreateSerializer();

            serializer.Serialize(writer, entity);
            writer.FinalizeDocument();
            var reader = writer.CreateReader();

            return(reader);
        }
示例#10
0
        public void ListOnDynamicJsonObjectFromJsonWillFailToBeAJsonList()
        {
            var conventions = new DocumentConvention();

            var jObject = JObject.FromObject(page, conventions.CreateSerializer());

            dynamic dynamicObject = new DynamicJsonObject(jObject);

            Assert.NotNull(dynamicObject.CoAuthors as IEnumerable);
            Assert.NotNull(dynamicObject.CoAuthors.Length);
            Assert.Equal(2, dynamicObject.CoAuthors.Length);
        }
示例#11
0
        public Task <LogItem[]> GetLogsAsync(bool errorsOnly)
        {
            var requestUri = url + "/logs";

            if (errorsOnly)
            {
                requestUri += "?type=error";
            }

            var request = jsonRequestFactory.CreateHttpJsonRequest(this, requestUri.NoCache(), "GET", credentials, convention);

            request.AddOperationHeaders(OperationsHeaders);

            return(request.ReadResponseStringAsync()
                   .ContinueWith(task =>
            {
                using (var reader = new JsonTextReader(new StringReader(task.Result)))
                {
                    return convention.CreateSerializer().Deserialize <LogItem[]>(reader);
                }
            }));
        }
示例#12
0
        /// <summary>
        /// Perform a single POST request containing multiple nested GET requests
        /// </summary>
        public Task <GetResponse[]> MultiGetAsync(GetRequest[] requests)
        {
            var postedData = JsonConvert.SerializeObject(requests);

            var httpJsonRequest = jsonRequestFactory.CreateHttpJsonRequest(this, url + "/multi_get/", "POST",
                                                                           credentials, convention);

            return(httpJsonRequest.WriteAsync(postedData)
                   .ContinueWith(
                       task =>
            {
                task.Wait();                                // will throw if write errored
                return httpJsonRequest.ReadResponseJsonAsync()
                .ContinueWith(replyTask => convention.CreateSerializer().Deserialize <GetResponse[]>(new RavenJTokenReader(replyTask.Result)));
            })
                   .Unwrap());
        }
示例#13
0
 private SupportCoverageResult QueryForSupportCoverage(string id)
 {
     try
     {
         var requestParam = new CreateHttpJsonRequestParams(null, "http://licensing.ravendb.net/license/support/" + id, HttpMethod.Get, null, null, conventions);
         using (var request = requestFactory.CreateHttpJsonRequest(requestParam))
         {
             var value = request.ReadResponseJson();
             return(conventions.CreateSerializer().Deserialize <SupportCoverageResult>(new RavenJTokenReader(value)));
         }
     }
     catch (Exception e)
     {
         log.WarnException("Failed to obtain support coverage information.", e);
         return(CreateDefaultSupportCoverageDocument());
     }
 }
        private Task UpdateIndexInReplicationAsync(IAsyncDatabaseCommands asyncDatabaseCommands,
                                                   DocumentConvention documentConvention, IndexDefinition indexDefinition)
        {
            var asyncServerClient = asyncDatabaseCommands as AsyncServerClient;

            if (asyncServerClient == null)
            {
                return(new CompletedTask());
            }
            return(asyncServerClient.GetAsync("Raven/Replication/Destinations").ContinueWith(doc =>
            {
                if (doc == null)
                {
                    return new CompletedTask();
                }
                var replicationDocument =
                    documentConvention.CreateSerializer().Deserialize <ReplicationDocument>(new RavenJTokenReader(doc.Result.DataAsJson));
                if (replicationDocument == null)
                {
                    return new CompletedTask();
                }
                var tasks = new List <Task>();
                foreach (var replicationDestination in replicationDocument.Destinations)
                {
                    if (replicationDestination.Disabled || replicationDestination.IgnoredClient)
                    {
                        continue;
                    }
                    tasks.Add(asyncServerClient.DirectPutIndexAsync(IndexName, indexDefinition, true, GetReplicationUrl(replicationDestination)));
                }
                return Task.Factory.ContinueWhenAll(tasks.ToArray(), indexingTask =>
                {
                    foreach (var indexTask in indexingTask)
                    {
                        if (indexTask.IsFaulted)
                        {
                            Logger.WarnException("Could not put index in replication server", indexTask.Exception);
                        }
                    }
                });
            }).Unwrap());
        }
示例#15
0
        public void CanSerializeYieldGetterMethods()
        {
            // If this test breaks, this is likely becaused we merged
            // a new version of JSON.Net, which can revert a modification that we made to the code
            // Look at the other changes that happened in this commit (see the git log for that)
            // And at any rate, the full explanation, including the full reasoning is here:
            // http://issues.hibernatingrhinos.com/issue/RavenDB-3931
            //
            // Don't try to fix this issue without reading the details, it is a single line fix, but it
            // takes time to get to the right reason

            var documentConvention = new DocumentConvention();
            var jsonSerializer     = documentConvention.CreateSerializer();
            var stringWriter       = new StringWriter();

            jsonSerializer.Serialize(stringWriter, new Item());
            var str = stringWriter.GetStringBuilder().ToString();

            jsonSerializer.Deserialize <Item>(new JsonTextReader(new StringReader(str)));
        }
示例#16
0
        internal async Task UpdateIndexInReplicationAsync(IAsyncDatabaseCommands asyncDatabaseCommands,
                                                          DocumentConvention documentConvention,
                                                          Func <AsyncServerClient, OperationMetadata, Task> action)
        {
            var asyncServerClient = asyncDatabaseCommands as AsyncServerClient;

            if (asyncServerClient == null)
            {
                return;
            }
            var doc = await asyncServerClient.GetAsync("Raven/Replication/Destinations");

            if (doc == null)
            {
                return;
            }
            var replicationDocument =
                documentConvention.CreateSerializer().Deserialize <ReplicationDocument>(new RavenJTokenReader(doc.DataAsJson));

            if (replicationDocument == null || replicationDocument.Destinations == null || replicationDocument.Destinations.Count == 0)
            {
                return;
            }
            var tasks = (
                from replicationDestination in replicationDocument.Destinations
                where !replicationDestination.Disabled && !replicationDestination.IgnoredClient
                select action(asyncServerClient, GetReplicationOperation(replicationDestination))
                )
                        .ToArray();
            await Task.Factory.ContinueWhenAll(tasks, indexingTask =>
            {
                foreach (var indexTask in indexingTask)
                {
                    if (indexTask.IsFaulted)
                    {
                        Logger.WarnException("Could not put index in replication server", indexTask.Exception);
                    }
                }
            });
        }
示例#17
0
        internal void UpdateIndexInReplication(IDatabaseCommands databaseCommands, DocumentConvention documentConvention,
                                               Action <ServerClient, OperationMetadata> action)
        {
            var serverClient = databaseCommands as ServerClient;

            if (serverClient == null)
            {
                return;
            }
            var doc = serverClient.Get("Raven/Replication/Destinations");

            if (doc == null)
            {
                return;
            }
            var replicationDocument =
                documentConvention.CreateSerializer().Deserialize <ReplicationDocument>(new RavenJTokenReader(doc.DataAsJson));

            if (replicationDocument == null)
            {
                return;
            }

            foreach (var replicationDestination in replicationDocument.Destinations)
            {
                try
                {
                    if (replicationDestination.Disabled || replicationDestination.IgnoredClient)
                    {
                        continue;
                    }
                    action(serverClient, GetReplicationOperation(replicationDestination));
                }
                catch (Exception e)
                {
                    Logger.WarnException("Could not put index in replication server", e);
                }
            }
        }
示例#18
0
        private TcpConnectionInfo GetTcpInfo()
        {
            var convention = new DocumentConvention();

            //since we use it only once when the connection is initialized, no reason to keep requestFactory around for long
            using (var requestFactory = new HttpJsonRequestFactory(1))
                using (var request = requestFactory.CreateHttpJsonRequest(new CreateHttpJsonRequestParams(null, string.Format("{0}/info/tcp",
                                                                                                                              MultiDatabase.GetRootDatabaseUrl(_destination.Url)),
                                                                                                          HttpMethod.Get,
                                                                                                          new OperationCredentials(_destination.ApiKey, CredentialCache.DefaultCredentials), convention)
                {
                    Timeout = TimeSpan.FromSeconds(15)
                }))
                {
                    var result            = request.ReadResponseJson();
                    var tcpConnectionInfo = convention.CreateSerializer().Deserialize <TcpConnectionInfo>(new RavenJTokenReader(result));
                    if (_log.IsInfoEnabled)
                    {
                        _log.Info($"Will replicate to {_destination.Database} @ {_destination.Url} via {tcpConnectionInfo.Url}");
                    }
                    return(tcpConnectionInfo);
                }
        }
示例#19
0
 /// <summary>
 /// Deserializes the specified instance <param name="self"/> to an instance of <param name="type"/> using the specified <param name="convention"/>
 /// </summary>
 public static object Deserialize(this JObject self, Type type, DocumentConvention convention)
 {
     return(convention.CreateSerializer().Deserialize(new JTokenReader(self), type));
 }
示例#20
0
 /// <summary>
 /// Deserializes the specified instance <param name="self"/> to an instance of <typeparam name="T"/> using the specified <param name="convention"/>
 /// </summary>
 public static T Deserialize <T>(this JObject self, DocumentConvention convention)
 {
     return((T)convention.CreateSerializer().Deserialize(new JTokenReader(self), typeof(T)));
 }
示例#21
0
        public void Register(IServiceCollection services)
        {
            var documentStore = DocumentStoreFactory.CreateStore(_connectionString, Assembly.GetExecutingAssembly());

            services.AddSingleton <IDocumentStore>(documentStore);

            var filesStore = FileStoreFactory.CreateStore(_connectionString);

            services.AddSingleton <IFilesStore>(filesStore);

            services.AddScoped <IDocumentSession>(x =>
            {
                var docStore           = x.GetService <IDocumentStore>();
                var documentConvention = new DocumentConvention();
                var jsonSerializer     = documentConvention.CreateSerializer();
                docStore.Conventions.CreateSerializer();
                return(docStore.OpenSession());
            });
            services.AddScoped <IAsyncDocumentSession>(x => x.GetService <IDocumentStore>().OpenAsyncSession());
            services.AddScoped <IAsyncFilesSession>(x => x.GetService <IFilesStore>().OpenAsyncSession());
            services.AddScoped <IIdentityGenerator, RavenIdentityGenerator>();
            services.AddScoped <IRavenIdentityGenerator, RavenIdentityGenerator>();

            //Repositories
            services.AddScoped <IBookingPositionRepository, RavenBookingPositionRepository>();
            services.AddScoped <IBookingPositionGroupRepository, RavenBookingPositionGroupRepository>();
            services.AddScoped <IUniverseRepository, RavenUniverseRepository>();
            services.AddScoped <ISalesAreaRepository, RavenSalesAreaRepository>();
            services.AddScoped <IDemographicRepository, RavenDemographicRepository>();
            services.AddScoped <IMetadataRepository, RavenMetadataRepository>();
            services.AddScoped <IScheduleRepository, RavenScheduleRepository>();
            services.AddScoped <IProgrammeRepository, RavenProgrammeRepository>();
            services.AddScoped <IProgrammeClassificationRepository, RavenProgrammeClassificationRepository>();
            services.AddScoped <IClashRepository, RavenClashRepository>();
            services.AddScoped <IProductRepository, RavenProductRepository>();
            services.AddScoped <IRatingsScheduleRepository, RavenRatingsScheduleRepository>();
            services.AddScoped <IRestrictionRepository, RavenRestrictionRepository>();
            services.AddScoped <IClearanceRepository, RavenClearanceRepository>();
            services.AddScoped <ISpotRepository, RavenSpotRepository>();
            services.AddScoped <IBreakRepository, RavenBreakRepository>();
            services.AddScoped <IBusinessTypeRepository, RavenBusinessTypeRepository>();
            services.AddScoped <IClashExceptionRepository, RavenClashExceptionRepository>();
            services.AddScoped <ICampaignRepository, RavenCampaignRepository>();
            services.AddScoped <ICampaignSettingsRepository, RavenCampaignSettingsRepository>();
            services.AddScoped <ITenantSettingsRepository, RavenTenantSettingsRepository>();
            services.AddScoped <IScenarioRepository, RavenScenarioRepository>();
            services.AddScoped <IPassRepository, RavenPassRepository>();
            services.AddScoped <ISalesAreaDemographicRepository, RavenSalesAreaDemographicRepository>();
            services.AddScoped <IProgrammeCategoryHierarchyRepository, RavenProgrammeCategoryHierarchyRepository>();
            services.AddScoped <IProgrammeEpisodeRepository, RavenProgrammeEpisodeRepository>();
            services.AddScoped <IInventoryTypeRepository, RavenInventoryTypeRepository>();
            services.AddScoped <IInventoryLockRepository, RavenInventoryLockRepository>();
            services.AddScoped <ILockTypeRepository, RavenLockTypeRepository>();
            services.AddScoped <ITotalRatingRepository, RavenTotalRatingRepository>();
            services.AddScoped <IStandardDayPartRepository, RavenStandardDayPartRepository>();
            services.AddScoped <IStandardDayPartGroupRepository, RavenStandardDayPartGroupRepository>();
            services.AddScoped <ILengthFactorRepository, RavenLengthFactorRepository>();

            // ValidationServices
            services.AddScoped <IClashExceptionValidations, ClashExceptionValidations>();
        }