Esempio n. 1
0
        public virtual async Task SideBySideExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, Etag minimumEtagBeforeReplace = null, DateTime?replaceTimeUtc = null, CancellationToken token = default(CancellationToken))
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();
            var serverDef       = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false);

            if (serverDef != null)
            {
                if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition))
                {
                    return;
                }

                var replaceIndexName = "ReplacementOf/" + IndexName;
                await asyncDatabaseCommands.PutIndexAsync(replaceIndexName, indexDefinition, token).ConfigureAwait(false);

                await asyncDatabaseCommands
                .PutAsync(Constants.IndexReplacePrefix + replaceIndexName,
                          null,
                          RavenJObject.FromObject(new IndexReplaceDocument {
                    IndexToReplace = serverDef.Name, MinimumEtagBeforeReplace = minimumEtagBeforeReplace, ReplaceTimeUtc = replaceTimeUtc
                }),
                          new RavenJObject(),
                          token).ConfigureAwait(false);
            }
            else
            {
                // since index doesn't exist yet - create it in normal mode
                await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, token).ConfigureAwait(false);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, CancellationToken token = default(CancellationToken))
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

#if !DNXCORE50
            if (documentConvention.PrettifyGeneratedLinqExpressions)
            {
                var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false);

                if (serverDef != null && CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition))
                {
                    await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false);

                    return;
                }
            }
#endif

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new definition.
            await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true, token).ConfigureAwait(false);

            if (Priority != null)
            {
                await asyncDatabaseCommands.SetIndexPriorityAsync(IndexName, Priority.Value, token).ConfigureAwait(false);
            }

            await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false);
        }
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention)
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            if (documentConvention.PrettifyGeneratedLinqExpressions)
            {
                var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName);

                if (serverDef != null)
                {
                    if (serverDef.Equals(indexDefinition))
                    {
                        return;
                    }

                    // now we need to check if this is a legacy index...
                    var legacyIndexDefinition = GetLegacyIndexDefinition(documentConvention);
                    if (serverDef.Equals(legacyIndexDefinition))
                    {
                        return;                         // if it matches the legacy definition, do not change that (to avoid re-indexing)
                    }
                }
            }

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new definition.
            await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true);

            await UpdateIndexInReplicationAsync(asyncDatabaseCommands, documentConvention, (client, operationMetadata) => client.DirectPutIndexAsync(IndexName, indexDefinition, true, operationMetadata));
        }
Esempio n. 4
0
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, CancellationToken token = default(CancellationToken))
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            if (documentConvention.PrettifyGeneratedLinqExpressions)
            {
                var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token);

                if (serverDef != null && CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition))
                {
                    return;
                }
            }

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new definition.
            await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true, token);

            if (Priority != null)
            {
                await asyncDatabaseCommands.SetIndexPriorityAsync(IndexName, Priority.Value, token);
            }

            if (Conventions.IndexAndTransformerReplicationMode.HasFlag(IndexAndTransformerReplicationMode.Indexes))
            {
                await ReplicateIndexesIfNeededAsync(asyncDatabaseCommands);
            }
        }
Esempio n. 5
0
            private Task WriteIndexes(JsonTextReader jsonReader)
            {
                while (jsonReader.Read() && jsonReader.TokenType != JsonToken.EndArray)
                {
                    var json      = JToken.ReadFrom(jsonReader);
                    var indexName = json.Value <string>("name");
                    if (indexName.StartsWith("Temp/"))
                    {
                        continue;
                    }

                    var index = JsonConvert.DeserializeObject <IndexDefinition>(json.Value <JObject>("definition").ToString());

                    totalIndexes++;

                    output(string.Format("Importing index: {0}", indexName));

                    return(asyncDatabaseCommands.PutIndexAsync(indexName, index, overwrite: true)
                           .ContinueOnSuccess(() => WriteIndexes(jsonReader)));
                }

                var tcs = new TaskCompletionSource <object>();

                tcs.SetResult(null);
                return(tcs.Task);
            }
