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); }
private void UpdateIndex(IndexDefinition definition, Index existingIndex, IndexDefinitionCompareDifferences indexDifferences) { UpdateStaticIndexLockModeAndPriority(definition, existingIndex, indexDifferences); switch (definition.Type) { case IndexType.Map: MapIndex.Update(existingIndex, definition, _documentDatabase); break; case IndexType.MapReduce: MapReduceIndex.Update(existingIndex, definition, _documentDatabase); break; default: throw new NotSupportedException($"Cannot update {definition.Type} index from IndexDefinition"); } }
internal IndexCreationOptions GetIndexCreationOptions(object indexDefinition, Index existingIndex, out IndexDefinitionCompareDifferences differences) { differences = IndexDefinitionCompareDifferences.All; if (existingIndex == null) { return(IndexCreationOptions.Create); } //if (existingIndex.Definition.IsTestIndex) // TODO [ppekrol] // return IndexCreationOptions.Update; differences = IndexDefinitionCompareDifferences.None; var indexDef = indexDefinition as IndexDefinition; if (indexDef != null) { differences = existingIndex.Definition.Compare(indexDef); } var indexDefBase = indexDefinition as IndexDefinitionBase; if (indexDefBase != null) { differences = existingIndex.Definition.Compare(indexDefBase); } if (differences == IndexDefinitionCompareDifferences.All) { return(IndexCreationOptions.Update); } if (differences == IndexDefinitionCompareDifferences.None) { return(IndexCreationOptions.Noop); } if ((differences & IndexDefinitionCompareDifferences.Maps) == IndexDefinitionCompareDifferences.Maps || (differences & IndexDefinitionCompareDifferences.Reduce) == IndexDefinitionCompareDifferences.Reduce) { return(IndexCreationOptions.Update); } if ((differences & IndexDefinitionCompareDifferences.Fields) == IndexDefinitionCompareDifferences.Fields) { return(IndexCreationOptions.Update); } if ((differences & IndexDefinitionCompareDifferences.Configuration) == IndexDefinitionCompareDifferences.Configuration) { var currentConfiguration = existingIndex.Configuration as SingleIndexConfiguration; if (currentConfiguration == null) // should not happen { return(IndexCreationOptions.Update); } var newConfiguration = new SingleIndexConfiguration(indexDef.Configuration, _documentDatabase.Configuration); var configurationResult = currentConfiguration.CalculateUpdateType(newConfiguration); switch (configurationResult) { case IndexUpdateType.None: break; case IndexUpdateType.Refresh: return(IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex); case IndexUpdateType.Reset: return(IndexCreationOptions.Update); default: throw new ArgumentOutOfRangeException(); } } if ((differences & IndexDefinitionCompareDifferences.MapsFormatting) == IndexDefinitionCompareDifferences.MapsFormatting || (differences & IndexDefinitionCompareDifferences.ReduceFormatting) == IndexDefinitionCompareDifferences.ReduceFormatting) { return(IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex); } if ((differences & IndexDefinitionCompareDifferences.Priority) == IndexDefinitionCompareDifferences.Priority) { return(IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex); } if ((differences & IndexDefinitionCompareDifferences.LockMode) == IndexDefinitionCompareDifferences.LockMode) { return(IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex); } return(IndexCreationOptions.Update); }
private static void UpdateStaticIndexLockModeAndPriority(IndexDefinition definition, Index existingIndex, IndexDefinitionCompareDifferences indexDifferences) { if (definition.LockMode.HasValue && (indexDifferences & IndexDefinitionCompareDifferences.LockMode) != 0) { existingIndex.SetLock(definition.LockMode.Value); } if (definition.Priority.HasValue && (indexDifferences & IndexDefinitionCompareDifferences.Priority) != 0) { existingIndex.SetPriority(definition.Priority.Value); } }
private void HandleStaticIndexChange(string name, IndexDefinition definition) { var creationOptions = IndexCreationOptions.Create; var currentIndex = GetIndex(name); IndexDefinitionCompareDifferences currentDifferences = IndexDefinitionCompareDifferences.None; if (currentIndex != null) { creationOptions = GetIndexCreationOptions(definition, currentIndex, out currentDifferences); } var replacementIndexName = Constants.Documents.Indexing.SideBySideIndexNamePrefix + definition.Name; if (creationOptions == IndexCreationOptions.Noop) { Debug.Assert(currentIndex != null); var replacementIndex = GetIndex(replacementIndexName); if (replacementIndex != null) { DeleteIndexInternal(replacementIndex); } return; } if (creationOptions == IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex) { Debug.Assert(currentIndex != null); var replacementIndex = GetIndex(replacementIndexName); if (replacementIndex != null) { DeleteIndexInternal(replacementIndex); } if (currentDifferences != IndexDefinitionCompareDifferences.None) { UpdateIndex(definition, currentIndex, currentDifferences); } return; } UpdateStaticIndexLockModeAndPriority(definition, currentIndex, currentDifferences); if (creationOptions == IndexCreationOptions.Update) { Debug.Assert(currentIndex != null); definition.Name = replacementIndexName; var replacementIndex = GetIndex(replacementIndexName); if (replacementIndex != null) { creationOptions = GetIndexCreationOptions(definition, replacementIndex, out IndexDefinitionCompareDifferences sideBySideDifferences); if (creationOptions == IndexCreationOptions.Noop) { return; } if (creationOptions == IndexCreationOptions.UpdateWithoutUpdatingCompiledIndex) { UpdateIndex(definition, replacementIndex, sideBySideDifferences); return; } DeleteIndexInternal(replacementIndex); } } Index index; switch (definition.Type) { case IndexType.Map: index = MapIndex.CreateNew(definition, _documentDatabase); break; case IndexType.MapReduce: index = MapReduceIndex.CreateNew(definition, _documentDatabase); break; default: throw new NotSupportedException($"Cannot create {definition.Type} index from IndexDefinition"); } CreateIndexInternal(index); }