/// <inheritdoc /> public async Task <IList <IPendingFolderUpdate> > GetPendingFolderUpdatesAsync(long limit, IConnectionFactory factory, CancellationToken token) { if (null == factory) { throw new ArgumentNullException(nameof(factory), "You have to be within a tansaction when calling this function."); } // the pending updates var pendingUpdates = new List <IPendingFolderUpdate>(); try { // we want to get the latest updated folders. var sql = $"SELECT fu.folderid as folderid, fu.type as type, f.path as path FROM {Tables.FolderUpdates} fu, {Tables.Folders} f WHERE f.id=fu.folderid " + $"ORDER BY fu.ticks DESC LIMIT { limit}"; using (var cmd = factory.CreateCommand(sql)) using (var reader = await factory.ExecuteReadAsync(cmd, token).ConfigureAwait(false)) { var folderIdPos = reader.GetOrdinal("folderid"); var pathPos = reader.GetOrdinal("path"); var typePos = reader.GetOrdinal("type"); while (reader.Read()) { // get out if needed. token.ThrowIfCancellationRequested(); // the folder id var folderId = (long)reader[folderIdPos]; // the directory for that folder var directory = new DirectoryInfo((string)reader[pathPos]); // the update type var type = (UpdateType)(long)reader[typePos]; // Get the files currently on record this can be null if we have nothing. // if the folder was just created we are not going to bother getting more data. var filesOnRecord = type == UpdateType.Created ? new List <FileInfo>() : await _files.GetFilesAsync(folderId, token).ConfigureAwait(false); // add this update pendingUpdates.Add(new PendingFolderUpdate( folderId, directory, filesOnRecord, type )); } } } catch (OperationCanceledException) { _logger.Warning("Received cancellation request - Get pending folder updates"); throw; } catch (Exception e) { _logger.Exception(e); return(null); } // return whatever we found return(pendingUpdates); }