Esempio n. 6
0
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, CancellationToken token = default(CancellationToken))
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            if (documentConvention.PrettifyGeneratedLinqExpressions)
            {
                var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token);

                if (serverDef != null)
                {
                    if (serverDef.Equals(indexDefinition))
                    {
                        return;
                    }

                    // now we need to check if this is a legacy index...
                    var legacyIndexDefinition = GetLegacyIndexDefinition(documentConvention);
                    if (serverDef.Equals(legacyIndexDefinition))
                    {
                        return;                         // if it matches the legacy definition, do not change that (to avoid re-indexing)
                    }
                }
            }

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new definition.
            await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true, token);

            if (Conventions.IndexAndTransformerReplicationMode.HasFlag(IndexAndTransformerReplicationMode.Indexes))
            {
                await ReplicateIndexesIfNeededAsync(asyncDatabaseCommands);
            }
        }
        public virtual async Task SideBySideExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, Etag minimumEtagBeforeReplace = null, DateTime?replaceTimeUtc = null, CancellationToken token = default(CancellationToken))
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            if (minimumEtagBeforeReplace != null && indexDefinition.IsMapReduce)
            {
                throw new InvalidOperationException("We do not support side-by-side execution for Map-Reduce indexes when 'minimum last indexed etag' scenario is used.");
            }

            var replaceIndexName = Constants.SideBySideIndexNamePrefix + IndexName;
            //check if side by side index exists
            var sideBySideDef = await asyncDatabaseCommands.GetIndexAsync(replaceIndexName, token).ConfigureAwait(false);

            if (sideBySideDef != null)
            {
                if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, sideBySideDef, indexDefinition))
                {
                    return;
                }

                await UpdateSideBySideIndexAsync(asyncDatabaseCommands, minimumEtagBeforeReplace, replaceTimeUtc, token, replaceIndexName, indexDefinition, documentConvention).ConfigureAwait(false);

                return;
            }

            var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false);

            if (serverDef != null)
            {
                if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition))
                {
                    return;
                }

                switch (serverDef.LockMode)
                {
                case IndexLockMode.SideBySide:
                    //keep the SideBySide lock mode from the replaced index
                    indexDefinition.LockMode = IndexLockMode.SideBySide;
                    break;

                case IndexLockMode.LockedIgnore:
                    //Nothing to do we just ignore this index
                    return;

                case IndexLockMode.LockedError:
                    throw new InvalidOperationException(string.Format("Can't replace locked index {0} its lock mode is set to: LockedError", serverDef.IndexId));
                }

                await UpdateSideBySideIndexAsync(asyncDatabaseCommands, minimumEtagBeforeReplace, replaceTimeUtc, token, replaceIndexName, indexDefinition, documentConvention).ConfigureAwait(false);
            }
            else
            {
                // since index doesn't exist yet - create it in normal mode
                await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, token).ConfigureAwait(false);
                await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false);
            }
        }
Esempio n. 8
0
        /// <summary>
        /// Asynchronously creates an index
        /// </summary>
        /// <typeparam name="T">The type that defines the index to be create.</typeparam>
        /// <param name="commands">The hook to the database commands.</param>
        /// <param name="overwrite">Should the index be overwritten if it already exists.</param>
        /// <returns></returns>
        public static Task <string> PutIndexAsync <T>(this IAsyncDatabaseCommands commands, bool overwrite)
            where T : AbstractIndexCreationTask, new()
        {
            var indexCreationTask = new T();

            return(commands.PutIndexAsync(
                       indexCreationTask.IndexName,
                       indexCreationTask.CreateIndexDefinition(), overwrite));
        }
Esempio n. 9
0
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention)
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new defintion.
            return(asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true));
        }
Esempio n. 10
0
        protected override Task PutIndex(string indexName, RavenJToken index)
        {
            if (index != null)
            {
                var indexDefinition = JsonConvert.DeserializeObject <IndexDefinition>(index.Value <RavenJObject>("definition").ToString());

                return(Commands.PutIndexAsync(indexName, indexDefinition, overwrite: true));
            }

            return(FlushBatch());
        }
Esempio n. 11
0
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual async Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention)
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new definition.
            await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true);

            await UpdateIndexInReplicationAsync(asyncDatabaseCommands, documentConvention, (client, operationMetadata) => client.DirectPutIndexAsync(IndexName, indexDefinition, true, operationMetadata));
        }
Esempio n. 12
0
        /// <summary>
        /// Executes the index creation against the specified document store.
        /// </summary>
        public virtual Task ExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention)
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            // This code take advantage on the fact that RavenDB will turn an index PUT
            // to a noop of the index already exists and the stored definition matches
            // the new definition.
            return(asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, true)
                   .ContinueWith(task => UpdateIndexInReplicationAsync(asyncDatabaseCommands, documentConvention, (client, url) =>
                                                                       client.DirectPutIndexAsync(IndexName, indexDefinition, true, url)))
                   .Unwrap());
        }
