Exemplo n.º 1
0
 public Task <SynchronizationResult> SynchronizeNonFictionAsync(IProgress <object> progressHandler, CancellationToken cancellationToken)
 {
     return(Task.Run(async() =>
     {
         Logger.Debug("Synchronization has started.");
         if (NonFictionBookCount == 0)
         {
             throw new Exception("Non-fiction table must not be empty.");
         }
         CheckAndCreateNonFictionIndexes(progressHandler, cancellationToken);
         if (cancellationToken.IsCancellationRequested)
         {
             Logger.Debug("Synchronization has been cancelled.");
             return SynchronizationResult.CANCELLED;
         }
         Logger.Debug("Loading last non-fiction book.");
         NonFictionBook lastModifiedNonFictionBook = localDatabase.GetLastModifiedNonFictionBook();
         Logger.Debug($"Last non-fiction book: Libgen ID = {lastModifiedNonFictionBook.LibgenId}, last modified date/time = {lastModifiedNonFictionBook.LastModifiedDateTime}.");
         string jsonApiUrl = Mirrors[AppSettings.Mirrors.NonFictionSynchronizationMirrorName].NonFictionSynchronizationUrl;
         if (jsonApiUrl == null)
         {
             throw new Exception("JSON API URL is null.");
         }
         JsonApiClient jsonApiClient = new JsonApiClient(HttpClient, jsonApiUrl, lastModifiedNonFictionBook.LastModifiedDateTime,
                                                         lastModifiedNonFictionBook.LibgenId);
         progressHandler.Report(new ImportLoadLibgenIdsProgress());
         BitArray existingLibgenIds = localDatabase.GetNonFictionLibgenIdsBitArray();
         NonFictionImporter importer = new NonFictionImporter(localDatabase, existingLibgenIds, lastModifiedNonFictionBook);
         progressHandler.Report(new SynchronizationProgress(0, 0, 0));
         int downloadedBookCount = 0;
         int totalAddedBookCount = 0;
         int totalUpdatedBookCount = 0;
         Importer.ImportProgressReporter importProgressReporter = (int objectsAdded, int objectsUpdated) =>
         {
             progressHandler.Report(new SynchronizationProgress(downloadedBookCount, totalAddedBookCount + objectsAdded,
                                                                totalUpdatedBookCount + objectsUpdated));
         };
         while (true)
         {
             List <NonFictionBook> currentBatch;
             try
             {
                 Logger.Debug("Downloading next batch from the server.");
                 currentBatch = await jsonApiClient.DownloadNextBatchAsync(cancellationToken);
             }
             catch (TaskCanceledException)
             {
                 Logger.Debug("Synchronization has been cancelled.");
                 return SynchronizationResult.CANCELLED;
             }
             if (!currentBatch.Any())
             {
                 Logger.Debug("Current batch is empty, download is complete.");
                 break;
             }
             downloadedBookCount += currentBatch.Count;
             Logger.Debug($"Batch download is complete, {downloadedBookCount} books have been downloaded so far.");
             progressHandler.Report(new SynchronizationProgress(downloadedBookCount, totalAddedBookCount, totalUpdatedBookCount));
             if (cancellationToken.IsCancellationRequested)
             {
                 Logger.Debug("Synchronization has been cancelled.");
                 return SynchronizationResult.CANCELLED;
             }
             Logger.Debug("Importing downloaded batch.");
             Importer.ImportResult importResult =
                 importer.Import(currentBatch, importProgressReporter, SYNCHRONIZATION_PROGRESS_UPDATE_INTERVAL, cancellationToken);
             if (cancellationToken.IsCancellationRequested)
             {
                 Logger.Debug("Synchronization has been cancelled.");
                 return SynchronizationResult.CANCELLED;
             }
             totalAddedBookCount += importResult.AddedObjectCount;
             totalUpdatedBookCount += importResult.UpdatedObjectCount;
             Logger.Debug($"Batch has been imported, total added book count = {totalAddedBookCount}, total updated book count = {totalUpdatedBookCount}.");
         }
         Logger.Debug("Synchronization has been completed successfully.");
         return SynchronizationResult.COMPLETED;
     }));
 }
Exemplo n.º 2
0
 public Task <SynchronizationResult> SynchronizeNonFiction(IProgress <object> progressHandler, CancellationToken cancellationToken)
 {
     return(Task.Run(async() =>
     {
         Logger.Debug("Synchronization has started.");
         if (NonFictionBookCount == 0)
         {
             throw new Exception("Non-fiction table must not be empty.");
         }
         CheckAndCreateNonFictionIndexes(progressHandler, cancellationToken);
         if (cancellationToken.IsCancellationRequested)
         {
             Logger.Debug("Synchronization has been cancelled.");
             return SynchronizationResult.CANCELLED;
         }
         Logger.Debug("Loading last non-fiction book.");
         NonFictionBook lastModifiedNonFictionBook = localDatabase.GetLastModifiedNonFictionBook();
         Logger.Debug($"Last non-fiction book: Libgen ID = {lastModifiedNonFictionBook.LibgenId}, last modified date/time = {lastModifiedNonFictionBook.LastModifiedDateTime}.");
         string jsonApiUrl = Mirrors[AppSettings.Mirrors.NonFictionSynchronizationMirrorName].NonFictionSynchronizationUrl;
         if (jsonApiUrl == null)
         {
             throw new Exception("JSON API URL is null.");
         }
         JsonApiClient jsonApiClient = new JsonApiClient(HttpClient, jsonApiUrl, lastModifiedNonFictionBook.LastModifiedDateTime,
                                                         lastModifiedNonFictionBook.LibgenId);
         List <NonFictionBook> downloadedBooks = new List <NonFictionBook>();
         progressHandler.Report(new JsonApiDownloadProgress(0));
         while (true)
         {
             List <NonFictionBook> currentBatch;
             try
             {
                 Logger.Debug("Downloading next batch from the server.");
                 currentBatch = await jsonApiClient.DownloadNextBatchAsync(cancellationToken);
             }
             catch (TaskCanceledException)
             {
                 Logger.Debug("Synchronization has been cancelled.");
                 return SynchronizationResult.CANCELLED;
             }
             if (!currentBatch.Any())
             {
                 Logger.Debug("Current batch is empty, download is complete.");
                 break;
             }
             downloadedBooks.AddRange(currentBatch);
             Logger.Debug($"{downloadedBooks.Count} books have been downloaded so far.");
             progressHandler.Report(new JsonApiDownloadProgress(downloadedBooks.Count));
             if (cancellationToken.IsCancellationRequested)
             {
                 Logger.Debug("Synchronization has been cancelled.");
                 return SynchronizationResult.CANCELLED;
             }
         }
         NonFictionImporter importer = new NonFictionImporter(localDatabase, lastModifiedNonFictionBook);
         Logger.Debug("Importing data.");
         importer.Import(downloadedBooks, progressHandler, cancellationToken);
         if (cancellationToken.IsCancellationRequested)
         {
             Logger.Debug("Synchronization has been cancelled.");
             return SynchronizationResult.CANCELLED;
         }
         Logger.Debug("Synchronization has been completed successfully.");
         return SynchronizationResult.COMPLETED;
     }));
 }