Example #1
0
        public int CreateIndex(IndexDefinitionBase definition)
        {
            if (definition == null)
            {
                throw new ArgumentNullException(nameof(definition));
            }

            if (definition is MapIndexDefinition)
            {
                return(CreateIndex(((MapIndexDefinition)definition).IndexDefinition));
            }

            lock (_locker)
            {
                Index existingIndex;
                var   lockMode = ValidateIndexDefinition(definition.Name, out existingIndex);
                if (lockMode == IndexLockMode.LockedIgnore)
                {
                    return(existingIndex.IndexId);
                }

                switch (GetIndexCreationOptions(definition, existingIndex))
                {
                case IndexCreationOptions.Noop:
                    return(existingIndex.IndexId);

                case IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex:
                    throw new NotSupportedException();

                case IndexCreationOptions.Update:
                    DeleteIndex(existingIndex.IndexId);
                    break;
                }

                var indexId = _indexes.GetNextIndexId();

                Index index;

                if (definition is AutoMapIndexDefinition)
                {
                    index = AutoMapIndex.CreateNew(indexId, (AutoMapIndexDefinition)definition, _documentDatabase);
                }
                else if (definition is AutoMapReduceIndexDefinition)
                {
                    index = AutoMapReduceIndex.CreateNew(indexId, (AutoMapReduceIndexDefinition)definition, _documentDatabase);
                }
                else
                {
                    throw new NotImplementedException($"Unknown index definition type: {definition.GetType().FullName}");
                }

                return(CreateIndexInternal(index, indexId));
            }
        }
Example #2
0
        private void HandleAutoIndexChange(string name, AutoIndexDefinitionBase definition)
        {
            var creationOptions = IndexCreationOptions.Create;
            var existingIndex   = GetIndex(name);
            IndexDefinitionCompareDifferences differences = IndexDefinitionCompareDifferences.None;

            if (existingIndex != null)
            {
                creationOptions = GetIndexCreationOptions(definition, existingIndex, out differences);
            }

            if (creationOptions == IndexCreationOptions.Noop)
            {
                Debug.Assert(existingIndex != null);

                return;
            }

            if (creationOptions == IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex || creationOptions == IndexCreationOptions.Update)
            {
                Debug.Assert(existingIndex != null);

                if ((differences & IndexDefinitionCompareDifferences.LockMode) != 0)
                {
                    existingIndex.SetLock(definition.LockMode);
                }

                if ((differences & IndexDefinitionCompareDifferences.Priority) != 0)
                {
                    existingIndex.SetPriority(definition.Priority);
                }

                existingIndex.Update(definition, existingIndex.Configuration);

                return;
            }

            Index index;

            if (definition is AutoMapIndexDefinition)
            {
                index = AutoMapIndex.CreateNew((AutoMapIndexDefinition)definition, _documentDatabase);
            }
            else if (definition is AutoMapReduceIndexDefinition)
            {
                index = AutoMapReduceIndex.CreateNew((AutoMapReduceIndexDefinition)definition, _documentDatabase);
            }
            else
            {
                throw new NotImplementedException($"Unknown index definition type: {definition.GetType().FullName}");
            }

            CreateIndexInternal(index);
        }
Example #3
0
        private Index ResetIndexInternal(Index index)
        {
            DeleteIndexInternal(index);

            var definitionBase = index.Definition;

            if (definitionBase is AutoMapIndexDefinition)
            {
                index = AutoMapIndex.CreateNew((AutoMapIndexDefinition)definitionBase, _documentDatabase);
            }
            else if (definitionBase is AutoMapReduceIndexDefinition)
            {
                index = AutoMapReduceIndex.CreateNew((AutoMapReduceIndexDefinition)definitionBase, _documentDatabase);
            }
            else
            {
                var staticIndexDefinition = index.Definition.ConvertToIndexDefinition(index);
                switch (staticIndexDefinition.Type)
                {
                case IndexType.Map:
                    index = MapIndex.CreateNew(staticIndexDefinition, _documentDatabase);
                    break;

                case IndexType.MapReduce:
                    index = MapReduceIndex.CreateNew(staticIndexDefinition, _documentDatabase);
                    break;

                default:
                    throw new NotSupportedException($"Cannot create {staticIndexDefinition.Type} index from IndexDefinition");
                }
            }

            CreateIndexInternal(index);

            return(index);
        }