Esempio n. 13
0
        private async Task UpdateSideBySideIndexAsync(IAsyncDatabaseCommands asyncDatabaseCommands, Etag minimumEtagBeforeReplace, DateTime?replaceTimeUtc, CancellationToken token, string replaceIndexName, IndexDefinition indexDefinition, DocumentConvention documentConvention)
        {
            await asyncDatabaseCommands.PutIndexAsync(replaceIndexName, indexDefinition, true, token).ConfigureAwait(false);

            await asyncDatabaseCommands
            .PutAsync(Constants.IndexReplacePrefix + replaceIndexName,
                      null,
                      RavenJObject.FromObject(new IndexReplaceDocument {
                IndexToReplace = IndexName, MinimumEtagBeforeReplace = minimumEtagBeforeReplace, ReplaceTimeUtc = replaceTimeUtc
            }),
                      new RavenJObject(),
                      token).ConfigureAwait(false);

            await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token).ConfigureAwait(false);
        }
Esempio n. 14
0
            private IEnumerable <Task> CreateSampleData()
            {
                // this code assumes a small enough dataset, and doesn't do any sort
                // of paging or batching whatsoever.

                HomeModel.ShowCreateSampleData.Value = false;
                model.IsGeneratingSampleData         = true;

                using (var sampleData = typeof(HomeModel).Assembly.GetManifestResourceStream("Raven.Studio.Assets.EmbeddedData.MvcMusicStore_Dump.json"))
                    using (var streamReader = new StreamReader(sampleData))
                    {
                        var musicStoreData = (RavenJObject)RavenJToken.ReadFrom(new JsonTextReader(streamReader));
                        foreach (var index in musicStoreData.Value <RavenJArray>("Indexes"))
                        {
                            var indexName    = index.Value <string>("name");
                            var ravenJObject = index.Value <RavenJObject>("definition");
                            var putDoc       = databaseCommands
                                               .PutIndexAsync(indexName,
                                                              ravenJObject.JsonDeserialization <IndexDefinition>(),
                                                              true);
                            yield return(putDoc);
                        }

                        var batch = databaseCommands.BatchAsync(
                            musicStoreData.Value <RavenJArray>("Docs").OfType <RavenJObject>().Select(
                                doc =>
                        {
                            var metadata = doc.Value <RavenJObject>("@metadata");
                            doc.Remove("@metadata");
                            return(new PutCommandData
                            {
                                Document = doc,
                                Metadata = metadata,
                                Key = metadata.Value <string>("@id"),
                            });
                        }).ToArray()
                            );

                        yield return(batch);

                        model.IsGeneratingSampleData = false;
                    }
            }
Esempio n. 15
0
        public virtual async Task SideBySideExecuteAsync(IAsyncDatabaseCommands asyncDatabaseCommands, DocumentConvention documentConvention, Etag minimumEtagBeforeReplace = null, DateTime?replaceTimeUtc = null, CancellationToken token = default(CancellationToken))
        {
            Conventions = documentConvention;
            var indexDefinition = CreateIndexDefinition();

            var replaceIndexName = Constants.SideBySideIndexNamePrefix + IndexName;
            //check if side by side index exists
            var sideBySideDef = await asyncDatabaseCommands.GetIndexAsync(replaceIndexName, token).ConfigureAwait(false);

            if (sideBySideDef != null)
            {
                if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, sideBySideDef, indexDefinition))
                {
                    return;
                }

                await UpdateSideBySideIndexAsync(asyncDatabaseCommands, minimumEtagBeforeReplace, replaceTimeUtc, token, replaceIndexName, indexDefinition, documentConvention);

                return;
            }

            var serverDef = await asyncDatabaseCommands.GetIndexAsync(IndexName, token).ConfigureAwait(false);

            if (serverDef != null)
            {
                if (CurrentOrLegacyIndexDefinitionEquals(documentConvention, serverDef, indexDefinition))
                {
                    return;
                }

                await UpdateSideBySideIndexAsync(asyncDatabaseCommands, minimumEtagBeforeReplace, replaceTimeUtc, token, replaceIndexName, indexDefinition, documentConvention);
            }
            else
            {
                // since index doesn't exist yet - create it in normal mode
                await asyncDatabaseCommands.PutIndexAsync(IndexName, indexDefinition, token).ConfigureAwait(false);
                await AfterExecuteAsync(asyncDatabaseCommands, documentConvention, token);
            }
        }