Пример #1
0
        public bool TryReplaceIndexes(string oldIndexName, string replacementIndexName)
        {
            bool lockTaken = false;

            try
            {
                Monitor.TryEnter(_locker, 16, ref lockTaken);
                if (lockTaken == false)
                {
                    return(false);
                }

                if (_indexes.TryGetByName(replacementIndexName, out Index newIndex) == false)
                {
                    return(true);
                }

                if (_indexes.TryGetByName(oldIndexName, out Index oldIndex))
                {
                    oldIndexName = oldIndex.Name;

                    if (oldIndex.Type.IsStatic() && newIndex.Type.IsStatic())
                    {
                        var oldIndexDefinition = oldIndex.GetIndexDefinition();
                        var newIndexDefinition = newIndex.Definition.GetOrCreateIndexDefinitionInternal();

                        if (newIndex.Definition.LockMode == IndexLockMode.Unlock && newIndexDefinition.LockMode.HasValue == false && oldIndexDefinition.LockMode.HasValue)
                        {
                            newIndex.SetLock(oldIndexDefinition.LockMode.Value);
                        }

                        if (newIndex.Definition.Priority == IndexPriority.Normal && newIndexDefinition.Priority.HasValue == false && oldIndexDefinition.Priority.HasValue)
                        {
                            newIndex.SetPriority(oldIndexDefinition.Priority.Value);
                        }
                    }
                }

                _indexes.ReplaceIndex(oldIndexName, oldIndex, newIndex);
                newIndex.Rename(oldIndexName);
                newIndex.ResetIsSideBySideAfterReplacement();

                if (oldIndex != null)
                {
                    while (_documentDatabase.DatabaseShutdown.IsCancellationRequested == false)
                    {
                        try
                        {
                            using (oldIndex.DrainRunningQueries())
                                DeleteIndexInternal(oldIndex);

                            break;
                        }
                        catch (TimeoutException)
                        {
                        }
                    }
                }

                if (newIndex.Configuration.RunInMemory == false)
                {
                    while (_documentDatabase.DatabaseShutdown.IsCancellationRequested == false)
                    {
                        try
                        {
                            using (newIndex.DrainRunningQueries())
                                using (newIndex.StorageOperation())
                                {
                                    var oldIndexDirectoryName         = IndexDefinitionBase.GetIndexNameSafeForFileSystem(oldIndexName);
                                    var replacementIndexDirectoryName = IndexDefinitionBase.GetIndexNameSafeForFileSystem(replacementIndexName);

                                    IOExtensions.MoveDirectory(newIndex.Configuration.StoragePath.Combine(replacementIndexDirectoryName).FullPath,
                                                               newIndex.Configuration.StoragePath.Combine(oldIndexDirectoryName).FullPath);

                                    if (newIndex.Configuration.TempPath != null)
                                    {
                                        IOExtensions.MoveDirectory(newIndex.Configuration.TempPath.Combine(replacementIndexDirectoryName).FullPath,
                                                                   newIndex.Configuration.TempPath.Combine(oldIndexDirectoryName).FullPath);
                                    }
                                }
                            break;
                        }
                        catch (TimeoutException)
                        {
                        }
                    }
                }

                _documentDatabase.Changes.RaiseNotifications(
                    new IndexChange
                {
                    Name = oldIndexName,
                    Type = IndexChangeTypes.SideBySideReplace
                });

                return(true);
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(_locker);
                }
            }
        }
Пример #2
0
        private void OpenIndexesFromRecord(PathSetting path, DatabaseRecord record)
        {
            if (_logger.IsInfoEnabled)
            {
                _logger.Info("Starting to load indexes from record");
            }

            List <Exception> exceptions = null;

            if (_documentDatabase.Configuration.Core.ThrowIfAnyIndexCannotBeOpened)
            {
                exceptions = new List <Exception>();
            }

            // delete all unrecognized index directories
            //foreach (var indexDirectory in new DirectoryInfo(path.FullPath).GetDirectories().Concat(indexesCustomPaths.Values.SelectMany(x => new DirectoryInfo(x).GetDirectories())))
            //{
            //    if (record.Indexes.ContainsKey(indexDirectory.Name) == false)
            //    {
            //        IOExtensions.DeleteDirectory(indexDirectory.FullName);

            //        continue;
            //    }

            //    // delete all redundant index instances
            //    var indexInstances = indexDirectory.GetDirectories();
            //    if (indexInstances.Length > 2)
            //    {
            //        var orderedIndexes = indexInstances.OrderByDescending(x =>
            //            int.Parse(x.Name.Substring(x.Name.LastIndexOf("\\") +1)));

            //        foreach (var indexToRemove in orderedIndexes.Skip(2))
            //        {
            //            Directory.Delete(indexToRemove.FullName);
            //        }
            //    }
            //}

            foreach (var kvp in record.Indexes)
            {
                if (_documentDatabase.DatabaseShutdown.IsCancellationRequested)
                {
                    return;
                }

                var name       = kvp.Key;
                var definition = kvp.Value;

                var safeName  = IndexDefinitionBase.GetIndexNameSafeForFileSystem(definition.Name);
                var indexPath = path.Combine(safeName).FullPath;
                if (Directory.Exists(indexPath))
                {
                    OpenIndex(path, indexPath, exceptions, name);
                }
            }

            foreach (var kvp in record.AutoIndexes)
            {
                if (_documentDatabase.DatabaseShutdown.IsCancellationRequested)
                {
                    return;
                }

                var name       = kvp.Key;
                var definition = kvp.Value;

                var safeName  = IndexDefinitionBase.GetIndexNameSafeForFileSystem(definition.Name);
                var indexPath = path.Combine(safeName).FullPath;
                if (Directory.Exists(indexPath))
                {
                    OpenIndex(path, indexPath, exceptions, name);
                }
            }

            if (exceptions != null && exceptions.Count > 0)
            {
                throw new AggregateException("Could not load some of the indexes", exceptions);
            }
        }
Пример #3
0
 public abstract IndexDefinitionCompareDifferences Compare(IndexDefinitionBase indexDefinition);