Example #4
0
        public async Task Cleanup()
        {
            using (var database = CreateDocumentDatabase())
                using (var index = AutoMapIndex.CreateNew(new AutoMapIndexDefinition("Users", new[]
                {
                    new AutoIndexField
                    {
                        Name = "Name",
                        Storage = FieldStorage.No
                    }
                }), database))
                    using (var context = DocumentsOperationContext.ShortTermSingleUse(database))
                    {
                        using (var tx = context.OpenWriteTransaction())
                        {
                            using (var doc = CreateDocument(context, "key/1", new DynamicJsonValue
                            {
                                ["Name"] = "John",
                                [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                                {
                                    [Constants.Documents.Metadata.Collection] = "Users"
                                }
                            }))
                            {
                                database.DocumentsStorage.Put(context, "key/1", null, doc);
                            }

                            using (var doc = CreateDocument(context, "key/2", new DynamicJsonValue
                            {
                                ["Name"] = "Edward",
                                [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                                {
                                    [Constants.Documents.Metadata.Collection] = "Users"
                                }
                            }))
                            {
                                database.DocumentsStorage.Put(context, "key/2", null, doc);
                            }

                            using (var doc = CreateDocument(context, "key/3", new DynamicJsonValue
                            {
                                ["Name"] = "William",
                                [Constants.Documents.Metadata.Key] = new DynamicJsonValue
                                {
                                    [Constants.Documents.Metadata.Collection] = "Users"
                                }
                            }))
                            {
                                database.DocumentsStorage.Put(context, "key/3", null, doc);
                            }

                            tx.Commit();
                        }

                        var batchStats = new IndexingRunStats();
                        var stats      = new IndexingStatsScope(batchStats);
                        index.DoIndexingWork(stats, CancellationToken.None);

                        var tombstones = index.GetLastProcessedTombstonesPerCollection();
                        Assert.Equal(1, tombstones.Count);
                        Assert.Equal(0, tombstones["Users"]);

                        using (context.OpenReadTransaction())
                        {
                            var count = database.DocumentsStorage.GetTombstonesFrom(context, "Users", 0, 0, 128).Count();
                            Assert.Equal(0, count);
                        }

                        await database.TombstoneCleaner.ExecuteCleanup();

                        using (var tx = context.OpenWriteTransaction())
                        {
                            database.DocumentsStorage.Delete(context, "key/1", null);

                            tx.Commit();
                        }

                        tombstones = index.GetLastProcessedTombstonesPerCollection();
                        Assert.Equal(1, tombstones.Count);
                        Assert.Equal(0, tombstones["Users"]);

                        using (context.OpenReadTransaction())
                        {
                            var count = database.DocumentsStorage.GetTombstonesFrom(context, "Users", 0, 0, 128).Count();
                            Assert.Equal(1, count);
                        }

                        await database.TombstoneCleaner.ExecuteCleanup();

                        using (context.OpenReadTransaction())
                        {
                            var count = database.DocumentsStorage.GetTombstonesFrom(context, "Users", 0, 0, 128).Count();
                            Assert.Equal(1, count);
                        }

                        batchStats = new IndexingRunStats();
                        stats      = new IndexingStatsScope(batchStats);
                        index.DoIndexingWork(stats, CancellationToken.None);

                        tombstones = index.GetLastProcessedTombstonesPerCollection();
                        Assert.Equal(1, tombstones.Count);
                        Assert.Equal(4, tombstones["Users"]);

                        using (var tx = context.OpenWriteTransaction())
                        {
                            database.DocumentsStorage.Delete(context, "key/2", null);

                            tx.Commit();
                        }

                        using (context.OpenReadTransaction())
                        {
                            var count = database.DocumentsStorage.GetTombstonesFrom(context, "Users", 0, 0, 128).Count();
                            Assert.Equal(2, count);
                        }

                        await database.TombstoneCleaner.ExecuteCleanup();

                        using (context.OpenReadTransaction())
                        {
                            var list = database.DocumentsStorage.GetTombstonesFrom(context, "Users", 0, 0, 128).ToList();
                            Assert.Equal(1, list.Count);
                            Assert.Equal(5, list[0].Etag);
                        }
                    }
        }