Ejemplo n.º 1
0
        public bool Initialize(Indices indices)
        {
            if (indices == null)
            {
                return(true);
            }
            var databaseDirectory = _databaseContext.DatabaseConfigurations.Storage.StorageProvider.DatabasePath + _databaseContext.DatabaseName;

            foreach (var config in indices.IndexConfigurations.Values)
            {
                IIndex index = new BPlusIndex(config, _collectionName, databaseDirectory, this);

                if (_attributeToNameReference.TryAdd(index.IndexKey.ToString(), index.Name))
                {
                    try
                    {
                        index.Initialize();
                        if (!_indexes.TryAdd(index.Name, index))
                        {
                            string removedAttribs;
                            _attributeToNameReference.TryRemove(index.IndexKey.ToString(), out removedAttribs);
                            index.Dispose();
                            if (LoggerManager.Instance.IndexLogger != null)
                            {
                                LoggerManager.Instance.IndexLogger.Error("BPlusIndex",
                                                                         "Failed to initialize index: " + index.Name + ", an index with name " +
                                                                         index.Name + "already exists. ");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        if (LoggerManager.Instance.IndexLogger != null)
                        {
                            LoggerManager.Instance.IndexLogger.Error("BPlusIndex",
                                                                     "Failed to initialize index: " + index.Name + ", Corruption detected, " + ex.Message +
                                                                     index.Name + ", regenerating index... ");
                        }
                        string removedAttribs;
                        _attributeToNameReference.TryRemove(index.IndexKey.ToString(), out removedAttribs);
                        index.Destroy();
                        RecreateIndex(config);
                        continue;
                    }
                }
                else
                {
                    if (LoggerManager.Instance.IndexLogger != null)
                    {
                        LoggerManager.Instance.IndexLogger.Error("BPlusIndex",
                                                                 "Failed to initialize index: " + index.Name + ", an index on attributes " +
                                                                 index.Attributes.ToString() + "already exists. ");
                    }
                }
                index.IsFunctional = true;
                persistanceManager.RegisterPersister((BPlusIndex)index);
            }

            return(true);
        }
Ejemplo n.º 2
0
        public void RecreateIndex(IndexConfiguration indexConfig)
        {
            var databaseDirectory = _databaseContext.DatabaseConfigurations.Storage.StorageProvider.DatabasePath +
                                    _databaseContext.DatabaseName;

            IIndex removedIndex;

            if (_indexes.TryRemove(indexConfig.Name, out removedIndex))
            {
                string attributes;
                _attributeToNameReference.TryRemove(removedIndex.IndexKey.ToString(), out attributes);
            }

            IIndex index = new BPlusIndex(indexConfig, _collectionName, databaseDirectory, this);

            if (!_attributeToNameReference.TryAdd(index.IndexKey.ToString(), index.Name))
            {
                _indexes.TryAdd(removedIndex.Name, removedIndex);
                _attributeToNameReference.TryAdd(removedIndex.IndexKey.ToString(), removedIndex.Name);
                throw new IndexException(ErrorCodes.Indexes.INDEX_ALREADY_DEFINED_FOR_ATTRIBUTES,
                                         new[] { index.IndexKey.ToString() });
            }

            if (removedIndex != null)
            {
                removedIndex.Destroy();
            }
            index.Initialize();

            if (_store != null)
            {
                if (!_indexes.TryAdd(index.Name, index))
                {
                    index.Destroy();
                    if (LoggerManager.Instance.IndexLogger != null)
                    {
                        LoggerManager.Instance.IndexLogger.Error("BPlusIndex",
                                                                 "Failed to initialize index: " + index.Name + ", an index with name " +
                                                                 index.Name + "already exists. ");
                    }
                }

                PopulateIndex(index, _store);
            }
        }