Exemplo n.º 1
0
 /// <inheritdoc />
 /// <summary>
 /// Init this storage
 /// </summary>
 protected override void OnInit()
 {
     Ensure.ReferenceNotNull(BasePath, "The FileStorage BasePath, is null.");
     Core.Log.InfoBasic("Initializing FileStorage...");
     if (_handlers?.Any() == true)
     {
         Core.Log.InfoBasic("Disposing previous instances...");
         _handlers.Each(fsto => fsto.Dispose());
         _handlers = null;
     }
     if (!Directory.Exists(BasePath))
     {
         Core.Log.InfoBasic("Creating base folder");
         Directory.CreateDirectory(BasePath);
     }
     Core.Log.InfoBasic("Configuring {0} Subfolders", NumberOfSubFolders);
     _handlers = new FolderHandler[NumberOfSubFolders];
     for (var i = 0; i < NumberOfSubFolders; i++)
     {
         var folder = Path.Combine(BasePath, i.ToString());
         Core.Log.InfoBasic("Initializing Subfolder: {0} on {1}", i, folder);
         _handlers[i] = new FolderHandler(folder, this);
     }
     Core.Log.InfoBasic("Waiting the folder handlers to be loaded.");
     TaskHelper.SleepUntil(() => _handlers.All(s => s.Loaded)).WaitAsync();
     Core.Log.InfoBasic("All folder handlers are loaded, Index Count: {0}", Metas.Count());
     SetReady(true);
 }
Exemplo n.º 2
0
        private async Task InitAsync(CancellationToken cancellationToken = default)
        {
            Ensure.ReferenceNotNull(BasePath, "The FileStorage BasePath, is null.");
            Core.Log.InfoBasic("Initializing FileStorage...");
            if (_handlers?.Any() == true)
            {
                Core.Log.InfoBasic("Disposing previous instances...");
                foreach (var hnd in _handlers)
                {
                    await hnd.DisposeAsync(true).ConfigureAwait(false);
                }
                _handlers = null;
            }
            if (!Directory.Exists(BasePath))
            {
                Core.Log.InfoBasic("Creating base folder");
                Directory.CreateDirectory(BasePath);
            }
            Core.Log.InfoBasic("Configuring {0} Subfolders", NumberOfSubFolders);
            _handlers = new FolderHandler[NumberOfSubFolders];
            var loadTasks = new Task[NumberOfSubFolders];

            for (var i = 0; i < NumberOfSubFolders; i++)
            {
                var folder = Path.Combine(BasePath, i.ToString("00"));
                Core.Log.InfoBasic("Initializing Subfolder: {0} on {1}", i, folder);
                _handlers[i] = new FolderHandler(this, _metas, folder);
                loadTasks[i] = _handlers[i].LoadAsync(cancellationToken);
            }

            Core.Log.InfoBasic("Waiting the folder handlers to be loaded.");
            await Task.WhenAll(loadTasks).ConfigureAwait(false);

            if (_handlers.Any(hnd => hnd.Status != FolderHandlerStatus.Loaded))
            {
                Core.Log.Error("There were some errors loading folders, the Storage can be loaded.");
                return;
            }

            Core.Log.InfoBasic("All folder handlers are loaded, Index Count: {0}", _handlers.Sum(i => i.Count));
            SetReady(true);
        }