public override IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>(async(token, progress) => { var toReport = new SaveGalleryProgress(-1, this.RecordCount); progress.Report(toReport); while (this.HasMoreItems) { await this.LoadMoreItemsAsync((uint)PageSize); token.ThrowIfCancellationRequested(); } for (var i = 0; i < this.Count; i++) { if (this[i] is GalleryImagePlaceHolder ph) { await ph.LoadImageAsync(false, strategy, true); token.ThrowIfCancellationRequested(); } } var load = base.SaveAsync(strategy); load.Progress = (sender, pro) => progress.Report(pro); token.Register(load.Cancel); await load; })); }
public static bool IsLofiRequired(ConnectionStrategy strategy) { switch (strategy) { case ConnectionStrategy.AllLofi: return(true); case ConnectionStrategy.LofiOnMetered: var netProfile = NetworkInformation.GetInternetConnectionProfile(); var cost = netProfile.GetConnectionCost(); var lofi = false; if (cost.NetworkCostType != NetworkCostType.Unrestricted) { lofi = true; } if (cost.OverDataLimit || cost.ApproachingDataLimit) { lofi = true; } return(lofi); case ConnectionStrategy.AllFull: return(false); } throw new ArgumentOutOfRangeException(nameof(strategy)); }
public override IAsyncAction LoadImageAsync(bool reload, ConnectionStrategy strategy, bool throwIfFailed) { return(Run(async token => { await((CachedGallery)Owner).LoadImageAsync(this); })); }
public void SetConnectionStringSettings(string connectionString, string providerName, ConnectionStrategy connectionStrategy = ConnectionStrategy.Auto) { Guard.IsNullOrEmpty(connectionString); Guard.IsNullOrEmpty(providerName); this.WrappedConnection = new ConnectionWrapper(connectionString, providerName, connectionStrategy); this.queryService = QueryServiceFactory.GetQueryService(providerName); }
public virtual IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>(async(token, progress) => { if (this.HasMoreItems) { progress.Report(new SaveGalleryProgress(-1, this.RecordCount)); while (this.HasMoreItems) { await this.LoadMoreItemsAsync((uint)PageSize); token.ThrowIfCancellationRequested(); } } await Task.Delay(0).ConfigureAwait(false); var loadedCount = 0; progress.Report(new SaveGalleryProgress(loadedCount, this.RecordCount)); using (var semaphore = new SemaphoreSlim(16, 16)) { var loadTasks = this.Select(image => Task.Run(async() => { await semaphore.WaitAsync(); try { token.ThrowIfCancellationRequested(); await image.LoadImageAsync(false, strategy, true); Interlocked.Increment(ref loadedCount); progress.Report(new SaveGalleryProgress(loadedCount, this.RecordCount)); } finally { semaphore.Release(); } })).ToArray(); await Task.WhenAll(loadTasks).ConfigureAwait(false); } using (var db = new GalleryDb()) { var gid = this.ID; var myModel = db.SavedSet.SingleOrDefault(model => model.GalleryId == gid); if (myModel == null) { db.SavedSet.Add(new SavedGalleryModel().Update(this)); } else { myModel.Update(this); } await db.SaveChangesAsync(); } })); }
public ConnectionWrapper(string connectionString, string providerName, ConnectionStrategy connectionStrategy) { this.Connection = DbProviderFactories.GetFactory(providerName).CreateConnection(); Guard.IsNull(Connection, "Cannot resolve connection with informations provided."); this.Connection.ConnectionString = connectionString; this.ConnectionString = connectionString; this.ProviderName = providerName; this.ConnectionStrategy = connectionStrategy; }
public void SetConnectionStringSettings(string connectionStringName = "DefaultConnection", ConnectionStrategy connectionStrategy = ConnectionStrategy.Auto) { Guard.IsNullOrEmpty(connectionStringName); var section = ConfigurationManager.ConnectionStrings[connectionStringName]; if (section != null) { this.SetConnectionStringSettings(section.ConnectionString, section.ProviderName, connectionStrategy); } else { Guard.Throw("No connection string section found for the connection name " + connectionStringName); } }
public static bool IsLofiRequired(ConnectionStrategy strategy) { switch (strategy) { case ConnectionStrategy.AllLofi: return(true); case ConnectionStrategy.AllFull: return(false); case ConnectionStrategy.LofiOnMetered: default: var netProfile = NetworkInformation.GetInternetConnectionProfile(); var cost = netProfile.GetConnectionCost(); if (cost.NetworkCostType != NetworkCostType.Unrestricted || cost.OverDataLimit || cost.ApproachingDataLimit) { return(true); } return(false); } }
public virtual IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>((token, progress) => Task.Run(async() => { await Task.Yield(); progress.Report(new SaveGalleryProgress(-1, Count)); var loadOP = LoadItemsAsync(0, Count); token.Register(loadOP.Cancel); await loadOP; token.ThrowIfCancellationRequested(); var loadedCount = 0; progress.Report(new SaveGalleryProgress(loadedCount, Count)); using (var semaphore = new SemaphoreSlim(10, 10)) { async Task loadSingleImage(GalleryImage i) { try { Debug.WriteLine($"Start {i.PageId}"); token.ThrowIfCancellationRequested(); var firstFailed = false; try { var firstChance = i.LoadImageAsync(false, strategy, true); var firstTask = firstChance.AsTask(token); var c = await Task.WhenAny(Task.Delay(30_000), firstTask); if (c != firstTask) { Debug.WriteLine($"Timeout 1st {i.PageId}"); firstFailed = true; firstChance.Cancel(); } } catch (Exception) { Debug.WriteLine($"Fail 1st {i.PageId}"); firstFailed = true; } if (firstFailed) { Debug.WriteLine($"Retry {i.PageId}"); token.ThrowIfCancellationRequested(); await i.LoadImageAsync(true, strategy, true).AsTask(token); } progress.Report(new SaveGalleryProgress(Interlocked.Increment(ref loadedCount), Count)); Debug.WriteLine($"Success {i.PageId}"); } finally { semaphore.Release(); Debug.WriteLine($"End {i.PageId}"); } } var pendingTasks = new List <Task>(Count); await Task.Run(async() => { foreach (var item in this) { await semaphore.WaitAsync().ConfigureAwait(false); pendingTasks.Add(loadSingleImage(item)); } }, token).ConfigureAwait(false); await Task.WhenAll(pendingTasks).ConfigureAwait(false); } using (var db = new GalleryDb()) { var gid = Id; var myModel = db.SavedSet.SingleOrDefault(model => model.GalleryId == gid); if (myModel is null) { db.SavedSet.Add(new SavedGalleryModel().Update(this)); } else { myModel.Update(this); } await db.SaveChangesAsync(); } }))); }
public virtual IAsyncActionWithProgress <SaveGalleryProgress> SaveAsync(ConnectionStrategy strategy) { return(Run <SaveGalleryProgress>((token, progress) => Task.Run(async() => { await Task.Yield(); progress.Report(new SaveGalleryProgress(-1, this.Count)); var loadOP = LoadItemsAsync(0, this.Count); token.Register(loadOP.Cancel); await loadOP; token.ThrowIfCancellationRequested(); var loadedCount = 0; progress.Report(new SaveGalleryProgress(loadedCount, this.Count)); using (var semaphore = new SemaphoreSlim(16, 16)) { var loadTasks = this.Select(image => Task.Run(async() => { await semaphore.WaitAsync(); try { token.ThrowIfCancellationRequested(); var firstFailed = false; try { var firstChance = image.LoadImageAsync(false, strategy, true); var firstTask = firstChance.AsTask(token); var c = await Task.WhenAny(Task.Delay(30_000), firstTask); if (c != firstTask) { firstFailed = true; firstChance.Cancel(); } } catch (Exception) { firstFailed = true; } if (firstFailed) { token.ThrowIfCancellationRequested(); await image.LoadImageAsync(false, strategy, true).AsTask(token); } progress.Report(new SaveGalleryProgress(Interlocked.Increment(ref loadedCount), this.Count)); } finally { semaphore.Release(); } })).ToArray(); await Task.WhenAll(loadTasks).ConfigureAwait(false); } using (var db = new GalleryDb()) { var gid = this.ID; var myModel = db.SavedSet.SingleOrDefault(model => model.GalleryId == gid); if (myModel is null) { db.SavedSet.Add(new SavedGalleryModel().Update(this)); } else { myModel.Update(this); } await db.SaveChangesAsync(); } }))); }
public void SetConnectionStrategy(ConnectionStrategy connectionStrategy) { Guard.IsNull(this.WrappedConnection, "Connection not initialized. No connection informations provided."); this.WrappedConnection.ConnectionStrategy = connectionStrategy; }
/// <summary> /// Returns a <see cref="System.String" /> that represents this configuration. Usable as Connection String. /// </summary> /// <returns> A <see cref="System.String" /> that represents this instance. </returns> public override string ToString() { var builder = new StringBuilder(); builder.Append("Servers="); builder.Append(string.Join(",", Nodes)); builder.Append(";"); if (Port != DefaultPort) { builder.Append("Port="); builder.Append(Port); builder.Append(";"); } if (Username != null) { builder.Append("User="******";"); } if (Password != null) { builder.Append("Password="******";"); } if (!CqlVersion.Equals(DefaultCqlVersion)) { builder.Append("CqlVersion="); builder.Append(CqlVersion); builder.Append(";"); } if (!DiscoveryScope.Equals(DefaultDiscoveryScope)) { builder.Append("DiscoveryScope="); builder.Append(DiscoveryScope); builder.Append(";"); } if (!ConnectionStrategy.Equals(DefaultConnectionStrategy)) { builder.Append("ConnectionStrategy="); builder.Append(ConnectionStrategy); builder.Append(";"); } if (!NewConnectionTreshold.Equals(DefaultNewConnectionTreshold)) { builder.Append("NewConnectionTreshold="); builder.Append(NewConnectionTreshold); builder.Append(";"); } if (!MaxDownTime.Equals(DefaultMaxDownTime)) { builder.Append("MaxDownTime="); builder.Append(MaxDownTime); builder.Append(";"); } if (!MinDownTime.Equals(DefaultMinDownTime)) { builder.Append("MinDownTime="); builder.Append(MinDownTime); builder.Append(";"); } if (MaxConnections > 0) { builder.Append("MaxConnections="); builder.Append(MaxConnections); builder.Append(";"); } if (!MaxConnectionsPerNode.Equals(DefaultMaxConnectionsPerNode)) { builder.Append("MaxConnectionsPerNode="); builder.Append(MaxConnectionsPerNode); builder.Append(";"); } if (MaxConcurrentQueries > 0) { builder.Append("MaxConcurrentQueries="); builder.Append(MaxConcurrentQueries); builder.Append(";"); } if (MaxConnectionIdleTime != DefaultMaxConnectionIdleTime) { builder.Append("MaxConnectionIdleTime="); builder.Append(MaxConnectionIdleTime.TotalSeconds); builder.Append(";"); } if (MaxQueryRetries != DefaultMaxQueryRetries) { builder.Append("MaxQueryRetries="); builder.Append(MaxQueryRetries); builder.Append(";"); } if (UseBuffering != DefaultUseBuffering) { builder.Append("UseBuffering="); builder.Append(UseBuffering); builder.Append(";"); } if (AllowCompression != DefaultAllowCompression) { builder.Append("AllowCompression="); builder.Append(AllowCompression); builder.Append(";"); } if (AllowCompression && CompressionTreshold != DefaultCompressionTreshold) { builder.Append("CompressionTreshold="); builder.Append(CompressionTreshold); builder.Append(";"); } if (!LoggerFactory.Equals(DefaultLoggerFactory, StringComparison.InvariantCultureIgnoreCase)) { builder.Append("LoggerFactory="); builder.Append(LoggerFactory); builder.Append(";"); } if (!LogLevel.Equals(DefaultLogLevel)) { builder.Append("LogLevel="); builder.Append(LogLevel); builder.Append(";"); } return(builder.ToString()); }
public void SetStrategy(ConnectionStrategy connectionStrategy) { this._connectionStrategy = connectionStrategy; }
public ConnectionContext(ConnectionStrategy connectionStrategy) { this._connectionStrategy = connectionStrategy; }