Exemplo n.º 1
0
 public void Dispose()
 {
     writer?.Dispose();
     analyzer?.Dispose();
     directory?.Dispose();
 }
Exemplo n.º 2
0
        //todo: make that as background task. Need input from someone how to handle that correctly as now it is as sync task to avoid issues, but need be change
        public override void RebuildCache()
        {
            lock (RebuildLock)
            {
                //Needs locking
                LoggingService.Log(new LogEntry(LogLevel.Info, null, $"Rebuilding cache"));

                var tempDir = new DirectoryInfo(
                    Path.Combine(_cacheDirectoryPath,
                                 _cacheDirectoryName, DateTimeOffset.UtcNow.ToString("yyyyMMddTHHmmssfffffff")));
                if (tempDir.Exists == false)
                {
                    tempDir.Create();
                }
                Lucene.Net.Store.Directory newIndex = new SimpleFSDirectory(tempDir);
                foreach (string file in GetAllBlobFiles())
                {
                    //   newIndex.TouchFile(file);
                    if (file.EndsWith(".lock"))
                    {
                        continue;
                    }
                    var status = RemoteDirectory.SyncFile(newIndex, file, CompressBlobs);
                    if (!status)
                    {
                        LoggingService.Log(new LogEntry(LogLevel.Error, null, $"Rebuilding cache failed"));
                        newIndex.Dispose();
                    }
                }

                var oldIndex = CacheDirectory;
                newIndex.Dispose();
                newIndex = new SimpleFSDirectory(tempDir);

                CacheDirectory = newIndex;
                _lockFactory   = newIndex.LockFactory;
                if (oldIndex != null)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(LockFactory.LockPrefix))
                        {
                            oldIndex.ClearLock(LockFactory.LockPrefix + "-write.lock");
                        }
                        else
                        {
                            oldIndex.ClearLock("write.lock");
                        }
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Log(new LogEntry(LogLevel.Error, ex, $"Exception on unlocking old cache index folder"));
                    }


                    oldIndex.Dispose();
                    try
                    {
                        DirectoryInfo oldindexDir = new DirectoryInfo(Path.Combine(_cacheDirectoryPath,
                                                                                   _cacheDirectoryName, _oldIndexFolderName));
                        foreach (var file in oldindexDir.GetFiles())
                        {
                            file.Delete();
                        }


                        oldindexDir.Delete();
                    }
                    catch (Exception ex)
                    {
                        LoggingService.Log(new LogEntry(LogLevel.Error, ex, $"Cleaning of old directory failed."));
                    }
                }

                _oldIndexFolderName = tempDir.Name;
            }
        }